feat: add profiles session API

This commit is contained in:
Matthieu
2025-09-17 23:11:25 +02:00
parent 83251b532c
commit df5bbeecb7
78 changed files with 3000 additions and 836 deletions

View File

@@ -0,0 +1,119 @@
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 } })
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,
},
})
}
}