This repository has been archived on 2024-09-23. You can view files and clone it, but cannot push or open issues or pull requests.
rocketchat-scim/src/endpoints/GroupsEndpoint.ts
2022-04-19 17:59:19 +02:00

47 lines
1.8 KiB
TypeScript

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<IApiResponse> {
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<IApiResponse> {
const u = SCIMGroup.fromPlain(ctx.content());
const o = await ctx.rc.team.create({
name: u.displayName,
type: 1,
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,
});
}
}