Skip to content

Commit

Permalink
add workspace-memberships api
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Feb 11, 2023
1 parent 2fb4b26 commit 2f1a671
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
43 changes: 42 additions & 1 deletion backend/src/controllers/v1/organizationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {
MembershipOrg,
Organization,
Workspace,
IncidentContactOrg
IncidentContactOrg,
IMembershipOrg
} from '../../models';
import { createOrganization as create } from '../../helpers/organization';
import { addMembershipsOrg } from '../../helpers/membershipOrg';
import { OWNER, ACCEPTED } from '../../variables';
import _ from 'lodash';

export const getOrganizations = async (req: Request, res: Response) => {
let organizations;
Expand Down Expand Up @@ -382,3 +384,42 @@ export const getOrganizationSubscriptions = async (
subscriptions
});
};


/**
* Given a org id, return the projects each member of the org belongs to
* @param req
* @param res
* @returns
*/
export const getOrganizationMembersAndTheirWorkspaces = async (
req: Request,
res: Response
) => {
const { organizationId } = req.params;
const orgMemberships = await MembershipOrg.find({ organization: organizationId });
const userIds = orgMemberships.map(orgMembership => orgMembership.user);
const memberships = await Membership.find({ user: { $in: userIds } });
const userToWorkspaceIds: any = {};

memberships.forEach(membership => {
const user = membership.user.toString();
if (userToWorkspaceIds[user]) {
userToWorkspaceIds[user].push(membership.workspace);
} else {
userToWorkspaceIds[user] = [membership.workspace];
}
});

const workspaceIds = Object.values(userToWorkspaceIds).flat()
const workspacesList = await Workspace.find({
organization: organizationId,
_id: { $in: workspaceIds }
});

const populatedUserWorkspaces = _.mapValues(userToWorkspaceIds, workspaceIds =>
_.map(workspaceIds, id => _.find(workspacesList, { _id: id }))
);

return res.json(populatedUserWorkspaces);
};
15 changes: 15 additions & 0 deletions backend/src/routes/v1/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,19 @@ router.get(
organizationController.getOrganizationSubscriptions
);

router.get(
'/:organizationId/workspace-memberships',
requireAuth({
acceptedAuthModes: ['jwt']
}),
requireOrganizationAuth({
acceptedRoles: [OWNER, ADMIN, MEMBER],
acceptedStatuses: [ACCEPTED]
}),
param('organizationId').exists().trim(),
validateRequest,
organizationController.getOrganizationMembersAndTheirWorkspaces
);


export default router;

0 comments on commit 2f1a671

Please sign in to comment.