Skip to content

Commit

Permalink
Merge pull request #179 from tnramalho/feature/cache-swagger-updates
Browse files Browse the repository at this point in the history
chore: add validations
  • Loading branch information
MrMaz authored Jun 28, 2024
2 parents 67bed32 + c3dc207 commit a9a647f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions packages/nestjs-cache/src/services/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/nestjs-common/src/reference/reference-id.dto.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,5 +10,6 @@ export class ReferenceIdDto implements ReferenceIdInterface {
type: 'string',
description: 'Unique identifier',
})
@IsString()
id = '';
}

0 comments on commit a9a647f

Please sign in to comment.