- Introduced new columns `status`, `created_by`, and `updated_by` in the `users` table to track user status and ownership. - Updated the `employees` table to include a foreign key reference to the `users` table via `user_id`. - Created migration script `0012_users_primary.sql` to apply these changes to the database schema. - Enhanced the `EmployeesService` and `EmployeesRepository` to support user assignments and related data retrieval. - Updated DTOs and service methods to reflect the new user and employee relationships. - Added unit tests to validate the new functionality and ensure data integrity. - Modified existing controllers to accommodate the new fields and relationships in user and employee management.
229 lines
6.4 KiB
TypeScript
229 lines
6.4 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 { PRIVILEGE_ACTIONS } from '../src/modules/privileges/privilege-action';
|
|
import { registerAndActivate } from './helpers/activate-user';
|
|
|
|
describe('Cycles (e2e)', () => {
|
|
let app: INestApplication<App>;
|
|
let db: DrizzleDB;
|
|
|
|
const password = 'password123';
|
|
const adminUsername = `cyc_admin_${Date.now()}`;
|
|
const otherUsername = `cyc_other_${Date.now()}`;
|
|
|
|
let adminAccessToken: string;
|
|
let adminUserId: string;
|
|
let otherAccessToken: string;
|
|
let employeeId: string;
|
|
let startBranchId: string;
|
|
let endBranchId: string;
|
|
let customerId: 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: 'Cycle Admin',
|
|
code: `CYC_ADMIN_${now}`,
|
|
status: 'active',
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
createdBy: adminUserId,
|
|
updatedBy: adminUserId,
|
|
})
|
|
.returning();
|
|
|
|
const keys = await db.select().from(privilegeKeys);
|
|
await db.insert(privilegeDetails).values(
|
|
keys.flatMap((key) =>
|
|
PRIVILEGE_ACTIONS.map((action) => ({
|
|
privilegeId: priv.id,
|
|
privilegeKeyId: key.id,
|
|
action,
|
|
value: true,
|
|
})),
|
|
),
|
|
);
|
|
await db
|
|
.update(users)
|
|
.set({ privilegeId: priv.id, updatedAt: Date.now() })
|
|
.where(eq(users.id, adminUserId));
|
|
|
|
const suffix = Date.now().toString().slice(-6);
|
|
const employee = await request(app.getHttpServer())
|
|
.post('/employees')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
code: `CYC_E_${suffix}`,
|
|
name: 'Ada Lovelace',
|
|
phone: '+6281234567890',
|
|
position: 'sales',
|
|
})
|
|
.expect(201);
|
|
employeeId = (employee.body as { id: string }).id;
|
|
|
|
const start = await request(app.getHttpServer())
|
|
.post('/branches')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
code: `CYC_S_${suffix}`,
|
|
name: 'Start Branch',
|
|
phone: '+6281234567890',
|
|
address: 'Jl Sudirman No 1',
|
|
latitude: -6.2,
|
|
longitude: 106.8,
|
|
workingDaysStart: 'monday',
|
|
workingDaysEnd: 'friday',
|
|
workingHoursStart: '08:00',
|
|
workingHoursEnd: '17:00',
|
|
})
|
|
.expect(201);
|
|
startBranchId = (start.body as { id: string }).id;
|
|
|
|
const end = await request(app.getHttpServer())
|
|
.post('/branches')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
code: `CYC_E_${suffix}`,
|
|
name: 'End Branch',
|
|
phone: '+6281234567891',
|
|
address: 'Jl Thamrin No 2',
|
|
latitude: -6.21,
|
|
longitude: 106.81,
|
|
workingDaysStart: 'monday',
|
|
workingDaysEnd: 'friday',
|
|
workingHoursStart: '08:00',
|
|
workingHoursEnd: '17:00',
|
|
})
|
|
.expect(201);
|
|
endBranchId = (end.body as { id: string }).id;
|
|
|
|
const customer = await request(app.getHttpServer())
|
|
.post('/customers')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
code: `CYC_C_${suffix}`,
|
|
name: 'Acme Corp',
|
|
phone: '+6281234567892',
|
|
address: 'Jl Gatot No 3',
|
|
latitude: -6.22,
|
|
longitude: 106.82,
|
|
})
|
|
.expect(201);
|
|
customerId = (customer.body as { id: string }).id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('forbids cycles list without permission', async () => {
|
|
await request(app.getHttpServer())
|
|
.get('/cycles')
|
|
.set('Authorization', `Bearer ${otherAccessToken}`)
|
|
.expect(403);
|
|
});
|
|
|
|
it('creates a sales cycle, rejects an incomplete day, and archives on delete', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/cycles')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
employeeId,
|
|
purpose: 'sales',
|
|
cycleNumber: 1,
|
|
weekdays: {
|
|
monday: {
|
|
startBranchId,
|
|
endBranchId,
|
|
customerIds: [],
|
|
},
|
|
},
|
|
})
|
|
.expect(400);
|
|
|
|
const created = await request(app.getHttpServer())
|
|
.post('/cycles')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
employeeId,
|
|
purpose: 'sales',
|
|
cycleNumber: 1,
|
|
weekdays: {
|
|
monday: {
|
|
startBranchId,
|
|
endBranchId,
|
|
customerIds: [customerId],
|
|
},
|
|
},
|
|
})
|
|
.expect(201);
|
|
|
|
const id = (created.body as { id: string }).id;
|
|
expect((created.body as { purpose: string }).purpose).toBe('sales');
|
|
expect(
|
|
(created.body as { weekdays: Array<{ weekday: string }> }).weekdays,
|
|
).toHaveLength(1);
|
|
|
|
await request(app.getHttpServer())
|
|
.post('/cycles')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
employeeId,
|
|
purpose: 'sales',
|
|
cycleNumber: 1,
|
|
weekdays: {
|
|
tuesday: {
|
|
startBranchId,
|
|
endBranchId,
|
|
customerIds: [customerId],
|
|
},
|
|
},
|
|
})
|
|
.expect(409);
|
|
|
|
await request(app.getHttpServer())
|
|
.delete(`/cycles/${id}`)
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.expect(204);
|
|
|
|
const detail = await request(app.getHttpServer())
|
|
.get(`/cycles/${id}`)
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.expect(200);
|
|
expect((detail.body as { status: string }).status).toBe('archived');
|
|
});
|
|
});
|