675 lines
28 KiB
JavaScript
675 lines
28 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.MachinesService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const prisma_service_1 = require("../prisma/prisma.service");
|
|
let MachinesService = class MachinesService {
|
|
prisma;
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
async create(createMachineDto) {
|
|
const typeMachine = await this.prisma.typeMachine.findUnique({
|
|
where: { id: createMachineDto.typeMachineId },
|
|
include: {
|
|
customFields: true,
|
|
},
|
|
});
|
|
if (!typeMachine) {
|
|
throw new Error('Type de machine non trouvé');
|
|
}
|
|
return await this.prisma.$transaction(async (prisma) => {
|
|
const machine = await prisma.machine.create({
|
|
data: createMachineDto,
|
|
include: {
|
|
site: true,
|
|
typeMachine: true,
|
|
},
|
|
});
|
|
const components = typeMachine.components;
|
|
if (components) {
|
|
await this.createComponentsFromType(prisma, machine.id, components);
|
|
}
|
|
const machinePieces = typeMachine.machinePieces;
|
|
if (machinePieces) {
|
|
await this.createMachinePiecesFromType(prisma, machine.id, machinePieces);
|
|
}
|
|
if (typeMachine.customFields && typeMachine.customFields.length > 0) {
|
|
await this.createMachineCustomFieldsFromType(prisma, machine.id, typeMachine.customFields);
|
|
}
|
|
return await prisma.machine.findUnique({
|
|
where: { id: machine.id },
|
|
include: {
|
|
site: true,
|
|
typeMachine: {
|
|
include: {
|
|
customFields: true,
|
|
},
|
|
},
|
|
composants: {
|
|
include: {
|
|
typeComposant: true,
|
|
sousComposants: true,
|
|
pieces: {
|
|
include: {
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pieces: {
|
|
include: {
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
documents: true,
|
|
},
|
|
});
|
|
});
|
|
}
|
|
async createComponentsFromType(prisma, machineId, components, parentComposantId) {
|
|
for (const component of components) {
|
|
if (!component.name)
|
|
continue;
|
|
let typeComposant = null;
|
|
if (component.customFields && component.customFields.length > 0) {
|
|
typeComposant = await prisma.typeComposant.findFirst({
|
|
where: { name: component.name }
|
|
});
|
|
if (!typeComposant) {
|
|
typeComposant = await prisma.typeComposant.create({
|
|
data: {
|
|
name: component.name,
|
|
description: component.description || '',
|
|
},
|
|
});
|
|
for (const customField of component.customFields) {
|
|
await prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typeComposantId: typeComposant.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
const createdComposant = await prisma.composant.create({
|
|
data: {
|
|
name: component.name,
|
|
reference: component.reference || '',
|
|
constructeur: component.constructeur || '',
|
|
emplacement: component.emplacement || '',
|
|
prix: component.prix || null,
|
|
machineId,
|
|
parentComposantId,
|
|
typeComposantId: typeComposant?.id || null,
|
|
},
|
|
});
|
|
if (typeComposant && typeComposant.id) {
|
|
const customFields = await prisma.customField.findMany({
|
|
where: { typeComposantId: typeComposant.id },
|
|
});
|
|
for (const customField of customFields) {
|
|
const defaultValue = component.customFields?.find(cf => cf.name === customField.name)?.defaultValue || '';
|
|
await prisma.customFieldValue.create({
|
|
data: {
|
|
value: defaultValue,
|
|
customFieldId: customField.id,
|
|
composantId: createdComposant.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
if (component.pieces) {
|
|
for (const piece of component.pieces) {
|
|
if (!piece || !piece.name)
|
|
continue;
|
|
let typePiece = null;
|
|
if (piece.customFields && piece.customFields.length > 0) {
|
|
typePiece = await prisma.typePiece.findFirst({
|
|
where: { name: piece.name }
|
|
});
|
|
if (!typePiece) {
|
|
typePiece = await prisma.typePiece.create({
|
|
data: {
|
|
name: piece.name,
|
|
description: piece.description || '',
|
|
},
|
|
});
|
|
for (const customField of piece.customFields) {
|
|
await prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typePieceId: typePiece.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
const createdPiece = await prisma.piece.create({
|
|
data: {
|
|
name: piece.name,
|
|
reference: piece.reference || '',
|
|
constructeur: piece.constructeur || '',
|
|
emplacement: piece.emplacement || '',
|
|
prix: piece.prix || null,
|
|
composantId: createdComposant.id,
|
|
typePieceId: typePiece?.id || null,
|
|
},
|
|
});
|
|
if (typePiece && typePiece.id) {
|
|
const customFields = await prisma.customField.findMany({
|
|
where: { typePieceId: typePiece.id },
|
|
});
|
|
for (const customField of customFields) {
|
|
const defaultValue = piece.customFields?.find(cf => cf.name === customField.name)?.defaultValue || '';
|
|
await prisma.customFieldValue.create({
|
|
data: {
|
|
value: defaultValue,
|
|
customFieldId: customField.id,
|
|
pieceId: createdPiece.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (component.subComponents) {
|
|
await this.createComponentsFromType(prisma, machineId, component.subComponents, createdComposant.id);
|
|
}
|
|
}
|
|
}
|
|
async createMachinePiecesFromType(prisma, machineId, machinePieces) {
|
|
for (const piece of machinePieces) {
|
|
if (!piece || !piece.name)
|
|
continue;
|
|
const createdPiece = await prisma.piece.create({
|
|
data: {
|
|
name: piece.name,
|
|
machineId,
|
|
},
|
|
});
|
|
if (piece.customFields && piece.customFields.length > 0) {
|
|
for (const customField of piece.customFields) {
|
|
const createdCustomField = await prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typePieceId: null,
|
|
},
|
|
});
|
|
await prisma.customFieldValue.create({
|
|
data: {
|
|
value: customField.defaultValue || '',
|
|
customFieldId: createdCustomField.id,
|
|
pieceId: createdPiece.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
async createMachineCustomFieldsFromType(prisma, machineId, machineCustomFields) {
|
|
for (const customField of machineCustomFields) {
|
|
if (!customField || !customField.name)
|
|
continue;
|
|
const createdCustomField = await prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typeMachineId: null,
|
|
},
|
|
});
|
|
await prisma.customFieldValue.create({
|
|
data: {
|
|
value: customField.defaultValue || '',
|
|
customFieldId: createdCustomField.id,
|
|
machineId: machineId,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
async findAll() {
|
|
return this.prisma.machine.findMany({
|
|
include: {
|
|
site: true,
|
|
typeMachine: {
|
|
include: {
|
|
customFields: true,
|
|
},
|
|
},
|
|
composants: {
|
|
include: {
|
|
typeComposant: true,
|
|
sousComposants: true,
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
pieces: {
|
|
include: {
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pieces: {
|
|
include: {
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
documents: true,
|
|
},
|
|
});
|
|
}
|
|
async findOne(id) {
|
|
return this.prisma.machine.findUnique({
|
|
where: { id },
|
|
include: {
|
|
site: true,
|
|
typeMachine: {
|
|
include: {
|
|
customFields: true,
|
|
},
|
|
},
|
|
composants: {
|
|
include: {
|
|
typeComposant: true,
|
|
sousComposants: true,
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
pieces: {
|
|
include: {
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pieces: {
|
|
include: {
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
documents: true,
|
|
},
|
|
});
|
|
}
|
|
async update(id, updateMachineDto) {
|
|
return this.prisma.machine.update({
|
|
where: { id },
|
|
data: updateMachineDto,
|
|
include: {
|
|
site: true,
|
|
typeMachine: {
|
|
include: {
|
|
customFields: true,
|
|
},
|
|
},
|
|
composants: {
|
|
include: {
|
|
typeComposant: true,
|
|
sousComposants: true,
|
|
pieces: true,
|
|
},
|
|
},
|
|
pieces: true,
|
|
customFieldValues: {
|
|
include: {
|
|
customField: true,
|
|
},
|
|
},
|
|
documents: true,
|
|
},
|
|
});
|
|
}
|
|
async remove(id) {
|
|
const machine = await this.prisma.machine.findUnique({
|
|
where: { id },
|
|
include: {
|
|
composants: true,
|
|
pieces: true,
|
|
documents: true,
|
|
customFieldValues: true,
|
|
},
|
|
});
|
|
if (!machine) {
|
|
throw new Error('Machine non trouvée');
|
|
}
|
|
return await this.prisma.$transaction(async (prisma) => {
|
|
if (machine.customFieldValues.length > 0) {
|
|
await prisma.customFieldValue.deleteMany({
|
|
where: { machineId: id },
|
|
});
|
|
}
|
|
if (machine.documents.length > 0) {
|
|
await prisma.document.deleteMany({
|
|
where: { machineId: id },
|
|
});
|
|
}
|
|
if (machine.pieces.length > 0) {
|
|
await prisma.piece.deleteMany({
|
|
where: { machineId: id },
|
|
});
|
|
}
|
|
if (machine.composants.length > 0) {
|
|
await prisma.composant.deleteMany({
|
|
where: { machineId: id },
|
|
});
|
|
}
|
|
return await prisma.machine.delete({
|
|
where: { id },
|
|
});
|
|
});
|
|
}
|
|
async addMissingCustomFields(machineId) {
|
|
const machine = await this.prisma.machine.findUnique({
|
|
where: { id: machineId },
|
|
include: {
|
|
typeMachine: true,
|
|
composants: {
|
|
include: {
|
|
pieces: true,
|
|
},
|
|
},
|
|
pieces: true,
|
|
},
|
|
});
|
|
if (!machine || !machine.typeMachine) {
|
|
throw new Error('Machine ou type de machine non trouvé');
|
|
}
|
|
const typeMachine = machine.typeMachine;
|
|
const components = typeMachine.components || [];
|
|
const machinePieces = typeMachine.machinePieces || [];
|
|
const machineCustomFields = typeMachine.customFields || [];
|
|
if (machineCustomFields && machineCustomFields.length > 0) {
|
|
for (const customField of machineCustomFields) {
|
|
const existingValue = await this.prisma.customFieldValue.findFirst({
|
|
where: {
|
|
machineId: machineId,
|
|
customField: {
|
|
name: customField.name,
|
|
},
|
|
},
|
|
include: {
|
|
customField: true,
|
|
},
|
|
});
|
|
if (!existingValue) {
|
|
const createdCustomField = await this.prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typeMachineId: null,
|
|
},
|
|
});
|
|
await this.prisma.customFieldValue.create({
|
|
data: {
|
|
value: customField.defaultValue || '',
|
|
customFieldId: createdCustomField.id,
|
|
machineId: machineId,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
for (const component of machine.composants) {
|
|
const typeComponent = components.find((c) => c.name === component.name);
|
|
if (typeComponent && typeComponent.customFields && typeComponent.customFields.length > 0) {
|
|
let typeComposant = await this.prisma.typeComposant.findFirst({
|
|
where: { name: component.name },
|
|
});
|
|
if (!typeComposant) {
|
|
typeComposant = await this.prisma.typeComposant.create({
|
|
data: {
|
|
name: component.name,
|
|
description: typeComponent.description || '',
|
|
},
|
|
});
|
|
}
|
|
for (const customField of typeComponent.customFields) {
|
|
const existingField = await this.prisma.customField.findFirst({
|
|
where: {
|
|
name: customField.name,
|
|
typeComposantId: typeComposant.id,
|
|
},
|
|
});
|
|
if (!existingField) {
|
|
await this.prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typeComposantId: typeComposant.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
await this.prisma.composant.update({
|
|
where: { id: component.id },
|
|
data: { typeComposantId: typeComposant.id },
|
|
});
|
|
const customFields = await this.prisma.customField.findMany({
|
|
where: { typeComposantId: typeComposant.id },
|
|
});
|
|
for (const customField of customFields) {
|
|
const existingValue = await this.prisma.customFieldValue.findFirst({
|
|
where: {
|
|
customFieldId: customField.id,
|
|
composantId: component.id,
|
|
},
|
|
});
|
|
if (!existingValue) {
|
|
const defaultValue = typeComponent.customFields.find((cf) => cf.name === customField.name)?.defaultValue || '';
|
|
await this.prisma.customFieldValue.create({
|
|
data: {
|
|
value: defaultValue,
|
|
customFieldId: customField.id,
|
|
composantId: component.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
for (const piece of component.pieces) {
|
|
const typePiece = typeComponent.pieces?.find((p) => p.name === piece.name);
|
|
if (typePiece && typePiece.customFields && typePiece.customFields.length > 0) {
|
|
let typePieceEntity = await this.prisma.typePiece.findFirst({
|
|
where: { name: piece.name },
|
|
});
|
|
if (!typePieceEntity) {
|
|
typePieceEntity = await this.prisma.typePiece.create({
|
|
data: {
|
|
name: piece.name,
|
|
description: typePiece.description || '',
|
|
},
|
|
});
|
|
}
|
|
for (const customField of typePiece.customFields) {
|
|
const existingField = await this.prisma.customField.findFirst({
|
|
where: {
|
|
name: customField.name,
|
|
typePieceId: typePieceEntity.id,
|
|
},
|
|
});
|
|
if (!existingField) {
|
|
await this.prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typePieceId: typePieceEntity.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
await this.prisma.piece.update({
|
|
where: { id: piece.id },
|
|
data: { typePieceId: typePieceEntity.id },
|
|
});
|
|
const customFields = await this.prisma.customField.findMany({
|
|
where: { typePieceId: typePieceEntity.id },
|
|
});
|
|
for (const customField of customFields) {
|
|
const existingValue = await this.prisma.customFieldValue.findFirst({
|
|
where: {
|
|
customFieldId: customField.id,
|
|
pieceId: piece.id,
|
|
},
|
|
});
|
|
if (!existingValue) {
|
|
const defaultValue = typePiece.customFields.find((cf) => cf.name === customField.name)?.defaultValue || '';
|
|
await this.prisma.customFieldValue.create({
|
|
data: {
|
|
value: defaultValue,
|
|
customFieldId: customField.id,
|
|
pieceId: piece.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (const piece of machine.pieces) {
|
|
const typePiece = machinePieces.find((p) => p.name === piece.name);
|
|
if (typePiece && typePiece.customFields && typePiece.customFields.length > 0) {
|
|
let typePieceEntity = await this.prisma.typePiece.findFirst({
|
|
where: { name: piece.name },
|
|
});
|
|
if (!typePieceEntity) {
|
|
typePieceEntity = await this.prisma.typePiece.create({
|
|
data: {
|
|
name: piece.name,
|
|
description: typePiece.description || '',
|
|
},
|
|
});
|
|
}
|
|
for (const customField of typePiece.customFields) {
|
|
const existingField = await this.prisma.customField.findFirst({
|
|
where: {
|
|
name: customField.name,
|
|
typePieceId: typePieceEntity.id,
|
|
},
|
|
});
|
|
if (!existingField) {
|
|
await this.prisma.customField.create({
|
|
data: {
|
|
name: customField.name,
|
|
type: customField.type,
|
|
required: customField.required || false,
|
|
defaultValue: customField.defaultValue,
|
|
options: customField.options || [],
|
|
typePieceId: typePieceEntity.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
await this.prisma.piece.update({
|
|
where: { id: piece.id },
|
|
data: { typePieceId: typePieceEntity.id },
|
|
});
|
|
const customFields = await this.prisma.customField.findMany({
|
|
where: { typePieceId: typePieceEntity.id },
|
|
});
|
|
for (const customField of customFields) {
|
|
const existingValue = await this.prisma.customFieldValue.findFirst({
|
|
where: {
|
|
customFieldId: customField.id,
|
|
pieceId: piece.id,
|
|
},
|
|
});
|
|
if (!existingValue) {
|
|
const defaultValue = typePiece.customFields.find((cf) => cf.name === customField.name)?.defaultValue || '';
|
|
await this.prisma.customFieldValue.create({
|
|
data: {
|
|
value: defaultValue,
|
|
customFieldId: customField.id,
|
|
pieceId: piece.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return this.findOne(machineId);
|
|
}
|
|
};
|
|
exports.MachinesService = MachinesService;
|
|
exports.MachinesService = MachinesService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
|
], MachinesService);
|
|
//# sourceMappingURL=machines.service.js.map
|