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:
+37
@@ -0,0 +1,37 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { PartnerAccountChargeTransactionService } from './chargeAccountQuotaTransactions.service'
|
||||
import { ChargeAccountQuotaDto } from './dto/chargeAccountQuotaTransactions.dto'
|
||||
|
||||
@ApiTags('Admin Partner Account Charge')
|
||||
@Controller('admin/partners/:partnerId/charge-account-transactions')
|
||||
export class PartnerAccountChargeTransactionController {
|
||||
constructor(private readonly service: PartnerAccountChargeTransactionService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('partnerId') partnerId: string) {
|
||||
return this.service.findAll(partnerId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('partnerId') partnerId: string, @Param('id') id: string) {
|
||||
return this.service.findOne(partnerId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Param('partnerId') partnerId: string,
|
||||
@Body() data: ChargeAccountQuotaDto,
|
||||
) {
|
||||
return this.service.create(partnerId, data)
|
||||
}
|
||||
|
||||
// @Patch(':id')
|
||||
// async update(
|
||||
// @Param('partnerId') partnerId: string,
|
||||
// @Param('id') id: string,
|
||||
// @Body() data: UpdateLicenseDto,
|
||||
// ) {
|
||||
// return this.licensesService.update(partnerId, id, data)
|
||||
// }
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PartnerAccountChargeTransactionController } from './chargeAccountQuotaTransactions.controller'
|
||||
import { PartnerAccountChargeTransactionService } from './chargeAccountQuotaTransactions.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [PartnerAccountChargeTransactionController],
|
||||
providers: [PartnerAccountChargeTransactionService],
|
||||
})
|
||||
export class PartnerAccountChargeTransactionModule {}
|
||||
+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
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { IsDateString, IsNumber } from 'class-validator'
|
||||
|
||||
export class ChargeAccountQuotaDto {
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
minimum: 1,
|
||||
maximum: 10_000,
|
||||
})
|
||||
@IsNumber({
|
||||
maxDecimalPlaces: 0,
|
||||
})
|
||||
quantity: number
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
})
|
||||
@IsDateString()
|
||||
activated_expires_at: string
|
||||
}
|
||||
Reference in New Issue
Block a user