Skip to content

Commit

Permalink
feat: delete link from cache aswell
Browse files Browse the repository at this point in the history
  • Loading branch information
orig committed Dec 1, 2023
1 parent a0dbc33 commit 1023e19
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/backend/src/cache/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class AppCacheService {

set = this.cacheManager.set;
get = this.cacheManager.get;
del = this.cacheManager.del;

getCacheManager = this.cacheManager;
}
12 changes: 11 additions & 1 deletion apps/backend/src/core/reports/reports.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { IFindAllOptions } from '../entity.service';
import { SortOrder } from '../../shared/enums/sort-order.enum';
import { LinksService } from '../links/links.service';
import { OptionalJwtAuthGuard } from '../../auth/guards/optional-jwt-auth.guard';
import { AppCacheService } from '../../cache/cache.service';

describe('ReportsController', () => {
let app: INestApplication;
let reportsService: ReportsService;
let linksService: LinksService;
let cacheService: AppCacheService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -38,6 +40,12 @@ describe('ReportsController', () => {
delete: jest.fn(),
},
},
{
provide: AppCacheService,
useValue: {
del: jest.fn(),
},
},
],
})
.overrideGuard(OptionalJwtAuthGuard)
Expand All @@ -61,6 +69,7 @@ describe('ReportsController', () => {

reportsService = module.get<ReportsService>(ReportsService);
linksService = module.get<LinksService>(LinksService);
cacheService = module.get<AppCacheService>(AppCacheService);
});

afterEach(async () => {
Expand Down Expand Up @@ -236,10 +245,11 @@ describe('ReportsController', () => {
});

it('should delete link if deleteLink query parameter is set to true', async () => {
jest.spyOn(reportsService, 'findById').mockResolvedValue({ link: { id: '1' }, category: 1 } as any);
jest.spyOn(reportsService, 'findById').mockResolvedValue({ link: { id: '1', key: 'some_key' }, category: 1 } as any);

await request(app.getHttpServer()).delete('/reports/1?deleteLink=true').expect(200);
expect(linksService.delete).toHaveBeenCalledWith('1');
expect(cacheService.del).toHaveBeenCalledWith('some_key');
});

it('should not delete link if deleteLink query parameter is not set', async () => {
Expand Down
10 changes: 9 additions & 1 deletion apps/backend/src/core/reports/reports.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import { ReportsService } from './reports.service';
import { FindAllQueryDto, CreateReportDto } from './dto';
import { LinksService } from '../links/links.service';
import { OptionalJwtAuthGuard } from '../../auth/guards/optional-jwt-auth.guard';
import { AppCacheService } from '../../cache/cache.service';

@UseGuards(OptionalJwtAuthGuard, RolesGuard)
@Controller({
path: 'reports',
version: '1',
})
export class ReportsController {
constructor(private readonly reportsService: ReportsService, private readonly linksService: LinksService) {}
constructor(
private readonly reportsService: ReportsService,
private readonly linksService: LinksService,
private readonly cacheService: AppCacheService
) {}

@Get()
@Roles(Role.ADMIN)
Expand Down Expand Up @@ -60,6 +65,9 @@ export class ReportsController {
}

if (deleteLink) {
// Should delete the link from the cache as well
await this.cacheService.del(report.link?.key);

return this.linksService.delete(report.link.id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HiXMarkOutline } from '@qwikest/icons/heroicons';
import { Form, globalAction$, zod$ } from '@builder.io/qwik-city';
import { z } from 'zod';
import { ACCESS_COOKIE_NAME } from '../../../../shared/auth.service';
import { normalizeUrl } from '../../../../utils';

export const LINK_MODAL_ID = 'link-modal';

Expand All @@ -15,7 +16,7 @@ const useCreateLink = globalAction$(
Authorization: `Bearer ${cookie.get(ACCESS_COOKIE_NAME)?.value}`,
},
body: JSON.stringify({
url,
url: normalizeUrl(url),
expirationTime: null, // forever
}),
});
Expand All @@ -41,7 +42,9 @@ const useCreateLink = globalAction$(
.min(1, {
message: "The url field can't be empty.",
})
.url("The url you've entered is not valid..."),
.regex(/^(?:https?:\/\/)?(?:[\w-]+\.)+[a-z]{2,}(?::\d{1,5})?(?:\/\S*)?$/, {
message: "The url you've entered is not valid...",
}),
})
);

Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/routes/dashboard/admin/reports/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default component$(() => {

return (
<>
<div class="rounded-xl w-full p-5">
<div class="shadow-[0_8px_30px_rgb(0,0,0,0.12)] rounded-xl w-full p-5">
<TableServerPagination endpoint={`${process.env.CLIENTSIDE_API_DOMAIN}/api/v1/reports`} refetch={refetchSignal} columns={columns} />
</div>
</>
Expand Down

0 comments on commit 1023e19

Please sign in to comment.