refactor: streamline Redis caching logic across services

- Implemented a unified `getAndSet` method in RedisService to handle caching for single, list, and paginated responses.
- Removed redundant cache checks and writes in various services, simplifying the code and improving readability.
- Updated GoodsService, StockKeepingUnitsService, PartnerActivatedLicensesService, and others to utilize the new caching mechanism.
- Adjusted Prisma connection limit for better resource management.
This commit is contained in:
2026-05-21 17:27:37 +03:30
parent 1d47fb1a1d
commit 9aa12184a1
16 changed files with 689 additions and 1896 deletions
+14 -18
View File
@@ -57,26 +57,22 @@ export class GoodsService {
async findAll(guildId: string) {
const cacheKey = RedisKeyMaker.guildGoodsList(guildId)
const cached = await this.redisService.getJson<{ goods: unknown[]; total: number }>(
return this.redisService.getAndSet(
cacheKey,
'paginate',
async () => {
const [goods, total] = await this.prisma.$transaction([
this.prisma.good.findMany({
where: this.defaultWhere(guildId),
select: this.defaultSelect,
}),
this.prisma.good.count(),
])
return { items: goods, total }
},
this.listCacheTtlSeconds,
)
if (cached) {
return ResponseMapper.paginate(cached.goods, { total: cached.total })
}
const [goods, total] = await this.prisma.$transaction([
this.prisma.good.findMany({
where: this.defaultWhere(guildId),
select: this.defaultSelect,
}),
this.prisma.good.count(),
])
await this.redisService.setJson(cacheKey, { goods, total }, this.listCacheTtlSeconds)
return ResponseMapper.paginate(goods, {
total,
})
}
async findOne(guild_id: string, id: string) {
@@ -31,20 +31,20 @@ export class StockKeepingUnitsService {
async findAll(guild_id: string) {
const cacheKey = this.getListCacheKey(guild_id)
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
return ResponseMapper.list(cached)
}
return await this.redisService.getAndSet(
cacheKey,
'list',
async () => {
const items = await this.prisma.stockKeepingUnits.findMany({
where: { guild_id },
orderBy: { created_at: 'desc' },
})
const items = await this.prisma.stockKeepingUnits.findMany({
where: { guild_id },
orderBy: { created_at: 'desc' },
})
const mappedData = items.map(this.mapData)
await this.redisService.setJson(cacheKey, mappedData, this.listCacheTtlSeconds)
return ResponseMapper.list(mappedData)
const mappedData = items.map(this.mapData)
return mappedData
},
this.listCacheTtlSeconds,
)
}
async create(guild_id: string, data: CreateStockKeepingUnitDto) {
@@ -17,14 +17,6 @@ export class PartnerActivatedLicensesService {
) {}
async findAll(partner_id: string, page = 1, perPage = 10) {
const cacheKey = RedisKeyMaker.partnerActivatedLicensesList(partner_id, page, perPage)
const cached = await this.redisService.getJson<{ data: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.data, { page, perPage, total: cached.total })
}
const defaultWhere: LicenseActivationWhereInput = {
license: {
charge_transaction: {
@@ -32,69 +24,70 @@ export class PartnerActivatedLicensesService {
},
},
}
const [activatedLicense, total] = await this.prisma.$transaction(async tx => [
await tx.licenseActivation.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: {
id: true,
starts_at: true,
expires_at: true,
created_at: true,
account_allocations: {
select: {
id: true,
account_id: true,
const cacheKey = RedisKeyMaker.partnerActivatedLicensesList(partner_id, page, perPage)
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const [activatedLicense, total] = await this.prisma.$transaction(async tx => [
await tx.licenseActivation.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: {
id: true,
starts_at: true,
expires_at: true,
created_at: true,
account_allocations: {
select: {
id: true,
account_id: true,
},
},
},
business_activity: {
select: {
id: true,
name: true,
consumer: {
select: {
id: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
business_activity: {
select: {
id: true,
name: true,
consumer: {
select: {
id: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
},
},
},
},
},
},
}),
await tx.licenseActivation.count({
where: defaultWhere,
}),
])
}),
await tx.licenseActivation.count({
where: defaultWhere,
}),
])
const mappedLicenses = activatedLicense.map(license => {
const { account_allocations, business_activity, ...rest } = license
const { consumer, ...businessActivityRest } = business_activity
const mappedLicenses = activatedLicense.map(license => {
const { account_allocations, business_activity, ...rest } = license
const { consumer, ...businessActivityRest } = business_activity
const mappedConsumer = consumer_mappersUtil(consumer)
const mappedConsumer = consumer_mappersUtil(consumer)
return {
...rest,
license: {
accounts_limit: account_allocations.length,
allocated_account_count: account_allocations.filter(
allocation => allocation.account_id !== null,
).length,
},
business_activity: {
...business_activity,
consumer_name: mappedConsumer.name,
},
}
})
return {
...rest,
license: {
accounts_limit: account_allocations.length,
allocated_account_count: account_allocations.filter(
allocation => allocation.account_id !== null,
).length,
},
business_activity: {
...business_activity,
consumer_name: mappedConsumer.name,
},
items: mappedLicenses,
page,
perPage,
total,
}
})
await this.redisService.setJson(cacheKey, { data: mappedLicenses, total }, 300)
return ResponseMapper.paginate(mappedLicenses, {
page,
perPage,
total,
})
}
// async update(partnerId: string, id: string, data: UpdateLicenseDto) {
@@ -65,56 +65,45 @@ export class PartnerLicenseChargeTransactionService {
page,
perPage,
)
const cached = await this.redisService.getJson<{ data: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.data, { page, perPage, total: cached.total })
}
const defaultWhere: LicenseChargeTransactionWhereInput = {
partner_id,
}
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const [transactions, total] = await this.prisma.$transaction(async tx => [
await tx.licenseChargeTransaction.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: this.defaultSelect,
}),
await tx.licenseChargeTransaction.count({
where: defaultWhere,
}),
])
const [transactions, total] = await this.prisma.$transaction(async tx => [
await tx.licenseChargeTransaction.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: this.defaultSelect,
}),
await tx.licenseChargeTransaction.count({
where: defaultWhere,
}),
])
const mappedTransactions = transactions.map(this.mappedTransaction)
await this.redisService.setJson(cacheKey, { data: mappedTransactions, total }, 300)
return ResponseMapper.paginate(mappedTransactions, {
page,
perPage,
total,
const mappedTransactions = transactions.map(this.mappedTransaction)
return {
items: mappedTransactions,
page,
perPage,
total,
}
})
}
async findOne(partner_id: string, id: string) {
const cacheKey = RedisKeyMaker.partnerLicenseChargeTransactionDetail(partner_id, id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
where: {
id,
partner_id,
},
select: this.defaultSelect,
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
where: {
id,
partner_id,
},
select: this.defaultSelect,
})
return this.mappedTransaction(transaction)
})
const mappedTransaction = this.mappedTransaction(transaction)
await this.redisService.setJson(cacheKey, mappedTransaction, 300)
return ResponseMapper.single(mappedTransaction)
}
async create(partner_id: string, data: ChargeLicenseDto) {
+12 -25
View File
@@ -143,39 +143,26 @@ export class PartnersService {
async findAll() {
const cacheKey = RedisKeyMaker.partnersList()
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
console.log('cached', cached)
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
const partners = await this.prisma.partner.findMany({
select: this.defaultSelect,
})
return ResponseMapper.list(cached)
}
const partners = await this.prisma.partner.findMany({
select: this.defaultSelect,
return partners.map(this.mapPartner)
})
const mappedPartners = partners.map(this.mapPartner)
await this.redisService.setJson(cacheKey, mappedPartners, 300)
return ResponseMapper.list(mappedPartners)
}
async findOne(id: string) {
const cacheKey = RedisKeyMaker.partnerDetail(id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const partner = await this.prisma.partner.findUniqueOrThrow({
where: { id },
select: this.defaultSelect,
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const partner = await this.prisma.partner.findUniqueOrThrow({
where: { id },
select: this.defaultSelect,
})
return this.mapPartner(partner)
})
const mappedPartner = this.mapPartner(partner)
await this.redisService.setJson(cacheKey, mappedPartner, 300)
return ResponseMapper.single(mappedPartner)
}
async create(data: CreatePartnerDto, logo?: any) {
@@ -61,34 +61,23 @@ export class BusinessActivitiesService {
async findAll(consumer_id: string) {
const cacheKey = RedisKeyMaker.consumerBusinessActivitiesList(consumer_id)
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
return ResponseMapper.list(cached)
}
const businessActivities =
await this.businessActivitiesQueryService.findAllByConsumer(
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
return await this.businessActivitiesQueryService.findAllByConsumer(
consumer_id,
this.defaultSelect,
)
await this.redisService.setJson(cacheKey, businessActivities, this.cacheTtlSeconds)
return ResponseMapper.list(businessActivities)
})
}
async findOne(consumer_id: string, id: string) {
const cacheKey = RedisKeyMaker.consumerBusinessActivityInfo(consumer_id, id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const businessActivity = await this.businessActivitiesQueryService.findOneByConsumer(
consumer_id,
id,
this.defaultSelect,
)
await this.redisService.setJson(cacheKey, businessActivity, this.cacheTtlSeconds)
return ResponseMapper.single(businessActivity)
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
return await this.businessActivitiesQueryService.findOneByConsumer(
consumer_id,
id,
this.defaultSelect,
)
})
}
async update(consumer_id: string, id: string, data: any) {
+13 -17
View File
@@ -21,25 +21,21 @@ export class ConsumerService {
async getInfo(consumer_id: string) {
const cacheKey = RedisKeyMaker.consumerInfo(consumer_id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const consumer = await this.prisma.consumer.findUniqueOrThrow({
where: {
id: consumer_id,
},
select: {
id: true,
status: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
},
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const consumer = await this.prisma.consumer.findUniqueOrThrow({
where: {
id: consumer_id,
},
select: {
id: true,
status: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
},
})
return consumer_mappersUtil(consumer)
})
const mappedConsumer = consumer_mappersUtil(consumer)
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
return ResponseMapper.single(mappedConsumer)
}
async updateInfo(consumer_id: string, data: UpdateConsumerInfoDto) {
@@ -103,30 +103,26 @@ export class BusinessActivitiesService {
page,
perPage,
)
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
}
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const where = this.defaultWhere(partner_id, consumer_id)
const [businessActivities, total] = await this.prisma.$transaction(async tx => [
await tx.businessActivity.findMany({
where,
select: this.defaultSelect,
skip: (page - 1) * perPage,
take: perPage,
}),
await tx.businessActivity.count({ where }),
])
const mappedItems = businessActivities.map(this.prepareBusinessActivityResponse)
const where = this.defaultWhere(partner_id, consumer_id)
const [businessActivities, total] = await this.prisma.$transaction(async tx => [
await tx.businessActivity.findMany({
where,
select: this.defaultSelect,
skip: (page - 1) * perPage,
take: perPage,
}),
await tx.businessActivity.count({ where }),
])
const mappedItems = businessActivities.map(this.prepareBusinessActivityResponse)
await this.redisService.setJson(
cacheKey,
{ items: mappedItems, total },
this.cacheTtlSeconds,
)
return ResponseMapper.paginate(mappedItems, { total, page, perPage })
return {
items: mappedItems,
total,
page,
perPage,
}
})
}
async findOne(partner_id: string, consumer_id: string, id: string) {
@@ -135,21 +131,16 @@ export class BusinessActivitiesService {
consumer_id,
id,
)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const businessActivity = await this.prisma.businessActivity.findUnique({
where: {
...this.defaultWhere(partner_id, consumer_id),
id,
},
select: this.defaultSelect,
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const businessActivity = await this.prisma.businessActivity.findUnique({
where: {
...this.defaultWhere(partner_id, consumer_id),
id,
},
select: this.defaultSelect,
})
return this.prepareBusinessActivityResponse(businessActivity)
})
const mappedItem = this.prepareBusinessActivityResponse(businessActivity)
await this.redisService.setJson(cacheKey, mappedItem, this.cacheTtlSeconds)
return ResponseMapper.single(mappedItem)
}
async create(
@@ -65,44 +65,40 @@ export class BusinessActivityComplexesService {
page,
perPage,
)
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
}
const where = this.defaultWhere(partner_id, consumer_id, business_activity_id)
const [complexes, total] = await this.prisma.$transaction(async tx => [
await tx.complex.findMany({
where,
select: {
...this.defaultSelect,
_count: {
select: {
pos_list: true,
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const where = this.defaultWhere(partner_id, consumer_id, business_activity_id)
const [complexes, total] = await this.prisma.$transaction(async tx => [
await tx.complex.findMany({
where,
select: {
...this.defaultSelect,
_count: {
select: {
pos_list: true,
},
},
},
},
skip: (page - 1) * perPage,
take: perPage,
}),
await tx.complex.count({ where }),
])
skip: (page - 1) * perPage,
take: perPage,
}),
await tx.complex.count({ where }),
])
const mappedComplexes = complexes.map(complex => {
const { _count, ...rest } = complex
return {
...rest,
pos_count: _count.pos_list,
}
})
const mappedComplexes = complexes.map(complex => {
const { _count, ...rest } = complex
return {
...rest,
pos_count: _count.pos_list,
items: mappedComplexes,
total,
page,
perPage,
}
})
await this.redisService.setJson(
cacheKey,
{ items: mappedComplexes, total },
this.cacheTtlSeconds,
)
return ResponseMapper.paginate(mappedComplexes, { total, page, perPage })
}
async findOne(
@@ -117,25 +113,21 @@ export class BusinessActivityComplexesService {
business_activity_id,
id,
)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const complex = await this.prisma.complex.findFirst({
where: {
...this.defaultWhere(partner_id, consumer_id, business_activity_id),
id,
},
select: this.defaultSelect,
})
const complex = await this.prisma.complex.findFirst({
where: {
...this.defaultWhere(partner_id, consumer_id, business_activity_id),
id,
},
select: this.defaultSelect,
if (!complex) {
throw new BadRequestException('شعبه مورد نظر یافت نشد.')
}
return complex
})
if (!complex) {
throw new BadRequestException('شعبه مورد نظر یافت نشد.')
}
await this.redisService.setJson(cacheKey, complex, this.cacheTtlSeconds)
return ResponseMapper.single(complex)
}
async create(
@@ -64,60 +64,42 @@ export class PartnerConsumersService {
async findAll(partner_id: string, page = 1, perPage = 10) {
const cacheKey = RedisKeyMaker.partnerConsumersList(partner_id, page, perPage)
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
}
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const [consumers, total] = await this.prisma.$transaction(async tx => [
await tx.consumer.findMany({
where: this.defaultWhere(partner_id),
select: this.defaultSelect,
skip: (page - 1) * perPage,
take: 10,
}),
await tx.consumer.count({
where: this.defaultWhere(partner_id),
}),
])
const mappedConsumers = consumers.map(mapConsumerWithLicenseUtil)
const [consumers, total] = await this.prisma.$transaction(async tx => [
await tx.consumer.findMany({
where: this.defaultWhere(partner_id),
select: this.defaultSelect,
skip: (page - 1) * perPage,
take: 10,
}),
await tx.consumer.count({
where: this.defaultWhere(partner_id),
}),
])
const mappedItems = consumers.map(mapConsumerWithLicenseUtil)
await this.redisService.setJson(
cacheKey,
{ items: mappedItems, total },
this.infoCacheTtlSeconds,
)
return ResponseMapper.paginate(mappedItems, {
total,
page,
perPage,
return {
items: mappedConsumers,
total,
page,
perPage,
}
})
}
async findOne(partner_id: string, consumer_id: string) {
const cacheKey = RedisKeyMaker.partnerConsumerInfo(partner_id, consumer_id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const consumer = await this.prisma.consumer.findUniqueOrThrow({
where: {
...(this.defaultWhere(partner_id) as any),
id: consumer_id,
},
select: this.defaultSelect,
})
const consumer = await this.prisma.consumer.findUniqueOrThrow({
where: {
...(this.defaultWhere(partner_id) as any),
id: consumer_id,
},
select: this.defaultSelect,
return mapConsumerWithLicenseUtil(consumer)
})
const mappedConsumer = mapConsumerWithLicenseUtil(consumer)
await this.redisService.setJson(
RedisKeyMaker.consumerInfo(consumer_id),
mappedConsumer,
this.infoCacheTtlSeconds,
)
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
return ResponseMapper.single(mappedConsumer)
}
async create(partner_id: string, data: CreateConsumerDto) {
+33 -36
View File
@@ -52,47 +52,44 @@ export class GoodsService {
guild_id: string,
consumer_account_id: string,
) {
const cacheKey = RedisKeyMaker.posGoodsList(guild_id, business_activity_id)
try {
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
return ResponseMapper.list(cached)
}
throw new Error('Cache miss')
} catch (error) {
const goods = await this.prisma.good.findMany({
where: {
OR: [
{
is_default_guild_good: true,
category: {
guild_id,
const cacheKey = RedisKeyMaker.posGoodsList(business_activity_id, guild_id)
return this.redisService.getAndSet(
cacheKey,
'list',
async () => {
const goods = await this.prisma.good.findMany({
where: {
OR: [
{
is_default_guild_good: true,
category: {
guild_id,
},
},
{
business_activity_id,
},
],
},
select: {
...this.defaultSelect,
consumer_account_good_favorites: {
where: {
consumer_account_id,
},
},
{
business_activity_id,
},
],
},
select: {
...this.defaultSelect,
consumer_account_good_favorites: {
where: {
consumer_account_id,
},
},
},
})
})
const mappedGoods = goods.map(good => ({
...good,
is_favorite: good.consumer_account_good_favorites.length > 0,
}))
const mappedGoods = goods.map(good => ({
...good,
is_favorite: good.consumer_account_good_favorites.length > 0,
}))
await this.redisService.setJson(cacheKey, mappedGoods, this.listCacheTtlSeconds)
return ResponseMapper.list(mappedGoods)
}
return mappedGoods
},
this.listCacheTtlSeconds,
)
}
async findOne(good_id: string, business_activity_id: string, guild_id: string) {
+109 -105
View File
@@ -19,93 +19,95 @@ export class PosService {
async getInfo(pos_id: string) {
const cacheKey = RedisKeyMaker.posInfo(pos_id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const pos = await this.prisma.pos.findUniqueOrThrow({
where: {
id: pos_id,
},
select: {
name: true,
complex: {
return await this.redisService.getAndSet(
cacheKey,
'single',
async () => {
const pos = await this.prisma.pos.findUniqueOrThrow({
where: {
id: pos_id,
},
select: {
id: true,
name: true,
branch_code: true,
business_activity: {
complex: {
select: {
id: true,
name: true,
economic_code: true,
license_activation: {
branch_code: true,
business_activity: {
select: {
expires_at: true,
license: {
id: true,
name: true,
economic_code: true,
license_activation: {
select: {
id: true,
charge_transaction: {
expires_at: true,
license: {
select: {
partner: {
id: true,
charge_transaction: {
select: {
id: true,
name: true,
code: true,
logo_url: true,
partner: {
select: {
id: true,
name: true,
code: true,
logo_url: true,
},
},
},
},
},
},
},
},
},
},
guild: {
select: {
id: true,
name: true,
code: true,
guild: {
select: {
id: true,
name: true,
code: true,
},
},
},
},
},
},
},
},
},
})
const { name, complex } = pos
const { name: complexName, id: complexId, business_activity } = complex
const {
name: businessName,
id: businessId,
guild,
economic_code,
license_activation,
} = business_activity
})
const { name, complex } = pos
const { name: complexName, id: complexId, business_activity } = complex
const {
name: businessName,
id: businessId,
guild,
economic_code,
license_activation,
} = business_activity
const payload = {
name,
complex: {
id: complexId,
name: complexName,
branch_code: complex.branch_code,
const payload = {
name,
complex: {
id: complexId,
name: complexName,
branch_code: complex.branch_code,
},
businessActivity: {
id: businessId,
name: businessName,
economic_code,
},
guild,
license_info: {
expires_at: license_activation?.expires_at,
license_id: license_activation?.license.id,
},
partner: license_activation?.license.charge_transaction.partner,
}
return payload
},
businessActivity: {
id: businessId,
name: businessName,
economic_code,
},
guild,
license_info: {
expires_at: license_activation?.expires_at,
license_id: license_activation?.license.id,
},
partner: license_activation?.license.charge_transaction.partner,
}
await this.redisService.setJson(cacheKey, payload, this.infoCacheTtlSeconds)
return ResponseMapper.single(payload)
this.infoCacheTtlSeconds,
)
}
async getAccessible(account_id: string) {
@@ -153,57 +155,59 @@ export class PosService {
async getMe(account_id: string, pos_id: string) {
const cacheKey = RedisKeyMaker.posMe(account_id, pos_id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const pos = await this.prisma.consumerAccount.findUniqueOrThrow({
where: {
id: account_id,
consumer: {
status: ConsumerStatus.ACTIVE,
business_activities: {
some: {
complexes: {
return await this.redisService.getAndSet(
cacheKey,
'single',
async () => {
const pos = await this.prisma.consumerAccount.findUniqueOrThrow({
where: {
id: account_id,
consumer: {
status: ConsumerStatus.ACTIVE,
business_activities: {
some: {
pos_list: {
complexes: {
some: {
id: pos_id,
pos_list: {
some: {
id: pos_id,
},
},
},
},
},
},
},
},
},
},
select: {
id: true,
role: true,
consumer: {
select: {
id: true,
type: true,
status: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
role: true,
consumer: {
select: {
id: true,
type: true,
status: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
},
},
account: {
select: {
username: true,
},
},
},
},
account: {
select: {
username: true,
},
},
})
const { consumer, ...rest } = pos
const payload = {
...rest,
consumer: consumer_mappersUtil(consumer),
}
return payload
},
})
const { consumer, ...rest } = pos
const payload = {
...rest,
consumer: consumer_mappersUtil(consumer),
}
await this.redisService.setJson(cacheKey, payload, this.meCacheTtlSeconds)
return ResponseMapper.single(payload)
this.meCacheTtlSeconds,
)
}
}