feat: Add Model gestion for piece and component
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
} from '@nestjs/common';
|
||||
import { CustomFieldsService } from './custom-fields.service';
|
||||
import {
|
||||
CreateCustomFieldValueDto,
|
||||
@@ -12,8 +20,12 @@ export class CustomFieldsController {
|
||||
constructor(private readonly customFieldsService: CustomFieldsService) {}
|
||||
|
||||
@Post('values')
|
||||
createCustomFieldValue(@Body() createCustomFieldValueDto: CreateCustomFieldValueDto) {
|
||||
return this.customFieldsService.createCustomFieldValue(createCustomFieldValueDto);
|
||||
createCustomFieldValue(
|
||||
@Body() createCustomFieldValueDto: CreateCustomFieldValueDto,
|
||||
) {
|
||||
return this.customFieldsService.createCustomFieldValue(
|
||||
createCustomFieldValueDto,
|
||||
);
|
||||
}
|
||||
|
||||
@Get('values/:entityType/:entityId')
|
||||
@@ -34,7 +46,10 @@ export class CustomFieldsController {
|
||||
@Param('id') id: string,
|
||||
@Body() updateCustomFieldValueDto: UpdateCustomFieldValueDto,
|
||||
) {
|
||||
return this.customFieldsService.updateCustomFieldValue(id, updateCustomFieldValueDto);
|
||||
return this.customFieldsService.updateCustomFieldValue(
|
||||
id,
|
||||
updateCustomFieldValueDto,
|
||||
);
|
||||
}
|
||||
|
||||
@Delete('values/:id')
|
||||
@@ -51,4 +66,4 @@ export class CustomFieldsController {
|
||||
body.value,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,4 @@ import { PrismaModule } from '../prisma/prisma.module';
|
||||
providers: [CustomFieldsService],
|
||||
exports: [CustomFieldsService],
|
||||
})
|
||||
export class CustomFieldsModule {}
|
||||
export class CustomFieldsModule {}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import {
|
||||
CreateCustomFieldValueDto,
|
||||
@@ -11,7 +15,9 @@ export class CustomFieldsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
// Créer une valeur de champ personnalisé
|
||||
async createCustomFieldValue(createCustomFieldValueDto: CreateCustomFieldValueDto) {
|
||||
async createCustomFieldValue(
|
||||
createCustomFieldValueDto: CreateCustomFieldValueDto,
|
||||
) {
|
||||
return this.prisma.customFieldValue.create({
|
||||
data: createCustomFieldValueDto,
|
||||
include: {
|
||||
@@ -30,11 +36,16 @@ export class CustomFieldsService {
|
||||
case CustomFieldEntityType.PIECE:
|
||||
return 'pieceId' as const;
|
||||
default:
|
||||
throw new BadRequestException('Type d\'entité de champ personnalisé invalide.');
|
||||
throw new BadRequestException(
|
||||
"Type d'entité de champ personnalisé invalide.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async resolveEntityContext(entityType: CustomFieldEntityType, entityId: string) {
|
||||
private async resolveEntityContext(
|
||||
entityType: CustomFieldEntityType,
|
||||
entityId: string,
|
||||
) {
|
||||
switch (entityType) {
|
||||
case CustomFieldEntityType.MACHINE: {
|
||||
const machine = await this.prisma.machine.findUnique({
|
||||
@@ -103,7 +114,9 @@ export class CustomFieldsService {
|
||||
};
|
||||
}
|
||||
default:
|
||||
throw new BadRequestException('Type d\'entité de champ personnalisé invalide.');
|
||||
throw new BadRequestException(
|
||||
"Type d'entité de champ personnalisé invalide.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +148,10 @@ export class CustomFieldsService {
|
||||
}
|
||||
|
||||
// Mettre à jour une valeur de champ personnalisé
|
||||
async updateCustomFieldValue(id: string, updateCustomFieldValueDto: UpdateCustomFieldValueDto) {
|
||||
async updateCustomFieldValue(
|
||||
id: string,
|
||||
updateCustomFieldValueDto: UpdateCustomFieldValueDto,
|
||||
) {
|
||||
return this.prisma.customFieldValue.update({
|
||||
where: { id },
|
||||
data: updateCustomFieldValueDto,
|
||||
@@ -159,10 +175,8 @@ export class CustomFieldsService {
|
||||
entityId: string,
|
||||
value: string,
|
||||
) {
|
||||
const { typeId, customFieldTypeField, valueKey } = await this.resolveEntityContext(
|
||||
entityType,
|
||||
entityId,
|
||||
);
|
||||
const { typeId, customFieldTypeField, valueKey } =
|
||||
await this.resolveEntityContext(entityType, entityId);
|
||||
|
||||
const allowedCustomField = await this.prisma.customField.findFirst({
|
||||
where: {
|
||||
@@ -173,7 +187,7 @@ export class CustomFieldsService {
|
||||
|
||||
if (!allowedCustomField) {
|
||||
throw new BadRequestException(
|
||||
'Le champ personnalisé n\'est pas autorisé pour cette entité.',
|
||||
"Le champ personnalisé n'est pas autorisé pour cette entité.",
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user