fix: corrige les associations constructeurs

This commit is contained in:
Matthieu
2025-10-28 16:37:06 +01:00
parent 4db64351b7
commit 635ea0e84e
17 changed files with 578 additions and 200 deletions

View File

@@ -120,7 +120,7 @@ async function createPiece(options: {
name: string;
reference: string;
price: number;
constructeurId?: string | null;
constructeurIds?: string[] | null;
typeId: string;
fieldValues: Record<string, string>;
}) {
@@ -143,17 +143,35 @@ async function createPiece(options: {
},
);
return prisma.piece.create({
data: {
name: options.name,
reference: options.reference,
prix: new Prisma.Decimal(options.price),
typePieceId: options.typeId,
constructeurId: options.constructeurId ?? null,
customFieldValues: {
create: customFieldValues,
},
const constructeurIds = Array.isArray(options.constructeurIds)
? Array.from(
new Set(
options.constructeurIds
.filter((value): value is string => typeof value === 'string')
.map((value) => value.trim())
.filter((value) => value.length > 0),
),
)
: [];
const data: any = {
name: options.name,
reference: options.reference,
prix: new Prisma.Decimal(options.price),
typePieceId: options.typeId,
customFieldValues: {
create: customFieldValues,
},
};
if (constructeurIds.length) {
data.constructeurs = {
connect: constructeurIds.map((id) => ({ id })),
};
}
return prisma.piece.create({
data,
});
}
@@ -161,7 +179,7 @@ async function createComponent(options: {
name: string;
reference: string;
price: number;
constructeurId?: string | null;
constructeurIds?: string[] | null;
typeId: string;
fieldValues: Record<string, string>;
structure?: Prisma.InputJsonValue;
@@ -185,21 +203,39 @@ async function createComponent(options: {
},
);
return prisma.composant.create({
data: {
name: options.name,
reference: options.reference,
prix: new Prisma.Decimal(options.price),
typeComposantId: options.typeId,
constructeurId: options.constructeurId ?? null,
structure:
options.structure === undefined
? Prisma.JsonNull
: options.structure ?? Prisma.JsonNull,
customFieldValues: {
create: customFieldValues,
},
const constructeurIds = Array.isArray(options.constructeurIds)
? Array.from(
new Set(
options.constructeurIds
.filter((value): value is string => typeof value === 'string')
.map((value) => value.trim())
.filter((value) => value.length > 0),
),
)
: [];
const data: any = {
name: options.name,
reference: options.reference,
prix: new Prisma.Decimal(options.price),
typeComposantId: options.typeId,
structure:
options.structure === undefined
? Prisma.JsonNull
: options.structure ?? Prisma.JsonNull,
customFieldValues: {
create: customFieldValues,
},
};
if (constructeurIds.length) {
data.constructeurs = {
connect: constructeurIds.map((id) => ({ id })),
};
}
return prisma.composant.create({
data,
});
}