feat: Modules de fonctionnalités avancées - Ajout des modules Documents et Champs personnalisés pour la gestion des fichiers et métadonnées
This commit is contained in:
54
src/custom-fields/custom-fields.controller.ts
Normal file
54
src/custom-fields/custom-fields.controller.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { CustomFieldsService } from './custom-fields.service';
|
||||
import { CreateCustomFieldValueDto, UpdateCustomFieldValueDto } from '../shared/dto/custom-field.dto';
|
||||
|
||||
@Controller('custom-fields')
|
||||
export class CustomFieldsController {
|
||||
constructor(private readonly customFieldsService: CustomFieldsService) {}
|
||||
|
||||
@Post('values')
|
||||
createCustomFieldValue(@Body() createCustomFieldValueDto: CreateCustomFieldValueDto) {
|
||||
return this.customFieldsService.createCustomFieldValue(createCustomFieldValueDto);
|
||||
}
|
||||
|
||||
@Get('values/:entityType/:entityId')
|
||||
findCustomFieldValuesByEntity(
|
||||
@Param('entityType') entityType: string,
|
||||
@Param('entityId') entityId: string,
|
||||
) {
|
||||
return this.customFieldsService.findCustomFieldValuesByEntity(entityType, entityId);
|
||||
}
|
||||
|
||||
@Get('values/:id')
|
||||
findOneCustomFieldValue(@Param('id') id: string) {
|
||||
return this.customFieldsService.findOneCustomFieldValue(id);
|
||||
}
|
||||
|
||||
@Patch('values/:id')
|
||||
updateCustomFieldValue(
|
||||
@Param('id') id: string,
|
||||
@Body() updateCustomFieldValueDto: UpdateCustomFieldValueDto,
|
||||
) {
|
||||
return this.customFieldsService.updateCustomFieldValue(id, updateCustomFieldValueDto);
|
||||
}
|
||||
|
||||
@Delete('values/:id')
|
||||
removeCustomFieldValue(@Param('id') id: string) {
|
||||
return this.customFieldsService.removeCustomFieldValue(id);
|
||||
}
|
||||
|
||||
@Post('values/upsert')
|
||||
upsertCustomFieldValue(@Body() body: {
|
||||
customFieldId: string;
|
||||
entityType: string;
|
||||
entityId: string;
|
||||
value: string;
|
||||
}) {
|
||||
return this.customFieldsService.upsertCustomFieldValue(
|
||||
body.customFieldId,
|
||||
body.entityType,
|
||||
body.entityId,
|
||||
body.value,
|
||||
);
|
||||
}
|
||||
}
|
||||
12
src/custom-fields/custom-fields.module.ts
Normal file
12
src/custom-fields/custom-fields.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CustomFieldsService } from './custom-fields.service';
|
||||
import { CustomFieldsController } from './custom-fields.controller';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [CustomFieldsController],
|
||||
providers: [CustomFieldsService],
|
||||
exports: [CustomFieldsService],
|
||||
})
|
||||
export class CustomFieldsModule {}
|
||||
94
src/custom-fields/custom-fields.service.ts
Normal file
94
src/custom-fields/custom-fields.service.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateCustomFieldValueDto, UpdateCustomFieldValueDto } from '../shared/dto/custom-field.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CustomFieldsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
// Créer une valeur de champ personnalisé
|
||||
async createCustomFieldValue(createCustomFieldValueDto: CreateCustomFieldValueDto) {
|
||||
return this.prisma.customFieldValue.create({
|
||||
data: createCustomFieldValueDto,
|
||||
include: {
|
||||
customField: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Trouver toutes les valeurs de champs personnalisés pour une entité
|
||||
async findCustomFieldValuesByEntity(entityType: string, entityId: string) {
|
||||
const whereClause = {
|
||||
[entityType + 'Id']: entityId,
|
||||
};
|
||||
|
||||
return this.prisma.customFieldValue.findMany({
|
||||
where: whereClause,
|
||||
include: {
|
||||
customField: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Trouver une valeur de champ personnalisé par ID
|
||||
async findOneCustomFieldValue(id: string) {
|
||||
return this.prisma.customFieldValue.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
customField: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Mettre à jour une valeur de champ personnalisé
|
||||
async updateCustomFieldValue(id: string, updateCustomFieldValueDto: UpdateCustomFieldValueDto) {
|
||||
return this.prisma.customFieldValue.update({
|
||||
where: { id },
|
||||
data: updateCustomFieldValueDto,
|
||||
include: {
|
||||
customField: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Supprimer une valeur de champ personnalisé
|
||||
async removeCustomFieldValue(id: string) {
|
||||
return this.prisma.customFieldValue.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
// Créer ou mettre à jour une valeur de champ personnalisé
|
||||
async upsertCustomFieldValue(customFieldId: string, entityType: string, entityId: string, value: string) {
|
||||
// D'abord, essayer de trouver une valeur existante
|
||||
const existingValue = await this.prisma.customFieldValue.findFirst({
|
||||
where: {
|
||||
customFieldId,
|
||||
[entityType + 'Id']: entityId,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingValue) {
|
||||
// Mettre à jour la valeur existante
|
||||
return this.prisma.customFieldValue.update({
|
||||
where: { id: existingValue.id },
|
||||
data: { value },
|
||||
include: {
|
||||
customField: true,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Créer une nouvelle valeur
|
||||
return this.prisma.customFieldValue.create({
|
||||
data: {
|
||||
customFieldId,
|
||||
value,
|
||||
[entityType + 'Id']: entityId,
|
||||
},
|
||||
include: {
|
||||
customField: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/documents/documents.controller.spec.ts
Normal file
21
src/documents/documents.controller.spec.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { DocumentsController } from './documents.controller';
|
||||
import { DocumentsService } from './documents.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('DocumentsController', () => {
|
||||
let controller: DocumentsController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [DocumentsController],
|
||||
providers: [DocumentsService, PrismaService],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<DocumentsController>(DocumentsController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
48
src/documents/documents.controller.ts
Normal file
48
src/documents/documents.controller.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { DocumentsService } from './documents.service';
|
||||
import { CreateDocumentDto, UpdateDocumentDto } from '../shared/dto/document.dto';
|
||||
|
||||
@Controller('documents')
|
||||
export class DocumentsController {
|
||||
constructor(private readonly documentsService: DocumentsService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createDocumentDto: CreateDocumentDto) {
|
||||
return this.documentsService.create(createDocumentDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.documentsService.findAll();
|
||||
}
|
||||
|
||||
@Get('machine/:machineId')
|
||||
findByMachine(@Param('machineId') machineId: string) {
|
||||
return this.documentsService.findByMachine(machineId);
|
||||
}
|
||||
|
||||
@Get('composant/:composantId')
|
||||
findByComposant(@Param('composantId') composantId: string) {
|
||||
return this.documentsService.findByComposant(composantId);
|
||||
}
|
||||
|
||||
@Get('piece/:pieceId')
|
||||
findByPiece(@Param('pieceId') pieceId: string) {
|
||||
return this.documentsService.findByPiece(pieceId);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.documentsService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateDocumentDto: UpdateDocumentDto) {
|
||||
return this.documentsService.update(id, updateDocumentDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.documentsService.remove(id);
|
||||
}
|
||||
}
|
||||
9
src/documents/documents.module.ts
Normal file
9
src/documents/documents.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DocumentsController } from './documents.controller';
|
||||
import { DocumentsService } from './documents.service';
|
||||
|
||||
@Module({
|
||||
controllers: [DocumentsController],
|
||||
providers: [DocumentsService]
|
||||
})
|
||||
export class DocumentsModule {}
|
||||
19
src/documents/documents.service.spec.ts
Normal file
19
src/documents/documents.service.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { DocumentsService } from './documents.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('DocumentsService', () => {
|
||||
let service: DocumentsService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [DocumentsService, PrismaService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<DocumentsService>(DocumentsService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
91
src/documents/documents.service.ts
Normal file
91
src/documents/documents.service.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateDocumentDto, UpdateDocumentDto } from '../shared/dto/document.dto';
|
||||
|
||||
@Injectable()
|
||||
export class DocumentsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async create(createDocumentDto: CreateDocumentDto) {
|
||||
return this.prisma.document.create({
|
||||
data: createDocumentDto,
|
||||
include: {
|
||||
machine: true,
|
||||
composant: true,
|
||||
piece: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
return this.prisma.document.findMany({
|
||||
include: {
|
||||
machine: true,
|
||||
composant: true,
|
||||
piece: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
return this.prisma.document.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
machine: true,
|
||||
composant: true,
|
||||
piece: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findByMachine(machineId: string) {
|
||||
return this.prisma.document.findMany({
|
||||
where: { machineId },
|
||||
include: {
|
||||
machine: true,
|
||||
composant: true,
|
||||
piece: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findByComposant(composantId: string) {
|
||||
return this.prisma.document.findMany({
|
||||
where: { composantId },
|
||||
include: {
|
||||
machine: true,
|
||||
composant: true,
|
||||
piece: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findByPiece(pieceId: string) {
|
||||
return this.prisma.document.findMany({
|
||||
where: { pieceId },
|
||||
include: {
|
||||
machine: true,
|
||||
composant: true,
|
||||
piece: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: string, updateDocumentDto: UpdateDocumentDto) {
|
||||
return this.prisma.document.update({
|
||||
where: { id },
|
||||
data: updateDocumentDto,
|
||||
include: {
|
||||
machine: true,
|
||||
composant: true,
|
||||
piece: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
return this.prisma.document.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user