fix: C-7 webhook tenant lookup, C-5 full tax calc §15, M-2/M-3/M-4/M-8/L-7/L-8/L-9 gap fixes

This commit is contained in:
Lizandro Guarnizo
2026-06-01 21:10:43 -05:00
parent a047a8b032
commit a5842278fb
11 changed files with 156 additions and 54 deletions
@@ -5,6 +5,7 @@ import {
Logger,
} from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
import { NotificationsService } from "../notifications/notifications.service";
function genCode(): string {
const date = new Date().toISOString().slice(0, 10).replace(/-/g, "");
@@ -16,7 +17,10 @@ function genCode(): string {
export class ConsolidationsService {
private readonly logger = new Logger(ConsolidationsService.name);
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private notifications: NotificationsService,
) {}
/** Lista consolidaciones del tenant. Cliente solo ve las suyas. */
async list(tenantId: string, userId?: string): Promise<any[]> {
@@ -115,24 +119,38 @@ export class ConsolidationsService {
if (c.status !== "CERRADA") throw new BadRequestException("La consolidación debe estar CERRADA para despachar");
// Actualizar todos los paquetes a EN_TRANSITO_ECUADOR
const pkgIds = await this.prisma.client.consolidationPackage.findMany({
const pkgLinks = await this.prisma.client.consolidationPackage.findMany({
where: { consolidationId: id },
select: { packageId: true },
});
await this.prisma.client.package.updateMany({
where: { id: { in: pkgIds.map(p => p.packageId) } },
where: { id: { in: pkgLinks.map(p => p.packageId) } },
data: { status: "EN_TRANSITO_ECUADOR" },
});
this.logger.log(`[CONSOLIDATION] Dispatched ${id}${pkgIds.length} packages → EN_TRANSITO_ECUADOR`);
// Notificar a cada cliente cuyo paquete fue despachado
for (const { packageId } of pkgLinks) {
try {
const pkg = await this.prisma.client.package.findUnique({
where: { id: packageId },
include: { user: true },
});
if (pkg) {
await this.notifications.notifyStatusChange(pkg, pkg.user);
}
} catch (err: any) {
this.logger.warn(`[CONSOLIDATION] Notif failed for pkg ${packageId}: ${err.message}`);
}
}
this.logger.log(`[CONSOLIDATION] Dispatched ${id}${pkgLinks.length} packages → EN_TRANSITO_ECUADOR`);
return this.prisma.client.consolidation.update({
where: { id },
data: { status: "DESPACHADA", courierTracking },
});
}
/** Recalcula totales de peso y valor */
private async recalcTotals(id: string, _tenantId: string): Promise<any> {
const cp = await this.prisma.client.consolidationPackage.findMany({