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,
|
|
|
|
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);
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|