Skip to content

Commit

Permalink
Revert to use global group members endpoint and filter departments
Browse files Browse the repository at this point in the history
Previous work to avoid the global group members endpoint caused high
load on the API server as we needed an additional request per group.

Instead we now revert to the old behavior which requires to give the
API user "administer persons" permissions and therefore see all persons.

In order to still limit the exported persons to a certain department,
let's introduce a department filter for the persons.
  • Loading branch information
fschrempf committed Nov 2, 2023
1 parent f0bb618 commit e4031f3
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 e4031f3

Please sign in to comment.