Files
trackgo-be/src/common/http/response/relation-fields.ts
T
shancheas 4c45a4371e Enhance pagination and ordering capabilities in API responses
- Updated pagination-response and read-write-controllers documentation to include `orderBy` and `orderType` parameters for sorting results.
- Introduced new `order-clause` module to handle ordering logic, including validation for order types and columns.
- Enhanced `PaginationQueryDto` to support ordering fields in API requests.
- Updated various repository and service classes to implement ordering in database queries.
- Added unit tests for new ordering functionality and ensured existing tests cover the updated behavior.
- Refactored related DTOs to include user and code relations for better data representation in responses.
2026-08-27 13:09:41 +07:00

62 lines
1.5 KiB
TypeScript

export const DEFAULT_RELATION_FIELDS = ['id', 'code', 'name'] as const;
export const USER_RELATION_FIELDS = ['id', 'username'] as const;
export const CODE_RELATION_FIELDS = ['id', 'code'] as const;
export type DefaultRelation = {
readonly id: string;
readonly code: string;
readonly name: string;
};
export type UserRelation = {
readonly id: string;
readonly username: string;
};
export type CodeRelation = {
readonly id: string;
readonly code: string;
};
export function pickRelation<T, K extends keyof NonNullable<T>>(
source: T | null | undefined,
fields: readonly K[],
): Pick<NonNullable<T>, K> | null {
if (source == null) {
return null;
}
const result = {} as Pick<NonNullable<T>, K>;
for (const field of fields) {
result[field] = source[field];
}
return result;
}
export function pickDefaultRelation(
source: DefaultRelation | null | undefined,
): DefaultRelation | null {
return pickRelation(source, DEFAULT_RELATION_FIELDS);
}
export function pickUserRelation(source: UserRelation): UserRelation {
return (
pickRelation(source, USER_RELATION_FIELDS) ?? {
id: source.id,
username: source.username,
}
);
}
export function pickCodeRelation(
source: CodeRelation | null | undefined,
): CodeRelation | null {
return pickRelation(source, CODE_RELATION_FIELDS);
}
export function fallbackUserRelation(
source: UserRelation | null | undefined,
fallbackId: string,
): UserRelation {
return pickUserRelation(source ?? { id: fallbackId, username: '' });
}