2026-04-23 20:59:39 +03:30
|
|
|
import {
|
|
|
|
|
generateTrackingCode,
|
|
|
|
|
isTrackingCodeUniqueViolation,
|
|
|
|
|
} from '@/common/utils/tracking-code-generator.util'
|
|
|
|
|
import {
|
|
|
|
|
PartnerAccountQuotaChargeTransactionSelect,
|
|
|
|
|
PartnerAccountQuotaChargeTransactionWhereInput,
|
2026-04-24 23:02:05 +03:30
|
|
|
PartnerAccountQuotaCreditCreateInput,
|
2026-04-23 20:59:39 +03:30
|
|
|
} from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
import { BadRequestException, Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import { ChargeAccountQuotaDto } from './dto/chargeAccountQuotaTransactions.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class PartnerAccountChargeTransactionService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
private readonly TRACKING_CODE_LENGTH = 8
|
|
|
|
|
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
|
|
|
|
|
|
|
|
|
|
private readonly mappedTransaction = (transaction: any) => {
|
|
|
|
|
const { allocations, purchased_count, _count, ...rest } = transaction
|
|
|
|
|
|
|
|
|
|
const activation_count = _count.allocations
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
charged_license_count: purchased_count,
|
|
|
|
|
activation_count,
|
|
|
|
|
remained_license_count: purchased_count - activation_count,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly defaultSelect: PartnerAccountQuotaChargeTransactionSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
tracking_code: true,
|
|
|
|
|
activation_expires_at: true,
|
|
|
|
|
purchased_count: true,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
2026-04-24 23:02:05 +03:30
|
|
|
credits: {
|
2026-04-23 20:59:39 +03:30
|
|
|
where: {
|
2026-04-24 23:02:05 +03:30
|
|
|
allocation: {
|
|
|
|
|
isNot: null,
|
2026-04-23 20:59:39 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
async findAll(partner_id: string, page = 1, perPage = 10) {
|
2026-04-23 20:59:39 +03:30
|
|
|
const defaultWhere: PartnerAccountQuotaChargeTransactionWhereInput = {
|
|
|
|
|
partner_id,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
const [transactions, total] = await this.prisma.$transaction(async tx => [
|
2026-04-23 20:59:39 +03:30
|
|
|
await tx.partnerAccountQuotaChargeTransaction.findMany({
|
|
|
|
|
where: defaultWhere,
|
2026-04-26 09:54:48 +03:30
|
|
|
skip: (page - 1) * perPage,
|
|
|
|
|
take: perPage,
|
2026-04-23 20:59:39 +03:30
|
|
|
select: this.defaultSelect,
|
|
|
|
|
}),
|
|
|
|
|
await tx.partnerAccountQuotaChargeTransaction.count({
|
|
|
|
|
where: defaultWhere,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const mappedTransactions = transactions.map(this.mappedTransaction)
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.paginate(mappedTransactions, {
|
|
|
|
|
page,
|
2026-04-26 09:54:48 +03:30
|
|
|
perPage,
|
|
|
|
|
total,
|
2026-04-23 20:59:39 +03:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(partner_id: string, id: string) {
|
|
|
|
|
const transaction =
|
|
|
|
|
this.prisma.partnerAccountQuotaChargeTransaction.findUniqueOrThrow({
|
|
|
|
|
where: {
|
|
|
|
|
id,
|
|
|
|
|
partner_id,
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.single(this.mappedTransaction(transaction))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(partner_id: string, data: ChargeAccountQuotaDto) {
|
|
|
|
|
try {
|
|
|
|
|
return await this.prisma.$transaction(async tx => {
|
|
|
|
|
let transaction: { id: string } | null = null
|
|
|
|
|
|
|
|
|
|
for (let attempt = 0; attempt < this.TRACKING_CODE_MAX_ATTEMPTS; attempt++) {
|
|
|
|
|
try {
|
|
|
|
|
transaction = await tx.partnerAccountQuotaChargeTransaction.create({
|
|
|
|
|
data: {
|
|
|
|
|
activation_expires_at: data.activated_expires_at,
|
|
|
|
|
tracking_code: generateTrackingCode('AQC', this.TRACKING_CODE_LENGTH),
|
|
|
|
|
purchased_count: data.quantity,
|
|
|
|
|
partner: { connect: { id: partner_id } },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
break
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (
|
|
|
|
|
isTrackingCodeUniqueViolation(error) &&
|
|
|
|
|
attempt < this.TRACKING_CODE_MAX_ATTEMPTS - 1
|
|
|
|
|
) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!transaction) {
|
|
|
|
|
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 23:02:05 +03:30
|
|
|
const creditCreationPromises: any[] = []
|
2026-04-23 20:59:39 +03:30
|
|
|
for (let i = 0; i < data.quantity; i++) {
|
2026-04-24 23:02:05 +03:30
|
|
|
const credit: PartnerAccountQuotaCreditCreateInput = {
|
2026-04-23 20:59:39 +03:30
|
|
|
charge_transaction: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: transaction.id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 23:02:05 +03:30
|
|
|
creditCreationPromises.push(
|
|
|
|
|
tx.partnerAccountQuotaCredit.create({ data: credit }),
|
2026-04-23 20:59:39 +03:30
|
|
|
)
|
|
|
|
|
}
|
2026-04-24 23:02:05 +03:30
|
|
|
const createdCredits = await Promise.all(creditCreationPromises)
|
2026-04-23 20:59:39 +03:30
|
|
|
|
2026-04-24 23:02:05 +03:30
|
|
|
if (createdCredits.some(createdCredit => createdCredit.activation_id === null)) {
|
2026-04-23 20:59:39 +03:30
|
|
|
throw new BadRequestException('متاسفانه مشکلی پیش آمده است. ')
|
|
|
|
|
}
|
2026-04-24 23:02:05 +03:30
|
|
|
return { transaction, createdCredits }
|
2026-04-23 20:59:39 +03:30
|
|
|
})
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|