Skip to content

Commit

Permalink
Merge pull request #173 from mitaai/blms/reduce-api-calls
Browse files Browse the repository at this point in the history
🐎 #146 Fetch group names in batches
  • Loading branch information
blms authored Apr 9, 2021
2 parents ade80e1 + 9d1c226 commit 3c712ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
22 changes: 16 additions & 6 deletions src/utils/annotationUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,25 @@ const getOwnAnnotations = async ({
};

const addGroupNamesToAnnotations = async (annosToAlter) => {
const allGroupIds = [];
annosToAlter.forEach((annotation) => {
annotation.permissions.groups.forEach((group) => {
if (!allGroupIds.includes(group)) { allGroupIds.push(group); }
});
});
const groupObjects = await getManyGroupNamesById(allGroupIds)
.then((res) => res.groups);
const altered = Promise.all(annosToAlter.map(async (annotation) => {
const anno = annotation;
if (annotation.permissions.groups && annotation.permissions.groups.length > 0) {
await getManyGroupNamesById(annotation.permissions.groups)
.then((res) => {
anno.permissions.groups = res.groups;
});
let alteredGroups = [];
if (annotation.permissions.groups) {
alteredGroups = annotation.permissions.groups.map(
// eslint-disable-next-line no-underscore-dangle
(group) => groupObjects.find((groupObject) => groupObject._id === group),
);
}
return anno;
const alteredPermissions = { ...anno.permissions, groups: alteredGroups };
return { ...anno, permissions: alteredPermissions };
}));
return altered;
};
Expand Down
21 changes: 15 additions & 6 deletions src/utils/docUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,24 @@ const getManyGroupNamesById = async (groupIds) => {
};

const addGroupNamesToDocuments = async (docsToAlter) => {
const allGroupIds = [];
docsToAlter.forEach((doc) => {
doc.groups.forEach((group) => {
if (!allGroupIds.includes(group)) { allGroupIds.push(group); }
});
});
const groupObjects = await getManyGroupNamesById(allGroupIds)
.then((res) => res.groups);
const altered = Promise.all(docsToAlter.map(async (document) => {
const doc = document;
if (document.groups && document.groups.length > 0) {
await getManyGroupNamesById(document.groups)
.then((res) => {
doc.groups = res.groups;
});
let alteredGroups = [];
if (document.groups) {
alteredGroups = document.groups.map(
// eslint-disable-next-line no-underscore-dangle
(group) => groupObjects.find((groupObject) => groupObject._id === group),
);
}
return doc;
return { ...doc, groups: alteredGroups };
}));
return altered;
};
Expand Down

1 comment on commit 3c712ed

@vercel
Copy link

@vercel vercel bot commented on 3c712ed Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.