Skip to content

Commit

Permalink
fix: relations in crud options does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
WBDC-Phong-Do committed Nov 7, 2023
1 parent 07b42c0 commit 311bc52
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/lib/interceptor/search-request.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,31 @@ describe('SearchRequestInterceptor', () => {
expect(await interceptor.validateBody({ take: 100_000 })).toEqual({ take: 100_000, withDeleted: false });
});
});

it('should be get relation values per each condition', () => {
const Interceptor = SearchRequestInterceptor({ entity: {} as typeof BaseEntity }, { relations: [], logger: new CrudLogger() });
const interceptor = new Interceptor();

expect(interceptor.getRelations({ relations: [] })).toEqual([]);
expect(interceptor.getRelations({ relations: ['table'] })).toEqual(['table']);
expect(interceptor.getRelations({})).toEqual([]);

const InterceptorWithOptions = SearchRequestInterceptor(
{ entity: {} as typeof BaseEntity, routes: { readMany: { relations: ['option'] } } },
{ relations: [], logger: new CrudLogger() },
);
const interceptorWithOptions = new InterceptorWithOptions();
expect(interceptorWithOptions.getRelations({ relations: [] })).toEqual([]);
expect(interceptorWithOptions.getRelations({ relations: ['table'] })).toEqual(['table']);
expect(interceptorWithOptions.getRelations({})).toEqual(['option']);

const InterceptorWithFalseOptions = SearchRequestInterceptor(
{ entity: {} as typeof BaseEntity, routes: { readMany: { relations: false } } },
{ relations: [], logger: new CrudLogger() },
);
const interceptorWithFalseOptions = new InterceptorWithFalseOptions();
expect(interceptorWithFalseOptions.getRelations({ relations: [] })).toEqual([]);
expect(interceptorWithFalseOptions.getRelations({ relations: ['table'] })).toEqual(['table']);
expect(interceptorWithFalseOptions.getRelations({})).toEqual([]);
});
});
15 changes: 14 additions & 1 deletion src/lib/interceptor/search-request.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function SearchRequestInterceptor(crudOptions: CrudOptions, factoryOption
.setWithDeleted(
requestSearchDto.withDeleted ?? crudOptions.routes?.[method]?.softDelete ?? CRUD_POLICY[method].default.softDeleted,
)
.setRelations(customSearchRequestOptions?.relations ?? factoryOption.relations)
.setRelations(this.getRelations(customSearchRequestOptions))
.setDeserialize(this.deserialize)
.generate();

Expand Down Expand Up @@ -260,6 +260,19 @@ export function SearchRequestInterceptor(crudOptions: CrudOptions, factoryOption
return takeNumber;
}

getRelations(customSearchRequestOptions: CustomSearchRequestOptions): string[] {
if (Array.isArray(customSearchRequestOptions?.relations)) {
return customSearchRequestOptions.relations;
}
if (crudOptions.routes?.[method]?.relations === false) {
return [];
}
if (crudOptions.routes?.[method] && Array.isArray(crudOptions.routes?.[method]?.relations)) {
return crudOptions.routes[method].relations;
}
return factoryOption.relations;
}

deserialize<T>({ pagination, findOptions, sort }: CrudReadManyRequest<T>): Array<FindOptionsWhere<T>> {
const where = findOptions.where as Array<FindOptionsWhere<BaseEntity>>;
if (pagination.type === PaginationType.OFFSET) {
Expand Down

0 comments on commit 311bc52

Please sign in to comment.