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