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.
128 lines
3.6 KiB
TypeScript
128 lines
3.6 KiB
TypeScript
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
|
import { ResponseMapper } from '@/common/response/response-mapper'
|
|
import { PasswordUtil } from '@/common/utils'
|
|
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
|
import { ConsumerUpdateInput } from '@/generated/prisma/models'
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
import { RedisService } from '@/redis/redis.service'
|
|
import { Injectable } from '@nestjs/common'
|
|
import consumer_mappersUtil from '../../common/utils/mappers/consumer_mappers.util'
|
|
import { UpdateConsumerInfoDto } from './dto/update-info-request.dto'
|
|
import { UpdateConsumerPasswordDto } from './dto/update-password-request.dto'
|
|
|
|
@Injectable()
|
|
export class ConsumerService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly redisService: RedisService,
|
|
) {}
|
|
|
|
private readonly infoCacheTtlSeconds = 300
|
|
|
|
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,
|
|
},
|
|
})
|
|
|
|
const mappedConsumer = consumer_mappersUtil(consumer)
|
|
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
|
|
return ResponseMapper.single(mappedConsumer)
|
|
}
|
|
|
|
async updateInfo(consumer_id: string, data: UpdateConsumerInfoDto) {
|
|
const consumerExists = await this.prisma.consumer.findUnique({
|
|
where: {
|
|
id: consumer_id,
|
|
},
|
|
select: {
|
|
type: true,
|
|
},
|
|
})
|
|
|
|
let dataForUpdate: ConsumerUpdateInput = {}
|
|
|
|
if (consumerExists?.type === 'LEGAL') {
|
|
dataForUpdate = {
|
|
legal: {
|
|
update: {
|
|
name: data.company_name,
|
|
registration_code: data.registration_code,
|
|
},
|
|
},
|
|
}
|
|
} else if (consumerExists?.type === 'INDIVIDUAL') {
|
|
dataForUpdate = {
|
|
individual: {
|
|
update: {
|
|
first_name: data.first_name,
|
|
last_name: data.last_name,
|
|
mobile_number: data.mobile_number,
|
|
national_code: data.national_code,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
const consumer = await this.prisma.consumer.update({
|
|
where: {
|
|
id: consumer_id,
|
|
},
|
|
data: {
|
|
...dataForUpdate,
|
|
},
|
|
})
|
|
|
|
await this.redisService.delete(RedisKeyMaker.consumerInfo(consumer_id))
|
|
const partnerOwner = await this.prisma.consumer.findUnique({
|
|
where: { id: consumer_id },
|
|
select: {
|
|
legal: { select: { partner_id: true } },
|
|
individual: { select: { partner_id: true } },
|
|
},
|
|
})
|
|
const partnerId =
|
|
partnerOwner?.legal?.partner_id || partnerOwner?.individual?.partner_id || null
|
|
if (partnerId) {
|
|
await this.redisService.delete(
|
|
RedisKeyMaker.partnerConsumerInfo(partnerId, consumer_id),
|
|
)
|
|
}
|
|
|
|
return ResponseMapper.update(consumer)
|
|
}
|
|
|
|
async updatePassword(
|
|
consumer_id: string,
|
|
accountId: string,
|
|
data: UpdateConsumerPasswordDto,
|
|
) {
|
|
const consumer = await this.prisma.consumerAccount.update({
|
|
where: {
|
|
id: accountId,
|
|
consumer_id,
|
|
},
|
|
data: {
|
|
account: {
|
|
update: {
|
|
password: await PasswordUtil.hash(data.password),
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
return ResponseMapper.update(consumer)
|
|
}
|
|
}
|