feat(partners): add utility functions for partner business activity allocation limits and remaining licenses
- Implemented `getPartnerBusinessActivityAllocationLimits` to retrieve allocation limits for a partner's business activity. - Added `ensurePartnerBusinessActivityHasRemainingAllocation` to validate remaining allocation credits. - Created `getPartnerRemainingLicenses` to count remaining licenses for a partner. - Developed `getPartnerFirstRemainingLicense` to fetch the first unused license for a partner. - Introduced `ensurePartnerHasRemainingLicense` to ensure a partner has at least one unused license.
This commit is contained in:
+1
-1
@@ -36,7 +36,7 @@ export class BusinessActivityComplexesController {
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Body() data: CreateComplexDto,
|
||||
) {
|
||||
return this.service.create(businessActivityId, data)
|
||||
return this.service.create(partnerId, businessActivityId, data)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
|
||||
@@ -31,11 +31,11 @@ export class BusinessActivityComplexesService {
|
||||
})
|
||||
|
||||
async findAll(partner_id: string, consumer_id: string, business_activity_id: string) {
|
||||
const accounts = await this.prisma.complex.findMany({
|
||||
const complexes = await this.prisma.complex.findMany({
|
||||
where: this.defaultWhere(partner_id, consumer_id, business_activity_id),
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.list(accounts)
|
||||
return ResponseMapper.list(complexes)
|
||||
}
|
||||
|
||||
async findOne(
|
||||
@@ -44,18 +44,18 @@ export class BusinessActivityComplexesService {
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
) {
|
||||
const account = await this.prisma.complex.findUnique({
|
||||
const complex = await this.prisma.complex.findUnique({
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id, business_activity_id),
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
return ResponseMapper.single(complex)
|
||||
}
|
||||
|
||||
async create(business_id: string, data: CreateComplexDto) {
|
||||
const account = await this.prisma.complex.create({
|
||||
async create(partner_id: string, business_id: string, data: CreateComplexDto) {
|
||||
const complex = await this.prisma.complex.create({
|
||||
data: {
|
||||
...data,
|
||||
business_activity: {
|
||||
@@ -65,7 +65,7 @@ export class BusinessActivityComplexesService {
|
||||
},
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
return ResponseMapper.create(complex)
|
||||
}
|
||||
|
||||
async update(
|
||||
@@ -75,11 +75,11 @@ export class BusinessActivityComplexesService {
|
||||
id: string,
|
||||
data: UpdateComplexDto,
|
||||
) {
|
||||
const account = await this.prisma.complex.update({
|
||||
const complex = await this.prisma.complex.update({
|
||||
where: { ...this.defaultWhere(partner_id, consumer_id, business_activity_id), id },
|
||||
data,
|
||||
})
|
||||
return ResponseMapper.update(account)
|
||||
return ResponseMapper.update(complex)
|
||||
}
|
||||
|
||||
// async delete(id: string) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { POSStatus, POSType } from '@/generated/prisma/enums'
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { CreateConsumerAccountDto } from '@/modules/partners/consumers/accounts/dto/account.dto'
|
||||
import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger'
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreatePosDto {
|
||||
export class CreatePosDto extends OmitType(CreateConsumerAccountDto, ['role']) {
|
||||
@IsString()
|
||||
@ApiProperty({})
|
||||
name: string
|
||||
|
||||
+2
-1
@@ -31,10 +31,11 @@ export class ComplexPosesController {
|
||||
@Post()
|
||||
async create(
|
||||
@PartnerInfo('id') partnerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('complexId') complexId: string,
|
||||
@Body() data: CreatePosDto,
|
||||
) {
|
||||
return this.service.create(complexId, data)
|
||||
return this.service.create(partnerId, businessActivityId, complexId, data)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
|
||||
+131
-17
@@ -1,7 +1,13 @@
|
||||
import { POSStatus } from '@/generated/prisma/enums'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
ConsumerRole,
|
||||
POSStatus,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { PosCreateInput, PosSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
|
||||
|
||||
@@ -18,6 +24,16 @@ export class ComplexPosesService {
|
||||
created_at: true,
|
||||
pos_type: true,
|
||||
|
||||
account: {
|
||||
select: {
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
provider: {
|
||||
select: {
|
||||
id: true,
|
||||
@@ -67,10 +83,22 @@ export class ComplexPosesService {
|
||||
},
|
||||
})
|
||||
|
||||
private readonly mapPos = (pos: any) => {
|
||||
const { account, ...rest } = pos
|
||||
const { account: accountInfo } = account
|
||||
|
||||
return {
|
||||
...rest,
|
||||
account: {
|
||||
username: accountInfo.username,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
private readonly defaultInsert = (data: CreatePosDto, complex_id: string) => {
|
||||
const { device_id, provider_id, ...rest } = data
|
||||
|
||||
return {
|
||||
const dataToCreate: PosCreateInput = {
|
||||
...rest,
|
||||
complex: {
|
||||
connect: {
|
||||
@@ -91,15 +119,20 @@ export class ComplexPosesService {
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
} as PosCreateInput
|
||||
}
|
||||
|
||||
return dataToCreate
|
||||
}
|
||||
|
||||
async findAll(partner_id: string, business_activity_id: string, complex_id: string) {
|
||||
const accounts = await this.prisma.pos.findMany({
|
||||
const poses = await this.prisma.pos.findMany({
|
||||
where: this.defaultWhere(partner_id, complex_id, business_activity_id),
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.list(accounts)
|
||||
|
||||
const mappedPoses = poses.map(this.mapPos)
|
||||
|
||||
return ResponseMapper.list(mappedPoses)
|
||||
}
|
||||
|
||||
async findOne(
|
||||
@@ -108,21 +141,102 @@ export class ComplexPosesService {
|
||||
complex_id: string,
|
||||
id: string,
|
||||
) {
|
||||
const account = await this.prisma.pos.findUniqueOrThrow({
|
||||
const pos = await this.prisma.pos.findUniqueOrThrow({
|
||||
where: { ...this.defaultWhere(partner_id, complex_id, business_activity_id), id },
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
return ResponseMapper.single(this.mapPos(pos))
|
||||
}
|
||||
|
||||
async create(complex_id: string, data: CreatePosDto) {
|
||||
const account = await this.prisma.pos.create({
|
||||
data: {
|
||||
...this.defaultInsert(data, complex_id),
|
||||
status: POSStatus.ACTIVE,
|
||||
},
|
||||
async create(
|
||||
partner_id: string,
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
data: CreatePosDto,
|
||||
) {
|
||||
const pos = this.prisma.$transaction(async tx => {
|
||||
const ba = await tx.businessActivity.findUniqueOrThrow({
|
||||
where: {
|
||||
id: business_activity_id,
|
||||
consumer: {
|
||||
partner_id,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
consumer_id: true,
|
||||
license_activation: {
|
||||
select: {
|
||||
_count: {
|
||||
select: {
|
||||
account_allocations: {
|
||||
where: {
|
||||
account_id: {
|
||||
not: undefined,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!ba.license_activation?._count.account_allocations) {
|
||||
throw new BadRequestException(
|
||||
`محدودیت تعریف تعداد کاربران این فعالیت تجاری به پایان رسیده است.`,
|
||||
)
|
||||
}
|
||||
|
||||
const { device_id, provider_id, username, password, ...rest } = data
|
||||
|
||||
return await tx.pos.create({
|
||||
data: {
|
||||
...rest,
|
||||
status: POSStatus.ACTIVE,
|
||||
|
||||
account: {
|
||||
create: {
|
||||
role: ConsumerRole.OPERATOR,
|
||||
consumer: {
|
||||
connect: {
|
||||
id: ba.consumer_id,
|
||||
},
|
||||
},
|
||||
account: {
|
||||
create: {
|
||||
username,
|
||||
password: await PasswordUtil.hash(password),
|
||||
type: AccountType.CONSUMER,
|
||||
status: AccountStatus.ACTIVE,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
complex: {
|
||||
connect: {
|
||||
id: complex_id,
|
||||
},
|
||||
},
|
||||
device: device_id
|
||||
? {
|
||||
connect: {
|
||||
id: device_id,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
provider: provider_id
|
||||
? {
|
||||
connect: {
|
||||
id: provider_id,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
})
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
|
||||
return ResponseMapper.create(pos)
|
||||
}
|
||||
|
||||
async update(
|
||||
@@ -133,7 +247,7 @@ export class ComplexPosesService {
|
||||
data: UpdatePosDto,
|
||||
) {
|
||||
const { device_id, provider_id, ...rest } = data
|
||||
const account = await this.prisma.pos.update({
|
||||
const pos = await this.prisma.pos.update({
|
||||
where: { ...this.defaultWhere(partner_id, complex_id, business_activity_id), id },
|
||||
data: {
|
||||
complex: {
|
||||
@@ -158,7 +272,7 @@ export class ComplexPosesService {
|
||||
...rest,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.update(account)
|
||||
return ResponseMapper.update(pos)
|
||||
}
|
||||
|
||||
// async delete(id: string) {
|
||||
|
||||
Reference in New Issue
Block a user