-
Notifications
You must be signed in to change notification settings - Fork 39
/
workflowitem_create.v2.ts
287 lines (271 loc) · 10 KB
/
workflowitem_create.v2.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { MultipartFile } from "@fastify/multipart";
import Joi = require("joi");
import { VError } from "verror";
import { extractUser } from "./handlerUtils";
import { toHttpError } from "./http_errors";
import * as NotAuthenticated from "./http_errors/not_authenticated";
import { AuthenticatedRequest } from "./httpd/lib";
import { Ctx } from "./lib/ctx";
import { safeStringSchema } from "./lib/joiValidation";
import * as Result from "./result";
import { UploadedDocument, uploadedDocumentSchema } from "./service/domain/document/document";
import {
amountTypeSchema,
conversionRateSchema,
moneyAmountSchema,
} from "./service/domain/workflow/money";
import * as Project from "./service/domain/workflow/project";
import * as Subproject from "./service/domain/workflow/subproject";
import Type, { workflowitemTypeSchema } from "./service/domain/workflowitem_types/types";
import * as WorkflowitemCreate from "./service/workflowitem_create";
import { AugmentedFastifyInstance } from "./types";
const parseMultiPartFile = async (part: MultipartFile): Promise<any> => {
const id = "";
const buffer = await part.toBuffer();
// TODO downstream functionality expects base64, but we should work with buffer directly in the future
const base64 = buffer.toString("base64");
const fileName = part.filename;
return { id, base64, fileName };
};
const parseMultiPartRequest = async (request: AuthenticatedRequest): Promise<any> => {
let data = {};
let uploadedDocuments: any[] = [];
const parts = request.parts();
for await (const part of parts) {
if (part.type === "file") {
uploadedDocuments.push(await parseMultiPartFile(part));
} else {
if (part.fieldname === "apiVersion") {
continue;
} else if (part.fieldname === "tags") {
if (part.value === "") {
data[part.fieldname] = [];
} else {
data[part.fieldname] = (part.value as string).split(",");
}
continue;
}
if (part.value === "null") {
data[part.fieldname] = undefined;
continue;
}
data[part.fieldname] = part.value;
}
}
data["documents"] = uploadedDocuments;
return data;
};
/**
* Represents the request body of the endpoint
*/
interface CreateWorkflowV2RequestBody {
apiVersion: "2.0";
data: {
projectId: Project.Id;
subprojectId: Subproject.Id;
status?: "open" | "closed";
displayName: string;
description?: string;
assignee?: string;
currency?: string;
amount?: string;
amountType: "N/A" | "disbursed" | "allocated";
billingDate?: string;
dueDate?: string;
exchangeRate?: string;
documents?: UploadedDocument[];
additionalData?: object;
workflowitemType?: Type;
tags?: string[];
};
}
const requestBodyV2Schema = Joi.object({
apiVersion: Joi.valid("2.0").required(),
data: Joi.object({
projectId: Project.idSchema,
subprojectId: Subproject.idSchema,
status: Joi.valid("open"),
displayName: safeStringSchema.required(),
description: safeStringSchema.allow(""),
assignee: safeStringSchema,
currency: safeStringSchema,
amount: moneyAmountSchema,
amountType: amountTypeSchema.required(),
billingDate: safeStringSchema,
dueDate: Joi.string().allow("").optional().isoDate(),
exchangeRate: conversionRateSchema,
documents: Joi.array().items(uploadedDocumentSchema),
additionalData: Joi.object(),
workflowitemType: workflowitemTypeSchema,
tags: Joi.array().items(safeStringSchema),
}).required(),
});
type RequestBody = CreateWorkflowV2RequestBody;
const requestBodySchema = requestBodyV2Schema;
/**
* 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, { stripUnknown: true });
return !error ? value : error;
}
/**
* Creates the swagger schema for the `/v2/subproject.createWorkflowitem` endpoint
*
* @param server fastify server
* @returns the swagger schema for this endpoint
*/
function mkSwaggerSchema(server: AugmentedFastifyInstance): Object {
return {
preValidation: [server.authenticate],
schema: {
description:
"Create a workflowitem, upload related files, and associate it to the given subproject.\n.\n" +
"Request body must be multipart/form-data. The following fields are required:\n\n" +
"- `projectId` (string): the id of the project\n" +
"- `subprojectId` (string): the id of the subproject\n" +
"- `displayName` (string): the name of the workflowitem\n" +
"- `amountType` (string): the type of the amount\n\n" +
"The following fields are optional:\n\n" +
"- `status` (string): the status of the workflowitem\n" +
"- `description` (string): the description of the workflowitem\n" +
"- `assignee` (string): the assignee of the workflowitem\n" +
"- `currency` (string): the currency of the workflowitem\n" +
"- `amount` (string): the amount of the workflowitem\n" +
"- `billingDate` (string): the billing date of the workflowitem\n" +
"- `dueDate` (string): the due date of the workflowitem\n" +
"- `exchangeRate` (string): the exchange rate of the workflowitem\n" +
"- `documents` (array): an array of documents to be uploaded\n" +
"- `additionalData` (object): additional data\n" +
"- `workflowitemType` (string): the type of the workflowitem\n" +
"- `tags` (array): an array of tags\n\n" +
"Note that the only possible values for 'amountType' are: 'disbursed', 'allocated', 'N/A'\n.\n" +
"The only possible values for 'status' are: 'open' and 'closed'\n\n",
tags: ["subproject", "v2"],
summary: "Create a workflowitem",
security: [
{
bearerToken: [],
},
],
consumes: ["multipart/form-data"],
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "2.0" },
data: {
type: "object",
properties: {
project: {
type: "object",
properties: {
id: { type: "string" },
},
},
subproject: {
type: "object",
properties: {
id: { type: "string" },
},
},
workflowitem: {
type: "object",
properties: {
id: { type: "string" },
documents: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string", example: "05c22b92-abb1-4f7c-8f6f-7ff5e8a2bfd3" },
fileName: { type: "string", example: "test-document" },
},
},
},
},
},
},
},
},
},
401: NotAuthenticated.schema,
},
},
};
}
/**
* Creates an http handler that handles incoming http requests for the `/v2/subproject.createWorkflowitem` 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: WorkflowitemCreate.Service,
): void {
server.register(async function () {
server.post(
`${urlPrefix}/v2/subproject.createWorkflowitem`,
mkSwaggerSchema(server),
async (request: AuthenticatedRequest, reply) => {
let body = {
apiVersion: "2.0",
data: await parseMultiPartRequest(request),
};
const ctx: Ctx = { requestId: request.id, source: "http" };
const user = extractUser(request as AuthenticatedRequest);
const bodyResult = validateRequestBody(body);
if (Result.isErr(bodyResult)) {
const { code, body } = toHttpError(
new VError(bodyResult, "failed to create workflowitem"),
);
request.log.error({ err: bodyResult }, "Invalid request body");
return reply.status(code).send(body);
}
const reqData: WorkflowitemCreate.RequestData = {
projectId: bodyResult.data.projectId,
subprojectId: bodyResult.data.subprojectId,
status: bodyResult.data.status,
displayName: bodyResult.data.displayName,
description: bodyResult.data.description,
assignee: bodyResult.data.assignee,
currency: bodyResult.data.currency,
amount: bodyResult.data.amount,
amountType: bodyResult.data.amountType,
billingDate: bodyResult.data.billingDate,
dueDate: bodyResult.data.dueDate,
exchangeRate: bodyResult.data.exchangeRate,
additionalData: bodyResult.data.additionalData,
documents: bodyResult.data.documents,
workflowitemType: bodyResult.data.workflowitemType,
tags: bodyResult.data.tags,
};
try {
const resourceIdsResult = await service.createWorkflowitem(ctx, user, reqData);
if (Result.isErr(resourceIdsResult)) {
throw new VError(resourceIdsResult, "v2/subproject.createWorkflowitem failed");
}
const code = 200;
const body = {
apiVersion: "2.0",
data: {
...resourceIdsResult,
},
};
reply.status(code).send(body);
} catch (err) {
const { code, body } = toHttpError(err);
reply.status(code).send(body);
request.log.error({ err }, "Error while creating Workflowitem");
}
},
);
});
}