Fix null values with prisma
This commit is contained in:
parent
9bfc33a5c7
commit
3e88c93357
@ -101,9 +101,9 @@ export default class UsersRepository extends BaseRepository {
|
|||||||
update: {
|
update: {
|
||||||
first_name: user.contact?.first_name,
|
first_name: user.contact?.first_name,
|
||||||
last_name: user.contact?.last_name,
|
last_name: user.contact?.last_name,
|
||||||
email: user.contact?.email,
|
email: user.contact?.email === null ? undefined : user.contact?.email,
|
||||||
phone_number: user.contact?.phone_number,
|
phone_number: user.contact?.phone_number === null ? undefined : user.contact?.phone_number,
|
||||||
cell_phone_number: user.contact?.cell_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],
|
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 } } } });
|
return this.model.update({ ...updateArgs, include: { contact: true, office_membership: { include: { address: true } } } });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -370,8 +370,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!);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -582,17 +585,36 @@ 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) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user