Assist_Design/apps/bff/src/vendors/whmcs/services/whmcs-invoice.service.ts
2025-08-23 18:02:05 +09:00

229 lines
7.2 KiB
TypeScript

import { getErrorMessage } from "../../../common/utils/error.util";
import { Logger } from "nestjs-pino";
import { Injectable, NotFoundException, Inject } from "@nestjs/common";
import { Invoice, InvoiceList } from "@customer-portal/shared";
import { WhmcsConnectionService } from "./whmcs-connection.service";
import { WhmcsDataTransformer } from "../transformers/whmcs-data.transformer";
import { WhmcsCacheService } from "../cache/whmcs-cache.service";
import { WhmcsGetInvoicesParams } from "../types/whmcs-api.types";
export interface InvoiceFilters {
status?: "Paid" | "Unpaid" | "Cancelled" | "Overdue" | "Collections";
page?: number;
limit?: number;
}
@Injectable()
export class WhmcsInvoiceService {
constructor(
@Inject(Logger) private readonly logger: Logger,
private readonly connectionService: WhmcsConnectionService,
private readonly dataTransformer: WhmcsDataTransformer,
private readonly cacheService: WhmcsCacheService
) {}
/**
* Get paginated invoices for a client with caching
*/
async getInvoices(
clientId: number,
userId: string,
filters: InvoiceFilters = {}
): Promise<InvoiceList> {
const { status, page = 1, limit = 10 } = filters;
try {
// Try cache first
const cached = await this.cacheService.getInvoicesList(userId, page, limit, status);
if (cached) {
this.logger.debug(`Cache hit for invoices: user ${userId}, page ${page}`);
return cached;
}
// Calculate pagination for WHMCS API
const limitstart = (page - 1) * limit;
// Fetch from WHMCS API
const params: WhmcsGetInvoicesParams = {
userid: clientId, // WHMCS API uses 'userid' parameter, not 'clientid'
limitstart,
limitnum: limit,
orderby: "date",
order: "DESC",
...(status && { status: status as WhmcsGetInvoicesParams["status"] }),
};
const response = await this.connectionService.getInvoices(params);
if (!response.invoices?.invoice) {
this.logger.warn(`No invoices found for client ${clientId}`);
return {
invoices: [],
pagination: {
page,
totalPages: 0,
totalItems: 0,
},
};
}
// Transform invoices (note: items are not included by GetInvoices API)
const invoices = response.invoices.invoice
.map(whmcsInvoice => {
try {
return this.dataTransformer.transformInvoice(whmcsInvoice);
} catch (error) {
this.logger.error(`Failed to transform invoice ${whmcsInvoice.id}`, {
error: getErrorMessage(error),
});
return null;
}
})
.filter((invoice): invoice is Invoice => invoice !== null);
// Build result with pagination
this.logger.debug(`WHMCS GetInvoices Response Analysis for Client ${clientId}:`, {
totalresults: response.totalresults,
numreturned: response.numreturned,
startnumber: response.startnumber,
actualInvoicesReturned: invoices.length,
requestParams: {
userid: clientId,
limitstart,
limitnum: limit,
orderby: "date",
order: "DESC",
},
});
const totalItems = response.totalresults || 0;
const totalPages = Math.ceil(totalItems / limit);
const result: InvoiceList = {
invoices,
pagination: {
page,
totalPages,
totalItems,
nextCursor: page < totalPages ? (page + 1).toString() : undefined,
},
};
// Cache the result
await this.cacheService.setInvoicesList(userId, page, limit, status, result);
this.logger.log(`Fetched ${invoices.length} invoices for client ${clientId}, page ${page}`);
return result;
} catch (error) {
this.logger.error(`Failed to fetch invoices for client ${clientId}`, {
error: getErrorMessage(error),
filters,
});
throw error;
}
}
/**
* Get invoices with items (for subscription linking)
* This method fetches invoices and then enriches them with item details
*/
async getInvoicesWithItems(
clientId: number,
userId: string,
filters: InvoiceFilters = {}
): Promise<InvoiceList> {
try {
// First get the basic invoices list
const invoiceList = await this.getInvoices(clientId, userId, filters);
// For each invoice, fetch the detailed version with items
const invoicesWithItems = await Promise.all(
invoiceList.invoices.map(async invoice => {
try {
// Get detailed invoice with items
const detailedInvoice = await this.getInvoiceById(clientId, userId, invoice.id);
return detailedInvoice;
} catch (error) {
this.logger.warn(
`Failed to fetch details for invoice ${invoice.id}`,
getErrorMessage(error)
);
// Return the basic invoice if detailed fetch fails
return invoice;
}
})
);
const result: InvoiceList = {
invoices: invoicesWithItems,
pagination: invoiceList.pagination,
};
this.logger.log(
`Fetched ${invoicesWithItems.length} invoices with items for client ${clientId}`
);
return result;
} catch (error) {
this.logger.error(`Failed to fetch invoices with items for client ${clientId}`, {
error: getErrorMessage(error),
filters,
});
throw error;
}
}
/**
* Get individual invoice by ID with caching
*/
async getInvoiceById(clientId: number, userId: string, invoiceId: number): Promise<Invoice> {
try {
// Try cache first
const cached = await this.cacheService.getInvoice(userId, invoiceId);
if (cached) {
this.logger.debug(`Cache hit for invoice: user ${userId}, invoice ${invoiceId}`);
return cached;
}
// Fetch from WHMCS API
const response = await this.connectionService.getInvoice(invoiceId);
if (!response.invoiceid) {
throw new NotFoundException(`Invoice ${invoiceId} not found`);
}
// Verify the invoice belongs to this client
const invoiceClientId = response.userid;
if (invoiceClientId !== clientId) {
throw new NotFoundException(`Invoice ${invoiceId} not found`);
}
// Transform invoice
const invoice = this.dataTransformer.transformInvoice(response);
// Validate transformation
if (!this.dataTransformer.validateInvoice(invoice)) {
throw new Error(`Invalid invoice data after transformation`);
}
// Cache the result
await this.cacheService.setInvoice(userId, invoiceId, invoice);
this.logger.log(`Fetched invoice ${invoiceId} for client ${clientId}`);
return invoice;
} catch (error) {
this.logger.error(`Failed to fetch invoice ${invoiceId} for client ${clientId}`, {
error: getErrorMessage(error),
});
throw error;
}
}
/**
* Invalidate cache for a specific invoice
*/
async invalidateInvoiceCache(userId: string, invoiceId: number): Promise<void> {
await this.cacheService.invalidateInvoice(userId, invoiceId);
this.logger.log(`Invalidated invoice cache for user ${userId}, invoice ${invoiceId}`);
}
}