67 lines
2.6 KiB
TypeScript
67 lines
2.6 KiB
TypeScript
/**
|
|
* Helpers purs de l'onglet Contact prestataire (M3 Technique, ERP-142) — miroir
|
|
* reduit de `supplierFormRules.ts` / `supplierEdit.ts` (M2). Testables sans Vue
|
|
* ni API : detection de bloc vide (RG-3.04) et construction du payload de
|
|
* sous-ressource contacts.
|
|
*/
|
|
|
|
import type { ProviderContactFormDraft } from '~/modules/technique/types/providerForm'
|
|
|
|
/** Vrai si une chaine porte au moins un caractere non-espace. */
|
|
function isFilled(value: string | null | undefined): boolean {
|
|
return value !== null && value !== undefined && value.trim() !== ''
|
|
}
|
|
|
|
/**
|
|
* RG-3.04 : un bloc Contact est VIDE tant qu'aucun des champs comptant pour la
|
|
* validite n'est rempli — prenom / nom / fonction / telephone principal / email.
|
|
*
|
|
* `phoneSecondary` est volontairement EXCLU : le back (CHECK
|
|
* `chk_provider_contact_name` + `ProviderContactProcessor`) ne le compte pas non
|
|
* plus, un bloc ne portant qu'un 2e numero reste invalide. Garder la meme
|
|
* definition cote front evite tout drift (un bloc « vide » front == bloc rejete
|
|
* back).
|
|
*/
|
|
export function isProviderContactBlank(contact: ProviderContactFormDraft): boolean {
|
|
return ![
|
|
contact.firstName,
|
|
contact.lastName,
|
|
contact.jobTitle,
|
|
contact.phonePrimary,
|
|
contact.email,
|
|
].some(isFilled)
|
|
}
|
|
|
|
/**
|
|
* RG-3.04 : un contact est « nomme » (valide) des qu'il porte un prenom OU un nom
|
|
* — aligne sur le M1/M2. Sert le gating « + Nouveau contact » et la notion de
|
|
* contact valide (la fonction / le telephone / l'email seuls ne suffisent pas).
|
|
*/
|
|
export function isProviderContactNamed(contact: ProviderContactFormDraft): boolean {
|
|
return isFilled(contact.firstName) || isFilled(contact.lastName)
|
|
}
|
|
|
|
/**
|
|
* RG-3.12 : l'onglet Contact ne peut etre finalise que s'il reste au moins un
|
|
* contact nomme (prenom ou nom).
|
|
*/
|
|
export function hasAtLeastOneFilledContact(contacts: ProviderContactFormDraft[]): boolean {
|
|
return contacts.some(isProviderContactNamed)
|
|
}
|
|
|
|
/**
|
|
* Payload de la sous-ressource contacts (groupe `provider:write:contacts`). Les
|
|
* chaines vides sont envoyees a null (le serveur normalise/trim de toute facon).
|
|
* `phoneSecondary` n'est envoye que si le 2e numero a ete revele (max 2 tel).
|
|
*/
|
|
export function buildProviderContactPayload(contact: ProviderContactFormDraft): Record<string, unknown> {
|
|
return {
|
|
firstName: contact.firstName || null,
|
|
lastName: contact.lastName || null,
|
|
jobTitle: contact.jobTitle || null,
|
|
phonePrimary: contact.phonePrimary || null,
|
|
phoneSecondary: contact.hasSecondaryPhone ? (contact.phoneSecondary || null) : null,
|
|
email: contact.email || null,
|
|
}
|
|
}
|