feat: Add Model gestion for piece and component

This commit is contained in:
Matthieu
2025-09-23 15:05:33 +02:00
parent bc225bf3e8
commit e64fba3ae7
54 changed files with 1640 additions and 569 deletions

View File

@@ -1,6 +1,6 @@
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common'
import { ProfilesService } from './profiles.service'
import { CreateProfileDto } from '../shared/dto/profile.dto'
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
import { ProfilesService } from './profiles.service';
import { CreateProfileDto } from '../shared/dto/profile.dto';
@Controller('profiles')
export class ProfilesController {
@@ -8,16 +8,16 @@ export class ProfilesController {
@Get()
async findAll() {
return this.profilesService.findAllActive()
return this.profilesService.findAllActive();
}
@Post()
async create(@Body() dto: CreateProfileDto) {
return this.profilesService.create(dto)
return this.profilesService.create(dto);
}
@Delete(':id')
async delete(@Param('id') id: string) {
return this.profilesService.deactivate(id)
return this.profilesService.deactivate(id);
}
}

View File

@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common'
import { ProfilesController } from './profiles.controller'
import { ProfilesService } from './profiles.service'
import { Module } from '@nestjs/common';
import { ProfilesController } from './profiles.controller';
import { ProfilesService } from './profiles.service';
@Module({
controllers: [ProfilesController],

View File

@@ -1,13 +1,18 @@
import { Injectable, NotFoundException, BadRequestException, OnModuleInit } from '@nestjs/common'
import { PrismaService } from '../prisma/prisma.service'
import { CreateProfileDto } from '../shared/dto/profile.dto'
import {
Injectable,
NotFoundException,
BadRequestException,
OnModuleInit,
} from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateProfileDto } from '../shared/dto/profile.dto';
@Injectable()
export class ProfilesService implements OnModuleInit {
constructor(private readonly prisma: PrismaService) {}
async onModuleInit() {
await this.ensureDefaultProfile()
await this.ensureDefaultProfile();
}
async findAllActive() {
@@ -21,11 +26,11 @@ export class ProfilesService implements OnModuleInit {
createdAt: true,
updatedAt: true,
},
})
});
}
async findActiveById(profileId: string) {
if (!profileId) return null
if (!profileId) return null;
return this.prisma.profile.findFirst({
where: {
@@ -40,15 +45,15 @@ export class ProfilesService implements OnModuleInit {
updatedAt: true,
isActive: true,
},
})
});
}
async create(dto: CreateProfileDto) {
const firstName = dto.firstName.trim()
const lastName = dto.lastName.trim()
const firstName = dto.firstName.trim();
const lastName = dto.lastName.trim();
if (!firstName || !lastName) {
throw new BadRequestException('Le prénom et le nom sont obligatoires.')
throw new BadRequestException('Le prénom et le nom sont obligatoires.');
}
return this.prisma.profile.create({
@@ -64,13 +69,15 @@ export class ProfilesService implements OnModuleInit {
createdAt: true,
updatedAt: true,
},
})
});
}
async deactivate(profileId: string) {
const existing = await this.prisma.profile.findUnique({ where: { id: profileId } })
const existing = await this.prisma.profile.findUnique({
where: { id: profileId },
});
if (!existing) {
throw new NotFoundException('Profil introuvable')
throw new NotFoundException('Profil introuvable');
}
if (!existing.isActive) {
@@ -84,7 +91,7 @@ export class ProfilesService implements OnModuleInit {
createdAt: true,
updatedAt: true,
},
})
});
}
return this.prisma.profile.update({
@@ -98,27 +105,34 @@ export class ProfilesService implements OnModuleInit {
createdAt: true,
updatedAt: true,
},
})
});
}
private async ensureDefaultProfile() {
const count = await this.prisma.profile.count({ where: { isActive: true } }).catch((err) => {
console.error('Failed to count profiles during ensureDefaultProfile:', err.message)
return 0
})
if (count > 0) return
const count = await this.prisma.profile
.count({ where: { isActive: true } })
.catch((err) => {
console.error(
'Failed to count profiles during ensureDefaultProfile:',
err.message,
);
return 0;
});
if (count > 0) return;
const firstName = process.env.DEFAULT_PROFILE_FIRST_NAME?.trim() || 'Admin'
const lastName = process.env.DEFAULT_PROFILE_LAST_NAME?.trim() || 'Général'
const firstName = process.env.DEFAULT_PROFILE_FIRST_NAME?.trim() || 'Admin';
const lastName = process.env.DEFAULT_PROFILE_LAST_NAME?.trim() || 'Général';
await this.prisma.profile.create({
data: {
firstName,
lastName,
isActive: true,
},
}).catch((err) => {
console.error('Failed to create default profile:', err.message)
})
await this.prisma.profile
.create({
data: {
firstName,
lastName,
isActive: true,
},
})
.catch((err) => {
console.error('Failed to create default profile:', err.message);
});
}
}