FactuGES_ERP/modules/suppliers/src/api/application/audit/supplier-audit.ts

84 lines
2.9 KiB
TypeScript

import type { AuditMutationTarget, AuditState } from "@repo/rdx-audit";
import type { Supplier } from "../../domain";
export const SUPPLIER_AUDIT_ACTIONS = {
CREATED: "supplier.created",
UPDATED: "supplier.updated",
ACTIVATED: "supplier.activated",
DEACTIVATED: "supplier.deactivated",
} as const;
type SupplierAuditState = Readonly<{
status: "active" | "inactive";
reference: string | null;
isCompany: boolean;
name: string;
tradeName: string | null;
tin: string | null;
address: Readonly<{
street: string | null;
street2: string | null;
city: string | null;
province: string | null;
postalCode: string | null;
country: string | null;
}>;
emailPrimary: string | null;
emailSecondary: string | null;
phonePrimary: string | null;
phoneSecondary: string | null;
mobilePrimary: string | null;
mobileSecondary: string | null;
fax: string | null;
website: string | null;
languageCode: string;
currencyCode: string;
}>;
const toNullablePrimitive = <T extends { toPrimitive(): string }>(value: {
match: <R>(some: (item: T) => R, none: () => R) => R;
}): string | null =>
value.match(
(item) => item.toPrimitive(),
() => null
);
/** Proyección histórica explícita del Supplier, sin infraestructura ni referencias de compras. */
export const toSupplierAuditState = (supplier: Supplier): SupplierAuditState => {
const status: SupplierAuditState["status"] = supplier.isActive ? "active" : "inactive";
return {
status,
reference: toNullablePrimitive(supplier.reference),
isCompany: supplier.isCompany,
name: supplier.name.toPrimitive(),
tradeName: toNullablePrimitive(supplier.tradeName),
tin: toNullablePrimitive(supplier.tin),
address: {
street: toNullablePrimitive(supplier.address.getProps().street),
street2: toNullablePrimitive(supplier.address.getProps().street2),
city: toNullablePrimitive(supplier.address.getProps().city),
province: toNullablePrimitive(supplier.address.getProps().province),
postalCode: toNullablePrimitive(supplier.address.getProps().postalCode),
country: toNullablePrimitive(supplier.address.getProps().country),
},
emailPrimary: toNullablePrimitive(supplier.emailPrimary),
emailSecondary: toNullablePrimitive(supplier.emailSecondary),
phonePrimary: toNullablePrimitive(supplier.phonePrimary),
phoneSecondary: toNullablePrimitive(supplier.phoneSecondary),
mobilePrimary: toNullablePrimitive(supplier.mobilePrimary),
mobileSecondary: toNullablePrimitive(supplier.mobileSecondary),
fax: toNullablePrimitive(supplier.fax),
website: toNullablePrimitive(supplier.website),
languageCode: supplier.languageCode.toPrimitive(),
currencyCode: supplier.currencyCode.toPrimitive(),
} satisfies AuditState;
};
export const toSupplierAuditTarget = (supplier: Supplier): AuditMutationTarget => ({
type: "supplier",
id: supplier.id.toPrimitive(),
label: supplier.name.toPrimitive(),
});