update license structures and setup file storage services
This commit is contained in:
@@ -1,43 +1,111 @@
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { ChargedLicenseTransactions } from '@/generated/prisma/client'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
PartnerRole,
|
||||
PartnerStatus,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { PartnerSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreatePartnerDto, LicenseChargeDto, UpdatePartnerDto } from './dto/partner.dto'
|
||||
import { CreatePartnerDto, UpdatePartnerDto } from './dto/partner.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private readonly defaultSelect: PartnerSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
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,
|
||||
) => {
|
||||
function toDateOnlyString(date) {
|
||||
return date.toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
const startOfTodayDate = toDateOnlyString(new Date())
|
||||
|
||||
const used = transactions?.reduce((sum, cur) => {
|
||||
const license = cur as any
|
||||
return (
|
||||
sum + license.licenses.filter(license => license.activated_license).length || 0
|
||||
)
|
||||
}, 0)
|
||||
|
||||
const total = transactions?.reduce((sum, cur) => {
|
||||
const license = cur as any
|
||||
|
||||
return sum + license.licenses.length || 0
|
||||
}, 0)
|
||||
|
||||
const expired = transactions?.reduce((sum, cur) => {
|
||||
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
|
||||
}, 0)
|
||||
|
||||
return {
|
||||
total,
|
||||
used,
|
||||
expired,
|
||||
}
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
const partners = await this.prisma.partner.findMany()
|
||||
return ResponseMapper.list(partners)
|
||||
const partners = await this.prisma.partner.findMany({
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
const mappedPartners = partners.map(partner => {
|
||||
const { chargedLicenseTransactions, ...rest } = partner
|
||||
return {
|
||||
...rest,
|
||||
licenses_status: this.separateLicenseCount(chargedLicenseTransactions),
|
||||
}
|
||||
})
|
||||
|
||||
return ResponseMapper.list(mappedPartners)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const [partner, relatedLicenseCount] = await this.prisma.$transaction([
|
||||
this.prisma.partner.findUnique({
|
||||
where: { id },
|
||||
}),
|
||||
this.prisma.license.count({
|
||||
where: {
|
||||
partner_id: id,
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
return ResponseMapper.single({
|
||||
...partner,
|
||||
remained_license: Math.max(
|
||||
(partner?.license_quota || 0) - (relatedLicenseCount || 0),
|
||||
0,
|
||||
),
|
||||
const partner = await this.prisma.partner.findUniqueOrThrow({
|
||||
where: { id },
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
const { chargedLicenseTransactions, ...rest } = partner
|
||||
const mappedPartner = {
|
||||
...rest,
|
||||
licenses_status: this.separateLicenseCount(chargedLicenseTransactions),
|
||||
}
|
||||
|
||||
return ResponseMapper.single(mappedPartner)
|
||||
}
|
||||
|
||||
async create(data: CreatePartnerDto) {
|
||||
@@ -60,23 +128,16 @@ export class PartnersService {
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.create(partner)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdatePartnerDto) {
|
||||
const partner = await this.prisma.partner.update({ where: { id }, data })
|
||||
return ResponseMapper.update(partner)
|
||||
}
|
||||
|
||||
async licenseCharge(id: string, data: LicenseChargeDto) {
|
||||
const partner = await this.prisma.partner.update({
|
||||
where: { id },
|
||||
data: {
|
||||
license_quota: {
|
||||
increment: data.count,
|
||||
},
|
||||
},
|
||||
data,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.update(partner)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user