feat: gérer l'ordre des champs personnalisés

This commit is contained in:
Matthieu
2025-10-28 18:08:08 +01:00
parent 635ea0e84e
commit 9f522a6dbb
15 changed files with 134 additions and 27 deletions

View File

@@ -8,7 +8,9 @@ import type { PieceModelStructure } from '../shared/types/inventory';
const PIECE_WITH_RELATIONS_INCLUDE = {
typePiece: {
include: {
pieceCustomFields: true,
pieceCustomFields: {
orderBy: { orderIndex: 'asc' },
},
},
},
constructeurs: true,
@@ -286,23 +288,35 @@ export class PiecesService {
const existing = await this.prisma.customField.findMany({
where: { typePieceId },
select: { id: true, name: true },
select: { id: true, name: true, orderIndex: true },
});
const existingByName = new Map(
existing.map((field) => [
this.normalizeIdentifier(field.name) ?? field.name,
field.id,
field,
]),
);
for (const field of customFields) {
for (let index = 0; index < customFields.length; index += 1) {
const field = customFields[index];
if (!field) {
continue;
}
const name = this.normalizeIdentifier(field.name);
if (!name || existingByName.has(name)) {
if (!name) {
continue;
}
const existingField = existingByName.get(name);
if (existingField) {
if (existingField.orderIndex !== index) {
await this.prisma.customField.update({
where: { id: existingField.id },
data: { orderIndex: index },
});
}
continue;
}
@@ -316,12 +330,13 @@ export class PiecesService {
type,
required,
options,
orderIndex: index,
typePieceId,
},
select: { id: true },
select: { id: true, name: true, orderIndex: true },
});
existingByName.set(name, created.id);
existingByName.set(name, created);
}
}