Skip to content

Commit

Permalink
fix: cache by user cache (#4416)
Browse files Browse the repository at this point in the history
* fix: cache by user cache

* [skip ci] Add swagger.yaml

---------

Co-authored-by: Ihar <[email protected]>
Co-authored-by: envision-ci-agent <[email protected]>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent d2cf3f3 commit 4f9117f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 13 deletions.
6 changes: 3 additions & 3 deletions api-gateway/src/helpers/interceptors/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export class CacheInterceptor implements NestInterceptor {
const isFastify = Reflect.getMetadata(META_DATA.FASTIFY, context.getHandler());

const token = request.headers.authorization?.split(' ')[1];
let user = {};
let user = null;

if (token) {
const users: Users = new Users();
try {
user = await users.getUserByToken(token);
user = await users.getUserByToken(token) ?? null;
} catch (error) {
throw new HttpException(error.message, HttpStatus.UNAUTHORIZED);
}
Expand Down Expand Up @@ -99,4 +99,4 @@ export class CacheInterceptor implements NestInterceptor {
}),
);
}
}
}
4 changes: 2 additions & 2 deletions api-gateway/src/helpers/interceptors/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IAuthUser } from '@guardian/common';
//constants
import { CACHE_PREFIXES } from '../../../constants/index.js';

export function getCacheKey(routes: string[], user: IAuthUser | {}, prefix: string = CACHE_PREFIXES.TAG): string[] {
export function getCacheKey(routes: string[], user: IAuthUser | null, prefix: string = CACHE_PREFIXES.TAG): string[] {
const hashUser: string = getHash(user)
return routes.map(route => `${prefix}${route}:${hashUser}`);
}
}
8 changes: 4 additions & 4 deletions api-gateway/src/helpers/interceptors/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import crypto from 'crypto';

//types and interfaces
import { IAuthUser } from '@guardian/common';
import {IAuthUser} from '@guardian/common';

export function getHash(user: IAuthUser | {}): string {
return crypto.createHash('md5').update(JSON.stringify(user)).digest('hex');
}
export function getHash(user: IAuthUser | null): string {
return crypto.createHash('md5').update(JSON.stringify(user.id ?? 'anonymous')).digest('hex');
}
2 changes: 1 addition & 1 deletion swagger-analytics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ info:
the heart of the Guardian solution is a sophisticated Policy Workflow Engine
(PWE) that enables applications to offer a requirements-based tokenization
implementation.
version: 2.27.1
version: 3.0.0
contact:
name: API developer
url: https://envisionblockchain.com
Expand Down
2 changes: 1 addition & 1 deletion swagger-indexer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ info:
the heart of the Guardian solution is a sophisticated Policy Workflow Engine
(PWE) that enables applications to offer a requirements-based tokenization
implementation.
version: 2.27.1
version: 3.0.0
contact:
name: API developer
url: https://envisionblockchain.com
Expand Down
3 changes: 1 addition & 2 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13142,7 +13142,7 @@ info:
the heart of the Guardian solution is a sophisticated Policy Workflow Engine
(PWE) that enables applications to offer a requirements-based tokenization
implementation.
version: 2.27.1
version: 3.0.0
contact:
name: API developer
url: https://envisionblockchain.com
Expand Down Expand Up @@ -16150,7 +16150,6 @@ components:
- ARTIFACTS_FILE_DELETE
- CONTRACTS_CONTRACT_READ
- CONTRACTS_CONTRACT_EXECUTE
- CONTRACTS_CONTRACT_MANAGE
- MODULES_MODULE_READ
- MODULES_MODULE_CREATE
- MODULES_MODULE_UPDATE
Expand Down

0 comments on commit 4f9117f

Please sign in to comment.