2026-03-16 17:56:51 +03:30
|
|
|
import { BusinessActivitySelect } from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class BusinessActivitiesService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
2026-04-24 02:20:15 +03:30
|
|
|
defaultSelect: BusinessActivitySelect = {
|
2026-04-24 04:27:28 +03:30
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
economic_code: true,
|
|
|
|
|
created_at: true,
|
2026-05-03 16:23:17 +03:30
|
|
|
partner_token: true,
|
|
|
|
|
fiscal_id: true,
|
|
|
|
|
invoice_number_sequence: true,
|
2026-03-16 17:56:51 +03:30
|
|
|
guild: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-24 23:02:05 +03:30
|
|
|
|
2026-04-24 02:20:15 +03:30
|
|
|
license_activation: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
starts_at: true,
|
|
|
|
|
expires_at: true,
|
|
|
|
|
license: {
|
|
|
|
|
select: {
|
2026-04-24 23:02:05 +03:30
|
|
|
activation: {
|
2026-04-24 02:20:15 +03:30
|
|
|
select: {
|
2026-04-24 23:02:05 +03:30
|
|
|
account_allocations: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
account_id: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
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-03-16 17:56:51 +03:30
|
|
|
const businessActivities = await this.prisma.businessActivity.findMany({
|
|
|
|
|
where: {
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-03-16 17:56:51 +03:30
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-04-24 02:20:15 +03:30
|
|
|
return ResponseMapper.list(
|
|
|
|
|
businessActivities.map(this.prepareBusinessActivityResponse),
|
|
|
|
|
)
|
2026-03-16 17:56:51 +03:30
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async findOne(consumer_id: string, id: string) {
|
2026-03-16 17:56:51 +03:30
|
|
|
const businessActivity = await this.prisma.businessActivity.findUniqueOrThrow({
|
2026-04-11 14:47:05 +03:30
|
|
|
where: { consumer_id, id },
|
2026-03-16 17:56:51 +03:30
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-04-24 02:20:15 +03:30
|
|
|
return ResponseMapper.single(this.prepareBusinessActivityResponse(businessActivity))
|
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-04-24 02:20:15 +03:30
|
|
|
return ResponseMapper.update(this.prepareBusinessActivityResponse(businessActivity))
|
2026-03-16 17:56:51 +03:30
|
|
|
}
|
|
|
|
|
}
|