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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with
14 additions and
11 deletions
-
apps/api/src/services/prisma/prisma.service.ts
-
apps/api/src/services/property/property.service.ts
-
apps/api/src/services/redis-cache/redis-cache.service.mock.ts
-
apps/api/src/services/redis-cache/redis-cache.service.ts
|
|
|
@ -25,7 +25,7 @@ export class PrismaService |
|
|
|
|
|
|
|
try { |
|
|
|
customLogLevels = JSON.parse( |
|
|
|
configService.get<string>('LOG_LEVELS') |
|
|
|
configService.get<string>('LOG_LEVELS') ?? '' |
|
|
|
) as LogLevel[]; |
|
|
|
} catch {} |
|
|
|
|
|
|
|
|
|
|
|
@ -16,8 +16,8 @@ import { PropertyValue } from './interfaces/interfaces'; |
|
|
|
export class PropertyService { |
|
|
|
private static readonly CACHE_TTL = ms('1 minute'); |
|
|
|
|
|
|
|
private cachedProperties: Promise<Property[]>; |
|
|
|
private cachedPropertiesExpiresAt: Date; |
|
|
|
private cachedProperties?: Promise<Property[]>; |
|
|
|
private cachedPropertiesExpiresAt?: Date; |
|
|
|
|
|
|
|
public constructor(private readonly prismaService: PrismaService) {} |
|
|
|
|
|
|
|
@ -91,6 +91,7 @@ export class PropertyService { |
|
|
|
|
|
|
|
if ( |
|
|
|
this.cachedProperties && |
|
|
|
this.cachedPropertiesExpiresAt && |
|
|
|
isBefore(new Date(), this.cachedPropertiesExpiresAt) |
|
|
|
) { |
|
|
|
return this.cachedProperties; |
|
|
|
|
|
|
|
@ -3,8 +3,8 @@ import { PerformanceCalculationType } from '@ghostfolio/common/types/performance |
|
|
|
|
|
|
|
export const RedisCacheServiceMock = { |
|
|
|
cache: new Map<string, string>(), |
|
|
|
get: (key: string): Promise<string> => { |
|
|
|
const value = RedisCacheServiceMock.cache.get(key) || null; |
|
|
|
get: (key: string): Promise<string | undefined> => { |
|
|
|
const value = RedisCacheServiceMock.cache.get(key); |
|
|
|
|
|
|
|
return Promise.resolve(value); |
|
|
|
}, |
|
|
|
@ -19,7 +19,7 @@ export const RedisCacheServiceMock = { |
|
|
|
}): string => { |
|
|
|
const filtersHash = filters?.length; |
|
|
|
|
|
|
|
return `portfolio-snapshot-${userId}-${calculationType}${filtersHash > 0 ? `-${filtersHash}` : ''}`; |
|
|
|
return `portfolio-snapshot-${userId}-${calculationType}${filtersHash && filtersHash > 0 ? `-${filtersHash}` : ''}`; |
|
|
|
}, |
|
|
|
reset: () => { |
|
|
|
RedisCacheServiceMock.cache.clear(); |
|
|
|
|
|
|
|
@ -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); |
|
|
|
} |
|
|
|
|
|
|
|
@ -42,9 +42,11 @@ export class RedisCacheService { |
|
|
|
const prefix = aPrefix; |
|
|
|
|
|
|
|
try { |
|
|
|
for await (const [key] of this.client.iterator({})) { |
|
|
|
if ((prefix && key.startsWith(prefix)) || !prefix) { |
|
|
|
keys.push(key); |
|
|
|
if (this.client.iterator) { |
|
|
|
for await (const [key] of this.client.iterator({})) { |
|
|
|
if ((prefix && key.startsWith(prefix)) || !prefix) { |
|
|
|
keys.push(key); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch {} |
|
|
|
@ -63,7 +65,7 @@ export class RedisCacheService { |
|
|
|
}) { |
|
|
|
let portfolioSnapshotKey = `${this.getPortfolioSnapshotKeyPrefix({ userId })}-${calculationType}`; |
|
|
|
|
|
|
|
if (filters?.length > 0) { |
|
|
|
if (filters && filters.length > 0) { |
|
|
|
const filtersHash = createHash('sha256') |
|
|
|
.update(JSON.stringify(filters)) |
|
|
|
.digest('hex'); |
|
|
|
|