Refactor PlansRepository to optimize data loading and enhance error handling

- Replaced the previous hydration logic with a new method, `loadChildrenForRows`, to efficiently load related data for plans in a single query.
- Implemented a `groupByPlanId` utility to organize related entities by plan ID, improving data retrieval performance.
- Updated error handling in the `getPlan` method to throw a `NotFoundException` if a plan is not found.
- Modified end-to-end tests to validate the new structure of the response, ensuring that related destinations are correctly included in the API output.
This commit is contained in:
shancheas
2026-09-01 19:25:19 +07:00
parent 6b3ddfcff9
commit 9428a983f5
2 changed files with 74 additions and 22 deletions
+18 -5
View File
@@ -251,7 +251,17 @@ describe('Plans (e2e)', () => {
.query({ employeeId, purpose: 'sales', date: from })
.set('Authorization', `Bearer ${adminAccessToken}`)
.expect(200);
const planId = (list.body.data as Array<{ id: string }>)[0].id;
const listed = (
list.body as {
data: Array<{
id: string;
destinations: Array<{ customer: { id: string } | null }>;
}>;
}
).data;
expect(listed[0].destinations).toHaveLength(1);
expect(listed[0].destinations[0].customer?.id).toBe(customerId);
const planId = listed[0].id;
const added = await request(app.getHttpServer())
.post(`/plans/${planId}/destinations`)
@@ -259,13 +269,16 @@ describe('Plans (e2e)', () => {
.send({ customerId: extraCustomerId })
.expect(201);
expect(
(added.body as { destinations: Array<{ customerId: string }> })
.destinations,
(added.body as { destinations: unknown[] }).destinations,
).toHaveLength(2);
const extraDest = (
added.body as { destinations: Array<{ id: string; customerId: string }> }
).destinations.find((d) => d.customerId === extraCustomerId);
added.body as {
destinations: Array<{ id: string; customer: { id: string } | null }>;
}
).destinations.find(
(destination) => destination.customer?.id === extraCustomerId,
);
const removed = await request(app.getHttpServer())
.delete(`/plans/${planId}/destinations/${extraDest?.id}`)
.set('Authorization', `Bearer ${adminAccessToken}`)