fix(vendors): support sparse updates on current target

This commit is contained in:
Alexandre Brandizzi 2026-07-28 17:20:07 -03:00
parent e6a3899f4c
commit 4fe9319d7a
2 changed files with 20 additions and 8 deletions

View file

@ -191,16 +191,25 @@ export function mapVendorUpdateToBackend(vendor: unknown): Record<string, unknow
] as const;
fieldMappings.forEach(([target, source]) => {
if (Object.hasOwn(item, target)) payload[target] = item[target];
if (Object.hasOwn(item, source)) payload[target] = item[source];
if (Object.prototype.hasOwnProperty.call(item, target)) payload[target] = item[target];
if (Object.prototype.hasOwnProperty.call(item, source)) payload[target] = item[source];
});
if (Object.hasOwn(item, "companyId") || Object.hasOwn(item, "CompanyId")) {
if (
Object.prototype.hasOwnProperty.call(item, "companyId") ||
Object.prototype.hasOwnProperty.call(item, "CompanyId")
) {
payload.companyId = readNumericCompanyId(item);
}
if (Object.hasOwn(item, "phone") || Object.hasOwn(item, "Phone")) {
if (
Object.prototype.hasOwnProperty.call(item, "phone") ||
Object.prototype.hasOwnProperty.call(item, "Phone")
) {
payload.phone = toCanonicalPhone(item.phone ?? item.Phone);
}
if (Object.hasOwn(item, "companyPhone") || Object.hasOwn(item, "CompanyPhone")) {
if (
Object.prototype.hasOwnProperty.call(item, "companyPhone") ||
Object.prototype.hasOwnProperty.call(item, "CompanyPhone")
) {
payload.companyPhone = toCanonicalPhone(item.companyPhone ?? item.CompanyPhone);
}
return payload;

View file

@ -64,14 +64,17 @@ const VENDOR_UPDATE_FIELDS = [
] as const satisfies readonly (keyof VendorFormValues)[];
export function getChangedVendorValues(
values: VendorFormValues,
values: Partial<VendorFormValues>,
original: VendorFormValues | null,
): VendorUpdateValues {
if (!original) return {};
return VENDOR_UPDATE_FIELDS.reduce<VendorUpdateValues>((changed, field) => {
if (!Object.is(values[field], original[field])) {
changed[field] = values[field];
if (
Object.prototype.hasOwnProperty.call(values, field) &&
!Object.is(values[field], original[field])
) {
Object.assign(changed, { [field]: values[field] });
}
return changed;
}, {});