From 695d56a6d3b57ca587b367d629cc612ac4276766 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Thu, 12 Mar 2026 12:08:43 +0100 Subject: [PATCH] feat(piece) : add quantity field to piece types, sanitization and hydration Co-Authored-By: Claude Opus 4.6 --- app/shared/model/componentStructure.ts | 3 +++ app/shared/model/componentStructureHydrate.ts | 2 ++ app/shared/model/componentStructureSanitize.ts | 5 +++++ app/shared/types/inventory.ts | 3 +++ app/shared/utils/structureAssignmentHelpers.ts | 1 + 5 files changed, 14 insertions(+) diff --git a/app/shared/model/componentStructure.ts b/app/shared/model/componentStructure.ts index be936bc..29ed772 100644 --- a/app/shared/model/componentStructure.ts +++ b/app/shared/model/componentStructure.ts @@ -175,6 +175,9 @@ export const normalizeStructureForSave = (input: any): any => { if (piece.reference) { payload.reference = piece.reference } + if ((piece as any).quantity !== undefined && (piece as any).quantity >= 1) { + payload.quantity = (piece as any).quantity + } return payload }) as any diff --git a/app/shared/model/componentStructureHydrate.ts b/app/shared/model/componentStructureHydrate.ts index df7ebad..6393ea6 100644 --- a/app/shared/model/componentStructureHydrate.ts +++ b/app/shared/model/componentStructureHydrate.ts @@ -103,6 +103,7 @@ export const hydratePieces = (pieces: any[]): ComponentModelPiece[] => { reference: piece?.reference ?? '', familyCode: piece?.familyCode ?? piece?.typePiece?.code ?? '', role: piece?.role ?? '', + ...(piece?.quantity !== undefined && piece.quantity >= 1 ? { quantity: piece.quantity } : {}), })) } @@ -175,6 +176,7 @@ export const mapComponentPieces = (pieces: any[]): ComponentModelPiece[] => { typePieceLabel: piece?.typePieceLabel ?? piece?.typePiece?.name ?? '', familyCode: piece?.familyCode ?? piece?.typePiece?.code ?? '', role: piece?.role ?? '', + ...(piece?.quantity !== undefined && piece.quantity >= 1 ? { quantity: piece.quantity } : {}), })) } diff --git a/app/shared/model/componentStructureSanitize.ts b/app/shared/model/componentStructureSanitize.ts index ad656b1..440bdb7 100644 --- a/app/shared/model/componentStructureSanitize.ts +++ b/app/shared/model/componentStructureSanitize.ts @@ -162,6 +162,8 @@ export const sanitizePieces = (pieces: any[]): ComponentModelPiece[] => { const rawRole = typeof piece?.role === 'string' ? piece.role.trim() : '' const role = rawRole.length > 0 ? rawRole : undefined + const quantity = typeof piece?.quantity === 'number' && piece.quantity >= 1 ? piece.quantity : undefined + if (!typePieceId && !typePieceLabel && !reference && !familyCode) { return null } @@ -182,6 +184,9 @@ export const sanitizePieces = (pieces: any[]): ComponentModelPiece[] => { if (typePieceLabel) { result.typePieceLabel = typePieceLabel } + if (quantity !== undefined) { + result.quantity = quantity + } return result }) .filter((piece): piece is ComponentModelPiece => !!piece) diff --git a/app/shared/types/inventory.ts b/app/shared/types/inventory.ts index a99b335..e12549a 100644 --- a/app/shared/types/inventory.ts +++ b/app/shared/types/inventory.ts @@ -21,6 +21,7 @@ export interface ComponentModelPiece { reference?: string familyCode?: string role?: string + quantity?: number } export interface ComponentModelProduct { @@ -156,6 +157,7 @@ const validatePiece = ( const reference = ensureString(value.reference) const familyCode = ensureString(value.familyCode) const role = ensureString(value.role) + const quantity = typeof value.quantity === 'number' && value.quantity >= 1 ? value.quantity : undefined if (!typePieceId && !typePieceLabel && !reference && !familyCode) { issues.push(`${path}: au moins un identifiant, une famille ou une référence de pièce est requis`) @@ -168,6 +170,7 @@ const validatePiece = ( ...(reference ? { reference } : {}), ...(familyCode ? { familyCode } : {}), ...(role ? { role } : {}), + ...(quantity ? { quantity } : {}), } } diff --git a/app/shared/utils/structureAssignmentHelpers.ts b/app/shared/utils/structureAssignmentHelpers.ts index c285044..cd141d4 100644 --- a/app/shared/utils/structureAssignmentHelpers.ts +++ b/app/shared/utils/structureAssignmentHelpers.ts @@ -176,6 +176,7 @@ export function sanitizePieceDefinition(definition: ComponentModelPiece) { typePieceLabel: definition.typePieceLabel ?? null, reference: definition.reference ?? null, familyCode: (definition as any).familyCode ?? null, + quantity: typeof (definition as any).quantity === 'number' ? (definition as any).quantity : null, }) }