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/GroupsEndpoint.ts
2022-02-15 13:38:33 +01:00

53 lines
1.8 KiB
TypeScript

import {
HttpStatusCode,
IHttp,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import {
IApiEndpointInfo,
IApiRequest,
IApiResponse,
} from "@rocket.chat/apps-engine/definition/api";
import crypto = require("crypto");
import { Context } from "./Context";
import { RcHttp } from "./RcHttp";
import { SCIMGroup } from "./scim/Group";
import { SCIMListResponse } from "./scim/ListResponse";
import { SCIMUser } from "./scim/User";
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: ITeam) => {
const membersRaw = await ctx.rc.team.members(team._id);
this.handleError(membersRaw);
return SCIMGroup.fromRC(team, membersRaw.members);
});
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: 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);
return this.response({
status: HttpStatusCode.CREATED,
content: group,
});
}
}