LoginCallback heavy refactoring (wip?)
This commit is contained in:
parent
66befaa7ed
commit
64f6b4ed98
@ -26,6 +26,7 @@ import RoleService from "src/common/Api/LeCoffreApi/sdk/RoleService";
|
||||
import OfficeService from "src/common/Api/LeCoffreApi/sdk/OfficeService";
|
||||
import OfficeRoleService from "src/common/Api/LeCoffreApi/sdk/OfficeRoleService";
|
||||
import CollaboratorService from "src/common/Api/LeCoffreApi/sdk/CollaboratorService";
|
||||
import { DEFAULT_STORAGE_URLS, DEFAULT_VALIDATOR_ID } from "@Front/Config/AppConstants";
|
||||
|
||||
export default function LoginCallBack() {
|
||||
const router = useRouter();
|
||||
@ -45,10 +46,23 @@ export default function LoginCallBack() {
|
||||
const getOffice = async (idNotUser: any) => {
|
||||
return await new Promise<any>((resolve: (office: any) => void) => {
|
||||
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) {
|
||||
resolve(officeFound);
|
||||
} 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 = {
|
||||
idNot: idNotUser.office.idNot,
|
||||
name: idNotUser.office.name,
|
||||
@ -60,17 +74,29 @@ export default function LoginCallBack() {
|
||||
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) => {
|
||||
if (process) {
|
||||
const office: any = process.processData;
|
||||
resolve(office);
|
||||
Auth.getInstance().getIdNotUserForOffice(idNotUser.office.idNot).then((users: any) => {
|
||||
console.log('users : ', users);
|
||||
const activeUsers = users.result.filter((user: any) => user.activite === 'En exercice');
|
||||
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) => {
|
||||
const processFound: any | null = await CollaboratorService.getCollaboratorBy({ idNot: idNotUser.idNot });
|
||||
if (processFound) {
|
||||
resolve(processFound.processData);
|
||||
} else {
|
||||
const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0';
|
||||
console.log('Found a collaborator for idNot', idNotUser.idNot);
|
||||
// TODO: check if the collaborator is in the office process
|
||||
const office: any = await getOffice(idNotUser);
|
||||
|
||||
if (!await ImportData.isDone()) {
|
||||
LoaderService.getInstance().hide();
|
||||
setShowProgress(true);
|
||||
|
||||
await ImportData.import(office, validatorId, (info: ProgressInfo) => {
|
||||
setProgressInfo(info);
|
||||
// Take the role of the collaborator
|
||||
MessageBus.getInstance().getRolesForProcess(processFound.processId).then((roles: any) => {
|
||||
console.log('roles : ', roles);
|
||||
// We should find one pairing id in the role 'owner'
|
||||
const owners = roles['owner'].members;
|
||||
if (owners.length !== 1) {
|
||||
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);
|
||||
LoaderService.getInstance().show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('No collaborator found for idNot', idNotUser.idNot);
|
||||
const office: any = await getOffice(idNotUser);
|
||||
|
||||
const role: any = (await RoleService.getRoles())
|
||||
.map((process: any) => process.processData)
|
||||
@ -102,9 +142,21 @@ export default function LoginCallBack() {
|
||||
|
||||
const officeRole: any = (await OfficeRoleService.getOfficeRoles())
|
||||
.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);
|
||||
|
||||
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 = {
|
||||
idNot: idNotUser.idNot,
|
||||
contact: idNotUser.contact,
|
||||
@ -119,12 +171,40 @@ export default function LoginCallBack() {
|
||||
}
|
||||
};
|
||||
|
||||
CollaboratorService.createCollaborator(collaboratorData, validatorId).then((process: any) => {
|
||||
if (process) {
|
||||
const collaborator: any = process.processData;
|
||||
resolve(collaborator);
|
||||
CollaboratorService.createCollaborator(collaboratorData, DEFAULT_VALIDATOR_ID).then((newCollaborator: any) => {
|
||||
if (newCollaborator) {
|
||||
// Now that we created the collaborator, we must check that it's in the office roles (probably not)
|
||||
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);
|
||||
setIdNotUser(user.idNotUser);
|
||||
setIsAuthModalOpen(true);
|
||||
console.log('[LoginCallback] idNotUser', idNotUser);
|
||||
/*
|
||||
const token: any = null;
|
||||
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().isReady().then(async () => {
|
||||
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();
|
||||
LoaderService.getInstance().hide();
|
||||
|
Loading…
x
Reference in New Issue
Block a user