style: clean up the invoice and setup event for warehouse.

This commit is contained in:
Nathnael
2026-06-30 13:28:14 +00:00
parent fa3138f2ac
commit 0056dec924
5 changed files with 30 additions and 68 deletions

View File

@@ -1,8 +1,9 @@
import { BadRequestException, ConflictException, Injectable, Logger, NotFoundException } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { Freight } from '@edr/types';
import { DataSource } from 'typeorm';
import { BillingService, InvoiceLineInput } from '../billing/billing.service';
import { BillingService, InvoiceEventPayload, InvoiceLineInput } from '../billing/billing.service';
import { Invoice } from '../billing/entities/invoice.entity';
import { InvoiceLine } from '../billing/entities/invoice-line.entity';
import {
@@ -35,8 +36,6 @@ export interface PayInvoiceDto {
/** Warehouse fee invoices live in the global billing system under this source. */
const SOURCE = Freight.InvoiceSource.Warehouse;
/** Document number prefix kept for warehouse fee invoices (e.g. `WHF-20260630-00001`). */
const NUMBER_CODE = 'WHF';
/** Global statuses that still owe money and therefore block terminal release. */
const BLOCKING_STATUSES: Freight.InvoiceStatus[] = [
@@ -216,7 +215,6 @@ export class WarehouseInvoiceService {
currency: billingCurrency,
lines,
status: Freight.InvoiceStatus.Issued,
numberCode: NUMBER_CODE,
});
const detail = await this.findById(invoice.id);
@@ -307,6 +305,21 @@ export class WarehouseInvoiceService {
return detail;
}
/**
* Notify on online (gateway) settlement — the domain side-effect of a warehouse
* fee being paid through billing's payment flow. The counter {@link pay} path
* notifies inline (and carries driver details from the request), so this only
* handles gateway payments: those stamp the invoice `paymentId`, whereas a
* counter settlement leaves it null. Skipping null-`paymentId` events avoids
* double-notifying a counter payment that already sent its SMS.
*/
@OnEvent('warehouse.invoice.paid')
async onWarehouseInvoicePaid(payload: InvoiceEventPayload): Promise<void> {
if (!payload.paymentId) return;
const detail = await this.findById(payload.invoiceId);
await this.notifyWarehouseFeePayment(detail, { amount: Number(detail.totalAmount) });
}
// ── Release blocking ──────────────────────────────────────────────────────
/** Returns the first unpaid invoice that blocks terminal release, or null. */
async findBlockingInvoice(inventoryId: string): Promise<WarehouseFeeInvoiceView | null> {