apps/api: add methods to proxycontroller

This commit is contained in:
obarykina 2024-03-07 17:32:42 +03:00
parent 0fdef002ed
commit 144fadb652
2 changed files with 43 additions and 6 deletions

View File

@ -63,8 +63,15 @@ export class ProxyController {
@Get('/queries')
public async getQueriesList(@Res() reply: FastifyReply) {
const res = await this.getAllQueries();
return reply.send(res);
}
private async getAllQueries() {
const list = await this.cacheManager.store.keys('*');
const res = (Object.keys(queryTTL) as Array<keyof typeof queryTTL>).reduce(
return (Object.keys(queryTTL) as Array<keyof typeof queryTTL>).reduce(
(acc, queryName) => {
const queries = list.filter((x) => x.split(' ').at(0) === queryName);
const ttl = queryTTL[queryName];
@ -74,8 +81,6 @@ export class ProxyController {
},
{} as Record<string, QueryItem>,
);
return reply.send(res);
}
@Delete('/delete-query')
@ -102,4 +107,37 @@ export class ProxyController {
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Delete('/delete-queries-by-key')
public async deleteQueriesByKey(
@Query('queriesGroup') queriesGroup: string,
@Res() reply: FastifyReply,
) {
try {
const allQueries = await this.getAllQueries();
const { queries } = allQueries[queriesGroup];
queries.forEach(async (query) => {
await this.cacheManager.del(query);
});
return reply.send('ok');
} catch (error) {
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Get('/get-query-value')
public async getQueryValue(
@Query('queryKey') queryKey: string,
@Res() reply: FastifyReply,
) {
try {
const value = this.cacheManager.get(queryKey);
return reply.send(value);
} catch (error) {
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

View File

@ -1,13 +1,12 @@
import type { ResponseQueries } from './types';
import getUrls from '@/config/urls';
import { withHandleError } from '@/utils/axios';
import type { QueryFunctionContext } from '@tanstack/react-query';
import axios from 'axios';
const { URL_CACHE_GET_QUERIES, URL_CACHE_DELETE_QUERY, URL_CACHE_RESET_QUERIES } = getUrls();
export function getQueries({ signal }: QueryFunctionContext) {
return withHandleError(axios.get<ResponseQueries>(URL_CACHE_GET_QUERIES, { signal })).then(
export function getQueries() {
return withHandleError(axios.get<ResponseQueries>(URL_CACHE_GET_QUERIES)).then(
({ data }) => data
);
}