feat: Refactor accounts service and related DTOs to enhance consumer account management

- Updated AccountsService to filter out OWNER roles in findAll method.
- Removed CreateConsumerAccountDto and its role property from account creation logic.
- Enhanced BusinessActivitiesService to include complex counts in responses.
- Modified ComplexesController and ComplexesService to handle consumer-specific logic.
- Introduced CreateConsumerComplexDto and UpdateConsumerComplexDto for complex management.
- Updated PosesController and PosesService to manage POS creation and updates with consumer context.
- Added utility function to calculate remaining accounts for business activities.
- Updated Prisma migration to add account_id to poses and enforce unique constraints.
This commit is contained in:
2026-04-24 04:27:28 +03:30
parent 11488093a7
commit 9b652a3603
21 changed files with 770 additions and 125 deletions
@@ -0,0 +1,99 @@
import { BadRequestException } from '@nestjs/common'
type BusinessActivityAccountQuotaClient = {
licenseActivation: {
findFirst: (args: any) => Promise<any>
}
pos: {
count: (args: any) => Promise<number>
}
}
type GetBusinessActivityRemainingAccountsParams = {
consumer_id: string
business_activity_id: string
referenceDate?: Date
}
export type BusinessActivityRemainingAccounts = {
total_allocated_accounts: number
used_accounts: number
remaining_accounts: number
}
export const getBusinessActivityRemainingAccounts = async (
prisma: BusinessActivityAccountQuotaClient,
params: GetBusinessActivityRemainingAccountsParams,
): Promise<BusinessActivityRemainingAccounts> => {
const { consumer_id, business_activity_id, referenceDate = new Date() } = params
const startOfDay = new Date(referenceDate)
startOfDay.setHours(0, 0, 0, 0)
const relatedLicense = await prisma.licenseActivation.findFirst({
where: {
business_activity_id,
business_activity: {
consumer_id,
},
OR: [
{
expires_at: {
gte: startOfDay,
},
},
{
license_renews: {
some: {
expires_at: {
gte: startOfDay,
},
},
},
},
],
},
select: {
license: {
select: {
accounts_limit: true,
_count: {
select: {
account_allocations: {
where: {
license_id: {
not: null,
},
},
},
},
},
},
},
},
})
if (!relatedLicense) {
throw new BadRequestException('برای این فعالیت تجاری لایسنس فعالی وجود ندارد.')
}
const { accounts_limit, _count } = relatedLicense.license
const totalAllocatedAccounts = accounts_limit + _count.account_allocations
const usedAccounts = await prisma.pos.count({
where: {
account_id: {
not: null,
},
complex: {
business_activity_id,
},
},
})
return {
total_allocated_accounts: totalAllocatedAccounts,
used_accounts: usedAccounts,
remaining_accounts: totalAllocatedAccounts - usedAccounts,
}
}