163 lines
3.8 KiB
TypeScript
163 lines
3.8 KiB
TypeScript
import { formHasAnyDirty } from "@erp/core/client";
|
|
import type { FieldNamesMarkedBoolean } from "react-hook-form";
|
|
|
|
import type { CustomerUpdateForm, CustomerUpdatePatch } from "../entities";
|
|
|
|
/**
|
|
* Construye un parche de actualización de cliente a partir de los datos
|
|
* del formulario y los campos sucios (dirty fields).
|
|
*
|
|
* Reglas:
|
|
* - solo incluye campos dirty
|
|
* - `undefined` = no enviar
|
|
* - `null` = borrar explícitamente
|
|
* - `[]` = colección vacía explícita
|
|
*
|
|
* @param formData
|
|
* @param dirtyFields
|
|
* @returns
|
|
*/
|
|
|
|
export const buildCustomerUpdatePatch = (
|
|
formData: CustomerUpdateForm,
|
|
dirtyFields: FieldNamesMarkedBoolean<CustomerUpdateForm>
|
|
): CustomerUpdatePatch => {
|
|
if (!formHasAnyDirty(dirtyFields)) {
|
|
return {};
|
|
}
|
|
const patch: CustomerUpdatePatch = {};
|
|
|
|
if (dirtyFields.reference) {
|
|
patch.reference = toNullableText(formData.reference);
|
|
}
|
|
|
|
if (dirtyFields.isCompany) {
|
|
patch.isCompany = formData.isCompany;
|
|
}
|
|
|
|
if (dirtyFields.name) {
|
|
patch.name = toRequiredText(formData.name);
|
|
}
|
|
|
|
if (dirtyFields.tradeName) {
|
|
patch.tradeName = toNullableText(formData.tradeName);
|
|
}
|
|
|
|
if (dirtyFields.tin) {
|
|
patch.tin = toNullableText(formData.tin);
|
|
}
|
|
|
|
if (dirtyFields.street) {
|
|
patch.street = toNullableText(formData.street);
|
|
}
|
|
|
|
if (dirtyFields.street2) {
|
|
patch.street2 = toNullableText(formData.street2);
|
|
}
|
|
|
|
if (dirtyFields.city) {
|
|
patch.city = toNullableText(formData.city);
|
|
}
|
|
|
|
if (dirtyFields.province) {
|
|
patch.province = toNullableText(formData.province);
|
|
}
|
|
|
|
if (dirtyFields.postalCode) {
|
|
patch.postalCode = toNullableText(formData.postalCode);
|
|
}
|
|
|
|
if (dirtyFields.country) {
|
|
patch.country = toNullableText(formData.country);
|
|
}
|
|
|
|
if (dirtyFields.primaryEmail) {
|
|
patch.primaryEmail = toNullableText(formData.primaryEmail);
|
|
}
|
|
|
|
if (dirtyFields.secondaryEmail) {
|
|
patch.secondaryEmail = toNullableText(formData.secondaryEmail);
|
|
}
|
|
|
|
if (dirtyFields.primaryPhone) {
|
|
patch.primaryPhone = toNullableText(formData.primaryPhone);
|
|
}
|
|
|
|
if (dirtyFields.secondaryPhone) {
|
|
patch.secondaryPhone = toNullableText(formData.secondaryPhone);
|
|
}
|
|
|
|
if (dirtyFields.primaryMobile) {
|
|
patch.primaryMobile = toNullableText(formData.primaryMobile);
|
|
}
|
|
|
|
if (dirtyFields.secondaryMobile) {
|
|
patch.secondaryMobile = toNullableText(formData.secondaryMobile);
|
|
}
|
|
|
|
if (dirtyFields.fax) {
|
|
patch.fax = toNullableText(formData.fax);
|
|
}
|
|
|
|
if (dirtyFields.website) {
|
|
patch.website = toNullableText(formData.website);
|
|
}
|
|
|
|
if (dirtyFields.legalRecord) {
|
|
patch.legalRecord = toNullableText(formData.legalRecord);
|
|
}
|
|
|
|
if (dirtyFields.paymentMethodId) {
|
|
patch.paymentMethodId = toNullableText(formData.paymentMethodId);
|
|
}
|
|
|
|
if (dirtyFields.paymentTermId) {
|
|
patch.paymentTermId = toNullableText(formData.paymentTermId);
|
|
}
|
|
|
|
if (dirtyFields.taxRegimeCode) {
|
|
patch.taxRegimeCode = toNullableText(formData.taxRegimeCode);
|
|
}
|
|
|
|
if (dirtyFields.usesEquivalenceSurcharge) {
|
|
patch.usesEquivalenceSurcharge = formData.usesEquivalenceSurcharge;
|
|
}
|
|
|
|
if (dirtyFields.usesRetention) {
|
|
patch.usesRetention = formData.usesRetention;
|
|
}
|
|
|
|
if (dirtyFields.languageCode) {
|
|
patch.languageCode = toRequiredText(formData.languageCode);
|
|
}
|
|
|
|
if (dirtyFields.currencyCode) {
|
|
patch.currencyCode = toRequiredText(formData.currencyCode);
|
|
}
|
|
|
|
console.log(dirtyFields, patch);
|
|
|
|
return patch;
|
|
};
|
|
|
|
/**
|
|
* Convierte string del formulario a texto requerido.
|
|
* No devuelve null.
|
|
*/
|
|
const toRequiredText = (value: string): string => {
|
|
return value.trim();
|
|
};
|
|
|
|
/**
|
|
* Convierte string del formulario a texto anulable.
|
|
*
|
|
* Reglas:
|
|
* - vacío o solo espacios => null
|
|
* - resto => trimmed string
|
|
*/
|
|
const toNullableText = (value: string): string | null => {
|
|
const trimmedValue = value.trim();
|
|
|
|
return trimmedValue === "" ? null : trimmedValue;
|
|
};
|