2026-04-13 15:47:59 +03:30
|
|
|
import { ResponseMapper } from '@/common/response/response-mapper'
|
2026-04-24 23:02:05 +03:30
|
|
|
import { PartnerAccountSelect } from '@/generated/prisma/models'
|
2026-04-13 15:47:59 +03:30
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
import { Injectable } from '@nestjs/common'
|
2026-04-24 02:20:15 +03:30
|
|
|
import { UpdatePartnerProfileDto } from './dto/partner.dto'
|
2026-04-13 15:47:59 +03:30
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class PartnerService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
2026-04-24 23:02:05 +03:30
|
|
|
private readonly defaultSelect: PartnerAccountSelect = {}
|
|
|
|
|
|
2026-04-25 15:16:59 +03:30
|
|
|
async me(partner_id: string, account_id: string) {
|
2026-04-24 23:02:05 +03:30
|
|
|
const partner = await this.prisma.partnerAccount.findUniqueOrThrow({
|
|
|
|
|
where: {
|
2026-04-25 15:16:59 +03:30
|
|
|
id: account_id,
|
2026-04-24 23:02:05 +03:30
|
|
|
partner_id,
|
|
|
|
|
},
|
2026-04-13 15:47:59 +03:30
|
|
|
select: {
|
2026-04-24 23:02:05 +03:30
|
|
|
id: true,
|
|
|
|
|
role: true,
|
|
|
|
|
account: {
|
2026-04-23 20:59:39 +03:30
|
|
|
select: {
|
2026-04-24 23:02:05 +03:30
|
|
|
username: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
partner: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
2026-04-23 20:59:39 +03:30
|
|
|
},
|
|
|
|
|
},
|
2026-04-13 15:47:59 +03:30
|
|
|
},
|
2026-04-24 23:02:05 +03:30
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single(partner)
|
2026-04-24 02:20:15 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getInfo(partner_id: string) {
|
|
|
|
|
const partner = await this.prisma.partner.findUniqueOrThrow({
|
|
|
|
|
where: {
|
|
|
|
|
id: partner_id,
|
|
|
|
|
},
|
2026-04-24 23:02:05 +03:30
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
code: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
license_charge_transactions: {
|
|
|
|
|
select: {
|
|
|
|
|
purchased_count: true,
|
|
|
|
|
activation_expires_at: true,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
licenses: {
|
|
|
|
|
where: {
|
|
|
|
|
activation: {
|
|
|
|
|
isNot: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
account_quota_charge_transactions: {
|
|
|
|
|
select: {
|
|
|
|
|
purchased_count: true,
|
|
|
|
|
activation_expires_at: true,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
credits: {
|
|
|
|
|
where: {
|
|
|
|
|
charge_transaction: {
|
|
|
|
|
isNot: null,
|
|
|
|
|
},
|
|
|
|
|
allocation: {
|
|
|
|
|
isNot: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
license_renew_charge_transactions: {
|
|
|
|
|
select: {
|
|
|
|
|
purchased_count: true,
|
|
|
|
|
activation_expires_at: true,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
license_renews: {
|
|
|
|
|
where: {
|
|
|
|
|
activation: {
|
|
|
|
|
isNot: undefined,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-13 15:47:59 +03:30
|
|
|
})
|
|
|
|
|
|
2026-04-24 23:02:05 +03:30
|
|
|
const {
|
|
|
|
|
license_charge_transactions,
|
|
|
|
|
account_quota_charge_transactions,
|
|
|
|
|
license_renew_charge_transactions,
|
|
|
|
|
...partnerInfo
|
|
|
|
|
} = partner
|
|
|
|
|
|
|
|
|
|
const licenses_status = {
|
|
|
|
|
total_purchased: 0,
|
|
|
|
|
total_activated: 0,
|
|
|
|
|
total_expired: 0,
|
|
|
|
|
}
|
|
|
|
|
const account_quotas_status = {
|
|
|
|
|
total_purchased: 0,
|
|
|
|
|
total_activated: 0,
|
|
|
|
|
total_expired: 0,
|
|
|
|
|
}
|
|
|
|
|
const license_renews_status = {
|
|
|
|
|
total_purchased: 0,
|
|
|
|
|
total_activated: 0,
|
|
|
|
|
total_expired: 0,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const now = new Date()
|
|
|
|
|
const checkExpired = (expires_at: Date) => expires_at < now
|
|
|
|
|
|
|
|
|
|
const prepareData = (
|
|
|
|
|
items_count: number,
|
|
|
|
|
activation_expires_at: Date,
|
|
|
|
|
purchased_count: number,
|
|
|
|
|
) => {
|
|
|
|
|
let totalActivated = 0
|
|
|
|
|
let totalExpired = 0
|
|
|
|
|
|
|
|
|
|
if (!checkExpired(activation_expires_at)) {
|
|
|
|
|
totalActivated = items_count
|
|
|
|
|
} else {
|
|
|
|
|
totalExpired = items_count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
total_purchased: purchased_count,
|
|
|
|
|
total_activated: totalActivated,
|
|
|
|
|
total_expired: totalExpired,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
license_charge_transactions.forEach(
|
|
|
|
|
({ _count, activation_expires_at, purchased_count }) => {
|
|
|
|
|
const status = prepareData(
|
|
|
|
|
_count.licenses,
|
|
|
|
|
activation_expires_at,
|
|
|
|
|
purchased_count,
|
|
|
|
|
)
|
|
|
|
|
licenses_status.total_purchased += status.total_purchased
|
|
|
|
|
licenses_status.total_activated += status.total_activated
|
|
|
|
|
licenses_status.total_expired += status.total_expired
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
account_quota_charge_transactions.forEach(
|
|
|
|
|
({ _count, activation_expires_at, purchased_count }) => {
|
|
|
|
|
const status = prepareData(_count.credits, activation_expires_at, purchased_count)
|
|
|
|
|
account_quotas_status.total_purchased += status.total_purchased
|
|
|
|
|
account_quotas_status.total_activated += status.total_activated
|
|
|
|
|
account_quotas_status.total_expired += status.total_expired
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
license_renew_charge_transactions.forEach(
|
|
|
|
|
({ _count, activation_expires_at, purchased_count }) => {
|
|
|
|
|
const status = prepareData(
|
|
|
|
|
_count.license_renews,
|
|
|
|
|
activation_expires_at,
|
|
|
|
|
purchased_count,
|
|
|
|
|
)
|
|
|
|
|
license_renews_status.total_purchased += status.total_purchased
|
|
|
|
|
license_renews_status.total_activated += status.total_activated
|
|
|
|
|
license_renews_status.total_expired += status.total_expired
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single({
|
|
|
|
|
licenses_status,
|
|
|
|
|
license_renews_status,
|
|
|
|
|
account_quotas_status,
|
|
|
|
|
...partnerInfo,
|
|
|
|
|
})
|
2026-04-13 15:47:59 +03:30
|
|
|
}
|
2026-04-24 02:20:15 +03:30
|
|
|
|
|
|
|
|
async updateInfo(partner_id: string, data: UpdatePartnerProfileDto) {
|
|
|
|
|
const updatedPartner = await this.prisma.partner.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: partner_id,
|
|
|
|
|
},
|
|
|
|
|
data,
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single(updatedPartner)
|
|
|
|
|
}
|
2026-04-13 15:47:59 +03:30
|
|
|
}
|