Browse Source

Task/improve type safety in prisma, property, and redis cache services (#8355)

Improve type safety
pull/8366/head
Kenrick Tandrian 4 days ago
committed by GitHub
parent
commit
bbefa35f4f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      apps/api/src/services/prisma/prisma.service.ts
  2. 5
      apps/api/src/services/property/property.service.ts
  3. 6
      apps/api/src/services/redis-cache/redis-cache.service.mock.ts
  4. 12
      apps/api/src/services/redis-cache/redis-cache.service.ts

2
apps/api/src/services/prisma/prisma.service.ts

@ -25,7 +25,7 @@ export class PrismaService
try { try {
customLogLevels = JSON.parse( customLogLevels = JSON.parse(
configService.get<string>('LOG_LEVELS') configService.get<string>('LOG_LEVELS') ?? ''
) as LogLevel[]; ) as LogLevel[];
} catch {} } catch {}

5
apps/api/src/services/property/property.service.ts

@ -16,8 +16,8 @@ import { PropertyValue } from './interfaces/interfaces';
export class PropertyService { export class PropertyService {
private static readonly CACHE_TTL = ms('1 minute'); private static readonly CACHE_TTL = ms('1 minute');
private cachedProperties: Promise<Property[]>; private cachedProperties?: Promise<Property[]>;
private cachedPropertiesExpiresAt: Date; private cachedPropertiesExpiresAt?: Date;
public constructor(private readonly prismaService: PrismaService) {} public constructor(private readonly prismaService: PrismaService) {}
@ -91,6 +91,7 @@ export class PropertyService {
if ( if (
this.cachedProperties && this.cachedProperties &&
this.cachedPropertiesExpiresAt &&
isBefore(new Date(), this.cachedPropertiesExpiresAt) isBefore(new Date(), this.cachedPropertiesExpiresAt)
) { ) {
return this.cachedProperties; return this.cachedProperties;

6
apps/api/src/services/redis-cache/redis-cache.service.mock.ts

@ -3,8 +3,8 @@ import { PerformanceCalculationType } from '@ghostfolio/common/types/performance
export const RedisCacheServiceMock = { export const RedisCacheServiceMock = {
cache: new Map<string, string>(), cache: new Map<string, string>(),
get: (key: string): Promise<string> => { get: (key: string): Promise<string | undefined> => {
const value = RedisCacheServiceMock.cache.get(key) || null; const value = RedisCacheServiceMock.cache.get(key);
return Promise.resolve(value); return Promise.resolve(value);
}, },
@ -19,7 +19,7 @@ export const RedisCacheServiceMock = {
}): string => { }): string => {
const filtersHash = filters?.length; const filtersHash = filters?.length;
return `portfolio-snapshot-${userId}-${calculationType}${filtersHash > 0 ? `-${filtersHash}` : ''}`; return `portfolio-snapshot-${userId}-${calculationType}${filtersHash && filtersHash > 0 ? `-${filtersHash}` : ''}`;
}, },
reset: () => { reset: () => {
RedisCacheServiceMock.cache.clear(); RedisCacheServiceMock.cache.clear();

12
apps/api/src/services/redis-cache/redis-cache.service.ts

@ -33,7 +33,7 @@ export class RedisCacheService {
}); });
} }
public async get(key: string): Promise<string> { public async get(key: string): Promise<string | undefined> {
return this.cache.get(key); return this.cache.get(key);
} }
@ -42,9 +42,11 @@ export class RedisCacheService {
const prefix = aPrefix; const prefix = aPrefix;
try { try {
for await (const [key] of this.client.iterator({})) { if (this.client.iterator) {
if ((prefix && key.startsWith(prefix)) || !prefix) { for await (const [key] of this.client.iterator({})) {
keys.push(key); if ((prefix && key.startsWith(prefix)) || !prefix) {
keys.push(key);
}
} }
} }
} catch {} } catch {}
@ -63,7 +65,7 @@ export class RedisCacheService {
}) { }) {
let portfolioSnapshotKey = `${this.getPortfolioSnapshotKeyPrefix({ userId })}-${calculationType}`; let portfolioSnapshotKey = `${this.getPortfolioSnapshotKeyPrefix({ userId })}-${calculationType}`;
if (filters?.length > 0) { if (filters && filters.length > 0) {
const filtersHash = createHash('sha256') const filtersHash = createHash('sha256')
.update(JSON.stringify(filters)) .update(JSON.stringify(filters))
.digest('hex'); .digest('hex');

Loading…
Cancel
Save