139 lines
3.1 KiB
TypeScript
139 lines
3.1 KiB
TypeScript
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();
|
|
}
|
|
|
|
async findAllActive() {
|
|
return this.prisma.profile.findMany({
|
|
where: { isActive: true },
|
|
orderBy: { createdAt: 'asc' },
|
|
select: {
|
|
id: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async findActiveById(profileId: string) {
|
|
if (!profileId) return null;
|
|
|
|
return this.prisma.profile.findFirst({
|
|
where: {
|
|
id: profileId,
|
|
isActive: true,
|
|
},
|
|
select: {
|
|
id: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
isActive: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async create(dto: CreateProfileDto) {
|
|
const firstName = dto.firstName.trim();
|
|
const lastName = dto.lastName.trim();
|
|
|
|
if (!firstName || !lastName) {
|
|
throw new BadRequestException('Le prénom et le nom sont obligatoires.');
|
|
}
|
|
|
|
return this.prisma.profile.create({
|
|
data: {
|
|
firstName,
|
|
lastName,
|
|
},
|
|
select: {
|
|
id: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
isActive: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async deactivate(profileId: string) {
|
|
const existing = await this.prisma.profile.findUnique({
|
|
where: { id: profileId },
|
|
});
|
|
if (!existing) {
|
|
throw new NotFoundException('Profil introuvable');
|
|
}
|
|
|
|
if (!existing.isActive) {
|
|
return this.prisma.profile.findUnique({
|
|
where: { id: profileId },
|
|
select: {
|
|
id: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
isActive: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
return this.prisma.profile.update({
|
|
where: { id: profileId },
|
|
data: { isActive: false },
|
|
select: {
|
|
id: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
isActive: true,
|
|
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 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);
|
|
});
|
|
}
|
|
}
|