feat: gérer l'ordre des champs personnalisés
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user