/
project_update.ts
183 lines (171 loc) · 5.62 KB
/
project_update.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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 AdditionalData from "./service/domain/additional_data";
import { ServiceUser } from "./service/domain/organization/service_user";
import * as Project from "./service/domain/workflow/project";
import * as ProjectUpdate from "./service/domain/workflow/project_update";
import { extractUser } from "./handlerUtils";
import Joi = require("joi");
/**
* Represents the request body of the endpoint
*/
interface RequestBodyV1 {
apiVersion: "1.0";
data: {
projectId: Project.Id;
displayName?: string;
description?: string;
thumbnail?: string;
additionalData?: object;
tags?: string[];
};
}
const requestBodyV1Schema = Joi.object({
apiVersion: Joi.valid("1.0").required(),
data: Joi.object({
projectId: Project.idSchema.required(),
displayName: Joi.string(),
description: Joi.string().allow(""),
thumbnail: Joi.string().allow(""),
additionalData: AdditionalData.schema,
tags: Joi.array().items(Joi.string()),
}).required(),
});
type RequestBody = RequestBodyV1;
const requestBodySchema = Joi.alternatives([requestBodyV1Schema]);
/**
* Validates the request body of the http request
*
* @param body the request body
* @returns the request body wrapped in a {@link Result.Type}. Contains either the object or an error
*/
function validateRequestBody(body: unknown): Result.Type<RequestBody> {
const { error, value } = requestBodySchema.validate(body);
return !error ? value : error;
}
/**
* Creates the swagger schema for the `/project.update` endpoint
*
* @param server fastify server
* @returns the swagger schema for this endpoint
*/
function mkSwaggerSchema(server: AugmentedFastifyInstance): Object {
return {
preValidation: [server.authenticate],
schema: {
description:
"Partially update a project. Only properties mentioned in the request body are touched, " +
"others are not affected. The assigned user will be notified about the change.",
tags: ["project"],
summary: "Update a project",
security: [
{
bearerToken: [],
},
],
body: {
type: "object",
required: ["apiVersion", "data"],
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
required: ["projectId"],
properties: {
projectId: { type: "string", example: "d0e8c69eg298c87e3899119e025eff1f" },
displayName: { type: "string", example: "townproject" },
description: { type: "string", example: "A town should be built" },
thumbnail: { type: "string", example: "/Thumbnail_0001.jpg" },
additionalData: { type: "object" },
tags: {
type: "array",
items: { type: "string", example: "project1" },
},
},
},
},
},
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
},
},
},
401: NotAuthenticated.schema,
},
},
};
}
/**
* Represents the service that updates projects
*/
interface Service {
updateProject(
ctx: Ctx,
user: ServiceUser,
projectId: Project.Id,
requestData: ProjectUpdate.RequestData,
): Promise<Result.Type<void>>;
}
/**
* Creates an http handler that handles incoming http requests for the `/project.update` 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.post(`${urlPrefix}/project.update`, mkSwaggerSchema(server), (request, reply) => {
const ctx: Ctx = { requestId: request.id, source: "http" };
const user = extractUser(request as AuthenticatedRequest);
const bodyResult = validateRequestBody(request.body);
if (Result.isErr(bodyResult)) {
const { code, body } = toHttpError(new VError(bodyResult, "failed to update project"));
request.log.error({ err: bodyResult }, "Invalid request body");
reply.status(code).send(body);
return;
}
const { projectId } = bodyResult.data;
const reqData = {
displayName: bodyResult.data.displayName,
description: bodyResult.data.description,
thumbnail: bodyResult.data.thumbnail,
additionalData: bodyResult.data.additionalData,
tags: bodyResult.data.tags,
};
service
.updateProject(ctx, user, projectId, reqData)
.then((result) => {
if (Result.isErr(result)) {
throw new VError(result, "project.update failed");
}
const code = 200;
const body = {
apiVersion: "1.0",
data: {},
};
reply.status(code).send(body);
})
.catch((err) => {
const { code, body } = toHttpError(err);
request.log.error({ err }, "Error while updating Project");
reply.status(code).send(body);
});
});
});
}