126 lines
4.3 KiB
JavaScript
126 lines
4.3 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ProfilesService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const prisma_service_1 = require("../prisma/prisma.service");
|
|
let ProfilesService = class ProfilesService {
|
|
prisma;
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
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) {
|
|
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) {
|
|
const firstName = dto.firstName.trim();
|
|
const lastName = dto.lastName.trim();
|
|
if (!firstName || !lastName) {
|
|
throw new common_1.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) {
|
|
const existing = await this.prisma.profile.findUnique({ where: { id: profileId } });
|
|
if (!existing) {
|
|
throw new common_1.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,
|
|
},
|
|
});
|
|
}
|
|
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,
|
|
},
|
|
});
|
|
}
|
|
};
|
|
exports.ProfilesService = ProfilesService;
|
|
exports.ProfilesService = ProfilesService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
|
], ProfilesService);
|
|
//# sourceMappingURL=profiles.service.js.map
|