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:
+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) {
|
||||
|
||||
Reference in New Issue
Block a user