Enhance branch management with foreign key relation handling

- Updated `BranchesModule` to include foreign key relations in list and write responses, ensuring they are represented as nested objects using `pickRelation`.
- Introduced new `relation-response.mdc` file to define guidelines for embedding foreign key relations.
- Modified `BranchesRepository` to support fetching related `division`, `createdByUser`, and `updatedByUser` data.
- Updated DTOs and service methods to reflect changes in response structure, removing direct foreign key IDs.
- Added unit tests to validate the new relation handling in branches service and repository.
- Enhanced e2e tests to verify the correct structure of branch responses with nested relations.
This commit is contained in:
shancheas
2026-08-26 13:44:13 +07:00
parent c9f9b31abf
commit f635ebeda0
13 changed files with 392 additions and 68 deletions
+18 -4
View File
@@ -26,6 +26,8 @@ describe('Branches (e2e)', () => {
let adminUserId: string;
let otherAccessToken: string;
let divisionId: string;
let divisionCode: string;
const divisionName = 'Jakarta Division';
const payload = {
code: `JKT_${Date.now().toString().slice(-6)}`,
@@ -103,11 +105,12 @@ describe('Branches (e2e)', () => {
.post('/divisions')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
name: 'Jakarta Division',
name: divisionName,
code: `JD_${Date.now().toString().slice(-6)}`,
})
.expect(201);
divisionId = (division.body as { id: string }).id;
divisionCode = (division.body as { code: string }).code;
});
afterAll(async () => {
@@ -137,11 +140,13 @@ describe('Branches (e2e)', () => {
name: 'Jakarta Pusat',
phone: '+6281234567890',
status: 'draft',
createdBy: adminUserId,
createdBy: { id: adminUserId, username: adminUsername },
updatedBy: { id: adminUserId, username: adminUsername },
latitude: null,
nfcId: null,
divisionId: null,
division: null,
});
expect(created.body).not.toHaveProperty('divisionId');
const id = (created.body as { id: string }).id;
await request(app.getHttpServer())
@@ -167,7 +172,7 @@ describe('Branches (e2e)', () => {
.set('Authorization', `Bearer ${adminAccessToken}`)
.expect(200);
await request(app.getHttpServer())
const patched = await request(app.getHttpServer())
.patch(`/branches/${id}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
@@ -178,6 +183,15 @@ describe('Branches (e2e)', () => {
divisionId,
})
.expect(200);
expect(patched.body).toMatchObject({
name: 'Jakarta Selatan',
division: {
id: divisionId,
code: divisionCode,
name: divisionName,
},
createdBy: { id: adminUserId, username: adminUsername },
});
await request(app.getHttpServer())
.patch(`/branches/${id}`)