feat: implement Redis caching for business activities and consumers
- Added Redis caching to BusinessActivitiesService for findAll and findOne methods. - Integrated Redis caching in BusinessActivityComplexesService for findAll and findOne methods. - Enhanced ConsumersService with Redis caching for findAll and findOne methods. - Introduced cache invalidation for partner consumers and business activities. - Created RedisKeyMaker utility for generating cache keys for consumers, partners, and POS. - Implemented cache invalidation services for partners and POS. - Added Redis service methods for JSON handling and key deletion by patterns. - Updated goods service to include caching and invalidation for goods list. - Introduced DTO for updating goods.
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
import { RedisKeyMaker } from '@/common/utils'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
|
||||
@Injectable()
|
||||
export class PartnersCacheInvalidationService {
|
||||
constructor(private readonly redisService: RedisService) {}
|
||||
|
||||
async invalidatePartnerSummary(partnerId: string): Promise<void> {
|
||||
await this.invalidatePartnersList()
|
||||
await this.redisService.delete(RedisKeyMaker.partnerDetail(partnerId))
|
||||
}
|
||||
|
||||
async invalidatePartnersList(): Promise<void> {
|
||||
await this.redisService.delete(RedisKeyMaker.partnersList())
|
||||
}
|
||||
|
||||
async invalidatePartnerDetail(partnerId: string): Promise<void> {
|
||||
await this.invalidatePartnerSummary(partnerId)
|
||||
}
|
||||
|
||||
async invalidatePartnerLicenseReadModels(partnerId: string): Promise<void> {
|
||||
await this.redisService.deleteByPatterns([
|
||||
RedisKeyMaker.partnerActivatedLicensesListPattern(partnerId),
|
||||
RedisKeyMaker.partnerLicenseChargeTransactionsListPattern(partnerId),
|
||||
RedisKeyMaker.partnerLicenseChargeTransactionDetailPattern(partnerId),
|
||||
])
|
||||
}
|
||||
|
||||
async invalidatePartnerLicenses(partnerId: string): Promise<void> {
|
||||
await this.invalidatePartnerSummary(partnerId)
|
||||
await this.invalidatePartnerLicenseReadModels(partnerId)
|
||||
}
|
||||
|
||||
// consumers //
|
||||
|
||||
async invalidatePartnerConsumers(
|
||||
partnerId: string,
|
||||
page: number,
|
||||
perPage: number,
|
||||
): Promise<void> {
|
||||
await this.redisService.delete(
|
||||
RedisKeyMaker.partnerConsumersList(partnerId, page, perPage),
|
||||
)
|
||||
}
|
||||
|
||||
async invalidatePartnerConsumerReadModels(
|
||||
partnerId: string,
|
||||
consumerId: string,
|
||||
): Promise<void> {
|
||||
await this.redisService.deleteByPatterns([
|
||||
RedisKeyMaker.partnerConsumersListPattern(partnerId),
|
||||
RedisKeyMaker.partnerConsumerInfo(partnerId, consumerId),
|
||||
])
|
||||
}
|
||||
|
||||
async invalidatePartnerConsumerBusinessActivitiesReadModels(
|
||||
partnerId: string,
|
||||
consumerId: string,
|
||||
): Promise<void> {
|
||||
await this.invalidatePartnerConsumerReadModels(partnerId, consumerId)
|
||||
await this.redisService.deleteByPattern(
|
||||
RedisKeyMaker.partnerConsumerBusinessActivitiesPattern(partnerId, consumerId),
|
||||
)
|
||||
}
|
||||
|
||||
async invalidatePartnerConsumerBusinessActivityReadModels(
|
||||
partnerId: string,
|
||||
consumerId: string,
|
||||
businessActivityId: string,
|
||||
): Promise<void> {
|
||||
await this.invalidatePartnerConsumerBusinessActivitiesReadModels(
|
||||
partnerId,
|
||||
consumerId,
|
||||
)
|
||||
await this.redisService.delete(
|
||||
RedisKeyMaker.partnerConsumerBusinessActivityInfo(
|
||||
partnerId,
|
||||
consumerId,
|
||||
businessActivityId,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
async invalidatePartnerConsumerBusinessActivityComplexesReadModels(
|
||||
partnerId: string,
|
||||
consumerId: string,
|
||||
businessActivityId: string,
|
||||
): Promise<void> {
|
||||
await this.invalidatePartnerConsumerBusinessActivitiesReadModels(
|
||||
partnerId,
|
||||
consumerId,
|
||||
)
|
||||
await this.redisService.deleteByPattern(
|
||||
RedisKeyMaker.partnerConsumerBusinessActivityComplexesPattern(
|
||||
partnerId,
|
||||
consumerId,
|
||||
businessActivityId,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
async invalidatePartnerConsumerBusinessActivityComplexReadModels(
|
||||
partnerId: string,
|
||||
consumerId: string,
|
||||
businessActivityId: string,
|
||||
complexId: string,
|
||||
): Promise<void> {
|
||||
await this.invalidatePartnerConsumerBusinessActivityComplexesReadModels(
|
||||
partnerId,
|
||||
consumerId,
|
||||
businessActivityId,
|
||||
)
|
||||
await this.redisService.delete(
|
||||
RedisKeyMaker.partnerConsumerBusinessActivityComplexInfo(
|
||||
partnerId,
|
||||
consumerId,
|
||||
businessActivityId,
|
||||
complexId,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
async invalidatePartnerConsumerBusinessActivityComplexPosesReadModels(
|
||||
partnerId: string,
|
||||
consumerId: string,
|
||||
businessActivityId: string,
|
||||
complexId: string,
|
||||
): Promise<void> {
|
||||
await this.invalidatePartnerConsumerBusinessActivitiesReadModels(
|
||||
partnerId,
|
||||
consumerId,
|
||||
)
|
||||
await this.redisService.deleteByPattern(
|
||||
RedisKeyMaker.partnerConsumerBusinessActivityComplexPosesPattern(
|
||||
partnerId,
|
||||
consumerId,
|
||||
businessActivityId,
|
||||
complexId,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
async invalidatePartnerConsumerBusinessActivityComplexPosReadModels(
|
||||
partnerId: string,
|
||||
consumerId: string,
|
||||
businessActivityId: string,
|
||||
complexId: string,
|
||||
posId: string,
|
||||
): Promise<void> {
|
||||
await this.invalidatePartnerConsumerBusinessActivityComplexPosesReadModels(
|
||||
partnerId,
|
||||
consumerId,
|
||||
businessActivityId,
|
||||
complexId,
|
||||
)
|
||||
await this.redisService.delete(
|
||||
RedisKeyMaker.partnerConsumerBusinessActivityComplexPosInfo(
|
||||
partnerId,
|
||||
consumerId,
|
||||
businessActivityId,
|
||||
complexId,
|
||||
posId,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,22 @@
|
||||
import { RedisKeyMaker } from '@/common/utils'
|
||||
import { BusinessActivitySelect } from '@/generated/prisma/models'
|
||||
import { getPartnerFirstRemainingLicense } from '@/modules/partners/utils/getPartnerRemainingLicenses.util'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { PartnersCacheInvalidationService } from '../../cache/partners-cache-invalidation.service'
|
||||
import { CreateBusinessActivitiesDto } from './dto/create-business-activities.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivitiesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly partnersCacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly cacheTtlSeconds = 300
|
||||
|
||||
private readonly setExpireDate = (starts_at: Date) => {
|
||||
return new Date(
|
||||
@@ -85,6 +94,19 @@ export class BusinessActivitiesService {
|
||||
}
|
||||
|
||||
async findAll(partner_id: string, consumer_id: string, page = 1, perPage = 10) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerBusinessActivitiesList(
|
||||
partner_id,
|
||||
consumer_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 })
|
||||
}
|
||||
|
||||
const where = this.defaultWhere(partner_id, consumer_id)
|
||||
const [businessActivities, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.businessActivity.findMany({
|
||||
@@ -95,13 +117,26 @@ export class BusinessActivitiesService {
|
||||
}),
|
||||
await tx.businessActivity.count({ where }),
|
||||
])
|
||||
return ResponseMapper.paginate(
|
||||
businessActivities.map(this.prepareBusinessActivityResponse),
|
||||
{ total, page, perPage },
|
||||
const mappedItems = businessActivities.map(this.prepareBusinessActivityResponse)
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedItems, total },
|
||||
this.cacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedItems, { total, page, perPage })
|
||||
}
|
||||
|
||||
async findOne(partner_id: string, consumer_id: string, id: string) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerBusinessActivityInfo(
|
||||
partner_id,
|
||||
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),
|
||||
@@ -109,7 +144,9 @@ export class BusinessActivitiesService {
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(this.prepareBusinessActivityResponse(businessActivity))
|
||||
const mappedItem = this.prepareBusinessActivityResponse(businessActivity)
|
||||
await this.redisService.setJson(cacheKey, mappedItem, this.cacheTtlSeconds)
|
||||
return ResponseMapper.single(mappedItem)
|
||||
}
|
||||
|
||||
async create(
|
||||
@@ -187,6 +224,10 @@ export class BusinessActivitiesService {
|
||||
})
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerBusinessActivitiesReadModels(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
)
|
||||
return ResponseMapper.create(businessActivity)
|
||||
}
|
||||
|
||||
@@ -196,6 +237,13 @@ export class BusinessActivitiesService {
|
||||
data,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerBusinessActivityReadModels(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
id,
|
||||
)
|
||||
|
||||
return ResponseMapper.update(businessActivity)
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -3,7 +3,12 @@ import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { BusinessActivityComplexesService } from './complexes.service'
|
||||
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
|
||||
import type { BusinessActivityComplexesServiceCreateResponseDto, BusinessActivityComplexesServiceFindAllResponseDto, BusinessActivityComplexesServiceFindOneResponseDto, BusinessActivityComplexesServiceUpdateResponseDto } from './dto/complexes-response.dto'
|
||||
import type {
|
||||
BusinessActivityComplexesServiceCreateResponseDto,
|
||||
BusinessActivityComplexesServiceFindAllResponseDto,
|
||||
BusinessActivityComplexesServiceFindOneResponseDto,
|
||||
BusinessActivityComplexesServiceUpdateResponseDto,
|
||||
} from './dto/complexes-response.dto'
|
||||
|
||||
@ApiTags('PartnerBusinessActivityComplexes')
|
||||
@Controller(
|
||||
@@ -34,10 +39,11 @@ export class BusinessActivityComplexesController {
|
||||
@Post()
|
||||
async create(
|
||||
@PartnerInfo('id') partnerId: string,
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Body() data: CreateComplexDto,
|
||||
): Promise<BusinessActivityComplexesServiceCreateResponseDto> {
|
||||
return this.service.create(partnerId, businessActivityId, data)
|
||||
return this.service.create(partnerId, consumerId, businessActivityId, data)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
||||
import { RedisKeyMaker } from '@/common/utils'
|
||||
import { ComplexSelect, ComplexWhereInput } from '@/generated/prisma/models'
|
||||
import { PartnersCacheInvalidationService } from '@/modules/partners/cache/partners-cache-invalidation.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivityComplexesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly partnersCacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly cacheTtlSeconds = 300
|
||||
|
||||
private readonly defaultSelect: ComplexSelect = {
|
||||
id: true,
|
||||
@@ -49,6 +58,20 @@ export class BusinessActivityComplexesService {
|
||||
page = 1,
|
||||
perPage = 10,
|
||||
) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerBusinessActivityComplexesList(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_activity_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 })
|
||||
}
|
||||
|
||||
const where = this.defaultWhere(partner_id, consumer_id, business_activity_id)
|
||||
const [complexes, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.complex.findMany({
|
||||
@@ -74,6 +97,11 @@ export class BusinessActivityComplexesService {
|
||||
pos_count: _count.pos_list,
|
||||
}
|
||||
})
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedComplexes, total },
|
||||
this.cacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedComplexes, { total, page, perPage })
|
||||
}
|
||||
|
||||
@@ -83,18 +111,40 @@ export class BusinessActivityComplexesService {
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
) {
|
||||
const complex = await this.prisma.complex.findUnique({
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerBusinessActivityComplexInfo(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_activity_id,
|
||||
id,
|
||||
)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
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('شعبه مورد نظر یافت نشد.')
|
||||
}
|
||||
|
||||
await this.redisService.setJson(cacheKey, complex, this.cacheTtlSeconds)
|
||||
return ResponseMapper.single(complex)
|
||||
}
|
||||
|
||||
async create(partner_id: string, business_id: string, data: CreateComplexDto) {
|
||||
const complex = this.prisma.$transaction(async tx => {
|
||||
async create(
|
||||
partner_id: string,
|
||||
business_id: string,
|
||||
consumer_id: string,
|
||||
data: CreateComplexDto,
|
||||
) {
|
||||
const complex = await this.prisma.$transaction(async tx => {
|
||||
const relatedLicense = await tx.licenseAccountAllocation.findFirst({
|
||||
where: {
|
||||
license_activation: {
|
||||
@@ -121,7 +171,7 @@ export class BusinessActivityComplexesService {
|
||||
)
|
||||
}
|
||||
|
||||
return await this.prisma.complex.create({
|
||||
return await tx.complex.create({
|
||||
data: {
|
||||
...data,
|
||||
business_activity: {
|
||||
@@ -133,6 +183,12 @@ export class BusinessActivityComplexesService {
|
||||
})
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerBusinessActivityComplexesReadModels(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_id,
|
||||
)
|
||||
|
||||
return ResponseMapper.create(complex)
|
||||
}
|
||||
|
||||
@@ -147,6 +203,13 @@ export class BusinessActivityComplexesService {
|
||||
where: { ...this.defaultWhere(partner_id, consumer_id, business_activity_id), id },
|
||||
data,
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerBusinessActivityComplexReadModels(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_activity_id,
|
||||
id,
|
||||
)
|
||||
return ResponseMapper.update(complex)
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -1,7 +1,7 @@
|
||||
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
||||
import { consumerRelatedPartner } from '@/common/queryConstants/consumer'
|
||||
import { PrismaErrorUtil } from '@/common/utils/prisma-error.util'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { PrismaErrorUtil } from '@/common/utils/prisma-error.util'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
@@ -9,14 +9,22 @@ import {
|
||||
POSStatus,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { PosSelect } from '@/generated/prisma/models'
|
||||
import { PartnersCacheInvalidationService } from '@/modules/partners/cache/partners-cache-invalidation.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
|
||||
|
||||
@Injectable()
|
||||
export class ComplexPosesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly partnersCacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly cacheTtlSeconds = 300
|
||||
|
||||
private readonly defaultSelect: PosSelect = {
|
||||
id: true,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
||||
import mapConsumerWithLicenseUtil from '@/common/utils/mappers/consumer_mappers.util'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
@@ -15,13 +16,21 @@ import {
|
||||
ConsumerWhereInput,
|
||||
} from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { PartnersCacheInvalidationService } from '../cache/partners-cache-invalidation.service'
|
||||
import { CreateConsumerDto, UpdateConsumerDto } from './dto/create-consumers.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerConsumersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly partnersCacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly infoCacheTtlSeconds = 300
|
||||
|
||||
private readonly setExpireDate = (starts_at: Date) => {
|
||||
return new Date(
|
||||
@@ -54,6 +63,14 @@ 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 })
|
||||
}
|
||||
|
||||
const [consumers, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.consumer.findMany({
|
||||
where: this.defaultWhere(partner_id),
|
||||
@@ -65,7 +82,13 @@ export class PartnerConsumersService {
|
||||
where: this.defaultWhere(partner_id),
|
||||
}),
|
||||
])
|
||||
return ResponseMapper.paginate(consumers.map(mapConsumerWithLicenseUtil), {
|
||||
const mappedItems = consumers.map(mapConsumerWithLicenseUtil)
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedItems, total },
|
||||
this.infoCacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedItems, {
|
||||
total,
|
||||
page,
|
||||
perPage,
|
||||
@@ -73,6 +96,12 @@ export class PartnerConsumersService {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
...(this.defaultWhere(partner_id) as any),
|
||||
@@ -81,7 +110,14 @@ export class PartnerConsumersService {
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.single(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) {
|
||||
@@ -224,11 +260,16 @@ export class PartnerConsumersService {
|
||||
})
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerReadModels(
|
||||
partner_id,
|
||||
consumer.id,
|
||||
)
|
||||
|
||||
return ResponseMapper.single(mapConsumerWithLicenseUtil(consumer))
|
||||
}
|
||||
|
||||
async update(id: string, partner_id: string, data: UpdateConsumerDto) {
|
||||
return this.prisma.consumer.update({
|
||||
const updatedConsumer = await this.prisma.consumer.update({
|
||||
where: {
|
||||
...(this.defaultWhere(partner_id) as any),
|
||||
id,
|
||||
@@ -243,5 +284,13 @@ export class PartnerConsumersService {
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerReadModels(
|
||||
partner_id,
|
||||
id,
|
||||
)
|
||||
await this.redisService.delete(RedisKeyMaker.consumerInfo(id))
|
||||
|
||||
return updatedConsumer
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { UploadedFileTypes } from '@/common/enums/enums'
|
||||
import { ResponseMapper } from '@/common/response/response-mapper'
|
||||
import { PasswordUtil } from '@/common/utils'
|
||||
import { PartnersCacheInvalidationService } from '@/modules/partners/cache/partners-cache-invalidation.service'
|
||||
import { UploaderService } from '@/modules/uploader/uploader.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
@@ -12,6 +13,7 @@ export class PartnerService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly uploaderService: UploaderService,
|
||||
private readonly cacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
async me(partner_id: string, account_id: string) {
|
||||
@@ -245,6 +247,9 @@ export class PartnerService {
|
||||
})
|
||||
})
|
||||
|
||||
await this.cacheInvalidationService.invalidatePartnersList()
|
||||
await this.cacheInvalidationService.invalidatePartnerDetail(partner_id)
|
||||
|
||||
return ResponseMapper.single(updatedPartner)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user