Migrate away from legacy component and piece models
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { PrismaClient, Prisma, ModelCategory } from '@prisma/client';
|
||||
import { normalizeComponentModelStructure } from '../src/component-models/structure.normalizer';
|
||||
import {
|
||||
ComponentModelStructureSchema,
|
||||
PieceModelStructureSchema,
|
||||
} from '../src/shared/schemas/inventory';
|
||||
import type { ComponentModelStructure } from '../src/shared/types/inventory';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
@@ -4066,8 +4070,6 @@ async function clearDatabaseExceptSitesAndProfiles() {
|
||||
prisma.typeMachineComponentRequirement.deleteMany(),
|
||||
prisma.typeMachinePieceRequirement.deleteMany(),
|
||||
prisma.customField.deleteMany(),
|
||||
prisma.pieceModel.deleteMany(),
|
||||
prisma.composantModel.deleteMany(),
|
||||
prisma.typeMachine.deleteMany(),
|
||||
prisma.modelType.deleteMany(),
|
||||
prisma.constructeur.deleteMany(),
|
||||
@@ -4190,44 +4192,51 @@ async function createModelTypes() {
|
||||
]);
|
||||
}
|
||||
|
||||
const componentTypesMap = Object.fromEntries(componentTypeEntries) as Record<
|
||||
string,
|
||||
{ id: string; customFields: Record<string, string> }
|
||||
>;
|
||||
const pieceTypesMap = Object.fromEntries(pieceTypeEntries) as Record<
|
||||
string,
|
||||
{ id: string; customFields: Record<string, string> }
|
||||
>;
|
||||
|
||||
await applyPieceTypeSkeletons(pieceTypesMap);
|
||||
await applyComponentTypeSkeletons(componentTypesMap, pieceTypesMap);
|
||||
|
||||
return {
|
||||
componentTypes: Object.fromEntries(componentTypeEntries) as Record<
|
||||
string,
|
||||
{ id: string; customFields: Record<string, string> }
|
||||
>,
|
||||
pieceTypes: Object.fromEntries(pieceTypeEntries) as Record<
|
||||
string,
|
||||
{ id: string; customFields: Record<string, string> }
|
||||
>,
|
||||
componentTypes: componentTypesMap,
|
||||
pieceTypes: pieceTypesMap,
|
||||
};
|
||||
}
|
||||
|
||||
async function createPieceModels(
|
||||
async function applyPieceTypeSkeletons(
|
||||
pieceTypes: Record<string, { id: string }>,
|
||||
) {
|
||||
console.log('🧩 Création des modèles de pièces...');
|
||||
console.log('🧩 Application des squelettes de pièces...');
|
||||
|
||||
const entries = await Promise.all(
|
||||
pieceModelDefinitions.map(async (definition) => {
|
||||
const type = pieceTypes[definition.typeCode];
|
||||
if (!type) {
|
||||
throw new Error(`Type de pièce introuvable: ${definition.typeCode}`);
|
||||
}
|
||||
const applied = new Set<string>();
|
||||
|
||||
const record = await prisma.pieceModel.create({
|
||||
data: {
|
||||
name: definition.name,
|
||||
description: definition.description,
|
||||
typePiece: { connect: { id: type.id } },
|
||||
structure: definition.structure,
|
||||
},
|
||||
for (const definition of pieceModelDefinitions) {
|
||||
const type = pieceTypes[definition.typeCode];
|
||||
if (!type || !definition.structure || applied.has(type.id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const skeleton = PieceModelStructureSchema.parse(definition.structure);
|
||||
await prisma.modelType.update({
|
||||
where: { id: type.id },
|
||||
data: { pieceSkeleton: skeleton as Prisma.InputJsonValue },
|
||||
});
|
||||
|
||||
return [definition.code, record] as const;
|
||||
}),
|
||||
);
|
||||
|
||||
return Object.fromEntries(entries) as Record<string, { id: string }>;
|
||||
applied.add(type.id);
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`⚠️ Impossible d'appliquer le squelette de pièce ${definition.code}:`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function buildComponentModelStructure(
|
||||
@@ -4409,37 +4418,44 @@ function buildComponentModelStructure(
|
||||
return normalizeComponentModelStructure(canonical) as Prisma.InputJsonValue;
|
||||
}
|
||||
|
||||
async function createComponentModels(
|
||||
async function applyComponentTypeSkeletons(
|
||||
componentTypes: Record<string, { id: string }>,
|
||||
pieceTypes: Record<string, { id: string }>,
|
||||
) {
|
||||
console.log('🛠️ Création des modèles de composants...');
|
||||
console.log('🛠️ Application des squelettes de composants...');
|
||||
|
||||
const entries = await Promise.all(
|
||||
componentModelDefinitions.map(async (definition) => {
|
||||
const type = componentTypes[definition.typeCode];
|
||||
if (!type) {
|
||||
throw new Error(`Type de composant introuvable: ${definition.typeCode}`);
|
||||
}
|
||||
const applied = new Set<string>();
|
||||
|
||||
const record = await prisma.composantModel.create({
|
||||
data: {
|
||||
name: definition.name,
|
||||
description: definition.description,
|
||||
typeComposant: { connect: { id: type.id } },
|
||||
structure: buildComponentModelStructure(
|
||||
definition.structure,
|
||||
componentTypes,
|
||||
pieceTypes,
|
||||
),
|
||||
},
|
||||
for (const definition of componentModelDefinitions) {
|
||||
const type = componentTypes[definition.typeCode];
|
||||
if (!type || applied.has(type.id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const structure = buildComponentModelStructure(
|
||||
definition.structure,
|
||||
componentTypes,
|
||||
pieceTypes,
|
||||
);
|
||||
|
||||
if (!structure) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const skeleton = ComponentModelStructureSchema.parse(structure);
|
||||
await prisma.modelType.update({
|
||||
where: { id: type.id },
|
||||
data: { componentSkeleton: skeleton as Prisma.InputJsonValue },
|
||||
});
|
||||
|
||||
return [definition.code, record] as const;
|
||||
}),
|
||||
);
|
||||
|
||||
return Object.fromEntries(entries) as Record<string, { id: string }>;
|
||||
applied.add(type.id);
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`⚠️ Impossible d'appliquer le squelette de composant ${definition.code}:`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function createTypeMachines(
|
||||
@@ -4544,9 +4560,7 @@ async function createComponentHierarchy(
|
||||
component: ComponentInstance,
|
||||
context: {
|
||||
componentTypes: Record<string, { id: string; customFields: Record<string, string> }>;
|
||||
componentModels: Record<string, { id: string }>;
|
||||
pieceTypes: Record<string, { id: string; customFields: Record<string, string> }>;
|
||||
pieceModels: Record<string, { id: string }>;
|
||||
constructeurs: Record<string, { id: string }>;
|
||||
requirementMap: Map<string, string>;
|
||||
},
|
||||
@@ -4564,9 +4578,6 @@ async function createComponentHierarchy(
|
||||
machine: { connect: { id: machineId } },
|
||||
parentComposant: parentId ? { connect: { id: parentId } } : undefined,
|
||||
typeComposant: { connect: { id: context.componentTypes[component.typeCode].id } },
|
||||
composantModel: {
|
||||
connect: { id: context.componentModels[component.modelCode].id },
|
||||
},
|
||||
constructeur: component.constructeur
|
||||
? { connect: { id: context.constructeurs[component.constructeur].id } }
|
||||
: undefined,
|
||||
@@ -4589,9 +4600,6 @@ async function createComponentHierarchy(
|
||||
reference: piece.reference,
|
||||
prix: piece.prix ? new Prisma.Decimal(piece.prix) : undefined,
|
||||
typePiece: { connect: { id: type.id } },
|
||||
pieceModel: {
|
||||
connect: { id: context.pieceModels[piece.modelCode].id },
|
||||
},
|
||||
constructeur: piece.constructeur
|
||||
? { connect: { id: context.constructeurs[piece.constructeur].id } }
|
||||
: undefined,
|
||||
@@ -4620,9 +4628,7 @@ async function createMachines(
|
||||
typeMachines: Record<string, TypeMachineRecord>,
|
||||
context: {
|
||||
componentTypes: Record<string, { id: string; customFields: Record<string, string> }>;
|
||||
componentModels: Record<string, { id: string }>;
|
||||
pieceTypes: Record<string, { id: string; customFields: Record<string, string> }>;
|
||||
pieceModels: Record<string, { id: string }>;
|
||||
constructeurs: Record<string, { id: string }>;
|
||||
},
|
||||
) {
|
||||
@@ -4696,7 +4702,6 @@ async function createMachines(
|
||||
prix: spare.prix ? new Prisma.Decimal(spare.prix) : undefined,
|
||||
machine: { connect: { id: machine.id } },
|
||||
typePiece: { connect: { id: pieceType.id } },
|
||||
pieceModel: { connect: { id: context.pieceModels[spare.modelCode].id } },
|
||||
constructeur: spare.constructeur
|
||||
? { connect: { id: context.constructeurs[spare.constructeur].id } }
|
||||
: undefined,
|
||||
@@ -4724,17 +4729,11 @@ async function main() {
|
||||
]);
|
||||
|
||||
const { componentTypes, pieceTypes } = await createModelTypes();
|
||||
const [pieceModels, componentModels, typeMachines] = await Promise.all([
|
||||
createPieceModels(pieceTypes),
|
||||
createComponentModels(componentTypes, pieceTypes),
|
||||
createTypeMachines(componentTypes, pieceTypes),
|
||||
]);
|
||||
const typeMachines = await createTypeMachines(componentTypes, pieceTypes);
|
||||
|
||||
await createMachines(site.id, typeMachines, {
|
||||
componentTypes,
|
||||
componentModels,
|
||||
pieceTypes,
|
||||
pieceModels,
|
||||
constructeurs,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user