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:
@@ -1,9 +1,5 @@
|
||||
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { BusinessActivitiesService } from './business-activities.service'
|
||||
import {
|
||||
CreateBusinessActivitiesDto,
|
||||
UpdateBusinessActivityDto,
|
||||
} from './dto/create-business-activities.dto'
|
||||
|
||||
@Controller('admin/consumers/:consumerId/business_activities')
|
||||
export class BusinessActivitiesController {
|
||||
@@ -19,22 +15,22 @@ export class BusinessActivitiesController {
|
||||
return this.businessActivitiesService.findOne(consumerId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Body() data: CreateBusinessActivitiesDto,
|
||||
) {
|
||||
return this.businessActivitiesService.create(consumerId, data)
|
||||
}
|
||||
// @Post()
|
||||
// async create(
|
||||
// @Param('consumerId') consumerId: string,
|
||||
// @Body() data: CreateBusinessActivitiesDto,
|
||||
// ) {
|
||||
// return this.businessActivitiesService.create(consumerId, data)
|
||||
// }
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateBusinessActivityDto,
|
||||
) {
|
||||
return this.businessActivitiesService.update(consumerId, id, data)
|
||||
}
|
||||
// @Put(':id')
|
||||
// async update(
|
||||
// @Param('consumerId') consumerId: string,
|
||||
// @Param('id') id: string,
|
||||
// @Body() data: UpdateBusinessActivityDto,
|
||||
// ) {
|
||||
// return this.businessActivitiesService.update(consumerId, id, data)
|
||||
// }
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
|
||||
@@ -2,7 +2,6 @@ import { BusinessActivitySelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateBusinessActivitiesDto } from './dto/create-business-activities.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivitiesService {
|
||||
@@ -39,37 +38,37 @@ export class BusinessActivitiesService {
|
||||
return ResponseMapper.single(businessActivity)
|
||||
}
|
||||
|
||||
async create(consumer_id: string, data: CreateBusinessActivitiesDto) {
|
||||
const businessActivity = await this.prisma.businessActivity.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
guild: {
|
||||
connect: {
|
||||
id: data.guild_id,
|
||||
},
|
||||
},
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.create(businessActivity)
|
||||
}
|
||||
// async create(consumer_id: string, data: CreateBusinessActivitiesDto) {
|
||||
// const businessActivity = await this.prisma.businessActivity.create({
|
||||
// data: {
|
||||
// name: data.name,
|
||||
// guild: {
|
||||
// connect: {
|
||||
// id: data.guild_id,
|
||||
// },
|
||||
// },
|
||||
// consumer: {
|
||||
// connect: {
|
||||
// id: consumer_id,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// select: this.defaultSelect,
|
||||
// })
|
||||
// return ResponseMapper.create(businessActivity)
|
||||
// }
|
||||
|
||||
async update(consumer_id: string, id: string, data: any) {
|
||||
const businessActivity = await this.prisma.businessActivity.update({
|
||||
where: { id, consumer_id },
|
||||
data,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.update(businessActivity)
|
||||
}
|
||||
// async update(consumer_id: string, id: string, data: any) {
|
||||
// const businessActivity = await this.prisma.businessActivity.update({
|
||||
// where: { id, consumer_id },
|
||||
// data,
|
||||
// select: this.defaultSelect,
|
||||
// })
|
||||
// return ResponseMapper.update(businessActivity)
|
||||
// }
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.businessActivity.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
// async delete(id: string) {
|
||||
// await this.prisma.businessActivity.delete({ where: { id } })
|
||||
// return ResponseMapper.delete()
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { BusinessActivityComplexesService } from './complexes.service'
|
||||
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
|
||||
|
||||
@ApiTags('AdminBusinessActivityComplexes')
|
||||
@Controller(
|
||||
@@ -27,23 +26,23 @@ export class BusinessActivityComplexesController {
|
||||
return this.service.findOne(consumerId, businessActivityId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Body() data: CreateComplexDto,
|
||||
) {
|
||||
return this.service.create(businessActivityId, data)
|
||||
}
|
||||
// @Post()
|
||||
// async create(
|
||||
// @Param('businessActivityId') businessActivityId: string,
|
||||
// @Body() data: CreateComplexDto,
|
||||
// ) {
|
||||
// return this.service.create(businessActivityId, data)
|
||||
// }
|
||||
|
||||
@Patch(':id')
|
||||
async update(
|
||||
@Param('id') id: string,
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Body() data: UpdateComplexDto,
|
||||
) {
|
||||
return this.service.update(consumerId, businessActivityId, id, data)
|
||||
}
|
||||
// @Patch(':id')
|
||||
// async update(
|
||||
// @Param('id') id: string,
|
||||
// @Param('consumerId') consumerId: string,
|
||||
// @Param('businessActivityId') businessActivityId: string,
|
||||
// @Body() data: UpdateComplexDto,
|
||||
// ) {
|
||||
// return this.service.update(consumerId, businessActivityId, id, data)
|
||||
// }
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
|
||||
@@ -2,7 +2,6 @@ import { ComplexSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivityComplexesService {
|
||||
@@ -47,40 +46,40 @@ export class BusinessActivityComplexesService {
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(business_id: string, data: CreateComplexDto) {
|
||||
const account = await this.prisma.complex.create({
|
||||
data: {
|
||||
...data,
|
||||
business_activity: {
|
||||
connect: {
|
||||
id: business_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
}
|
||||
// async create(business_id: string, data: CreateComplexDto) {
|
||||
// const account = await this.prisma.complex.create({
|
||||
// data: {
|
||||
// ...data,
|
||||
// business_activity: {
|
||||
// connect: {
|
||||
// id: business_id,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// return ResponseMapper.create(account)
|
||||
// }
|
||||
|
||||
async update(
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
data: UpdateComplexDto,
|
||||
) {
|
||||
const account = await this.prisma.complex.update({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
consumer: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
},
|
||||
data,
|
||||
})
|
||||
return ResponseMapper.update(account)
|
||||
}
|
||||
// async update(
|
||||
// consumer_id: string,
|
||||
// business_activity_id: string,
|
||||
// id: string,
|
||||
// data: UpdateComplexDto,
|
||||
// ) {
|
||||
// const account = await this.prisma.complex.update({
|
||||
// where: {
|
||||
// business_activity: {
|
||||
// id: business_activity_id,
|
||||
// consumer: {
|
||||
// id: consumer_id,
|
||||
// },
|
||||
// },
|
||||
// id,
|
||||
// },
|
||||
// data,
|
||||
// })
|
||||
// return ResponseMapper.update(account)
|
||||
// }
|
||||
|
||||
// async delete(id: string) {
|
||||
// await this.prisma.complex.delete({ where: { id } })
|
||||
|
||||
+14
-15
@@ -1,6 +1,5 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
|
||||
import { ComplexPosesService } from './poses.service'
|
||||
|
||||
@ApiTags('AdminComplexPoses')
|
||||
@@ -25,20 +24,20 @@ export class ComplexPosesController {
|
||||
return this.service.findOne(businessActivityId, complexId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Param('complexId') complexId: string, @Body() data: CreatePosDto) {
|
||||
return this.service.create(complexId, data)
|
||||
}
|
||||
// @Post()
|
||||
// async create(@Param('complexId') complexId: string, @Body() data: CreatePosDto) {
|
||||
// return this.service.create(complexId, data)
|
||||
// }
|
||||
|
||||
@Patch(':id')
|
||||
async update(
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('complexId') complexId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdatePosDto,
|
||||
) {
|
||||
return this.service.update(businessActivityId, complexId, id, data)
|
||||
}
|
||||
// @Patch(':id')
|
||||
// async update(
|
||||
// @Param('businessActivityId') businessActivityId: string,
|
||||
// @Param('complexId') complexId: string,
|
||||
// @Param('id') id: string,
|
||||
// @Body() data: UpdatePosDto,
|
||||
// ) {
|
||||
// return this.service.update(businessActivityId, complexId, id, data)
|
||||
// }
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { POSStatus } from '@/generated/prisma/enums'
|
||||
import { PosCreateInput, PosSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
|
||||
import { CreatePosDto } from './dto/pos.dto'
|
||||
|
||||
@Injectable()
|
||||
export class ComplexPosesService {
|
||||
@@ -108,58 +107,58 @@ export class ComplexPosesService {
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(complex_id: string, data: CreatePosDto) {
|
||||
const account = await this.prisma.pos.create({
|
||||
data: {
|
||||
...this.defaultInsert(data, complex_id),
|
||||
status: POSStatus.ACTIVE,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
}
|
||||
// async create(complex_id: string, data: CreatePosDto) {
|
||||
// const account = await this.prisma.pos.create({
|
||||
// data: {
|
||||
// ...this.defaultInsert(data, complex_id),
|
||||
// status: POSStatus.ACTIVE,
|
||||
// },
|
||||
// })
|
||||
// return ResponseMapper.create(account)
|
||||
// }
|
||||
|
||||
async update(
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
id: string,
|
||||
data: UpdatePosDto,
|
||||
) {
|
||||
const { device_id, provider_id, ...rest } = data
|
||||
const account = await this.prisma.pos.update({
|
||||
where: {
|
||||
complex: {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
complex: {
|
||||
connect: {
|
||||
id: complex_id,
|
||||
},
|
||||
},
|
||||
provider: provider_id
|
||||
? {
|
||||
connect: {
|
||||
id: provider_id,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
device: device_id
|
||||
? {
|
||||
connect: {
|
||||
id: device_id,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
...rest,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.update(account)
|
||||
}
|
||||
// async update(
|
||||
// business_activity_id: string,
|
||||
// complex_id: string,
|
||||
// id: string,
|
||||
// data: UpdatePosDto,
|
||||
// ) {
|
||||
// const { device_id, provider_id, ...rest } = data
|
||||
// const account = await this.prisma.pos.update({
|
||||
// where: {
|
||||
// complex: {
|
||||
// id: complex_id,
|
||||
// business_activity: {
|
||||
// id: business_activity_id,
|
||||
// },
|
||||
// },
|
||||
// id,
|
||||
// },
|
||||
// data: {
|
||||
// complex: {
|
||||
// connect: {
|
||||
// id: complex_id,
|
||||
// },
|
||||
// },
|
||||
// provider: provider_id
|
||||
// ? {
|
||||
// connect: {
|
||||
// id: provider_id,
|
||||
// },
|
||||
// }
|
||||
// : undefined,
|
||||
// device: device_id
|
||||
// ? {
|
||||
// connect: {
|
||||
// id: device_id,
|
||||
// },
|
||||
// }
|
||||
// : undefined,
|
||||
// ...rest,
|
||||
// },
|
||||
// })
|
||||
// return ResponseMapper.update(account)
|
||||
// }
|
||||
|
||||
// async delete(id: string) {
|
||||
// await this.prisma.complex.delete({ where: { id } })
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { AdminConsumersService } from './consumers.service'
|
||||
import { CreateConsumerDto, UpdateConsumerDto } from './dto/consumer.dto'
|
||||
|
||||
@Controller('admin/consumers')
|
||||
export class AdminUsersController {
|
||||
@@ -16,13 +15,13 @@ export class AdminUsersController {
|
||||
return this.usersService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateConsumerDto) {
|
||||
return this.usersService.create(data)
|
||||
}
|
||||
// @Post()
|
||||
// async create(@Body() data: CreateConsumerDto) {
|
||||
// return this.usersService.create(data)
|
||||
// }
|
||||
|
||||
@Patch(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateConsumerDto) {
|
||||
return this.usersService.update(id, data)
|
||||
}
|
||||
// @Patch(':id')
|
||||
// async update(@Param('id') id: string, @Body() data: UpdateConsumerDto) {
|
||||
// return this.usersService.update(id, data)
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
ConsumerRole,
|
||||
PartnerStatus,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { ConsumerCreateInput, ConsumerSelect } from '@/generated/prisma/models'
|
||||
import { ConsumerSelect } from '@/generated/prisma/models'
|
||||
import mapConsumerWithLicenseUtil from '@/modules/consumer/utils/mapConsumerWithLicense.util'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateConsumerDto, UpdateConsumerDto } from './dto/consumer.dto'
|
||||
|
||||
@Injectable()
|
||||
export class AdminConsumersService {
|
||||
@@ -22,28 +14,35 @@ export class AdminConsumersService {
|
||||
last_name: true,
|
||||
mobile_number: true,
|
||||
status: true,
|
||||
created_at: true,
|
||||
activated_licenses: {
|
||||
partner: {
|
||||
select: {
|
||||
id: true,
|
||||
starts_at: true,
|
||||
expires_at: true,
|
||||
license: {
|
||||
select: {
|
||||
charged_license_transaction: {
|
||||
select: {
|
||||
partner: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
name: true,
|
||||
code: true,
|
||||
},
|
||||
},
|
||||
created_at: true,
|
||||
// activation: {
|
||||
// select: {
|
||||
// id: true,
|
||||
// starts_at: true,
|
||||
// expires_at: true,
|
||||
// license: {
|
||||
// select: {
|
||||
// charge_transaction: {
|
||||
// select: {
|
||||
// partner: {
|
||||
// select: {
|
||||
// id: true,
|
||||
// name: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
@@ -66,70 +65,70 @@ export class AdminConsumersService {
|
||||
return ResponseMapper.single(mapConsumerWithLicenseUtil(consumer))
|
||||
}
|
||||
|
||||
async create(data: CreateConsumerDto) {
|
||||
const startOfToday = new Date()
|
||||
startOfToday.setHours(0, 0, 0, 0)
|
||||
// async create(data: CreateConsumerDto) {
|
||||
// const startOfToday = new Date()
|
||||
// startOfToday.setHours(0, 0, 0, 0)
|
||||
|
||||
const { username, password, partner_id, ...rest } = data
|
||||
const dataToCreate: ConsumerCreateInput = {
|
||||
...rest,
|
||||
consumer_accounts: {
|
||||
create: {
|
||||
role: ConsumerRole.OWNER,
|
||||
account: {
|
||||
create: {
|
||||
username,
|
||||
password: await PasswordUtil.hash(password),
|
||||
type: AccountType.CONSUMER,
|
||||
status: AccountStatus.ACTIVE,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if (partner_id) {
|
||||
const partnerLicense = await this.prisma.license.findFirst({
|
||||
where: {
|
||||
charged_license_transaction: {
|
||||
partner: {
|
||||
id: partner_id,
|
||||
status: PartnerStatus.ACTIVE,
|
||||
},
|
||||
},
|
||||
activated_license: null,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
charged_license_transaction: {
|
||||
select: {
|
||||
partner: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
status: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
// const { username, password, partner_id, ...rest } = data
|
||||
// const dataToCreate: ConsumerCreateInput = {
|
||||
// ...rest,
|
||||
// consumer_accounts: {
|
||||
// create: {
|
||||
// role: ConsumerRole.OWNER,
|
||||
// account: {
|
||||
// create: {
|
||||
// username,
|
||||
// password: await PasswordUtil.hash(password),
|
||||
// type: AccountType.CONSUMER,
|
||||
// status: AccountStatus.ACTIVE,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// }
|
||||
// if (partner_id) {
|
||||
// const partnerLicense = await this.prisma.license.findFirst({
|
||||
// where: {
|
||||
// charge_transaction: {
|
||||
// partner: {
|
||||
// id: partner_id,
|
||||
// status: PartnerStatus.ACTIVE,
|
||||
// },
|
||||
// },
|
||||
// activation: null,
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// charge_transaction: {
|
||||
// select: {
|
||||
// partner: {
|
||||
// select: {
|
||||
// id: true,
|
||||
// name: true,
|
||||
// status: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
|
||||
if (!partnerLicense) {
|
||||
throw new BadRequestException(
|
||||
`تعداد لایسنسهای فعلی شریک تجاری به پایان رسیده است.`,
|
||||
)
|
||||
}
|
||||
}
|
||||
return this.prisma.consumer.create({
|
||||
data: dataToCreate,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
}
|
||||
// if (!partnerLicense) {
|
||||
// throw new BadRequestException(
|
||||
// `تعداد لایسنسهای فعلی شریک تجاری به پایان رسیده است.`,
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
// return this.prisma.consumer.create({
|
||||
// data: dataToCreate,
|
||||
// select: this.defaultSelect,
|
||||
// })
|
||||
// }
|
||||
|
||||
async update(id: string, data: UpdateConsumerDto) {
|
||||
return this.prisma.consumer.update({
|
||||
where: { id },
|
||||
data,
|
||||
})
|
||||
}
|
||||
// async update(id: string, data: UpdateConsumerDto) {
|
||||
// return this.prisma.consumer.update({
|
||||
// where: { id },
|
||||
// data,
|
||||
// })
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreateLicenseDto } from './dto/license.dto'
|
||||
import { LicensesService } from './licenses.service'
|
||||
|
||||
@ApiTags('AdminConsumerAccounts')
|
||||
@@ -13,10 +12,10 @@ export class LicensesController {
|
||||
return this.service.findAll(consumerId)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Param('consumerId') consumerId: string, @Body() data: CreateLicenseDto) {
|
||||
return this.service.create(consumerId, data)
|
||||
}
|
||||
// @Post()
|
||||
// async create(@Param('consumerId') consumerId: string, @Body() data: CreateLicenseDto) {
|
||||
// return this.service.create(consumerId, data)
|
||||
// }
|
||||
|
||||
// @Patch(':id')
|
||||
// async update(@Param('id') id: string, @Body() data: UpdateLicenseDto) {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { PartnerStatus } from '@/generated/prisma/enums'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateLicenseDto } from './dto/license.dto'
|
||||
|
||||
@Injectable()
|
||||
export class LicensesService {
|
||||
@@ -19,10 +17,10 @@ export class LicensesService {
|
||||
}
|
||||
|
||||
async findAll(consumer_id: string) {
|
||||
const licenses = await this.prisma.activatedLicense.findMany({
|
||||
where: {
|
||||
consumer_id,
|
||||
},
|
||||
const licenses = await this.prisma.licenseActivation.findMany({
|
||||
// where: {
|
||||
// consumer_id,
|
||||
// },
|
||||
select: {
|
||||
id: true,
|
||||
starts_at: true,
|
||||
@@ -31,7 +29,7 @@ export class LicensesService {
|
||||
license: {
|
||||
select: {
|
||||
id: true,
|
||||
charged_license_transaction: {
|
||||
charge_transaction: {
|
||||
select: {
|
||||
partner: {
|
||||
select: {
|
||||
@@ -49,54 +47,54 @@ export class LicensesService {
|
||||
return ResponseMapper.list(licenses)
|
||||
}
|
||||
|
||||
async create(consumerId: string, data: CreateLicenseDto) {
|
||||
const { starts_at, expires_at, partner_id } = data
|
||||
// async create(consumerId: string, data: CreateLicenseDto) {
|
||||
// const { starts_at, expires_at, partner_id } = data
|
||||
|
||||
const license = await this.prisma.$transaction(async tx => {
|
||||
const partnerLicense = await tx.license.findFirst({
|
||||
where: {
|
||||
activated_license: null,
|
||||
charged_license_transaction: {
|
||||
activation_expires_at: {
|
||||
gte: this.startOfToday,
|
||||
},
|
||||
partner: {
|
||||
id: partner_id,
|
||||
status: PartnerStatus.ACTIVE,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
// const license = await this.prisma.$transaction(async tx => {
|
||||
// const partnerLicense = await tx.license.findFirst({
|
||||
// where: {
|
||||
// activation: null,
|
||||
// charge_transaction: {
|
||||
// activation_expires_at: {
|
||||
// gte: this.startOfToday,
|
||||
// },
|
||||
// partner: {
|
||||
// id: partner_id,
|
||||
// status: PartnerStatus.ACTIVE,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// },
|
||||
// })
|
||||
|
||||
if (!partnerLicense) {
|
||||
throw new BadRequestException(
|
||||
`تعداد لایسنسهای فعلی شریک تجاری به پایان رسیده است.`,
|
||||
)
|
||||
}
|
||||
// if (!partnerLicense) {
|
||||
// throw new BadRequestException(
|
||||
// `تعداد لایسنسهای فعلی شریک تجاری به پایان رسیده است.`,
|
||||
// )
|
||||
// }
|
||||
|
||||
return await this.prisma.activatedLicense.create({
|
||||
data: {
|
||||
starts_at,
|
||||
expires_at: expires_at ?? this.setExpireDate(starts_at),
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumerId,
|
||||
},
|
||||
},
|
||||
license: {
|
||||
connect: {
|
||||
id: partnerLicense.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
// return await this.prisma.licenseActivation.create({
|
||||
// data: {
|
||||
// starts_at,
|
||||
// expires_at: expires_at ?? this.setExpireDate(starts_at),
|
||||
// consumer: {
|
||||
// connect: {
|
||||
// id: consumerId,
|
||||
// },
|
||||
// },
|
||||
// license: {
|
||||
// connect: {
|
||||
// id: partnerLicense.id,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// })
|
||||
|
||||
return ResponseMapper.create(license)
|
||||
}
|
||||
// return ResponseMapper.create(license)
|
||||
// }
|
||||
|
||||
// async update(id: string, data: UpdateLicenseDto) {
|
||||
// const { starts_at, expires_at, partner_id } = data
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ActivatedLicenseSelect } from '@/generated/prisma/models'
|
||||
import { LicenseActivationSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
@@ -7,22 +7,27 @@ import { ResponseMapper } from 'common/response/response-mapper'
|
||||
export class PartnerLicensesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private readonly licenseDefaultSelect: ActivatedLicenseSelect = {
|
||||
private readonly licenseDefaultSelect: LicenseActivationSelect = {
|
||||
id: true,
|
||||
starts_at: true,
|
||||
expires_at: true,
|
||||
consumer: {
|
||||
business_activity: {
|
||||
select: {
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
mobile_number: true,
|
||||
status: true,
|
||||
id: true,
|
||||
name: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
license: {
|
||||
select: {
|
||||
id: true,
|
||||
charged_license_transaction: {
|
||||
charge_transaction: {
|
||||
select: {
|
||||
partner: {
|
||||
select: {
|
||||
@@ -39,12 +44,12 @@ export class PartnerLicensesService {
|
||||
|
||||
async findAll(page = 1, pageSize = 10) {
|
||||
const [licenses, count] = await this.prisma.$transaction([
|
||||
this.prisma.activatedLicense.findMany({
|
||||
this.prisma.licenseActivation.findMany({
|
||||
select: this.licenseDefaultSelect,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
}),
|
||||
this.prisma.activatedLicense.count(),
|
||||
this.prisma.licenseActivation.count(),
|
||||
])
|
||||
|
||||
return ResponseMapper.paginate(licenses, {
|
||||
@@ -55,7 +60,7 @@ export class PartnerLicensesService {
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const license = await this.prisma.activatedLicense.findFirst({
|
||||
const license = await this.prisma.licenseActivation.findFirst({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ActivatedLicenseWhereInput } from '@/generated/prisma/models'
|
||||
import { LicenseActivationWhereInput } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
@@ -8,15 +8,15 @@ export class PartnerActivatedLicensesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(partner_id: string, page = 1, pageSize = 10) {
|
||||
const defaultWhere: ActivatedLicenseWhereInput = {
|
||||
const defaultWhere: LicenseActivationWhereInput = {
|
||||
license: {
|
||||
charged_license_transaction: {
|
||||
charge_transaction: {
|
||||
partner_id,
|
||||
},
|
||||
},
|
||||
}
|
||||
const [licenses, count] = await this.prisma.$transaction(async tx => [
|
||||
await tx.activatedLicense.findMany({
|
||||
await tx.licenseActivation.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
@@ -26,16 +26,22 @@ export class PartnerActivatedLicensesService {
|
||||
expires_at: true,
|
||||
license_id: true,
|
||||
created_at: true,
|
||||
consumer: {
|
||||
business_activity: {
|
||||
select: {
|
||||
id: true,
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
name: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
await tx.activatedLicense.count({
|
||||
await tx.licenseActivation.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { PartnerAllocatedAccountsService } from './allocatedAccounts.service'
|
||||
|
||||
@ApiTags('AdminPartnerAllocatedAccounts')
|
||||
@Controller('admin/partners/:partnerId/allocated-accounts')
|
||||
export class PartnerAllocatedAccountsController {
|
||||
constructor(private readonly service: PartnerAllocatedAccountsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('partnerId') partnerId: string) {
|
||||
return this.service.findAll(partnerId)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PartnerAllocatedAccountsController } from './allocatedAccounts.controller'
|
||||
import { PartnerAllocatedAccountsService } from './allocatedAccounts.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [PartnerAllocatedAccountsController],
|
||||
providers: [PartnerAllocatedAccountsService],
|
||||
exports: [PartnerAllocatedAccountsService],
|
||||
})
|
||||
export class AdminPartnerAllocatedAccountsModule {}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { PartnerAccountQuotaAllocationWhereInput } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerAllocatedAccountsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(partner_id: string, page = 1, pageSize = 10) {
|
||||
const defaultWhere: PartnerAccountQuotaAllocationWhereInput = {
|
||||
charge_transaction: {
|
||||
partner_id,
|
||||
},
|
||||
}
|
||||
|
||||
const [allocations, count] = await this.prisma.$transaction(async tx => [
|
||||
await tx.partnerAccountQuotaAllocation.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
select: {
|
||||
id: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
charge_transaction_id: true,
|
||||
license_id: true,
|
||||
charge_transaction: {
|
||||
select: {
|
||||
id: true,
|
||||
tracking_code: true,
|
||||
purchased_count: true,
|
||||
created_at: true,
|
||||
},
|
||||
},
|
||||
license: {
|
||||
select: {
|
||||
id: true,
|
||||
accounts_limit: true,
|
||||
activation: {
|
||||
select: {
|
||||
id: true,
|
||||
business_activity_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
await tx.partnerAccountQuotaAllocation.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
|
||||
return ResponseMapper.paginate(allocations, {
|
||||
page,
|
||||
pageSize,
|
||||
count,
|
||||
})
|
||||
}
|
||||
}
|
||||
+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
|
||||
}
|
||||
+4
-4
@@ -1,12 +1,12 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { PartnerChargedLicenseTransactionsService } from './chargedLicenseTransactions.service'
|
||||
import { PartnerLicenseChargeTransactionService } from './chargedLicenseTransactions.service'
|
||||
import { ChargeLicenseDto } from './dto/chargedLicenseTransactions.dto'
|
||||
|
||||
@ApiTags('AdminPartnerChargedLicenseTransactions')
|
||||
@ApiTags('AdminPartnerLicenseChargeTransaction')
|
||||
@Controller('admin/partners/:partnerId/charge-license-transactions')
|
||||
export class PartnerChargedLicenseTransactionsController {
|
||||
constructor(private readonly service: PartnerChargedLicenseTransactionsService) {}
|
||||
export class PartnerLicenseChargeTransactionController {
|
||||
constructor(private readonly service: PartnerLicenseChargeTransactionService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('partnerId') partnerId: string) {
|
||||
|
||||
+6
-6
@@ -1,12 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PartnerChargedLicenseTransactionsController } from './chargedLicenseTransactions.controller'
|
||||
import { PartnerChargedLicenseTransactionsService } from './chargedLicenseTransactions.service'
|
||||
import { PartnerLicenseChargeTransactionController } from './chargedLicenseTransactions.controller'
|
||||
import { PartnerLicenseChargeTransactionService } from './chargedLicenseTransactions.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [PartnerChargedLicenseTransactionsController],
|
||||
providers: [PartnerChargedLicenseTransactionsService],
|
||||
exports: [PartnerChargedLicenseTransactionsService],
|
||||
controllers: [PartnerLicenseChargeTransactionController],
|
||||
providers: [PartnerLicenseChargeTransactionService],
|
||||
exports: [PartnerLicenseChargeTransactionService],
|
||||
})
|
||||
export class AdminPartnerChargedLicenseTransactionsModule {}
|
||||
export class AdminPartnerLicenseChargeTransactionModule {}
|
||||
|
||||
+43
-44
@@ -3,7 +3,8 @@ import {
|
||||
isTrackingCodeUniqueViolation,
|
||||
} from '@/common/utils/tracking-code-generator.util'
|
||||
import {
|
||||
ChargedLicenseTransactionsWhereInput,
|
||||
LicenseChargeTransactionSelect,
|
||||
LicenseChargeTransactionWhereInput,
|
||||
LicenseCreateInput,
|
||||
} from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
@@ -12,51 +13,57 @@ import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { ChargeLicenseDto } from './dto/chargedLicenseTransactions.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerChargedLicenseTransactionsService {
|
||||
export class PartnerLicenseChargeTransactionService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private readonly TRACKING_CODE_LENGTH = 8
|
||||
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
|
||||
|
||||
private readonly mappedTransaction = (transaction: any) => {
|
||||
const { licenses, ...rest } = transaction
|
||||
const { licenses, purchased_count, _count, ...rest } = transaction
|
||||
|
||||
const activation_count = _count.licenses
|
||||
|
||||
return {
|
||||
...rest,
|
||||
charged_license_count: licenses.length,
|
||||
activated_license_count: licenses.filter(license => license?.activated_license)
|
||||
.length,
|
||||
remained_license_count: licenses.filter(license => !license?.activated_license)
|
||||
.length,
|
||||
charged_license_count: purchased_count,
|
||||
activation_count,
|
||||
remained_license_count: purchased_count - activation_count,
|
||||
}
|
||||
}
|
||||
|
||||
private readonly defaultSelect: LicenseChargeTransactionSelect = {
|
||||
id: true,
|
||||
created_at: true,
|
||||
activation_expires_at: true,
|
||||
tracking_code: true,
|
||||
purchased_count: true,
|
||||
_count: {
|
||||
select: {
|
||||
licenses: {
|
||||
where: {
|
||||
activation: {
|
||||
isNot: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
async findAll(partner_id: string, page = 1, pageSize = 10) {
|
||||
const defaultWhere: ChargedLicenseTransactionsWhereInput = {
|
||||
const defaultWhere: LicenseChargeTransactionWhereInput = {
|
||||
partner_id,
|
||||
}
|
||||
|
||||
const [transactions, count] = await this.prisma.$transaction(async tx => [
|
||||
await tx.chargedLicenseTransactions.findMany({
|
||||
await tx.licenseChargeTransaction.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
select: {
|
||||
id: true,
|
||||
created_at: true,
|
||||
activation_expires_at: true,
|
||||
tracking_code: true,
|
||||
licenses: {
|
||||
select: {
|
||||
activated_license: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
}),
|
||||
await tx.chargedLicenseTransactions.count({
|
||||
await tx.licenseChargeTransaction.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
@@ -71,26 +78,12 @@ export class PartnerChargedLicenseTransactionsService {
|
||||
}
|
||||
|
||||
async findOne(partner_id: string, id: string) {
|
||||
const transaction = this.prisma.chargedLicenseTransactions.findUniqueOrThrow({
|
||||
const transaction = this.prisma.licenseChargeTransaction.findUniqueOrThrow({
|
||||
where: {
|
||||
id,
|
||||
partner_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
created_at: true,
|
||||
activation_expires_at: true,
|
||||
licenses: {
|
||||
select: {
|
||||
id: true,
|
||||
activated_license: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(this.mappedTransaction(transaction))
|
||||
}
|
||||
@@ -102,10 +95,11 @@ export class PartnerChargedLicenseTransactionsService {
|
||||
|
||||
for (let attempt = 0; attempt < this.TRACKING_CODE_MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
transaction = await tx.chargedLicenseTransactions.create({
|
||||
transaction = await tx.licenseChargeTransaction.create({
|
||||
data: {
|
||||
activation_expires_at: data.activated_expires_at,
|
||||
tracking_code: generateTrackingCode('LIC', this.TRACKING_CODE_LENGTH),
|
||||
purchased_count: data.quantity,
|
||||
partner: { connect: { id: partner_id } },
|
||||
},
|
||||
})
|
||||
@@ -128,7 +122,7 @@ export class PartnerChargedLicenseTransactionsService {
|
||||
const licenseCreationPromises: any[] = []
|
||||
for (let i = 0; i < data.quantity; i++) {
|
||||
const license: LicenseCreateInput = {
|
||||
charged_license_transaction: {
|
||||
charge_transaction: {
|
||||
connect: {
|
||||
id: transaction.id,
|
||||
},
|
||||
@@ -139,6 +133,11 @@ export class PartnerChargedLicenseTransactionsService {
|
||||
}
|
||||
const createdLicenses = await Promise.all(licenseCreationPromises)
|
||||
|
||||
if (
|
||||
createdLicenses.some(createdLicense => createdLicense.activation_id === null)
|
||||
) {
|
||||
throw new BadRequestException('متاسفانه مشکلی پیش آمده است. ')
|
||||
}
|
||||
return { transaction, createdLicenses }
|
||||
})
|
||||
} catch (error) {
|
||||
|
||||
@@ -2,7 +2,9 @@ import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
|
||||
import { AdminPartnerActivatedLicensesModule } from './activatedLicenses/activatedLicenses.module'
|
||||
import { AdminPartnerChargedLicenseTransactionsModule } from './chargedLicenseTransactions/chargedLicenseTransactions.module'
|
||||
import { AdminPartnerAllocatedAccountsModule } from './allocatedAccounts/allocatedAccounts.module'
|
||||
import { PartnerAccountChargeTransactionModule } from './chargeAccountQoutaTransactions/chargeAccountQuotaTransactions.module'
|
||||
import { AdminPartnerLicenseChargeTransactionModule } from './chargedLicenseTransactions/chargedLicenseTransactions.module'
|
||||
import { AdminPartnerAccountsModule } from './partnerAccounts/accounts.module'
|
||||
import { PartnersController } from './partners.controller'
|
||||
import { PartnersService } from './partners.service'
|
||||
@@ -10,8 +12,10 @@ import { PartnersService } from './partners.service'
|
||||
@Module({
|
||||
imports: [
|
||||
PrismaModule,
|
||||
AdminPartnerChargedLicenseTransactionsModule,
|
||||
AdminPartnerLicenseChargeTransactionModule,
|
||||
AdminPartnerActivatedLicensesModule,
|
||||
AdminPartnerAllocatedAccountsModule,
|
||||
PartnerAccountChargeTransactionModule,
|
||||
AdminPartnerAccountsModule,
|
||||
],
|
||||
controllers: [PartnersController],
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { ChargedLicenseTransactions } from '@/generated/prisma/client'
|
||||
import { LicenseChargeTransaction } from '@/generated/prisma/client'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
@@ -22,24 +22,10 @@ export class PartnersService {
|
||||
code: true,
|
||||
status: true,
|
||||
created_at: true,
|
||||
chargedLicenseTransactions: {
|
||||
select: {
|
||||
activation_expires_at: true,
|
||||
licenses: {
|
||||
select: {
|
||||
activated_license: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
private readonly separateLicenseCount = (
|
||||
transactions: ChargedLicenseTransactions[] | null,
|
||||
transactions: LicenseChargeTransaction[] | null,
|
||||
) => {
|
||||
function toDateOnlyString(date) {
|
||||
return date.toISOString().slice(0, 10)
|
||||
@@ -49,9 +35,7 @@ export class PartnersService {
|
||||
|
||||
const used = transactions?.reduce((sum, cur) => {
|
||||
const license = cur as any
|
||||
return (
|
||||
sum + license.licenses.filter(license => license.activated_license).length || 0
|
||||
)
|
||||
return sum + license.licenses.filter(license => license.activation).length || 0
|
||||
}, 0)
|
||||
|
||||
const total = transactions?.reduce((sum, cur) => {
|
||||
@@ -64,9 +48,7 @@ export class PartnersService {
|
||||
const license = cur as any
|
||||
const activationExpiresDate = toDateOnlyString(license.activation_expires_at)
|
||||
if (startOfTodayDate > activationExpiresDate)
|
||||
return (
|
||||
sum + license.licenses.filter(license => !license.activated_license).length || 0
|
||||
)
|
||||
return sum + license.licenses.filter(license => !license.activation).length || 0
|
||||
return sum
|
||||
}, 0)
|
||||
|
||||
@@ -82,27 +64,85 @@ export class PartnersService {
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
const mappedPartners = partners.map(partner => {
|
||||
const { chargedLicenseTransactions, ...rest } = partner
|
||||
return {
|
||||
...rest,
|
||||
licenses_status: this.separateLicenseCount(chargedLicenseTransactions),
|
||||
}
|
||||
})
|
||||
// const mappedPartners = partners.map(partner => {
|
||||
// const { license_charge_transactions, account_quota_charge_transactions, ...rest } = partner
|
||||
// const a = { total: 0, used: 0, expired: 0 }
|
||||
|
||||
return ResponseMapper.list(mappedPartners)
|
||||
// account_quota_charge_transactions.forEach(account_quota_charge_transaction => {
|
||||
// a.total += account_quota_charge_transaction.
|
||||
// })
|
||||
// return {
|
||||
// ...rest,
|
||||
// licenses_status: this.separateLicenseCount(license_charge_transactions),
|
||||
// }
|
||||
// })
|
||||
|
||||
return ResponseMapper.list(partners)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const partner = await this.prisma.partner.findUniqueOrThrow({
|
||||
where: { id },
|
||||
select: this.defaultSelect,
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
license_charge_transactions: {
|
||||
select: {
|
||||
purchased_count: true,
|
||||
_count: {
|
||||
select: {
|
||||
licenses: {
|
||||
where: {
|
||||
activation: {
|
||||
isNot: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
account_quota_charge_transactions: {
|
||||
select: {
|
||||
purchased_count: true,
|
||||
_count: {
|
||||
select: {
|
||||
allocations: {
|
||||
where: {
|
||||
license_id: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const { license_charge_transactions, account_quota_charge_transactions, ...rest } =
|
||||
partner
|
||||
|
||||
const account_quota_status = { total: 0, used: 0, expired: 0 }
|
||||
account_quota_charge_transactions.forEach(account_quota_charge_transaction => {
|
||||
account_quota_status.total += account_quota_charge_transaction.purchased_count
|
||||
account_quota_status.used += account_quota_charge_transaction._count.allocations
|
||||
account_quota_status.expired +=
|
||||
account_quota_charge_transaction.purchased_count -
|
||||
account_quota_charge_transaction._count.allocations
|
||||
})
|
||||
|
||||
const licenses_status = { total: 0, used: 0, expired: 0 }
|
||||
license_charge_transactions.forEach(license_charge_transaction => {
|
||||
licenses_status.total += license_charge_transaction.purchased_count
|
||||
licenses_status.used += license_charge_transaction._count.licenses
|
||||
licenses_status.expired +=
|
||||
license_charge_transaction.purchased_count -
|
||||
license_charge_transaction._count.licenses
|
||||
})
|
||||
|
||||
const { chargedLicenseTransactions, ...rest } = partner
|
||||
const mappedPartner = {
|
||||
...rest,
|
||||
licenses_status: this.separateLicenseCount(chargedLicenseTransactions),
|
||||
licenses_status,
|
||||
account_quota_status,
|
||||
}
|
||||
|
||||
return ResponseMapper.single(mappedPartner)
|
||||
|
||||
Reference in New Issue
Block a user