Skip to content

Commit

Permalink
fetch users and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
odeimaiz committed Dec 18, 2024
1 parent 29223c6 commit 68cbbd5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,54 +148,56 @@ qx.Class.define("osparc.dashboard.CardBase", {
return false;
},

// groups -> [orgMembs, orgs, [productEveryone], [everyone]];
setIconAndTooltip: function(shareIcon, accessRights, groups) {
shareIcon.setSource(osparc.dashboard.CardBase.SHARE_ICON);
if (osparc.data.model.Study.canIWrite(accessRights)) {
shareIcon.set({
toolTipText: qx.locale.Manager.tr("Share")
});
populateShareIcon: async function(shareIcon, accessRights) {
const gids = Object.keys(accessRights).map(key => parseInt(key));

const groupsStore = osparc.store.Groups.getInstance();

// Icon
const groupEveryone = groupsStore.getEveryoneGroup();
const groupProductEveryone = groupsStore.getEveryoneProductGroup();
const organizations = groupsStore.getOrganizations();
const organizationIds = Object.keys(organizations).map(key => parseInt(key));
if (gids.includes(groupEveryone.getGroupId()) || gids.includes(groupProductEveryone.getGroupId())) {
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ALL);
} else if (organizationIds.filter(value => gids.includes(value)).length) { // find intersection
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ORGS);
} else if (gids.length === 1) {
shareIcon.setSource(osparc.dashboard.CardBase.SHARE_ICON);
} else {
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_USER);
}
let sharedGrps = [];
const myGroupId = osparc.auth.Data.getInstance().getGroupId();
for (let i=0; i<groups.length; i++) {
if (groups[i].length === 0) {
// user has no read access to the productEveryone
continue;

// Tooltip
const sharedGrps = [];
const groups = [];
groups.push(groupEveryone);
groups.push(groupProductEveryone);
groups.push(...Object.values(organizations));
groups.forEach(group => {
const idx = gids.indexOf(group.getGroupId());
if (idx > -1) {
sharedGrps.push(group);
gids.splice(idx, 1);
}
const sharedGrp = [];
const gids = Object.keys(accessRights);
for (let j=0; j<gids.length; j++) {
const gid = parseInt(gids[j]);
if (gid === myGroupId) {
continue;
}
const grp = groups[i].find(group => group.getGroupId() === gid);
if (grp) {
sharedGrp.push(grp);
});
// once the groups were removed, the remaining group ids are users' primary groups ids
const usersStore = osparc.store.Users.getInstance();
const myGroupId = osparc.auth.Data.getInstance().getGroupId();
for (let i=0; i<gids.length; i++) {
const gid = gids[i];
if (myGroupId !== gid) {
const user = await usersStore.getUser(gid);
if (user) {
sharedGrps.push(user);
}
}
if (sharedGrp.length === 0) {
continue;
} else {
sharedGrps = sharedGrps.concat(sharedGrp);
}
switch (i) {
case 0:
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_USER);
break;
case 1:
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ORGS);
break;
case 2:
case 3:
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ALL);
break;
}
}

// tooltip
if (sharedGrps.length === 0) {
if (sharedGrps.length === 0 && osparc.data.model.Study.canIWrite(accessRights)) {
shareIcon.set({
toolTipText: qx.locale.Manager.tr("Share")
});
return;
}
const sharedGrpLabels = [];
Expand All @@ -215,18 +217,6 @@ qx.Class.define("osparc.dashboard.CardBase", {
shareIcon.addListener("mouseover", () => hint.show(), this);
shareIcon.addListener("mouseout", () => hint.exclude(), this);
},

// groups -> [users, orgs, [productEveryone], [everyone]];
populateShareIcon: function(shareIcon, accessRights) {
const groupsStore = osparc.store.Groups.getInstance();
const usersStore = osparc.store.Users.getInstance();
const users = usersStore.getUsers();
const orgs = Object.values(groupsStore.getOrganizations());
const productEveryone = [groupsStore.getEveryoneProductGroup()];
const everyone = [groupsStore.getEveryoneGroup()];
const groups = [users, orgs, productEveryone, everyone];
osparc.dashboard.CardBase.setIconAndTooltip(shareIcon, accessRights, groups);
},
},

properties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ qx.Class.define("osparc.data.Resources", {
"users": {
useCache: false, // osparc.store.Groups handles the cache
endpoints: {
get: {
method: "GET",
url: statics.API + "/groups/{gid}/users"
},
search: {
method: "POST",
url: statics.API + "/users:search"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ qx.Class.define("osparc.store.Users", {
},

members: {
fetchUser: function(groupId) {
const params = {
url: {
gid: groupId
}
};
return osparc.data.Resources.fetch("users", "get", params)
.then(userData => {
const user = this.addUser(userData[0]);
return user;
});
},

getUser: function(groupId, fetchIfNotFound = true) {
const userFound = this.getUsers().find(user => user.getGroupId() === groupId);
if (userFound) {
return new Promise(resolve => resolve(userFound));
} else if (fetchIfNotFound) {
return this.fetchUser(groupId);
}
return new Promise(reject => reject());
},

addUser: function(userData) {
const user = new osparc.data.model.User(userData);
const userFound = this.getUsers().find(usr => usr.getGroupId() === user.getGroupId());
Expand Down

0 comments on commit 68cbbd5

Please sign in to comment.