2026-04-23 20:59:39 +03:30
|
|
|
import { ComplexSelect, ComplexWhereInput } from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-04-25 15:16:59 +03:30
|
|
|
import { BadRequestException, Injectable } from '@nestjs/common'
|
2026-04-23 20:59:39 +03:30
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class BusinessActivityComplexesService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
private readonly defaultSelect: ComplexSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
address: true,
|
|
|
|
|
branch_code: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly defaultWhere = (
|
|
|
|
|
partner_id,
|
|
|
|
|
consumer_id,
|
|
|
|
|
business_activity_id,
|
|
|
|
|
): ComplexWhereInput => ({
|
|
|
|
|
business_activity: {
|
|
|
|
|
id: business_activity_id,
|
|
|
|
|
consumer: {
|
|
|
|
|
id: consumer_id,
|
|
|
|
|
partner_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
async findAll(partner_id: string, consumer_id: string, business_activity_id: string) {
|
2026-04-24 23:02:05 +03:30
|
|
|
const complexes = await this.prisma.complex.findMany({
|
2026-04-23 20:59:39 +03:30
|
|
|
where: this.defaultWhere(partner_id, consumer_id, business_activity_id),
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-04-24 23:02:05 +03:30
|
|
|
return ResponseMapper.list(complexes)
|
2026-04-23 20:59:39 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(
|
|
|
|
|
partner_id: string,
|
|
|
|
|
consumer_id: string,
|
|
|
|
|
business_activity_id: string,
|
|
|
|
|
id: string,
|
|
|
|
|
) {
|
2026-04-24 23:02:05 +03:30
|
|
|
const complex = await this.prisma.complex.findUnique({
|
2026-04-23 20:59:39 +03:30
|
|
|
where: {
|
|
|
|
|
...this.defaultWhere(partner_id, consumer_id, business_activity_id),
|
|
|
|
|
id,
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-04-24 23:02:05 +03:30
|
|
|
return ResponseMapper.single(complex)
|
2026-04-23 20:59:39 +03:30
|
|
|
}
|
|
|
|
|
|
2026-04-24 23:02:05 +03:30
|
|
|
async create(partner_id: string, business_id: string, data: CreateComplexDto) {
|
2026-04-25 15:16:59 +03:30
|
|
|
const complex = this.prisma.$transaction(async tx => {
|
|
|
|
|
const now = new Date()
|
|
|
|
|
|
|
|
|
|
const relatedLicense = await tx.licenseAccountAllocation.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
license_activation: {
|
|
|
|
|
license: {
|
|
|
|
|
charge_transaction: {
|
|
|
|
|
partner_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
business_activity: {
|
|
|
|
|
id: business_id,
|
|
|
|
|
},
|
|
|
|
|
OR: [
|
|
|
|
|
{
|
|
|
|
|
expires_at: {
|
|
|
|
|
gte: now,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
license_renews: {
|
|
|
|
|
some: {
|
|
|
|
|
expires_at: {
|
|
|
|
|
gte: now,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-04-23 20:59:39 +03:30
|
|
|
},
|
2026-04-25 15:16:59 +03:30
|
|
|
account_id: null,
|
2026-04-23 20:59:39 +03:30
|
|
|
},
|
2026-04-25 15:16:59 +03:30
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!relatedLicense) {
|
|
|
|
|
throw new BadRequestException(
|
|
|
|
|
`متاسفانه به دلیل به پایان رسیدن محدودیت تعریف کاربران برای این فعالیت اقتصادی،امکان ایجاد شعبهی جدید وجود ندارد.`,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await this.prisma.complex.create({
|
|
|
|
|
data: {
|
|
|
|
|
...data,
|
|
|
|
|
business_activity: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: business_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-04-23 20:59:39 +03:30
|
|
|
})
|
2026-04-25 15:16:59 +03:30
|
|
|
|
2026-04-24 23:02:05 +03:30
|
|
|
return ResponseMapper.create(complex)
|
2026-04-23 20:59:39 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(
|
|
|
|
|
partner_id: string,
|
|
|
|
|
consumer_id: string,
|
|
|
|
|
business_activity_id: string,
|
|
|
|
|
id: string,
|
|
|
|
|
data: UpdateComplexDto,
|
|
|
|
|
) {
|
2026-04-24 23:02:05 +03:30
|
|
|
const complex = await this.prisma.complex.update({
|
2026-04-23 20:59:39 +03:30
|
|
|
where: { ...this.defaultWhere(partner_id, consumer_id, business_activity_id), id },
|
|
|
|
|
data,
|
|
|
|
|
})
|
2026-04-24 23:02:05 +03:30
|
|
|
return ResponseMapper.update(complex)
|
2026-04-23 20:59:39 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// async delete(id: string) {
|
|
|
|
|
// await this.prisma.complex.delete({ where: { id } })
|
|
|
|
|
// return ResponseMapper.delete()
|
|
|
|
|
// }
|
|
|
|
|
}
|