update enum service
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import translates from '@/common/constants/translates/translates'
|
||||
import { translateEnumValue } from '@/common/utils/enum-translator.util'
|
||||
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
@@ -27,22 +26,12 @@ import {
|
||||
TspProviderResponseStatus,
|
||||
TspProviderType,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { GoldKarat, TspProviderCustomerType } from 'common/enums/enums'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
|
||||
@Injectable()
|
||||
export class EnumsService {
|
||||
constructor(private readonly redisService: RedisService) {}
|
||||
|
||||
private readonly cacheTtlSeconds = 24 * 60 * 60
|
||||
private readonly cacheBuildVersion =
|
||||
process.env.CACHE_BUILD_VERSION ||
|
||||
process.env.APP_BUILD_ID ||
|
||||
process.env.npm_package_version ||
|
||||
'local'
|
||||
|
||||
private prepareData(
|
||||
items: Record<string, string>,
|
||||
enumName: keyof typeof translates.enums,
|
||||
@@ -56,7 +45,7 @@ export class EnumsService {
|
||||
})
|
||||
}
|
||||
|
||||
private buildAllEnums() {
|
||||
getAllEnums() {
|
||||
return {
|
||||
PaymentMethodType: this.prepareData(PaymentMethodType, 'PaymentMethodType'),
|
||||
GoldKarat: this.prepareData(GoldKarat, 'GoldKarat'),
|
||||
@@ -102,28 +91,8 @@ export class EnumsService {
|
||||
}
|
||||
}
|
||||
|
||||
async getAllEnums() {
|
||||
const cacheKey = RedisKeyMaker.enumsAll(this.cacheBuildVersion)
|
||||
const cached = await this.redisService.getJson<Record<string, unknown[]>>(cacheKey)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
const enums = this.buildAllEnums()
|
||||
await this.redisService.setJson(cacheKey, enums, this.cacheTtlSeconds)
|
||||
return enums
|
||||
}
|
||||
|
||||
async getEnumValues(enumName: keyof typeof translates.enums) {
|
||||
const cacheKey = RedisKeyMaker.enumsValues(this.cacheBuildVersion, enumName)
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const enums = await this.getAllEnums()
|
||||
const items = enums[enumName] || []
|
||||
await this.redisService.setJson(cacheKey, items, this.cacheTtlSeconds)
|
||||
return ResponseMapper.list(items)
|
||||
getEnumValues(enumName: keyof typeof translates.enums) {
|
||||
const enums = this.getAllEnums()
|
||||
return ResponseMapper.list(enums[enumName] || [])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user