- 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.
34 lines
1.2 KiB
Plaintext
34 lines
1.2 KiB
Plaintext
---
|
|
description: List/detail (and write responses that reuse the mapper) embed FK relations as objects via pickRelation
|
|
globs: "src/modules/**/*.ts,src/common/http/response/**/*.ts"
|
|
alwaysApply: false
|
|
---
|
|
|
|
# Relation Response Objects
|
|
|
|
List, detail, and write handlers that reuse the same mapper MUST embed foreign keys as nested objects, not bare ids.
|
|
|
|
## Field lists
|
|
|
|
- Default catalog fields: `DEFAULT_RELATION_FIELDS` (`id`, `code`, `name`) from `src/common/http/response/`
|
|
- Override per entity with a module/local constant (users: `USER_RELATION_FIELDS` = `id`, `username`)
|
|
- Use `pickRelation(source, fields)` only — do not hand-roll partial copies
|
|
|
|
## Mapping
|
|
|
|
- Request DTOs still accept `*Id` (`divisionId`); the response key is the relation name (`division`, not `divisionId`)
|
|
- Null FK → `null` (not omitted)
|
|
- Never expose secrets (`passwordHash`, tokens) in relation objects
|
|
- Load relations in the repository (joins or batch-load); map with `pickRelation` in the service
|
|
|
|
```typescript
|
|
// BAD
|
|
return { divisionId: branch.divisionId, createdBy: branch.createdBy }
|
|
|
|
// GOOD
|
|
return {
|
|
division: pickRelation(branch.division, DEFAULT_RELATION_FIELDS),
|
|
createdBy: pickRelation(branch.createdByUser, USER_RELATION_FIELDS),
|
|
}
|
|
```
|