2021-08-14 16:55:40 +02:00
|
|
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service';
|
2022-06-05 19:00:20 +02:00
|
|
|
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
|
2021-09-11 09:27:22 +02:00
|
|
|
import type { RequestWithUser } from '@ghostfolio/common/types';
|
2022-06-05 19:00:20 +02:00
|
|
|
import {
|
|
|
|
Controller,
|
|
|
|
HttpException,
|
|
|
|
Inject,
|
|
|
|
Post,
|
|
|
|
UseGuards
|
|
|
|
} from '@nestjs/common';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { REQUEST } from '@nestjs/core';
|
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
2022-06-05 19:00:20 +02:00
|
|
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Controller('cache')
|
|
|
|
export class CacheController {
|
|
|
|
public constructor(
|
|
|
|
private readonly redisCacheService: RedisCacheService,
|
|
|
|
@Inject(REQUEST) private readonly request: RequestWithUser
|
2022-06-05 19:00:20 +02:00
|
|
|
) {}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Post('flush')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async flushCache(): Promise<void> {
|
2022-06-05 19:00:20 +02:00
|
|
|
if (
|
|
|
|
!hasPermission(
|
|
|
|
this.request.user.permissions,
|
|
|
|
permissions.accessAdminControl
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
|
|
StatusCodes.FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-11 13:40:15 +02:00
|
|
|
return this.redisCacheService.reset();
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
}
|