debug license
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { LicenseStatus } from '@/generated/prisma/enums'
|
||||
import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger'
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsDateString, IsEnum } from 'class-validator'
|
||||
|
||||
export class CreateLicenseDto {
|
||||
@@ -21,16 +21,7 @@ export class CreateLicenseDto {
|
||||
partner_id: string
|
||||
}
|
||||
|
||||
export class UpdateLicenseDto extends OmitType(PartialType(CreateLicenseDto), [
|
||||
'starts_at',
|
||||
]) {
|
||||
@ApiProperty({ required: true, default: new Date().toISOString() })
|
||||
@IsDateString(
|
||||
{ strict: true },
|
||||
{ message: 'invoice_date must be a valid ISO-8601 string' },
|
||||
)
|
||||
starts_at: Date
|
||||
|
||||
export class UpdateLicenseDto extends PartialType(CreateLicenseDto) {
|
||||
@ApiProperty({ enum: LicenseStatus })
|
||||
@IsEnum(LicenseStatus)
|
||||
status?: LicenseStatus
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { LicenseStatus } from '@/generated/prisma/enums'
|
||||
import { LicenseUpdateInput } 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 { CreateLicenseDto, UpdateLicenseDto } from './dto/license.dto'
|
||||
|
||||
@@ -25,7 +26,7 @@ export class LicensesService {
|
||||
id: data.partner_id,
|
||||
},
|
||||
},
|
||||
consumers: {
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumerId,
|
||||
},
|
||||
@@ -37,13 +38,54 @@ export class LicensesService {
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateLicenseDto) {
|
||||
const { starts_at, expires_at } = data
|
||||
const { starts_at, expires_at, partner_id } = data
|
||||
const currentLicense = await this.prisma.license.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
partner_id: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (partner_id && currentLicense?.partner_id !== partner_id) {
|
||||
const partner = await this.prisma.partner.findUnique({
|
||||
where: {
|
||||
id: partner_id,
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
license_quota: true,
|
||||
_count: {
|
||||
select: {
|
||||
licenses: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!((partner?.license_quota || 0) - (partner?._count.licenses || 0))) {
|
||||
throw new BadRequestException(`تعداد لایسنسهای ${partner?.name} کافی نیست.`)
|
||||
}
|
||||
}
|
||||
|
||||
const dataToUpdate: LicenseUpdateInput = {}
|
||||
|
||||
if (partner_id) {
|
||||
dataToUpdate.partner = {
|
||||
connect: {
|
||||
id: partner_id,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (starts_at) {
|
||||
dataToUpdate.starts_at = starts_at
|
||||
dataToUpdate.expires_at = this.setExpireDate(starts_at)
|
||||
}
|
||||
|
||||
const license = await this.prisma.license.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...data,
|
||||
starts_at,
|
||||
expires_at: expires_at ?? this.setExpireDate(starts_at),
|
||||
...dataToUpdate,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.update(license)
|
||||
|
||||
Reference in New Issue
Block a user