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 9f7883697..44bfa39d4 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 @@ -191,6 +191,53 @@ describe('CacheAssignmentController (e2e)', () => { }); }); + it('POST /cache/user null after create', async () => { + interface ExtendedCacheCreatableInterface + extends Pick< + CacheCreatableInterface, + 'key' | 'expiresIn' | 'type' | 'data' + > { + assignee: { id: string | null } | null; + } + const payload: ExtendedCacheCreatableInterface = { + key: 'dashboard-1', + type: 'filter', + data: '{}', + expiresIn: '1d', + assignee: { id: user.id }, + }; + + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(201) + .then((res) => { + expect(res.body.key).toBe(payload.key); + expect(res.body.assignee.id).toBe(user.id); + }); + + payload.data = '{ "name": "John Doe" }'; + payload.expiresIn = null; + payload.assignee = { id: null }; + + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(400); + + payload.assignee = { id: '' }; + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(500); + + payload.assignee = null; + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(400); + }); + it('POST /cache/user Update', async () => { const payload: CacheCreatableInterface = { key: 'dashboard-1', diff --git a/packages/nestjs-cache/src/services/cache.service.ts b/packages/nestjs-cache/src/services/cache.service.ts index a6d5d810f..b5f6b4c40 100644 --- a/packages/nestjs-cache/src/services/cache.service.ts +++ b/packages/nestjs-cache/src/services/cache.service.ts @@ -270,6 +270,9 @@ export class CacheService implements CacheServiceInterface { const { key, type, assignee } = cache; try { const repo = repoProxy.repository(queryOptions); + if (!key || !type || !assignee || !assignee.id) { + return null; + } const cache = await repo.findOne({ where: { key, diff --git a/packages/nestjs-common/src/reference/reference-id.dto.ts b/packages/nestjs-common/src/reference/reference-id.dto.ts index 3f1161cba..3d7f4d803 100644 --- a/packages/nestjs-common/src/reference/reference-id.dto.ts +++ b/packages/nestjs-common/src/reference/reference-id.dto.ts @@ -1,6 +1,7 @@ import { Exclude, Expose } from 'class-transformer'; import { ApiProperty } from '@nestjs/swagger'; import { ReferenceIdInterface } from '@concepta/ts-core'; +import { IsString } from 'class-validator'; @Exclude() export class ReferenceIdDto implements ReferenceIdInterface { @@ -9,5 +10,6 @@ export class ReferenceIdDto implements ReferenceIdInterface { type: 'string', description: 'Unique identifier', }) + @IsString() id = ''; }