- 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.
165 lines
4.8 KiB
TypeScript
165 lines
4.8 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('Packing slips (e2e)', () => {
|
|
let app: INestApplication<App>;
|
|
let db: DrizzleDB;
|
|
let adminAccessToken: string;
|
|
let otherAccessToken: string;
|
|
let customerId: string;
|
|
let productId: string;
|
|
|
|
const password = 'password123';
|
|
const suffix = Date.now().toString().slice(-6);
|
|
|
|
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,
|
|
`ps_admin_${suffix}`,
|
|
password,
|
|
);
|
|
adminAccessToken = admin.accessToken;
|
|
const adminUserId = admin.userId;
|
|
const other = await registerAndActivate(
|
|
app,
|
|
db,
|
|
`ps_other_${suffix}`,
|
|
password,
|
|
);
|
|
otherAccessToken = other.accessToken;
|
|
|
|
const now = Date.now();
|
|
const [priv] = await db
|
|
.insert(privileges)
|
|
.values({
|
|
name: 'Packing Admin',
|
|
code: `PS_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 customer = await request(app.getHttpServer())
|
|
.post('/customers')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
code: `C_${suffix}`,
|
|
name: 'Acme Corp',
|
|
phone: '+6281234567890',
|
|
address: 'Jl Sudirman 1',
|
|
})
|
|
.expect(201);
|
|
customerId = (customer.body as { id: string }).id;
|
|
const product = await request(app.getHttpServer())
|
|
.post('/products')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
code: `P_${suffix}`,
|
|
name: 'Fuel 95',
|
|
unit: 'L',
|
|
price: '12500.0000',
|
|
})
|
|
.expect(201);
|
|
productId = (product.body as { id: string }).id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('rejects unauthenticated access', async () => {
|
|
await request(app.getHttpServer()).get('/packing-slips').expect(401);
|
|
});
|
|
|
|
it('forbids list without permission', async () => {
|
|
await request(app.getHttpServer())
|
|
.get('/packing-slips')
|
|
.set('Authorization', `Bearer ${otherAccessToken}`)
|
|
.expect(403);
|
|
});
|
|
|
|
it('creates, patches, and deletes a packing slip', async () => {
|
|
const created = await request(app.getHttpServer())
|
|
.post('/packing-slips')
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({
|
|
date: '2026-08-24T10:00:00+07:00',
|
|
customerId,
|
|
address: 'Jl Sudirman 1',
|
|
products: [{ productId, quantity: '2' }],
|
|
})
|
|
.expect(201);
|
|
expect(created.body).toMatchObject({
|
|
customerId,
|
|
address: 'Jl Sudirman 1',
|
|
status: 'draft',
|
|
});
|
|
expect((created.body as { code: string }).code).toMatch(/^PS-/);
|
|
const id = (created.body as { id: string }).id;
|
|
|
|
await request(app.getHttpServer())
|
|
.patch(`/packing-slips/${id}`)
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({ status: 'processed' })
|
|
.expect(400);
|
|
|
|
await request(app.getHttpServer())
|
|
.patch(`/packing-slips/${id}/status`)
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.send({ status: 'processed' })
|
|
.expect(200);
|
|
|
|
const detail = await request(app.getHttpServer())
|
|
.get(`/packing-slips/${id}`)
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.expect(200);
|
|
expect((detail.body as { products: unknown[] }).products).toHaveLength(1);
|
|
|
|
await request(app.getHttpServer())
|
|
.delete(`/packing-slips/${id}`)
|
|
.set('Authorization', `Bearer ${adminAccessToken}`)
|
|
.expect(204);
|
|
});
|
|
});
|