apps/recallassess/recallassess-api/src/api/admin/participant-group/participant-group.service.ts
BNestBaseModuleService
Methods |
constructor(prisma: BNestPrismaService, systemLogService: SystemLogService)
|
|||||||||
|
Parameters :
|
| Async add | ||||||
add(data: any)
|
||||||
|
Override add method to validate required fields and remove foreign key fields before passing to Prisma Prisma only accepts relation objects (company, createdBy), not raw foreign keys (company_id, participant_id_created_by)
Parameters :
Returns :
Promise<any>
|
| Async delete | ||||||
delete(id: number)
|
||||||
|
Override delete method to log deletion
Parameters :
Returns :
Promise<void>
|
| Async save |
save(id: number, data: any)
|
|
Override save method to handle optional foreign key fields for updates Prisma only accepts relation objects (company, createdBy), not raw foreign keys
Returns :
Promise<any>
|
import { BNestBaseModuleService } from "@bish-nest/core/data/module-service/base-module.service";
import { BNestPrismaService } from "@bish-nest/core/services";
import { SystemLogService } from "@api/shared/services";
import { Injectable, NotFoundException, UnprocessableEntityException } from "@nestjs/common";
import { SystemLogEntityType } from "@prisma/client";
@Injectable()
export class ParticipantGroupService extends BNestBaseModuleService {
constructor(
protected prisma: BNestPrismaService,
private readonly systemLogService: SystemLogService,
) {
super();
}
/**
* Override add method to validate required fields and remove foreign key fields before passing to Prisma
* Prisma only accepts relation objects (company, createdBy), not raw foreign keys (company_id, participant_id_created_by)
*/
async add(data: any): Promise<any> {
// Validate required fields for creation
if (!data.company_id) {
throw new UnprocessableEntityException({
message: [
{
colName: "company_id",
errorMessage: "Company is required. Please select a company.",
},
],
code: "FORM_VALIDATION_ERROR",
});
}
if (!data.participant_id_created_by) {
throw new UnprocessableEntityException({
message: [
{
colName: "participant_id_created_by",
errorMessage: "Created by participant is required. Please select a participant.",
},
],
code: "FORM_VALIDATION_ERROR",
});
}
// Create a copy of data without the foreign key fields
// The transformed relation fields (company, createdBy) are already present
const { company_id, participant_id_created_by, ...prismaData } = data;
// Call parent add method with cleaned data
const addResponse = await super.add(prismaData);
const participantGroup = addResponse.data;
// Log the creation
await this.systemLogService.logInsert(
SystemLogEntityType.PARTICIPANT_GROUP,
participantGroup.id,
participantGroup as Record<string, unknown>,
);
return addResponse;
}
/**
* Override save method to handle optional foreign key fields for updates
* Prisma only accepts relation objects (company, createdBy), not raw foreign keys
*/
async save(id: number, data: any): Promise<any> {
// Get old data before update
const oldParticipantGroup = await this.prisma.client.participantGroup.findUnique({
where: { id },
});
if (!oldParticipantGroup) {
throw new NotFoundException(`Participant group with ID ${id} not found`);
}
// Create a copy of data without the foreign key fields if they exist
// The transformed relation fields (company, createdBy) are already present if foreign keys were provided
const { company_id, participant_id_created_by, ...prismaData } = data;
// Call parent save method with cleaned data
const saveResponse = await super.save(id, prismaData);
const updatedParticipantGroup = saveResponse.data;
// Calculate changed fields and log the update
const changedFields = SystemLogService.calculateChangedFields(
oldParticipantGroup as Record<string, unknown>,
updatedParticipantGroup as Record<string, unknown>,
);
await this.systemLogService.logUpdate(
SystemLogEntityType.PARTICIPANT_GROUP,
id,
oldParticipantGroup as Record<string, unknown>,
updatedParticipantGroup as Record<string, unknown>,
changedFields,
);
return saveResponse;
}
/**
* Override delete method to log deletion
*/
async delete(id: number): Promise<void> {
// Get participant group data before deletion
const participantGroup = await this.prisma.client.participantGroup.findUnique({
where: { id },
});
if (!participantGroup) {
throw new NotFoundException(`Participant group with ID ${id} not found`);
}
// Call parent delete method
await super.delete(id);
// Log the deletion
await this.systemLogService.logDelete(
SystemLogEntityType.PARTICIPANT_GROUP,
id,
participantGroup as Record<string, unknown>,
);
}
}