feat: Add Model gestion for piece and component
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user