feat: add consumer accounts and business activities management
- Implemented AccountsService for managing consumer accounts including create, update, delete, and find operations. - Created DTOs for account creation and updates. - Developed BusinessActivitiesController and BusinessActivitiesService for handling business activities related to consumers. - Added complexes management with ComplexesController and ComplexesService. - Introduced POS management with ComplexPosesController and ComplexPosesService. - Created necessary DTOs for business activities and POS. - Established Licenses management with LicensesController and LicensesService. - Integrated consumer management with PartnerConsumersController and PartnerConsumersService. - Added Prisma module imports and service connections across modules.
This commit is contained in:
+154
@@ -0,0 +1,154 @@
|
||||
import {
|
||||
generateTrackingCode,
|
||||
isTrackingCodeUniqueViolation,
|
||||
} from '@/common/utils/tracking-code-generator.util'
|
||||
import {
|
||||
PartnerAccountQuotaAllocationCreateInput,
|
||||
PartnerAccountQuotaChargeTransactionSelect,
|
||||
PartnerAccountQuotaChargeTransactionWhereInput,
|
||||
} 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: {
|
||||
allocations: {
|
||||
where: {
|
||||
license_id: {
|
||||
not: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
async findAll(partner_id: string, page = 1, pageSize = 10) {
|
||||
const defaultWhere: PartnerAccountQuotaChargeTransactionWhereInput = {
|
||||
partner_id,
|
||||
}
|
||||
|
||||
const [transactions, count] = await this.prisma.$transaction(async tx => [
|
||||
await tx.partnerAccountQuotaChargeTransaction.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
select: this.defaultSelect,
|
||||
}),
|
||||
await tx.partnerAccountQuotaChargeTransaction.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
|
||||
const mappedTransactions = transactions.map(this.mappedTransaction)
|
||||
|
||||
return ResponseMapper.paginate(mappedTransactions, {
|
||||
page,
|
||||
pageSize,
|
||||
count,
|
||||
})
|
||||
}
|
||||
|
||||
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('متاسفانه مشکلی پیش آمده است.')
|
||||
}
|
||||
|
||||
const accountCreationPromises: any[] = []
|
||||
for (let i = 0; i < data.quantity; i++) {
|
||||
const account: PartnerAccountQuotaAllocationCreateInput = {
|
||||
charge_transaction: {
|
||||
connect: {
|
||||
id: transaction.id,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
accountCreationPromises.push(
|
||||
tx.partnerAccountQuotaAllocation.create({ data: account }),
|
||||
)
|
||||
}
|
||||
const createdAllocations = await Promise.all(accountCreationPromises)
|
||||
|
||||
console.log(createdAllocations)
|
||||
|
||||
if (
|
||||
createdAllocations.some(
|
||||
createdAllocation => createdAllocation.activation_id === null,
|
||||
)
|
||||
) {
|
||||
throw new BadRequestException('متاسفانه مشکلی پیش آمده است. ')
|
||||
}
|
||||
return { transaction, createdAllocations }
|
||||
})
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user