Add products and sales management modules with database schema and validation
- Introduced `ProductsModule` to manage product data, including read and write controllers. - Created database migrations for the `products`, `sales_requests`, `sales_orders`, `sales_invoices`, and related tables, including constraints and unique indexes. - Implemented validation for product fields such as code, name, unit, and brand with corresponding utility functions. - Developed service and repository layers for handling product and sales data operations. - Added unit tests for the products and sales services, repositories, and controllers to ensure functionality and correctness. - Updated application module to include the new `ProductsModule` and related sales modules for better organization.
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
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('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 adminReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: `ps_admin_${suffix}`, 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);
|
||||
const adminUserId = (adminMe.body as { id: string }).id;
|
||||
const otherReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: `ps_other_${suffix}`, password })
|
||||
.expect(201);
|
||||
otherAccessToken = (otherReg.body as { accessToken: string }).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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,229 @@
|
||||
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('Products (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
let db: DrizzleDB;
|
||||
|
||||
const password = 'password123';
|
||||
const adminUsername = `prd_admin_${Date.now()}`;
|
||||
const otherUsername = `prd_other_${Date.now()}`;
|
||||
|
||||
let adminAccessToken: string;
|
||||
let adminUserId: string;
|
||||
let otherAccessToken: string;
|
||||
|
||||
const payload = {
|
||||
code: `FUEL_${Date.now().toString().slice(-6)}`,
|
||||
name: 'Fuel 95',
|
||||
unit: 'L',
|
||||
price: '12500.0000',
|
||||
brand: 'Pertamina',
|
||||
};
|
||||
|
||||
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: 'Employee Admin',
|
||||
code: `EMP_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 products list without permission', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/products')
|
||||
.set('Authorization', `Bearer ${otherAccessToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('rejects unauthenticated access', async () => {
|
||||
await request(app.getHttpServer()).get('/products').expect(401);
|
||||
});
|
||||
|
||||
it('CRUD products with name/code/phone/position rules, status, search, and bulk', async () => {
|
||||
const created = await request(app.getHttpServer())
|
||||
.post('/products')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send(payload)
|
||||
.expect(201);
|
||||
|
||||
expect(created.body).toMatchObject({
|
||||
code: payload.code,
|
||||
name: 'Ada Lovelace',
|
||||
phone: '+6281234567890',
|
||||
position: 'sales',
|
||||
status: 'draft',
|
||||
createdBy: adminUserId,
|
||||
});
|
||||
const id = (created.body as { id: string }).id;
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/products')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ...payload, code: 'BAD 01', name: 'Fuel 95' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/products')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ...payload, code: 'PHN_01', price: '1.23456' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/products')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ...payload, code: 'POS_01', unit: 'L!' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/products')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ...payload, code: payload.code, name: 'Fuel 98' })
|
||||
.expect(409);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get(`/products/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/products/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ name: 'Fuel 98', unit: 'L' })
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/products/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ status: 'active' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/products/${id}/status`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ status: 'active' })
|
||||
.expect(200);
|
||||
|
||||
const list = await request(app.getHttpServer())
|
||||
.get('/products?search=Jean')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.expect(200);
|
||||
expect(
|
||||
(list.body as { data: unknown[] }).data.length,
|
||||
).toBeGreaterThanOrEqual(1);
|
||||
expect((list.body as { meta?: unknown }).meta).toBeDefined();
|
||||
|
||||
const extra = await request(app.getHttpServer())
|
||||
.post('/products')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({
|
||||
...payload,
|
||||
code: `OTH_${Date.now().toString().slice(-6)}`,
|
||||
name: 'Grace Hopper',
|
||||
position: 'crew',
|
||||
})
|
||||
.expect(201);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/products/bulk-status')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ids: [(extra.body as { id: string }).id], status: 'archived' })
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/products/bulk-delete')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.send({ ids: [(extra.body as { id: string }).id] })
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/products/${id}`)
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.expect(204);
|
||||
});
|
||||
|
||||
it('imports products from CSV', async () => {
|
||||
const suffix = Date.now().toString().slice(-6);
|
||||
const csv =
|
||||
'code,name,unit,price,brand,status\n' +
|
||||
`IMP_${suffix},Imported Fuel,+6281234567890,L,12500.0000,Pertamina,draft\n`;
|
||||
const res = await request(app.getHttpServer())
|
||||
.post('/products/import')
|
||||
.set('Authorization', `Bearer ${adminAccessToken}`)
|
||||
.attach('file', Buffer.from(csv, 'utf8'), 'products.csv')
|
||||
.expect(201);
|
||||
|
||||
expect(res.body).toMatchObject({ imported: 1 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,188 @@
|
||||
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('Sales invoices (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
let db: DrizzleDB;
|
||||
let token: string;
|
||||
let otherToken: string;
|
||||
let salesPersonId: string;
|
||||
let branchId: string;
|
||||
let divisionId: 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 adminReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: `si_admin_${suffix}`, password })
|
||||
.expect(201);
|
||||
token = (adminReg.body as { accessToken: string }).accessToken;
|
||||
const adminMe = await request(app.getHttpServer())
|
||||
.get('/auth/me')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.expect(200);
|
||||
const adminUserId = (adminMe.body as { id: string }).id;
|
||||
const otherReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: `si_other_${suffix}`, password })
|
||||
.expect(201);
|
||||
otherToken = (otherReg.body as { accessToken: string }).accessToken;
|
||||
|
||||
const now = Date.now();
|
||||
const [priv] = await db
|
||||
.insert(privileges)
|
||||
.values({
|
||||
name: 'Invoice Admin',
|
||||
code: `SI_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 auth = { Authorization: `Bearer ${token}` };
|
||||
const division = await request(app.getHttpServer())
|
||||
.post('/divisions')
|
||||
.set(auth)
|
||||
.send({ code: `D_${suffix}`, name: 'Sales Division' })
|
||||
.expect(201);
|
||||
divisionId = (division.body as { id: string }).id;
|
||||
const branch = await request(app.getHttpServer())
|
||||
.post('/branches')
|
||||
.set(auth)
|
||||
.send({
|
||||
code: `B_${suffix}`,
|
||||
name: 'Jakarta Pusat',
|
||||
phone: '+6281234567890',
|
||||
address: 'Jl Sudirman 1',
|
||||
workingDaysStart: 'monday',
|
||||
workingDaysEnd: 'friday',
|
||||
workingHoursStart: '08:00',
|
||||
workingHoursEnd: '17:00',
|
||||
divisionId,
|
||||
})
|
||||
.expect(201);
|
||||
branchId = (branch.body as { id: string }).id;
|
||||
const employee = await request(app.getHttpServer())
|
||||
.post('/employees')
|
||||
.set(auth)
|
||||
.send({
|
||||
code: `E_${suffix}`,
|
||||
name: 'Ada Lovelace',
|
||||
phone: '+6281234567890',
|
||||
position: 'sales',
|
||||
})
|
||||
.expect(201);
|
||||
salesPersonId = (employee.body as { id: string }).id;
|
||||
const customer = await request(app.getHttpServer())
|
||||
.post('/customers')
|
||||
.set(auth)
|
||||
.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(auth)
|
||||
.send({
|
||||
code: `P_${suffix}`,
|
||||
name: 'Fuel 95',
|
||||
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('/sales-invoices').expect(401);
|
||||
});
|
||||
|
||||
it('forbids list without permission', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/sales-invoices')
|
||||
.set('Authorization', `Bearer ${otherToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('creates an invoice with catalog price and stored balance', async () => {
|
||||
const created = await request(app.getHttpServer())
|
||||
.post('/sales-invoices')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({
|
||||
date: '2026-08-24T10:00:00+07:00',
|
||||
salesPersonId,
|
||||
branchId,
|
||||
divisionId,
|
||||
customerId,
|
||||
products: [{ productId, quantity: '2' }],
|
||||
})
|
||||
.expect(201);
|
||||
expect(created.body).toMatchObject({
|
||||
status: 'draft',
|
||||
balance: '25000.0000',
|
||||
});
|
||||
expect((created.body as { code: string }).code).toMatch(/^SI-/);
|
||||
const id = (created.body as { id: string }).id;
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/sales-invoices/${id}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ status: 'processed' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/sales-invoices/${id}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.expect(204);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,199 @@
|
||||
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('Sales payments (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
let db: DrizzleDB;
|
||||
let token: string;
|
||||
let otherToken: string;
|
||||
let invoiceId: 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 adminReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: `sp_admin_${suffix}`, password })
|
||||
.expect(201);
|
||||
token = (adminReg.body as { accessToken: string }).accessToken;
|
||||
const adminMe = await request(app.getHttpServer())
|
||||
.get('/auth/me')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.expect(200);
|
||||
const adminUserId = (adminMe.body as { id: string }).id;
|
||||
const otherReg = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username: `sp_other_${suffix}`, password })
|
||||
.expect(201);
|
||||
otherToken = (otherReg.body as { accessToken: string }).accessToken;
|
||||
|
||||
const now = Date.now();
|
||||
const [priv] = await db
|
||||
.insert(privileges)
|
||||
.values({
|
||||
name: 'Payment Admin',
|
||||
code: `SP_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 auth = { Authorization: `Bearer ${token}` };
|
||||
const division = await request(app.getHttpServer())
|
||||
.post('/divisions')
|
||||
.set(auth)
|
||||
.send({ code: `D_${suffix}`, name: 'Sales Division' })
|
||||
.expect(201);
|
||||
const branch = await request(app.getHttpServer())
|
||||
.post('/branches')
|
||||
.set(auth)
|
||||
.send({
|
||||
code: `B_${suffix}`,
|
||||
name: 'Jakarta Pusat',
|
||||
phone: '+6281234567890',
|
||||
address: 'Jl Sudirman 1',
|
||||
workingDaysStart: 'monday',
|
||||
workingDaysEnd: 'friday',
|
||||
workingHoursStart: '08:00',
|
||||
workingHoursEnd: '17:00',
|
||||
divisionId: (division.body as { id: string }).id,
|
||||
})
|
||||
.expect(201);
|
||||
const employee = await request(app.getHttpServer())
|
||||
.post('/employees')
|
||||
.set(auth)
|
||||
.send({
|
||||
code: `E_${suffix}`,
|
||||
name: 'Ada Lovelace',
|
||||
phone: '+6281234567890',
|
||||
position: 'sales',
|
||||
})
|
||||
.expect(201);
|
||||
const customer = await request(app.getHttpServer())
|
||||
.post('/customers')
|
||||
.set(auth)
|
||||
.send({
|
||||
code: `C_${suffix}`,
|
||||
name: 'Acme Corp',
|
||||
phone: '+6281234567890',
|
||||
address: 'Jl Sudirman 1',
|
||||
})
|
||||
.expect(201);
|
||||
const product = await request(app.getHttpServer())
|
||||
.post('/products')
|
||||
.set(auth)
|
||||
.send({
|
||||
code: `P_${suffix}`,
|
||||
name: 'Fuel 95',
|
||||
price: '12500.0000',
|
||||
})
|
||||
.expect(201);
|
||||
const invoice = await request(app.getHttpServer())
|
||||
.post('/sales-invoices')
|
||||
.set(auth)
|
||||
.send({
|
||||
date: '2026-08-24T10:00:00+07:00',
|
||||
salesPersonId: (employee.body as { id: string }).id,
|
||||
branchId: (branch.body as { id: string }).id,
|
||||
divisionId: (division.body as { id: string }).id,
|
||||
customerId: (customer.body as { id: string }).id,
|
||||
products: [
|
||||
{
|
||||
productId: (product.body as { id: string }).id,
|
||||
quantity: '2',
|
||||
},
|
||||
],
|
||||
})
|
||||
.expect(201);
|
||||
invoiceId = (invoice.body as { id: string }).id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('rejects unauthenticated access', async () => {
|
||||
await request(app.getHttpServer()).get('/sales-payments').expect(401);
|
||||
});
|
||||
|
||||
it('forbids list without permission', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/sales-payments')
|
||||
.set('Authorization', `Bearer ${otherToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('creates a payment and marks the invoice partial when approved', async () => {
|
||||
const created = await request(app.getHttpServer())
|
||||
.post('/sales-payments')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({
|
||||
date: '2026-08-24T10:00:00+07:00',
|
||||
invoices: [{ invoiceId, amount: '10000.0000' }],
|
||||
})
|
||||
.expect(201);
|
||||
expect((created.body as { code: string }).code).toMatch(/^SP-/);
|
||||
const id = (created.body as { id: string }).id;
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/sales-payments/${id}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ status: 'approved' })
|
||||
.expect(400);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/sales-payments/${id}/status`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ status: 'approved' })
|
||||
.expect(200);
|
||||
|
||||
const invoice = await request(app.getHttpServer())
|
||||
.get(`/sales-invoices/${invoiceId}`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.expect(200);
|
||||
expect(invoice.body).toMatchObject({
|
||||
status: 'partial',
|
||||
balance: '15000.0000',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user