/
group_list.ts
134 lines (128 loc) · 4.09 KB
/
group_list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { AugmentedFastifyInstance } from "./types";
import { VError } from "verror";
import { AuthenticatedRequest } from "./httpd/lib";
import { toHttpError } from "./http_errors";
import * as NotAuthenticated from "./http_errors/not_authenticated";
import { Ctx } from "./lib/ctx";
import * as Result from "./result";
import * as Group from "./service/domain/organization/group";
import { ServiceUser } from "./service/domain/organization/service_user";
import * as UserRecord from "./service/domain/organization/user_record";
/**
* Creates the swagger schema for the `/group.list` endpoint
*
* @param server fastify server
* @returns the swagger schema for this endpoint
*/
function mkSwaggerSchema(server: AugmentedFastifyInstance): Object {
return {
preValidation: [server.authenticate],
schema: {
description: "List all user groups.",
tags: ["group"],
summary: "List all existing groups",
security: [
{
bearerToken: [],
},
],
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
properties: {
groups: {
type: "array",
items: {
type: "object",
properties: {
groupId: { type: "string", example: "Manager" },
displayName: { type: "string", example: "All Manager Group" },
users: {
type: "array",
items: { type: "string", example: "aSmith" },
},
permissions: { type: "object", additionalProperties: true },
},
},
},
},
},
},
},
401: NotAuthenticated.schema,
},
},
};
}
/**
* Represents the type of the group that will be returned in a list
* @notExported
*/
interface ExposedGroup {
groupId: string;
displayName: string;
users: UserRecord.Id[];
}
/**
* Represents the service that handles listing of groups
*/
interface Service {
listGroups(ctx: Ctx, user: ServiceUser): Promise<Result.Type<Group.Group[]>>;
}
/**
* Creates an http handler that handles incoming http requests for the `/group.list` route
*
* @param server the current fastify server instance
* @param urlPrefix the prefix of the http url
* @param service the service {@link Service} object used to offer an interface to the domain logic
*/
export function addHttpHandler(
server: AugmentedFastifyInstance,
urlPrefix: string,
service: Service,
): void {
server.register(async function () {
server.get(`${urlPrefix}/group.list`, mkSwaggerSchema(server), (request, reply) => {
const ctx: Ctx = { requestId: request.id, source: "http" };
const issuer: ServiceUser = {
id: (request as AuthenticatedRequest).user.userId,
groups: (request as AuthenticatedRequest).user.groups,
address: (request as AuthenticatedRequest).user.address,
};
service
.listGroups(ctx, issuer)
.then((groupResult: Result.Type<Group.Group[]>) => {
if (Result.isErr(groupResult)) throw new VError(groupResult, "group.list failed");
const groups = groupResult;
return groups.map((group) => {
return {
groupId: group.id,
displayName: group.displayName,
users: group.members,
permissions: group.permissions,
};
});
})
.then((groups: ExposedGroup[]) => {
const code = 200;
const body = {
apiVersion: "1.0",
data: {
groups,
},
};
reply.status(code).send(body);
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while fetching all groups");
reply.status(code).send(body);
});
});
});
}