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,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user