From fb1e14942cd4915866f5fc2614ba25a3f6d11039 Mon Sep 17 00:00:00 2001 From: Odei Maiz Date: Tue, 19 Nov 2024 17:17:44 +0100 Subject: [PATCH] Store back --- .../client/source/class/osparc/store/Store.js | 206 ++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/services/static-webserver/client/source/class/osparc/store/Store.js b/services/static-webserver/client/source/class/osparc/store/Store.js index d56b6e9c232..c96b8ee416a 100644 --- a/services/static-webserver/client/source/class/osparc/store/Store.js +++ b/services/static-webserver/client/source/class/osparc/store/Store.js @@ -174,6 +174,26 @@ qx.Class.define("osparc.store.Store", { check: "Array", init: [] }, + organizations: { + check: "Object", + init: {} + }, + organizationMembers: { + check: "Object", + init: {} + }, + reachableMembers: { + check: "Object", + init: {} + }, + everyoneProductGroup: { + check: "Object", + init: {} + }, + everyoneGroup: { + check: "Object", + init: {} + }, clusters: { check: "Array", init: [], @@ -436,6 +456,192 @@ qx.Class.define("osparc.store.Store", { }); }); }, + __getGroups: function(group) { + return new Promise(resolve => { + osparc.data.Resources.get("organizations") + .then(groups => { + resolve(groups[group]); + }) + .catch(err => console.error(err)); + }); + }, + + getGroupsMe: function() { + return this.__getGroups("me"); + }, + + getGroupsOrganizations: function() { + return this.__getGroups("organizations"); + }, + + getProductEveryone: function() { + return this.__getGroups("product"); + }, + + getGroupEveryone: function() { + return this.__getGroups("all"); + }, + + __getAllGroups: function() { + return new Promise(resolve => { + const promises = []; + promises.push(this.getGroupsMe()); + promises.push(this.getReachableMembers()); + promises.push(this.getGroupsOrganizations()); + promises.push(this.getProductEveryone()); + promises.push(this.getGroupEveryone()); + Promise.all(promises) + .then(values => { + const groups = []; + const groupMe = values[0]; + groupMe["collabType"] = 2; + groups.push(groupMe); + const orgMembers = values[1]; + for (const gid of Object.keys(orgMembers)) { + orgMembers[gid]["collabType"] = 2; + groups.push(orgMembers[gid]); + } + values[2].forEach(org => { + org["collabType"] = 1; + groups.push(org); + }); + const groupProductEveryone = values[3]; + if (groupProductEveryone) { + groupProductEveryone["collabType"] = 0; + groups.push(groupProductEveryone); + } + const groupEveryone = values[4]; + if (groupEveryone) { + groupEveryone["collabType"] = 0; + groups.push(groupEveryone); + } + resolve(groups); + }); + }); + }, + + getOrganizationOrUser: function(orgId) { + return new Promise(resolve => { + this.__getAllGroups() + .then(orgs => { + const idx = orgs.findIndex(org => org.gid === parseInt(orgId)); + if (idx > -1) { + resolve(orgs[idx]); + } + resolve(null); + }); + }); + }, + + getAllGroupsAndMembers: function() { + return new Promise(resolve => { + osparc.data.Resources.get("organizations") + .then(resp => { + this.setEveryoneGroup(resp["all"]); + this.setEveryoneProductGroup(resp["product"]); + const orgMembersPromises = []; + const orgs = resp["organizations"]; + orgs.forEach(org => { + const params = { + url: { + "gid": org["gid"] + } + }; + orgMembersPromises.push(osparc.data.Resources.get("organizationMembers", params)); + }); + Promise.all(orgMembersPromises) + .then(orgMemberss => { + const reachableMembers = this.getReachableMembers(); + orgMemberss.forEach(orgMembers => { + orgMembers.forEach(orgMember => { + orgMember["label"] = osparc.utils.Utils.firstsUp( + `${"first_name" in orgMember && orgMember["first_name"] != null ? orgMember["first_name"] : orgMember["login"]}`, + `${orgMember["last_name"] ? orgMember["last_name"] : ""}` + ); + reachableMembers[orgMember["gid"]] = orgMember; + }); + }); + resolve(); + }); + }); + }); + }, + + getPotentialCollaborators: function(includeMe = false, includeProductEveryone = false) { + return new Promise((resolve, reject) => { + const promises = []; + promises.push(this.getGroupsOrganizations()); + promises.push(this.getReachableMembers()); + promises.push(this.getEveryoneProductGroup()); + Promise.all(promises) + .then(values => { + const orgs = values[0]; // array + const members = values[1]; // object + const productEveryone = values[2]; // entry + const potentialCollaborators = {}; + orgs.forEach(org => { + if (org["accessRights"]["read"]) { + // maybe because of migration script, some users have access to the product everyone group + // rely on the includeProductEveryone argument to exclude it if necessary + if (org["gid"] === productEveryone["gid"] && !includeProductEveryone) { + return; + } + org["collabType"] = 1; + potentialCollaborators[org["gid"]] = org; + } + }); + for (const gid of Object.keys(members)) { + members[gid]["collabType"] = 2; + potentialCollaborators[gid] = members[gid]; + } + if (includeMe) { + const myData = osparc.auth.Data.getInstance(); + const myGid = myData.getGroupId(); + potentialCollaborators[myGid] = { + "login": myData.getEmail(), + "first_name": myData.getFirstName(), + "last_name": myData.getLastName(), + "collabType": 2 + }; + } + if (includeProductEveryone && productEveryone) { + productEveryone["collabType"] = 0; + potentialCollaborators[productEveryone["gid"]] = productEveryone; + } + resolve(potentialCollaborators); + }) + .catch(err => { + console.error(err); + reject(err); + }); + }); + }, + + getGroup: function(gid) { + return new Promise(resolve => { + if (gid) { + this.getPotentialCollaborators() + .then(potentialCollaborators => { + let group = null; + if (gid in potentialCollaborators) { + group = potentialCollaborators[gid]; + } + resolve(group); + }) + .catch(() => resolve(null)); + } else { + resolve(null); + } + }); + }, + + getUser: function(uid) { + if (uid) { + const visibleMembers = this.getReachableMembers(); + return Object.values(visibleMembers).find(member => member.id === uid); + } + return null; + }, reloadCreditPrice: function() { const store = osparc.store.Store.getInstance();