feat: refactor ComplexPosesService to remove unused defaultInsert method and improve findAll logic
fix: update PartnersService to use 'isNot' instead of 'not' for allocation checks refactor: enhance BusinessActivityComplexesService to validate license activation before creating a complex fix: adjust ComplexPosesService to ensure account allocation checks are accurate and handle errors properly refactor: modify ConsumerMiddleware to set consumerData instead of partnerData for better clarity feat: expand SaleInvoicesService to include additional fields in the invoice selection chore: update business-activities module to include accounts-charge module for better organization fix: ensure ComplexPosesService correctly handles account allocation during POS creation feat: implement PartnerBusinessActivityAccountsCharge module with create functionality for account charges refactor: streamline getPartnerBusinessActivityAllocationLimits utility for better clarity and functionality chore: add migration script to update database schema with necessary constraints and foreign keys feat: create DTO for accounts charge to validate incoming data
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
import { PartnerInfo } from '@/common/decorators/partnerInfo.decorator'
|
||||
import { Body, Controller, Param, Post } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { PartnerBusinessActivityAccountsChargeService } from './accounts-charge.service'
|
||||
import { CreateBusinessActivityAccountsChargeDto } from './dto/accounts-charge.dto'
|
||||
|
||||
@ApiTags('PartnerConsumerBAAccountsCharge')
|
||||
@Controller(
|
||||
'partner/consumers/:consumerId/business-activities/:businessActivityId/accounts-charge',
|
||||
)
|
||||
export class PartnerBusinessActivityAccountsChargeController {
|
||||
constructor(private readonly service: PartnerBusinessActivityAccountsChargeService) {}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@PartnerInfo('id') partnerId: string,
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Body() data: CreateBusinessActivityAccountsChargeDto,
|
||||
) {
|
||||
return this.service.create(partnerId, consumerId, businessActivityId, data)
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PartnerBusinessActivityAccountsChargeController } from './accounts-charge.controller'
|
||||
import { PartnerBusinessActivityAccountsChargeService } from './accounts-charge.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [PartnerBusinessActivityAccountsChargeController],
|
||||
providers: [PartnerBusinessActivityAccountsChargeService],
|
||||
})
|
||||
export class PartnerBusinessActivityAccountsChargeModule {}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateBusinessActivityAccountsChargeDto } from './dto/accounts-charge.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerBusinessActivityAccountsChargeService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async create(
|
||||
partner_id: string,
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
data: CreateBusinessActivityAccountsChargeDto,
|
||||
) {
|
||||
const { quantity } = data
|
||||
|
||||
const startOfDay = new Date()
|
||||
startOfDay.setHours(0, 0, 0, 0)
|
||||
|
||||
const result = await this.prisma.$transaction(async tx => {
|
||||
const activation = await tx.licenseActivation.findFirst({
|
||||
where: {
|
||||
business_activity_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
consumer: {
|
||||
id: consumer_id,
|
||||
partner_id,
|
||||
},
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
expires_at: {
|
||||
gte: startOfDay,
|
||||
},
|
||||
},
|
||||
{
|
||||
license_renews: {
|
||||
some: {
|
||||
expires_at: {
|
||||
gte: startOfDay,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!activation) {
|
||||
throw new BadRequestException('لایسنس فعال برای این فعالیت تجاری یافت نشد.')
|
||||
}
|
||||
|
||||
const credits = await tx.partnerAccountQuotaCredit.findMany({
|
||||
where: {
|
||||
allocation: null,
|
||||
charge_transaction: {
|
||||
partner_id,
|
||||
activation_expires_at: {
|
||||
gte: startOfDay,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
charge_transaction: {
|
||||
activation_expires_at: 'asc',
|
||||
},
|
||||
},
|
||||
{
|
||||
created_at: 'asc',
|
||||
},
|
||||
],
|
||||
take: quantity,
|
||||
select: {
|
||||
id: true,
|
||||
charge_transaction: {
|
||||
select: {
|
||||
activation_expires_at: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (credits.length < quantity) {
|
||||
throw new BadRequestException('اعتبار کافی برای تخصیص حساب وجود ندارد.')
|
||||
}
|
||||
|
||||
const allocations = await Promise.all(
|
||||
credits.map(credit =>
|
||||
tx.licenseAccountAllocation.create({
|
||||
data: {
|
||||
license_activation: {
|
||||
connect: {
|
||||
id: activation.id,
|
||||
},
|
||||
},
|
||||
credit: {
|
||||
connect: {
|
||||
id: credit.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
credit_id: true,
|
||||
license_activation_id: true,
|
||||
created_at: true,
|
||||
},
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
return {
|
||||
quantity,
|
||||
activation_id: activation.id,
|
||||
allocations,
|
||||
}
|
||||
})
|
||||
|
||||
return ResponseMapper.create(result.allocations)
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { IsInt, Min } from 'class-validator'
|
||||
|
||||
export class CreateBusinessActivityAccountsChargeDto {
|
||||
@ApiProperty({ minimum: 1, example: 1 })
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
quantity: number
|
||||
}
|
||||
Reference in New Issue
Block a user