2026-05-07 20:30:24 +03:30
|
|
|
import { BusinessActivitiesQueryService } from '@/common/services/businessActivities/business-activities-query.service'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
2026-03-16 17:56:51 +03:30
|
|
|
import { BusinessActivitySelect } from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { RedisService } from '@/redis/redis.service'
|
2026-03-16 17:56:51 +03:30
|
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class BusinessActivitiesService {
|
2026-05-07 20:30:24 +03:30
|
|
|
defaultSelect: BusinessActivitySelect
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly prisma: PrismaService,
|
|
|
|
|
private readonly businessActivitiesQueryService: BusinessActivitiesQueryService,
|
2026-05-19 15:40:45 +03:30
|
|
|
private readonly redisService: RedisService,
|
2026-05-07 20:30:24 +03:30
|
|
|
) {
|
|
|
|
|
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,
|
|
|
|
|
},
|
2026-04-24 23:02:05 +03:30
|
|
|
},
|
|
|
|
|
},
|
2026-04-24 02:20:15 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-05-07 20:30:24 +03:30
|
|
|
}
|
2026-04-24 02:20:15 +03:30
|
|
|
}
|
2026-05-19 15:40:45 +03:30
|
|
|
private readonly cacheTtlSeconds = 300
|
2026-04-24 02:20:15 +03:30
|
|
|
|
|
|
|
|
private readonly prepareBusinessActivityResponse = (businessActivity: any) => {
|
2026-04-24 04:27:28 +03:30
|
|
|
const { license_activation, complexes, ...rest } = businessActivity
|
2026-04-24 02:20:15 +03:30
|
|
|
const { license, ...license_activation_rest } = license_activation
|
2026-04-24 23:02:05 +03:30
|
|
|
const { account_allocations } = license.activation
|
2026-04-24 02:20:15 +03:30
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
license_info: {
|
|
|
|
|
...license_activation_rest,
|
2026-04-24 23:02:05 +03:30
|
|
|
accounts_limit: account_allocations.length,
|
|
|
|
|
allocated_account_count: account_allocations.filter(
|
|
|
|
|
allocation => allocation.account_id,
|
|
|
|
|
).length,
|
2026-04-24 02:20:15 +03:30
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-16 17:56:51 +03:30
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async findAll(consumer_id: string) {
|
2026-05-19 15:40:45 +03:30
|
|
|
const cacheKey = RedisKeyMaker.consumerBusinessActivitiesList(consumer_id)
|
2026-05-21 17:27:37 +03:30
|
|
|
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
|
|
|
|
|
return await this.businessActivitiesQueryService.findAllByConsumer(
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-05-07 20:30:24 +03:30
|
|
|
this.defaultSelect,
|
|
|
|
|
)
|
2026-05-21 17:27:37 +03:30
|
|
|
})
|
2026-03-16 17:56:51 +03:30
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async findOne(consumer_id: string, id: string) {
|
2026-05-19 15:40:45 +03:30
|
|
|
const cacheKey = RedisKeyMaker.consumerBusinessActivityInfo(consumer_id, id)
|
2026-05-21 17:27:37 +03:30
|
|
|
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
|
|
|
|
|
return await this.businessActivitiesQueryService.findOneByConsumer(
|
|
|
|
|
consumer_id,
|
|
|
|
|
id,
|
|
|
|
|
this.defaultSelect,
|
|
|
|
|
)
|
|
|
|
|
})
|
2026-03-16 17:56:51 +03:30
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async update(consumer_id: string, id: string, data: any) {
|
2026-03-16 17:56:51 +03:30
|
|
|
const businessActivity = await this.prisma.businessActivity.update({
|
2026-04-11 14:47:05 +03:30
|
|
|
where: { id, consumer_id },
|
2026-03-16 17:56:51 +03:30
|
|
|
data,
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-05-19 15:40:45 +03:30
|
|
|
await this.redisService.delete(
|
|
|
|
|
RedisKeyMaker.consumerBusinessActivityInfo(consumer_id, id),
|
|
|
|
|
)
|
|
|
|
|
await this.redisService.delete(
|
|
|
|
|
RedisKeyMaker.consumerBusinessActivitiesList(consumer_id),
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-24 02:20:15 +03:30
|
|
|
return ResponseMapper.update(this.prepareBusinessActivityResponse(businessActivity))
|
2026-03-16 17:56:51 +03:30
|
|
|
}
|
|
|
|
|
}
|