shoc-frontend-new/src/domain/vendors/utils/build-company-notes-patch.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

import type { VendorCompanyRoster } from "@/domain/vendors/types/vendor";
import type { VendorRosterAdditivePatch } from "@/domain/vendors/mappers/vendor-roster-mapper";
export type VendorCompanyNotesBaseline = Pick<
VendorCompanyRoster,
"companyId" | "rowVersion" | "notes"
>;
export interface VendorCompanyNotesUpdate {
companyId: string | number;
patch: VendorRosterAdditivePatch;
}
/**
* Company-level notes edited outside the Vendors page (e.g. the Create WO wizard)
* are written through the additive roster PATCH, which only touches the fields sent.
* Returns null when nothing changed. Clearing notes is not expressible on that PATCH
* (blank company fields mean "unchanged"), so an emptied field is not sent.
*/
export function buildCompanyNotesUpdate(
baseline: VendorCompanyNotesBaseline | null | undefined,
editedNotes: string,
): VendorCompanyNotesUpdate | null {
if (!baseline || baseline.companyId === null || !baseline.rowVersion) {
return null;
}
const notes = editedNotes.trim();
if (!notes || notes === baseline.notes.trim()) {
return null;
}
return {
companyId: baseline.companyId,
patch: { rowVersion: baseline.rowVersion, addTechnicians: [], companyFields: { notes } },
};
}