Fix updateUser
This commit is contained in:
parent
06e69d6974
commit
cbabd7a9fb
@ -11,6 +11,7 @@ import { EOfficeStatus } from "@prisma/client";
|
||||
import OfficeRolesService from "@Services/super-admin/OfficeRolesService/OfficeRolesService";
|
||||
import DeedTypesService from "@Services/super-admin/DeedTypesService/DeedTypesService";
|
||||
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
|
||||
import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
|
||||
import * as Sentry from "@sentry/node";
|
||||
|
||||
interface IIdNotToken {
|
||||
@ -111,6 +112,7 @@ export default class IdNotService extends BaseService {
|
||||
private officeRolesService: OfficeRolesService,
|
||||
private deedTypesService: DeedTypesService,
|
||||
private documentTypesService: DocumentTypesService,
|
||||
private officeFoldersService: OfficeFoldersService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
@ -220,7 +222,12 @@ export default class IdNotService extends BaseService {
|
||||
}
|
||||
|
||||
public async updateUser(userId: string) {
|
||||
const userInfos = await this.userService.getByUid(userId, { contact: true, role: true, office_membership: true });
|
||||
const userInfos = await this.userService.getByUid(userId, {
|
||||
contact: true,
|
||||
role: true,
|
||||
office_membership: true,
|
||||
office_folders: true
|
||||
});
|
||||
const user = User.hydrate<User>(userInfos!);
|
||||
const searchParams = new URLSearchParams({
|
||||
key: this.variables.IDNOT_API_KEY,
|
||||
@ -303,12 +310,27 @@ export default class IdNotService extends BaseService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If user was deleted and no valid office was found, delete the user
|
||||
if (userData.deleted) {
|
||||
console.log(`User ${user.uid} is deleted from all offices, deleting user from database`);
|
||||
try {
|
||||
await this.userService.delete(user.uid!);
|
||||
console.log(`Successfully deleted user ${user.uid}`);
|
||||
return;
|
||||
} catch (error) {
|
||||
console.error(`Error deleting user ${user.uid}:`, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let update = false;
|
||||
|
||||
if (user.office_membership!.idNot !== userData.entite.ou) {
|
||||
update = true;
|
||||
const oldOfficeId = user.office_membership!.uid;
|
||||
|
||||
let officeData = (await this.officeService.get({ where: { idNot: userData.entite.ou } }))[0];
|
||||
if (!officeData) {
|
||||
let officeLocationData: IOfficeLocation;
|
||||
@ -343,6 +365,36 @@ export default class IdNotService extends BaseService {
|
||||
}
|
||||
user.office_membership = officeData;
|
||||
console.log("New office_membership found", JSON.stringify(user.office_membership));
|
||||
|
||||
// Handle stakeholder relationships when user changes office
|
||||
try {
|
||||
// Get folders from the old office where the user is a stakeholder
|
||||
const oldOfficeFolders = user.office_folders?.filter(folder => (folder as any).office_uid === oldOfficeId) || [];
|
||||
|
||||
// Remove user from all stakeholder relationships in old office folders
|
||||
for (const folder of oldOfficeFolders) {
|
||||
// Get the current folder with stakeholders to update it
|
||||
const folderWithStakeholders = await this.officeFoldersService.getByUid(folder.uid!, { stakeholders: true });
|
||||
if (!folderWithStakeholders) continue;
|
||||
|
||||
const updatedStakeholders = (folderWithStakeholders as any).stakeholders.filter((stakeholder: any) => stakeholder.uid !== user.uid);
|
||||
|
||||
// Update the folder with the new stakeholder list (excluding the user)
|
||||
await this.officeFoldersService.update(folder.uid!, {
|
||||
...folderWithStakeholders,
|
||||
stakeholders: updatedStakeholders
|
||||
});
|
||||
|
||||
console.log(`Removed user ${user.uid} from stakeholder list of folder ${folder.uid} (old office ${oldOfficeId})`);
|
||||
}
|
||||
|
||||
if (oldOfficeFolders.length > 0) {
|
||||
console.log(`Removed user ${user.uid} from ${oldOfficeFolders.length} folders in old office ${oldOfficeId}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error updating stakeholder relationships for user ${user.uid}:`, error);
|
||||
// Don't fail the entire update if stakeholder update fails
|
||||
}
|
||||
}
|
||||
|
||||
if (userData.mailRattachement && (user.contact!.email === null || user.contact!.email === undefined || user.contact!.email !== userData.mailRattachement)) {
|
||||
@ -603,4 +655,4 @@ export default class IdNotService extends BaseService {
|
||||
|
||||
return converted;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user