Files
psp_api/src/modules/partners/partners.service.ts
T
ahasani b72e6c7194 feat: refactor ComplexPosesService to remove unused defaultInsert method and improve findAll logic
fix: update PartnersService to use 'isNot' instead of 'not' for allocation checks

refactor: enhance BusinessActivityComplexesService to validate license activation before creating a complex

fix: adjust ComplexPosesService to ensure account allocation checks are accurate and handle errors properly

refactor: modify ConsumerMiddleware to set consumerData instead of partnerData for better clarity

feat: expand SaleInvoicesService to include additional fields in the invoice selection

chore: update business-activities module to include accounts-charge module for better organization

fix: ensure ComplexPosesService correctly handles account allocation during POS creation

feat: implement PartnerBusinessActivityAccountsCharge module with create functionality for account charges

refactor: streamline getPartnerBusinessActivityAllocationLimits utility for better clarity and functionality

chore: add migration script to update database schema with necessary constraints and foreign keys

feat: create DTO for accounts charge to validate incoming data
2026-04-25 15:16:59 +03:30

208 lines
5.4 KiB
TypeScript

import { ResponseMapper } from '@/common/response/response-mapper'
import { PartnerAccountSelect } from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { Injectable } from '@nestjs/common'
import { UpdatePartnerProfileDto } from './dto/partner.dto'
@Injectable()
export class PartnerService {
constructor(private readonly prisma: PrismaService) {}
private readonly defaultSelect: PartnerAccountSelect = {}
async me(partner_id: string, account_id: string) {
const partner = await this.prisma.partnerAccount.findUniqueOrThrow({
where: {
id: account_id,
partner_id,
},
select: {
id: true,
role: true,
account: {
select: {
username: true,
},
},
partner: {
select: {
id: true,
name: true,
},
},
},
})
return ResponseMapper.single(partner)
}
async getInfo(partner_id: string) {
const partner = await this.prisma.partner.findUniqueOrThrow({
where: {
id: partner_id,
},
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,
},
},
},
},
},
},
},
},
})
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,
})
}
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)
}
}