LoginCallback heavy refactoring (wip?)

This commit is contained in:
Sosthene 2025-08-27 17:42:13 +02:00
parent 6f1f542dd2
commit 429d3243a4

View File

@ -26,6 +26,7 @@ import RoleService from "src/common/Api/LeCoffreApi/sdk/RoleService";
import OfficeService from "src/common/Api/LeCoffreApi/sdk/OfficeService"; import OfficeService from "src/common/Api/LeCoffreApi/sdk/OfficeService";
import OfficeRoleService from "src/common/Api/LeCoffreApi/sdk/OfficeRoleService"; import OfficeRoleService from "src/common/Api/LeCoffreApi/sdk/OfficeRoleService";
import CollaboratorService from "src/common/Api/LeCoffreApi/sdk/CollaboratorService"; import CollaboratorService from "src/common/Api/LeCoffreApi/sdk/CollaboratorService";
import { DEFAULT_STORAGE_URLS, DEFAULT_VALIDATOR_ID } from "@Front/Config/AppConstants";
export default function LoginCallBack() { export default function LoginCallBack() {
const router = useRouter(); const router = useRouter();
@ -45,10 +46,23 @@ export default function LoginCallBack() {
const getOffice = async (idNotUser: any) => { const getOffice = async (idNotUser: any) => {
return await new Promise<any>((resolve: (office: any) => void) => { return await new Promise<any>((resolve: (office: any) => void) => {
OfficeService.getOffices().then((processes: any[]) => { OfficeService.getOffices().then((processes: any[]) => {
const officeFound: any = processes.length > 0 ? processes.map((process: any) => process.processData).find((office: any) => office.idNot === idNotUser.office.idNot) : null; const officeFound: any = processes.length > 0 ? processes.find((office: any) => office.processData.idNot === idNotUser.office.idNot) : null;
if (officeFound) { if (officeFound) {
resolve(officeFound); resolve(officeFound);
} else { } else {
// Some info must be here or have some value, just to be sure
if (!idNotUser.office.office_status || idNotUser.office.office_status !== 'ACTIVATED') {
console.error(`[LoginCallback] office_status is not ACTIVATED for idNot ${idNotUser.office.idNot}`);
return;
}
// I guess if we don't have crpcen that's also a big problem
if (!idNotUser.office.crpcen) {
console.error(`[LoginCallback] crpcen is not set for idNot ${idNotUser.office.idNot}`);
return;
}
// We create office
const officeData: any = { const officeData: any = {
idNot: idNotUser.office.idNot, idNot: idNotUser.office.idNot,
name: idNotUser.office.name, name: idNotUser.office.name,
@ -60,17 +74,29 @@ export default function LoginCallBack() {
city: idNotUser.office.address.city city: idNotUser.office.address.city
} }
}, },
office_status: 'ACTIVATED' office_status: idNotUser.office.office_status // must be ACTIVATED though
}; };
const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0';
OfficeService.createOffice(officeData, validatorId).then((process: any) => { Auth.getInstance().getIdNotUserForOffice(idNotUser.office.idNot).then((users: any) => {
if (process) { console.log('users : ', users);
const office: any = process.processData; const activeUsers = users.result.filter((user: any) => user.activite === 'En exercice');
resolve(office); let officeCollaborators: any[] = [];
for (const user of activeUsers) {
CollaboratorService.getCollaboratorByUid(user.uid).then((collaborator: any) => {
console.log('collaborator : ', collaborator);
officeCollaborators.push(collaborator);
});
} }
OfficeService.createOffice(officeData, officeCollaborators, DEFAULT_VALIDATOR_ID, [...DEFAULT_STORAGE_URLS]).then((process: any) => {
if (process) {
const office: any = process.processData;
resolve(office);
}
});
}); });
} }
return;
}); });
}); });
}; };
@ -79,22 +105,36 @@ export default function LoginCallBack() {
return await new Promise<any>(async (resolve: (role: any) => void) => { return await new Promise<any>(async (resolve: (role: any) => void) => {
const processFound: any | null = await CollaboratorService.getCollaboratorBy({ idNot: idNotUser.idNot }); const processFound: any | null = await CollaboratorService.getCollaboratorBy({ idNot: idNotUser.idNot });
if (processFound) { if (processFound) {
resolve(processFound.processData); console.log('Found a collaborator for idNot', idNotUser.idNot);
} else { // TODO: check if the collaborator is in the office process
const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0';
const office: any = await getOffice(idNotUser); const office: any = await getOffice(idNotUser);
// Take the role of the collaborator
if (!await ImportData.isDone()) { MessageBus.getInstance().getRolesForProcess(processFound.processId).then((roles: any) => {
LoaderService.getInstance().hide(); console.log('roles : ', roles);
setShowProgress(true); // We should find one pairing id in the role 'owner'
const owners = roles['owner'].members;
await ImportData.import(office, validatorId, (info: ProgressInfo) => { if (owners.length !== 1) {
setProgressInfo(info); console.error('[LoginCallback] owner should have 1 member');
return;
}
const ownerPairingId = owners[0];
// Now we can check if the owner pairing id is in the office roles
MessageBus.getInstance().getRolesForProcess(office.processId).then((officeRoles: any) => {
const officeOwners = officeRoles['owner'].members;
if (!officeOwners.includes(ownerPairingId)) {
// We add the newly created collaborator to the office roles
OfficeService.addCollaborators(office, officeRoles, [ownerPairingId]).then((process: any) => {
resolve(processFound);
});
} else {
// Nothing to do
resolve(processFound);
}
}); });
});
setShowProgress(false); } else {
LoaderService.getInstance().show(); console.log('No collaborator found for idNot', idNotUser.idNot);
} const office: any = await getOffice(idNotUser);
const role: any = (await RoleService.getRoles()) const role: any = (await RoleService.getRoles())
.map((process: any) => process.processData) .map((process: any) => process.processData)
@ -102,9 +142,21 @@ export default function LoginCallBack() {
const officeRole: any = (await OfficeRoleService.getOfficeRoles()) const officeRole: any = (await OfficeRoleService.getOfficeRoles())
.map((process: any) => process.processData) .map((process: any) => process.processData)
.filter((officeRole: any) => officeRole.office.uid === office.uid) .filter((officeRole: any) => officeRole.office.uid === office.processData.uid)
.find((officeRole: any) => officeRole.name === idNotUser.office_role.name); .find((officeRole: any) => officeRole.name === idNotUser.office_role.name);
if (!office || !role || !officeRole) {
LoaderService.getInstance().hide();
setShowProgress(true);
await ImportData.import(office, DEFAULT_VALIDATOR_ID, (info: ProgressInfo) => {
setProgressInfo(info);
});
setShowProgress(false);
LoaderService.getInstance().show();
}
const collaboratorData: any = { const collaboratorData: any = {
idNot: idNotUser.idNot, idNot: idNotUser.idNot,
contact: idNotUser.contact, contact: idNotUser.contact,
@ -119,12 +171,40 @@ export default function LoginCallBack() {
} }
}; };
CollaboratorService.createCollaborator(collaboratorData, validatorId).then((process: any) => { CollaboratorService.createCollaborator(collaboratorData, DEFAULT_VALIDATOR_ID).then((newCollaborator: any) => {
if (process) { if (newCollaborator) {
const collaborator: any = process.processData; // Now that we created the collaborator, we must check that it's in the office roles (probably not)
resolve(collaborator); MessageBus.getInstance().getRolesForProcess(newCollaborator.processId).then((roles: any) => {
console.log('roles : ', roles);
// We should have our own pairing id in roles['owner']
const owner = roles['owner'].members;
if (owner.length !== 1) {
console.error('[LoginCallback] owner should have 1 member');
return;
}
const ownerPairingId = owner[0];
if (ownerPairingId !== newCollaborator.processData.uid) {
console.error('[LoginCallback] owner pairing id is not the same as the collaborator uid');
return;
}
// is ownerPairingId in roles for the office process?
MessageBus.getInstance().getRolesForProcess(office.processId).then((officeRoles: any) => {
const officeOwners = officeRoles['owner'].members;
if (!officeOwners.includes(ownerPairingId)) {
// We add the newly created collaborator to the office roles
OfficeService.addCollaborators(office, officeRoles, [ownerPairingId]).then((process: any) => {
resolve(newCollaborator);
});
} else {
// Nothing to do
resolve(newCollaborator);
}
});
});
} }
}); });
} }
}); });
}; };
@ -153,6 +233,7 @@ export default function LoginCallBack() {
const user: any = await Auth.getInstance().getIdNotUser(code as string); const user: any = await Auth.getInstance().getIdNotUser(code as string);
setIdNotUser(user.idNotUser); setIdNotUser(user.idNotUser);
setIsAuthModalOpen(true); setIsAuthModalOpen(true);
console.log('[LoginCallback] idNotUser', idNotUser);
/* /*
const token: any = null; const token: any = null;
if (!token) return router.push(Module.getInstance().get().modules.pages.Login.props.path); if (!token) return router.push(Module.getInstance().get().modules.pages.Login.props.path);
@ -274,7 +355,11 @@ export default function LoginCallBack() {
MessageBus.getInstance().initMessageListener(); MessageBus.getInstance().initMessageListener();
MessageBus.getInstance().isReady().then(async () => { MessageBus.getInstance().isReady().then(async () => {
const collaborator: any = await getCollaborator(idNotUser); const collaborator: any = await getCollaborator(idNotUser);
UserStore.instance.connect(collaborator); if (!UserStore.instance.connect(collaborator)) {
console.error('[LoginCallback] collaborator not connected');
router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=1");
return;
}
MessageBus.getInstance().destroyMessageListener(); MessageBus.getInstance().destroyMessageListener();
LoaderService.getInstance().hide(); LoaderService.getInstance().hide();