import { HttpStatusCode } from "@rocket.chat/apps-engine/definition/accessors"; import { IApiResponse } from "@rocket.chat/apps-engine/definition/api"; import { SCIMGroup } from "../scim/Group"; import { SCIMListResponse } from "../scim/ListResponse"; import { Context } from "./Context"; import { IScimEndpoint, ScimEndpoint } from "./ScimEndpoint"; export class GroupsEndpoint extends ScimEndpoint implements IScimEndpoint { public path = "Groups"; public async _get(ctx: Context): Promise { const teamsRaw = await ctx.rc.team.listAll(); this.handleError(teamsRaw); const groups = teamsRaw.teams.map(async (team) => { const membersRaw = await ctx.rc.team.members(team._id); this.handleError(membersRaw); const group = SCIMGroup.fromRC(team, membersRaw.members); const meta = await ctx.store.getGroup(group.id); if (meta) { group.externalId = meta.externalId; } return group; }); const list = new SCIMListResponse(); list.Resources = await Promise.all(groups); list.totalResults = teamsRaw.total; return this.success(list); } public async _post(ctx: Context): Promise { const u = SCIMGroup.fromPlain(ctx.content()); const o = await ctx.rc.team.create({ name: u.displayName, type: 0, members: u.members.map((x) => x.value), }); this.handleError(o); const m = await ctx.rc.team.members(o.team._id); const group = SCIMGroup.fromRC(o.team, m.members); group.externalId = u.externalId; await ctx.store.saveGroup(group.id, { externalId: group.externalId }); return this.response({ status: HttpStatusCode.CREATED, content: group, }); } }