update license structures and setup file storage services

This commit is contained in:
2026-04-16 22:19:20 +03:30
parent d098ef10e1
commit ca494ee82a
75 changed files with 4550 additions and 1255 deletions
@@ -0,0 +1,34 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { PartnerChargedLicenseTransactionsService } from './chargedLicenseTransactions.service'
import { ChargeLicenseDto } from './dto/chargedLicenseTransactions.dto'
@ApiTags('AdminPartnerChargedLicenseTransactions')
@Controller('admin/partners/:partnerId/charge-license-transactions')
export class PartnerChargedLicenseTransactionsController {
constructor(private readonly service: PartnerChargedLicenseTransactionsService) {}
@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: ChargeLicenseDto) {
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)
// }
}
@@ -0,0 +1,12 @@
import { PrismaModule } from '@/prisma/prisma.module'
import { Module } from '@nestjs/common'
import { PartnerChargedLicenseTransactionsController } from './chargedLicenseTransactions.controller'
import { PartnerChargedLicenseTransactionsService } from './chargedLicenseTransactions.service'
@Module({
imports: [PrismaModule],
controllers: [PartnerChargedLicenseTransactionsController],
providers: [PartnerChargedLicenseTransactionsService],
exports: [PartnerChargedLicenseTransactionsService],
})
export class AdminPartnerChargedLicenseTransactionsModule {}
@@ -0,0 +1,122 @@
import {
ChargedLicenseTransactionsWhereInput,
LicenseCreateInput,
} from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
import { ChargeLicenseDto } from './dto/chargedLicenseTransactions.dto'
@Injectable()
export class PartnerChargedLicenseTransactionsService {
constructor(private readonly prisma: PrismaService) {}
private readonly mappedTransaction = (transaction: any) => {
const { licenses, ...rest } = transaction
return {
...rest,
charged_license_count: licenses.length,
remained_license_count: licenses.filter(license => !license?.activated_license)
.length,
}
}
async findAll(partner_id: string, page = 1, pageSize = 10) {
const defaultWhere: ChargedLicenseTransactionsWhereInput = {
partner_id,
}
const [transactions, count] = await this.prisma.$transaction(async tx => [
await tx.chargedLicenseTransactions.findMany({
where: defaultWhere,
skip: (page - 1) * pageSize,
take: pageSize,
select: {
id: true,
created_at: true,
activation_expires_at: true,
licenses: {
select: {
activated_license: {
select: {
id: true,
},
},
},
},
},
}),
await tx.chargedLicenseTransactions.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.chargedLicenseTransactions.findUniqueOrThrow({
where: {
id,
partner_id,
},
select: {
id: true,
created_at: true,
activation_expires_at: true,
licenses: {
select: {
id: true,
activated_license: {
select: {
id: true,
},
},
},
},
},
})
return ResponseMapper.single(this.mappedTransaction(transaction))
}
async create(partner_id: string, data: ChargeLicenseDto) {
try {
return await this.prisma.$transaction(async tx => {
const transaction = await tx.chargedLicenseTransactions.create({
data: {
activation_expires_at: data.activated_expires_at,
partner: { connect: { id: partner_id } },
},
})
if (!transaction) {
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
}
const licenseCreationPromises: any[] = []
for (let i = 0; i < data.quantity; i++) {
const license: LicenseCreateInput = {
charged_license_transaction: {
connect: {
id: transaction.id,
},
},
}
licenseCreationPromises.push(tx.license.create({ data: license }))
}
const createdLicenses = await Promise.all(licenseCreationPromises)
return { transaction, createdLicenses }
})
} catch (error) {
throw error
}
}
}
@@ -0,0 +1,24 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsDateString, IsNumber } from 'class-validator'
export class CreatePartnerLicenseDto {}
export class UpdateLicenseDto {}
export class ChargeLicenseDto {
@ApiProperty({
required: true,
minimum: 1,
maximum: 10_000,
})
@IsNumber({
maxDecimalPlaces: 0,
})
quantity: number
@ApiProperty({
required: true,
})
@IsDateString()
activated_expires_at: string
}