Skip to content

Commit

Permalink
refactor: sort value is not set in search interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
JadenKim-dev committed Jul 6, 2024
1 parent ef24fe6 commit 6d89869
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/lib/interceptor/read-many-request.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export function ReadManyRequestInterceptor(crudOptions: CrudOptions, factoryOpti
(pagination.type === 'cursor' ? readManyOptions.numberOfTake : pagination.limit ?? readManyOptions.numberOfTake) ??
CRUD_POLICY[method].default.numberOfTake;
const sort = readManyOptions.sort ? Sort[readManyOptions.sort] : CRUD_POLICY[method].default.sort;
const order = paginationKeys.reduce((order, paginationKey) => ({ ...order, [paginationKey]: sort }), {});

const crudReadManyRequest: CrudReadManyRequest<typeof crudOptions.entity> = new CrudReadManyRequest<typeof crudOptions.entity>()
.setPaginationKeys(paginationKeys)
Expand All @@ -70,7 +69,6 @@ export function ReadManyRequestInterceptor(crudOptions: CrudOptions, factoryOpti
.setWhere(query)
.setTake(numberOfTake)
.setSort(sort)
.setOrder(order)
.setRelations(this.getRelations(customReadManyRequestOptions))
.setDeserialize(this.deserialize)
.generate();
Expand Down
10 changes: 5 additions & 5 deletions src/lib/interceptor/search-request.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export function SearchRequestInterceptor(crudOptions: CrudOptions, factoryOption
(pagination.type === 'cursor' ? requestSearchDto.take : pagination.limit) ??
searchOptions.numberOfTake ??
CRUD_POLICY[method].default.numberOfTake;
const sort = CRUD_POLICY[method].default.sort;
const order = requestSearchDto.order ?? paginationKeys.reduce((acc, key) => ({ ...acc, [key]: sort }), {});
const order =
requestSearchDto.order ?? paginationKeys.reduce((acc, key) => ({ ...acc, [key]: CRUD_POLICY[method].default.sort }), {});

const withDeleted =
requestSearchDto.withDeleted ?? crudOptions.routes?.[method]?.softDelete ?? CRUD_POLICY[method].default.softDeleted;
Expand All @@ -95,7 +95,6 @@ export function SearchRequestInterceptor(crudOptions: CrudOptions, factoryOption
.setExcludeColumn(searchOptions.exclude)
.setWhere(where)
.setTake(numberOfTake)
.setSort(sort)
.setOrder(order)
.setWithDeleted(withDeleted)
.setRelations(this.getRelations(customSearchRequestOptions))
Expand Down Expand Up @@ -290,14 +289,15 @@ export function SearchRequestInterceptor(crudOptions: CrudOptions, factoryOption
return factoryOption.relations;
}

deserialize<T>({ pagination, findOptions, sort }: CrudReadManyRequest<T>): Array<FindOptionsWhere<T>> {
deserialize<T>({ pagination, findOptions }: CrudReadManyRequest<T>): Array<FindOptionsWhere<T>> {
const where = findOptions.where as Array<FindOptionsWhere<EntityType>>;
if (pagination.type === PaginationType.OFFSET) {
return where;
}
const lastObject: Record<string, unknown> = PaginationHelper.deserialize(pagination.nextCursor);

const operator = (key: keyof T) => ((findOptions.order?.[key] ?? sort) === Sort.DESC ? LessThan : MoreThan);
const operator = (key: keyof T) =>
(findOptions.order?.[key] ?? CRUD_POLICY[method].default.sort) === Sort.DESC ? LessThan : MoreThan;

const cursorCondition: Record<string, FindOperator<T>> = Object.entries(lastObject).reduce(
(queryFilter, [key, operand]) => ({
Expand Down
1 change: 1 addition & 0 deletions src/lib/request/read-many.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class CrudReadManyRequest<T> {

setSort(sort: Sort): this {
this._sort = sort;
this._findOptions.order = this.paginationKeys.reduce((order, paginationKey) => ({ ...order, [paginationKey]: sort }), {});
return this;
}

Expand Down

0 comments on commit 6d89869

Please sign in to comment.