Skip to content

Commit

Permalink
chore: move save to service
Browse files Browse the repository at this point in the history
  • Loading branch information
tnramalho committed Jul 16, 2024
1 parent b7ceeb2 commit 6f28db8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 () => {
Expand Down
40 changes: 5 additions & 35 deletions packages/nestjs-cache/src/controllers/cache-crud.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReferenceAssignment } from '@concepta/ts-core';
import { QueryOptionsInterface } from '@concepta/typeorm-common';
import {
CacheClearInterface,
CacheCreatableInterface,
CacheCreateInterface,
CacheDeleteInterface,
CacheGetOneInterface,
Expand All @@ -15,6 +16,14 @@ export interface CacheServiceInterface
CacheUpdateInterface<QueryOptionsInterface>,
CacheGetOneInterface<QueryOptionsInterface>,
CacheClearInterface<QueryOptionsInterface> {

Check failure on line 19 in packages/nestjs-cache/src/interfaces/cache-service.interface.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Delete `⏎··`
// TODO: should i create a unique interface for save?
save(
assignment: ReferenceAssignment,
cache: CacheCreatableInterface,
options?: QueryOptionsInterface,
): Promise<CacheInterface>;

getAssignedCaches(
assignment: ReferenceAssignment,
cache: Pick<CacheInterface, 'assignee'>,
Expand Down
13 changes: 13 additions & 0 deletions packages/nestjs-cache/src/services/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ export class CacheService implements CacheServiceInterface {
protected readonly settings: CacheSettingsInterface,
) {}

async save(
assignment: ReferenceAssignment,
cache: CacheCreateDto,
queryOptions?: QueryOptionsInterface,
): Promise<CacheInterface> {
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.
*
Expand Down

0 comments on commit 6f28db8

Please sign in to comment.