- Introduced a comprehensive report engine for generating and managing reports, including sales and logistics reports. - Added new API endpoints for retrieving report configurations, data, and metadata, ensuring secure access with privilege checks. - Implemented a report bookmarks system to allow users to save and manage report filters and configurations. - Created database migrations for the `report_bookmarks` table and updated the schema to support new report functionalities. - Developed services and controllers for handling report queries and bookmarks, including CRUD operations for bookmarks. - Enhanced API documentation to reflect the new reporting features and endpoints. - Added unit and integration tests to validate the new functionalities and ensure data integrity across report operations.
143 lines
4.0 KiB
TypeScript
143 lines
4.0 KiB
TypeScript
import { INestApplication } from '@nestjs/common';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { eq } from 'drizzle-orm';
|
|
import request from 'supertest';
|
|
import { App } from 'supertest/types';
|
|
import { AppModule } from '../src/app.module';
|
|
import { configureApp } from '../src/common/configure-app';
|
|
import { DRIZZLE, type DrizzleDB } from '../src/database/database.module';
|
|
import {
|
|
privilegeDetails,
|
|
privilegeKeys,
|
|
privileges,
|
|
users,
|
|
} from '../src/database/schema';
|
|
import { REPORT_GROUP } from '../src/modules/reports/shared/constants/report-group';
|
|
import { PRIVILEGE_ACTIONS } from '../src/modules/privileges/privilege-action';
|
|
import { registerAndActivate } from './helpers/activate-user';
|
|
|
|
describe('Reports (e2e)', () => {
|
|
let app: INestApplication<App>;
|
|
let db: DrizzleDB;
|
|
|
|
const password = 'password123';
|
|
const adminUsername = `report_admin_${Date.now()}`;
|
|
const otherUsername = `report_other_${Date.now()}`;
|
|
|
|
let adminAccessToken: string;
|
|
let adminUserId: string;
|
|
let otherAccessToken: string;
|
|
|
|
beforeAll(async () => {
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
|
imports: [AppModule],
|
|
}).compile();
|
|
|
|
app = moduleFixture.createNestApplication();
|
|
configureApp(app, {
|
|
NODE_ENV: 'test',
|
|
SWAGGER_ENABLED: 'false',
|
|
});
|
|
await app.init();
|
|
db = app.get(DRIZZLE);
|
|
|
|
const admin = await registerAndActivate(app, db, adminUsername, password);
|
|
adminAccessToken = admin.accessToken;
|
|
adminUserId = admin.userId;
|
|
|
|
const other = await registerAndActivate(app, db, otherUsername, password);
|
|
otherAccessToken = other.accessToken;
|
|
|
|
const now = Date.now();
|
|
const [priv] = await db
|
|
.insert(privileges)
|
|
.values({
|
|
name: 'Report Admin',
|
|
code: `REPORT_ADMIN_${now}`,
|
|
status: 'active',
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
createdBy: adminUserId,
|
|
updatedBy: adminUserId,
|
|
})
|
|
.returning();
|
|
|
|
const keys = await db.select().from(privilegeKeys);
|
|
const detailRows = keys.flatMap((key) =>
|
|
PRIVILEGE_ACTIONS.map((action) => ({
|
|
privilegeId: priv.id,
|
|
privilegeKeyId: key.id,
|
|
action,
|
|
value: true,
|
|
})),
|
|
);
|
|
await db.insert(privilegeDetails).values(detailRows);
|
|
|
|
await db
|
|
.update(users)
|
|
.set({ privilegeId: priv.id, updatedAt: Date.now() })
|
|
.where(eq(users.id, adminUserId));
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
const baseQueryModel = {
|
|
startRow: 0,
|
|
endRow: 100,
|
|
rowGroupCols: [],
|
|
valueCols: [],
|
|
pivotCols: [],
|
|
pivotMode: false,
|
|
groupKeys: [],
|
|
filterModel: {},
|
|
sortModel: [],
|
|
};
|
|
|
|
it('forbids report config without permission', async () => {
|
|
await request(app.getHttpServer())
|
|
.get('/reports/config')
|
|
.query({ groupNames: REPORT_GROUP.SALES_REPORT })
|
|
.set('Authorization', `Bearer ${otherAccessToken}`)
|
|
.expect(403);
|
|
});
|
|
|
|
it('lists sales report configs', async () => {
|
|
const res = await request(app.getHttpServer())
|
|
.get('/reports/config')
|
|
.query({ groupNames: REPORT_GROUP.SALES_REPORT })
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.expect(200);
|
|
|
|
expect(Array.isArray(res.body)).toBe(true);
|
|
expect(res.body.length).toBeGreaterThan(0);
|
|
expect(res.body[0].groupName).toBe(REPORT_GROUP.SALES_REPORT);
|
|
});
|
|
|
|
it('runs sales order report meta and data', async () => {
|
|
const uniqueName = 'sales_report__sales_order';
|
|
const body = {
|
|
groupName: REPORT_GROUP.SALES_REPORT,
|
|
uniqueName,
|
|
queryModel: baseQueryModel,
|
|
};
|
|
|
|
const meta = await request(app.getHttpServer())
|
|
.post('/reports/meta')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send(body)
|
|
.expect(200);
|
|
|
|
expect(meta.body.totalRow).toBeDefined();
|
|
|
|
const data = await request(app.getHttpServer())
|
|
.post('/reports/data')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send(body)
|
|
.expect(200);
|
|
|
|
expect(Array.isArray(data.body)).toBe(true);
|
|
});
|
|
});
|