Fix null values with prisma
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m46s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 3s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s

This commit is contained in:
Sosthene 2025-07-23 22:59:25 +02:00
parent feeeb18f9f
commit 21e5cf55a7

View File

@ -371,8 +371,11 @@ export default class IdNotService extends BaseService {
user.contact!.cell_phone_number = userData.numeroMobile; user.contact!.cell_phone_number = userData.numeroMobile;
console.log("Updated user.contact.cell_phone_number", JSON.stringify(user.contact!.cell_phone_number)); console.log("Updated user.contact.cell_phone_number", JSON.stringify(user.contact!.cell_phone_number));
} }
console.log("updates", updates); if (updates != 0) {
if (updates != 0) await this.userService.update(user.uid!, user); // Filter out null values before updating to prevent Prisma errors
const convertedUser = this.convertNullToUndefined(user);
await this.userService.update(user.uid!, convertedUser);
}
await this.userService.updateCheckedAt(user.uid!); await this.userService.updateCheckedAt(user.uid!);
} }
@ -583,17 +586,35 @@ export default class IdNotService extends BaseService {
public async updateUsers() { public async updateUsers() {
const usersReq = await this.userService.getUsersToBeChecked(); const usersReq = await this.userService.getUsersToBeChecked();
const users = User.hydrateArray<User>(usersReq); const users = User.hydrateArray<User>(usersReq);
users.forEach(async (user) => { for (const user of users) {
await this.updateUser(user.uid!); await this.updateUser(user.uid!);
}); }
} }
public async updateOffices() { public async updateOffices() {
const officesReq = await this.officeService.getOfficesToBeChecked(); const officesReq = await this.officeService.getOfficesToBeChecked();
const offices = Office.hydrateArray<Office>(officesReq); const offices = Office.hydrateArray<Office>(officesReq);
console.log(`Updating ${offices.length} offices`); console.log(`Updating ${offices.length} offices`);
offices.forEach(async (office) => { for (const office of offices) {
await this.updateOffice(office.uid!); await this.updateOffice(office.uid!);
}); }
}
/**
* Utility function to convert null values to undefined
* This prevents Prisma from throwing errors about null values
*/
private convertNullToUndefined<T extends Record<string, any>>(obj: T): T {
const converted = { ...obj };
for (const [key, value] of Object.entries(converted)) {
if (value === null) {
(converted as any)[key] = undefined;
} else if (typeof value === 'object' && !Array.isArray(value)) {
(converted as any)[key] = this.convertNullToUndefined(value);
}
}
return converted;
} }
} }