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

48 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-02-15 13:09:03 +00:00
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";
2022-02-15 12:38:33 +00:00
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);
2022-02-15 13:09:03 +00:00
const groups = teamsRaw.teams.map(async (team) => {
2022-02-15 12:38:33 +00:00
const membersRaw = await ctx.rc.team.members(team._id);
this.handleError(membersRaw);
2022-02-15 14:53:18 +00:00
const group = SCIMGroup.fromRC(team, membersRaw.members);
const meta = await ctx.store.getGroup(group.id);
if (meta) {
group.externalId = meta.externalId;
}
return group;
2022-02-15 12:38:33 +00:00
});
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,
2022-04-19 15:59:19 +00:00
type: 1,
2022-02-15 12:38:33 +00:00
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);
2022-02-15 14:53:18 +00:00
group.externalId = u.externalId;
await ctx.store.saveGroup(group.id, { externalId: group.externalId });
2022-02-15 12:38:33 +00:00
return this.response({
status: HttpStatusCode.CREATED,
content: group,
});
}
}