fix: corrige les associations constructeurs
This commit is contained in:
@@ -11,7 +11,7 @@ const PIECE_WITH_RELATIONS_INCLUDE = {
|
||||
pieceCustomFields: true,
|
||||
},
|
||||
},
|
||||
constructeur: true,
|
||||
constructeurs: true,
|
||||
documents: true,
|
||||
customFieldValues: {
|
||||
include: {
|
||||
@@ -31,16 +31,23 @@ const PIECE_WITH_RELATIONS_INCLUDE = {
|
||||
export class PiecesService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private buildCreateInput(createPieceDto: CreatePieceDto): Prisma.PieceCreateInput {
|
||||
private async buildCreateInput(
|
||||
createPieceDto: CreatePieceDto,
|
||||
): Promise<Prisma.PieceCreateInput> {
|
||||
const data: Prisma.PieceCreateInput = {
|
||||
name: createPieceDto.name,
|
||||
reference: createPieceDto.reference ?? null,
|
||||
prix: createPieceDto.prix !== undefined ? createPieceDto.prix : null,
|
||||
};
|
||||
|
||||
if (createPieceDto.constructeurId) {
|
||||
data.constructeur = {
|
||||
connect: { id: createPieceDto.constructeurId },
|
||||
const constructeurIds = this.normalizeConstructeurIds(
|
||||
createPieceDto.constructeurIds,
|
||||
);
|
||||
const resolvedConstructeurIds =
|
||||
await this.resolveExistingConstructeurIds(constructeurIds);
|
||||
if (resolvedConstructeurIds.length) {
|
||||
data.constructeurs = {
|
||||
connect: resolvedConstructeurIds.map((id) => ({ id })),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -56,7 +63,7 @@ export class PiecesService {
|
||||
async create(createPieceDto: CreatePieceDto) {
|
||||
try {
|
||||
const created = await this.prisma.piece.create({
|
||||
data: this.buildCreateInput(createPieceDto),
|
||||
data: await this.buildCreateInput(createPieceDto),
|
||||
include: PIECE_WITH_RELATIONS_INCLUDE,
|
||||
});
|
||||
|
||||
@@ -103,10 +110,15 @@ export class PiecesService {
|
||||
data.prix = updatePieceDto.prix;
|
||||
}
|
||||
|
||||
if (updatePieceDto.constructeurId !== undefined) {
|
||||
data.constructeur = updatePieceDto.constructeurId
|
||||
? { connect: { id: updatePieceDto.constructeurId } }
|
||||
: { disconnect: true };
|
||||
if (updatePieceDto.constructeurIds !== undefined) {
|
||||
const constructeurIds = this.normalizeConstructeurIds(
|
||||
updatePieceDto.constructeurIds,
|
||||
);
|
||||
const resolvedConstructeurIds =
|
||||
await this.resolveExistingConstructeurIds(constructeurIds);
|
||||
data.constructeurs = {
|
||||
set: resolvedConstructeurIds.map((id) => ({ id })),
|
||||
};
|
||||
}
|
||||
|
||||
if (updatePieceDto.typePieceId !== undefined) {
|
||||
@@ -224,6 +236,30 @@ export class PiecesService {
|
||||
);
|
||||
}
|
||||
|
||||
private normalizeConstructeurIds(ids?: string[] | null): string[] {
|
||||
if (!Array.isArray(ids)) {
|
||||
return [];
|
||||
}
|
||||
const cleaned = ids
|
||||
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
||||
.filter((item) => item.length > 0);
|
||||
return Array.from(new Set(cleaned));
|
||||
}
|
||||
|
||||
private async resolveExistingConstructeurIds(
|
||||
ids: string[],
|
||||
): Promise<string[]> {
|
||||
if (!ids.length) {
|
||||
return [];
|
||||
}
|
||||
const existing = await this.prisma.constructeur.findMany({
|
||||
where: { id: { in: ids } },
|
||||
select: { id: true },
|
||||
});
|
||||
const existingIds = new Set(existing.map(({ id }) => id));
|
||||
return ids.filter((id) => existingIds.has(id));
|
||||
}
|
||||
|
||||
private parsePieceSkeleton(value: unknown): PieceModelStructure | null {
|
||||
if (!value) {
|
||||
return null;
|
||||
@@ -430,4 +466,6 @@ type PieceTypeWithSkeleton = Prisma.ModelTypeGetPayload<{
|
||||
include: { pieceCustomFields: true };
|
||||
}>;
|
||||
|
||||
type PieceCustomFieldEntry = NonNullable<PieceModelStructure['customFields']>[number];
|
||||
type PieceCustomFieldEntry = NonNullable<
|
||||
PieceModelStructure['customFields']
|
||||
>[number];
|
||||
|
||||
Reference in New Issue
Block a user