Skip to content

Commit

Permalink
Merge pull request #15 from fschrempf/feature/reduce-api-request-load
Browse files Browse the repository at this point in the history
Revert to use global group members endpoint and filter departments
  • Loading branch information
fschrempf authored Nov 2, 2023
2 parents f0bb618 + e4031f3 commit 9938ead
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions src/ctservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,42 @@ exports.getPersonsInGroups = async (site) => {
exports.getGroupMemberships = async (groupIds, site) => {
const members = [];

if (groupIds == null || !groupIds.length) {
const result = await getGroupsPaginated(
null,
c.GROUPMEMBERS_AP,
[{ key: 'with_deleted', value: 'false' }],
site,
);
result.forEach((el) => {
members.push({
personId: el.personId,
groupId: el.groupId,
groupTypeRoleId: el.groupTypeRoleId,
});
});
return members;
}

groupIds.forEach(async (groupId) => {
const result = await getGroupsPaginated(
groupIds,
`${c.GROUPS_AP}/${groupId}/members`,
[{ key: 'with_deleted', value: 'false' }],
site,
);
result.forEach((el) => {
members.push({
personId: el.personId,
groupId,
groupTypeRoleId: el.groupTypeRoleId,
});
const result = await getGroupsPaginated(
groupIds,
c.GROUPMEMBERS_AP,
[{ key: 'with_deleted', value: 'false' }],
site,
);
result.forEach((el) => {
members.push({
personId: el.personId,
groupId: el.groupId,
groupTypeRoleId: el.groupTypeRoleId,
});
});
return members;

/*
* This would avoid the global group member endpoint and allows to run with
* limited privileges, but causes too high API load as we need one additional
* request per group.
*/
// groupIds.forEach(async (groupId) => {
// const result = await getGroupsPaginated(
// null,
// `${c.GROUPS_AP}/${groupId}/members`,
// [{ key: 'with_deleted', value: 'false' }],
// site,
// );
// result.forEach((el) => {
// members.push({
// personId: el.personId,
// groupId,
// groupTypeRoleId: el.groupTypeRoleId,
// });
// });
// });
// return members;
};

exports.getGroups = async (groupIds, site) => {
Expand Down Expand Up @@ -153,6 +156,11 @@ exports.getPersons = async (ids, site) => {
for await (const idarray of chunkedIds) {
const result = await getGroupsPaginated(idarray, c.PERSONS_AP, [], site);
result.forEach((person) => {
if (site.departments) {
const deps = new Set(person.departmentIds);
// check if department IDs of person intersect with IDs from site config
if (![...new Set(site.departments)].some((x) => deps.has(x))) return;
}
persons.push(getPersonRecord(person));
});
}
Expand Down Expand Up @@ -181,8 +189,7 @@ exports.getChurchToolsData = async (site) => {
log.info('Get Groups from ChurchTools');
const ctGroups = await this.getGroups(allGroupsIds, site);
log.info('Get Group Memberships from ChurchTools');
const ctGroupIds = ctGroups.map((group) => group.id);
const ctGroupMembership = await this.getGroupMemberships(ctGroupIds, site);
const ctGroupMembership = await this.getGroupMemberships(allGroupsIds, site);
log.info('Get Person Details from ChurchTools');
if (allGroupsIds != null) {
ctPersonIds = ctGroupMembership.map((member) => member.id);
Expand Down

0 comments on commit 9938ead

Please sign in to comment.