Checkpoint: Ajout d’un filtre par action dans la liste des automatismes, avec catégories Type d’achat, Service, Ventilation, Abonnement, Abonnement Oui et Abonnement Non. Tests, TypeScript, build et rendu visuel validés en sandbox.
This commit is contained in:
51
shared/automationActions.ts
Normal file
51
shared/automationActions.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/** Les catégories d’actions proposées dans les règles d’automatisme. */
|
||||
export const AUTOMATION_ACTION_FILTERS = [
|
||||
{ value: "all", label: "Toutes les actions" },
|
||||
{ value: "typeAchat", label: "Type d’achat" },
|
||||
{ value: "serviceConcerne", label: "Service concerné" },
|
||||
{ value: "ventilationComptable", label: "Ventilation comptable" },
|
||||
{ value: "subscription", label: "Abonnement (Oui ou Non)" },
|
||||
{ value: "subscriptionYes", label: "Abonnement : Oui" },
|
||||
{ value: "subscriptionNo", label: "Abonnement : Non" },
|
||||
] as const;
|
||||
|
||||
export type AutomationActionFilter = (typeof AUTOMATION_ACTION_FILTERS)[number]["value"];
|
||||
|
||||
type RuleActions = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Lit défensivement le JSON stocké en base : une règle historique invalide ne
|
||||
* doit jamais empêcher l’affichage ni le filtrage de la liste complète.
|
||||
*/
|
||||
function parseRuleActions(actionsJson: string): RuleActions {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(actionsJson);
|
||||
return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
|
||||
? parsed as RuleActions
|
||||
: {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function getSubscriptionValue(actions: RuleActions): 0 | 1 | undefined {
|
||||
const value = actions.isSubscription;
|
||||
if (value === 1 || value === "1" || value === "OUI") return 1;
|
||||
if (value === 0 || value === "0" || value === "NON") return 0;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Retourne si les actions JSON d’une règle correspondent au filtre choisi. */
|
||||
export function matchesAutomationActionFilter(
|
||||
actionsJson: string,
|
||||
filter: AutomationActionFilter,
|
||||
): boolean {
|
||||
if (filter === "all") return true;
|
||||
|
||||
const actions = parseRuleActions(actionsJson);
|
||||
if (filter === "subscription") return Object.hasOwn(actions, "isSubscription");
|
||||
if (filter === "subscriptionYes") return getSubscriptionValue(actions) === 1;
|
||||
if (filter === "subscriptionNo") return getSubscriptionValue(actions) === 0;
|
||||
|
||||
return typeof actions[filter] === "string" && actions[filter].trim().length > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user