diff --git a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts index 3673bd78..e140ed21 100644 --- a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts +++ b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts @@ -111,7 +111,7 @@ describe('CacheAssignmentController (e2e)', () => { }); }); - it('POST /cache/user', async () => { + it('POST /cache/user creating user with success', async () => { const payload: CacheCreatableInterface = { key: 'dashboard-1', type: 'filter', @@ -265,6 +265,23 @@ describe('CacheAssignmentController (e2e)', () => { expect(res.body.data).toBe(payload.data); expect(res.body.assignee.id).toBe(user.id); }); + + const url = + `/cache/user/` + + `?filter[0]=key||$eq||${payload.key}` + + `&filter[1]=type||$eq||${payload.type}` + + `&filter[2]=assignee.id||$eq||${payload.assignee.id}`; + // Assuming your endpoint can filter by key and type + await supertest(app.getHttpServer()) + .get(url) + .expect(200) + .then((res) => { + const response = res.body[0]; + assert.strictEqual(response.assignee.id, user.id); + assert.strictEqual(response.key, payload.key); + assert.strictEqual(response.type, payload.type); + assert.strictEqual(response.data, payload.data); + }); }); it('DELETE /cache/user/:id', async () => { diff --git a/packages/nestjs-cache/src/controllers/cache-crud.controller.ts b/packages/nestjs-cache/src/controllers/cache-crud.controller.ts index 31df075c..4735b310 100644 --- a/packages/nestjs-cache/src/controllers/cache-crud.controller.ts +++ b/packages/nestjs-cache/src/controllers/cache-crud.controller.ts @@ -115,50 +115,20 @@ export class CacheCrudController /** * Create one * - * @param crudRequest - the CRUD request object + * @param _crudRequest - the CRUD request object * @param cacheCreateDto - cache create dto * @param assignment - The cache assignment */ @CrudCreateOne() @AccessControlCreateOne(CacheResource.One) async createOne( - @CrudRequest() crudRequest: CrudRequestInterface, + // TODO: this is not being used anymore + @CrudRequest() _crudRequest: CrudRequestInterface, @CrudBody() cacheCreateDto: CacheCreateDto, @Param('assignment') assignment: ReferenceAssignment, ) { - const expirationDate = getExpirationDate( - cacheCreateDto.expiresIn ?? this.settings.expiresIn, - ); - - const existingCache = await this.cacheService.get( - assignment, - cacheCreateDto, - ); - - // update or create - if (existingCache) { - crudRequest.parsed.search.$and?.push({ - id: { - $eq: existingCache.id, - }, - }); - // call crud service to create - const response = await this.getCrudService(assignment).updateOne( - crudRequest, - { - id: existingCache.id, - ...cacheCreateDto, - expirationDate, - }, - ); - return response; - } else { - // call crud service to create - return this.getCrudService(assignment).createOne(crudRequest, { - ...cacheCreateDto, - expirationDate, - }); - } + const response = await this.cacheService.save(assignment, cacheCreateDto); + return response; } /** diff --git a/packages/nestjs-cache/src/interfaces/cache-service.interface.ts b/packages/nestjs-cache/src/interfaces/cache-service.interface.ts index 00eb3407..3d47a4b8 100644 --- a/packages/nestjs-cache/src/interfaces/cache-service.interface.ts +++ b/packages/nestjs-cache/src/interfaces/cache-service.interface.ts @@ -2,6 +2,7 @@ import { ReferenceAssignment } from '@concepta/ts-core'; import { QueryOptionsInterface } from '@concepta/typeorm-common'; import { CacheClearInterface, + CacheCreatableInterface, CacheCreateInterface, CacheDeleteInterface, CacheGetOneInterface, @@ -15,6 +16,14 @@ export interface CacheServiceInterface CacheUpdateInterface, CacheGetOneInterface, CacheClearInterface { + + // TODO: should i create a unique interface for save? + save( + assignment: ReferenceAssignment, + cache: CacheCreatableInterface, + options?: QueryOptionsInterface, + ): Promise; + getAssignedCaches( assignment: ReferenceAssignment, cache: Pick, diff --git a/packages/nestjs-cache/src/services/cache.service.ts b/packages/nestjs-cache/src/services/cache.service.ts index ac10b617..b01aced5 100644 --- a/packages/nestjs-cache/src/services/cache.service.ts +++ b/packages/nestjs-cache/src/services/cache.service.ts @@ -32,6 +32,19 @@ export class CacheService implements CacheServiceInterface { protected readonly settings: CacheSettingsInterface, ) {} + async save( + assignment: ReferenceAssignment, + cache: CacheCreateDto, + queryOptions?: QueryOptionsInterface, + ): Promise { + const existingCache = await this.get(assignment, cache, queryOptions); + if (existingCache) { + return await this.update(assignment, cache, queryOptions); + } else { + return await this.create(assignment, cache, queryOptions); + } + } + /** * Create a cache with a for the given assignee. *