62b659246f
- 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.
110 lines
3.6 KiB
TypeScript
110 lines
3.6 KiB
TypeScript
import { BusinessActivitiesQueryService } from '@/common/services/businessActivities/business-activities-query.service'
|
|
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
|
import { BusinessActivitySelect } from '@/generated/prisma/models'
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
import { RedisService } from '@/redis/redis.service'
|
|
import { Injectable } from '@nestjs/common'
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
@Injectable()
|
|
export class BusinessActivitiesService {
|
|
defaultSelect: BusinessActivitySelect
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly businessActivitiesQueryService: BusinessActivitiesQueryService,
|
|
private readonly redisService: RedisService,
|
|
) {
|
|
this.defaultSelect = {
|
|
...this.businessActivitiesQueryService.baseSelect,
|
|
invoice_number_sequence: true,
|
|
license_activation: {
|
|
select: {
|
|
id: true,
|
|
starts_at: true,
|
|
expires_at: true,
|
|
license: {
|
|
select: {
|
|
activation: {
|
|
select: {
|
|
account_allocations: {
|
|
select: {
|
|
id: true,
|
|
account_id: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
private readonly cacheTtlSeconds = 300
|
|
|
|
private readonly prepareBusinessActivityResponse = (businessActivity: any) => {
|
|
const { license_activation, complexes, ...rest } = businessActivity
|
|
const { license, ...license_activation_rest } = license_activation
|
|
const { account_allocations } = license.activation
|
|
|
|
return {
|
|
...rest,
|
|
license_info: {
|
|
...license_activation_rest,
|
|
accounts_limit: account_allocations.length,
|
|
allocated_account_count: account_allocations.filter(
|
|
allocation => allocation.account_id,
|
|
).length,
|
|
},
|
|
}
|
|
}
|
|
|
|
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(
|
|
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)
|
|
}
|
|
|
|
async update(consumer_id: string, id: string, data: any) {
|
|
const businessActivity = await this.prisma.businessActivity.update({
|
|
where: { id, consumer_id },
|
|
data,
|
|
select: this.defaultSelect,
|
|
})
|
|
await this.redisService.delete(
|
|
RedisKeyMaker.consumerBusinessActivityInfo(consumer_id, id),
|
|
)
|
|
await this.redisService.delete(
|
|
RedisKeyMaker.consumerBusinessActivitiesList(consumer_id),
|
|
)
|
|
|
|
return ResponseMapper.update(this.prepareBusinessActivityResponse(businessActivity))
|
|
}
|
|
}
|