Add divisions management module with database schema and validation
- Introduced `DivisionsModule` to manage organizational divisions, including read and write controllers. - Created database migrations for the `divisions` table and related constraints. - Implemented validation for division name and code with corresponding utility functions. - Added service and repository layers for handling division data operations. - Developed unit tests for the divisions service, repository, and controllers to ensure functionality and correctness. - Updated application module to include the new `ConfigurationModule` for better organization.
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
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 { PRIVILEGE_ACTIONS } from '../src/modules/privileges/privilege-action';
|
||||
|
||||
describe('Divisions (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
let db: DrizzleDB;
|
||||
|
||||
const password = 'password123';
|
||||
const adminUsername = `div_admin_${Date.now()}`;
|
||||
const otherUsername = `div_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 adminReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: adminUsername, password })
|
||||
.expect(201);
|
||||
adminAccessToken = (adminReg.body as { accessToken: string }).accessToken;
|
||||
|
||||
const adminMe = await request(app.getHttpServer())
|
||||
.get('/auth/me')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.expect(200);
|
||||
adminUserId = (adminMe.body as { id: string }).id;
|
||||
|
||||
const otherReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: otherUsername, password })
|
||||
.expect(201);
|
||||
otherAccessToken = (otherReg.body as { accessToken: string }).accessToken;
|
||||
|
||||
const now = Date.now();
|
||||
const [priv] = await db
|
||||
.insert(privileges)
|
||||
.values({
|
||||
name: 'Division Admin',
|
||||
code: `DIV_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();
|
||||
});
|
||||
|
||||
it('forbids divisions list without permission', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/divisions')
|
||||
.set('Authorization', `Bearer ${otherAccessToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('rejects unauthenticated access', async () => {
|
||||
await request(app.getHttpServer()).get('/divisions').expect(401);
|
||||
});
|
||||
|
||||
it('CRUD divisions with name/code rules, status, search, and bulk', async () => {
|
||||
const created = await request(app.getHttpServer())
|
||||
.post('/divisions')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ name: 'Human Resources', code: 'HR' })
|
||||
.expect(201);
|
||||
|
||||
expect(created.body).toMatchObject({
|
||||
name: 'Human Resources',
|
||||
code: 'HR',
|
||||
status: 'draft',
|
||||
createdBy: adminUserId,
|
||||
});
|
||||
const id = created.body.id as string;
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/divisions')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ name: 'Finance1', code: 'FIN' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/divisions')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ name: 'Finance', code: 'FIN 01' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get(`/divisions/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/divisions/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ name: 'People Operations' })
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/divisions/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ status: 'active' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/divisions/${id}/status`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ status: 'active' })
|
||||
.expect(200);
|
||||
|
||||
const list = await request(app.getHttpServer())
|
||||
.get('/divisions?search=People')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.expect(200);
|
||||
expect(list.body.data.length).toBeGreaterThanOrEqual(1);
|
||||
expect(list.body.meta).toBeDefined();
|
||||
|
||||
const extra = await request(app.getHttpServer())
|
||||
.post('/divisions')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ name: 'Operations', code: 'OPS' })
|
||||
.expect(201);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/divisions/bulk-status')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ids: [extra.body.id], status: 'archived' })
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/divisions/bulk-delete')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ids: [extra.body.id] })
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/divisions/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.expect(204);
|
||||
});
|
||||
|
||||
it('imports divisions from CSV', async () => {
|
||||
const csv = `name,code,status\nImported Division,IMP_${Date.now().toString().slice(-8)},draft\n`;
|
||||
const res = await request(app.getHttpServer())
|
||||
.post('/divisions/import')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.attach('file', Buffer.from(csv, 'utf8'), 'divisions.csv')
|
||||
.expect(201);
|
||||
|
||||
expect(res.body).toMatchObject({ imported: 1 });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user