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:
+2
-1
@@ -31,10 +31,11 @@ export class BusinessActivitiesController {
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@PartnerInfo('id') partnerId: string,
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Body() data: CreateBusinessActivitiesDto,
|
||||
) {
|
||||
return this.businessActivitiesService.create(consumerId, data)
|
||||
return this.businessActivitiesService.create(partnerId, consumerId, data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { BusinessActivitySelect } from '@/generated/prisma/models'
|
||||
import { getPartnerFirstRemainingLicense } from '@/modules/partners/utils/getPartnerRemainingLicenses.util'
|
||||
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 { CreateBusinessActivitiesDto } from './dto/create-business-activities.dto'
|
||||
|
||||
@@ -8,6 +9,12 @@ import { CreateBusinessActivitiesDto } from './dto/create-business-activities.dt
|
||||
export class BusinessActivitiesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private readonly setExpireDate = (starts_at: Date) => {
|
||||
return new Date(
|
||||
new Date(starts_at).setFullYear(new Date(starts_at).getFullYear() + 1),
|
||||
).toISOString()
|
||||
}
|
||||
|
||||
defaultSelect: BusinessActivitySelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
@@ -27,10 +34,13 @@ export class BusinessActivitiesService {
|
||||
expires_at: true,
|
||||
license: {
|
||||
select: {
|
||||
accounts_limit: true,
|
||||
_count: {
|
||||
activation: {
|
||||
select: {
|
||||
account_allocations: true,
|
||||
_count: {
|
||||
select: {
|
||||
account_allocations: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -42,13 +52,13 @@ export class BusinessActivitiesService {
|
||||
private readonly prepareBusinessActivityResponse = (businessActivity: any) => {
|
||||
const { license_activation, ...rest } = businessActivity
|
||||
const { license, ...license_activation_rest } = license_activation
|
||||
const { accounts_limit, _count } = license
|
||||
const { _count } = license.activation
|
||||
|
||||
return {
|
||||
...rest,
|
||||
license_info: {
|
||||
...license_activation_rest,
|
||||
accounts_limit: accounts_limit + _count.account_allocations,
|
||||
accounts_limit: _count.account_allocations,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -82,24 +92,80 @@ export class BusinessActivitiesService {
|
||||
return ResponseMapper.single(this.prepareBusinessActivityResponse(businessActivity))
|
||||
}
|
||||
|
||||
async create(consumer_id: string, data: CreateBusinessActivitiesDto) {
|
||||
const { guild_id, ...rest } = data
|
||||
const businessActivity = await this.prisma.businessActivity.create({
|
||||
data: {
|
||||
...rest,
|
||||
guild: {
|
||||
connect: {
|
||||
id: data.guild_id,
|
||||
async create(
|
||||
partner_id: string,
|
||||
consumer_id: string,
|
||||
data: CreateBusinessActivitiesDto,
|
||||
) {
|
||||
const businessActivity = await this.prisma.$transaction(async tx => {
|
||||
const license = await getPartnerFirstRemainingLicense(tx, { partner_id })
|
||||
|
||||
const { guild_id, license_starts_at, expires_at, ...rest } = data
|
||||
const createdBusinessActivity = await tx.businessActivity.create({
|
||||
data: {
|
||||
...rest,
|
||||
guild: {
|
||||
connect: {
|
||||
id: guild_id,
|
||||
},
|
||||
},
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
license_activation: {
|
||||
create: {
|
||||
starts_at: license_starts_at ?? new Date(),
|
||||
expires_at:
|
||||
expires_at ?? this.setExpireDate(new Date(license_starts_at || '')),
|
||||
license: {
|
||||
connect: {
|
||||
id: license.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumer_id,
|
||||
select: {
|
||||
id: true,
|
||||
license_activation: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
const activationId = createdBusinessActivity.license_activation?.id
|
||||
if (!activationId) {
|
||||
throw new BadRequestException('لایسنس برای این فعالیت تجاری ایجاد نشد.')
|
||||
}
|
||||
|
||||
const licenseAllocationCreation = Array.from({
|
||||
length: license.accounts_limit,
|
||||
}).map(() =>
|
||||
tx.licenseAccountAllocation.create({
|
||||
data: {
|
||||
license_activation: {
|
||||
connect: {
|
||||
id: activationId,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
await Promise.all(licenseAllocationCreation)
|
||||
|
||||
return tx.businessActivity.findUniqueOrThrow({
|
||||
where: {
|
||||
id: createdBusinessActivity.id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
})
|
||||
|
||||
return ResponseMapper.create(businessActivity)
|
||||
}
|
||||
|
||||
|
||||
+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) {
|
||||
|
||||
+12
-2
@@ -1,5 +1,5 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
import { IsDateString, IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateBusinessActivitiesDto {
|
||||
@IsString()
|
||||
@@ -11,8 +11,18 @@ export class CreateBusinessActivitiesDto {
|
||||
economic_code: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({})
|
||||
@ApiProperty({ required: true })
|
||||
guild_id: string
|
||||
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
@ApiProperty({ required: false })
|
||||
license_starts_at: string
|
||||
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
@ApiProperty({ required: false })
|
||||
expires_at: string
|
||||
}
|
||||
|
||||
export class UpdateBusinessActivityDto extends PartialType(CreateBusinessActivitiesDto) {}
|
||||
|
||||
Reference in New Issue
Block a user