Files
psp_api/src/modules/admin/partners/partners.service.ts
T

187 lines
5.1 KiB
TypeScript
Raw Normal View History

2026-03-16 00:33:40 +03:30
import { PasswordUtil } from '@/common/utils/password.util'
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, UpdatePartnerDto } from './dto/partner.dto'
@Injectable()
export class PartnersService {
private now = new Date().getTime()
constructor(private readonly prisma: PrismaService) {}
private readonly defaultSelect: PartnerSelect = {
id: true,
name: true,
code: true,
status: true,
created_at: true,
license_charge_transactions: {
select: {
activation_expires_at: true,
purchased_count: true,
_count: {
select: {
licenses: {
where: {
activation: {
isNot: null,
},
},
},
},
},
},
},
license_renew_charge_transactions: {
select: {
activation_expires_at: true,
purchased_count: true,
_count: {
select: {
license_renews: {
where: {
activation: {
isNot: undefined,
},
},
},
},
},
},
},
account_quota_charge_transactions: {
select: {
activation_expires_at: true,
purchased_count: true,
_count: {
select: {
credits: {
where: {
allocation_id: {
not: null,
},
},
},
},
},
},
},
}
private readonly mapPartner = (partner: any) => {
const {
license_charge_transactions,
account_quota_charge_transactions,
license_renew_charge_transactions,
...rest
} = partner
const account_quota_status = { total: 0, used: 0, expired: 0 }
account_quota_charge_transactions.forEach((account_quota_charge_transaction: any) => {
const total = account_quota_charge_transaction.purchased_count
const used = account_quota_charge_transaction._count.credits
account_quota_status.total += total
account_quota_status.used += used
account_quota_status.expired +=
account_quota_charge_transaction.activation_expires_at.getTime() <= this.now
? total - used
: 0
})
const licenses_status = { total: 0, used: 0, expired: 0 }
license_charge_transactions.forEach((license_charge_transaction: any) => {
const total = license_charge_transaction.purchased_count
const used = license_charge_transaction._count.licenses
licenses_status.total += total
licenses_status.used += used
licenses_status.expired +=
license_charge_transaction.activation_expires_at.getTime() <= this.now
? total - used
: 0
})
const license_renew_status = { total: 0, used: 0, expired: 0 }
license_renew_charge_transactions.forEach((license_renew_charge_transaction: any) => {
const total = license_renew_charge_transaction.purchased_count
const used = license_renew_charge_transaction._count.license_renews
license_renew_status.total += total
license_renew_status.used += used
license_renew_status.expired +=
license_renew_charge_transaction.activation_expires_at.getTime() <= this.now
? total - used
: 0
})
return {
...rest,
licenses_status,
license_renew_status,
account_quota_status,
}
}
async findAll() {
const partners = await this.prisma.partner.findMany({
select: this.defaultSelect,
})
const mappedPartners = partners.map(this.mapPartner)
return ResponseMapper.list(mappedPartners)
}
async findOne(id: string) {
const partner = await this.prisma.partner.findUniqueOrThrow({
where: { id },
select: this.defaultSelect,
})
return ResponseMapper.single(this.mapPartner(partner))
}
async create(data: CreatePartnerDto) {
2026-03-16 00:33:40 +03:30
const { username, password, ...rest } = data
const partner = await this.prisma.partner.create({
data: {
...rest,
status: PartnerStatus.ACTIVE,
partner_accounts: {
2026-03-16 00:33:40 +03:30
create: {
role: PartnerRole.OWNER,
account: {
create: {
username,
password: await PasswordUtil.hash(data.password),
status: AccountStatus.ACTIVE,
type: AccountType.PARTNER,
},
},
},
},
},
select: this.defaultSelect,
2026-03-16 00:33:40 +03:30
})
return ResponseMapper.create(partner)
}
async update(id: string, data: UpdatePartnerDto) {
2026-04-06 18:33:04 +03:30
const partner = await this.prisma.partner.update({
where: { id },
data,
select: this.defaultSelect,
2026-04-06 18:33:04 +03:30
})
return ResponseMapper.update(partner)
}
async delete(id: string) {
await this.prisma.partner.delete({ where: { id } })
return ResponseMapper.delete()
}
}