Fix null values with prisma

This commit is contained in:
Sosthene 2025-07-23 22:59:25 +02:00
parent 9bfc33a5c7
commit 3e88c93357
2 changed files with 33 additions and 9 deletions

View File

@ -101,9 +101,9 @@ export default class UsersRepository extends BaseRepository {
update: {
first_name: user.contact?.first_name,
last_name: user.contact?.last_name,
email: user.contact?.email,
phone_number: user.contact?.phone_number,
cell_phone_number: user.contact?.cell_phone_number,
email: user.contact?.email === null ? undefined : user.contact?.email,
phone_number: user.contact?.phone_number === null ? undefined : user.contact?.phone_number,
cell_phone_number: user.contact?.cell_phone_number === null ? undefined : user.contact?.cell_phone_number,
civility: ECivility[user.contact?.civility as keyof typeof ECivility],
},
},
@ -142,6 +142,8 @@ export default class UsersRepository extends BaseRepository {
};
}
console.log("updateArgs", JSON.stringify(updateArgs));
return this.model.update({ ...updateArgs, include: { contact: true, office_membership: { include: { address: true } } } });
}

View File

@ -370,8 +370,11 @@ export default class IdNotService extends BaseService {
user.contact!.cell_phone_number = userData.numeroMobile;
console.log("Updated user.contact.cell_phone_number", JSON.stringify(user.contact!.cell_phone_number));
}
console.log("updates", updates);
if (updates != 0) await this.userService.update(user.uid!, user);
if (updates != 0) {
// 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!);
}
@ -582,17 +585,36 @@ export default class IdNotService extends BaseService {
public async updateUsers() {
const usersReq = await this.userService.getUsersToBeChecked();
const users = User.hydrateArray<User>(usersReq);
users.forEach(async (user) => {
for (const user of users) {
await this.updateUser(user.uid!);
});
}
}
public async updateOffices() {
const officesReq = await this.officeService.getOfficesToBeChecked();
const offices = Office.hydrateArray<Office>(officesReq);
console.log(`Updating ${offices.length} offices`);
offices.forEach(async (office) => {
for (const office of offices) {
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) {
console.log(`Converting null to undefined for key: ${key}`);
(converted as any)[key] = undefined;
} else if (typeof value === 'object' && !Array.isArray(value)) {
(converted as any)[key] = this.convertNullToUndefined(value);
}
}
return converted;
}
}