-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
post_comment.ts
62 lines (59 loc) · 1.79 KB
/
post_comment.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { schema } from '@kbn/config-schema';
import { formatNewComment, wrapError } from './utils';
import { NewCommentSchema } from './schema';
import { RouteDeps } from '.';
import { CASE_SAVED_OBJECT } from '../../constants';
export function initPostCommentApi({ caseService, router }: RouteDeps) {
router.post(
{
path: '/api/cases/{id}/comment',
validate: {
params: schema.object({
id: schema.string(),
}),
body: NewCommentSchema,
},
},
async (context, request, response) => {
let createdBy;
let newComment;
try {
await caseService.getCase({
client: context.core.savedObjects.client,
caseId: request.params.id,
});
} catch (error) {
return response.customError(wrapError(error));
}
try {
createdBy = await caseService.getUser({ request, response });
} catch (error) {
return response.customError(wrapError(error));
}
try {
newComment = await caseService.postNewComment({
client: context.core.savedObjects.client,
attributes: formatNewComment({
newComment: request.body,
...createdBy,
}),
references: [
{
type: CASE_SAVED_OBJECT,
name: `associated-${CASE_SAVED_OBJECT}`,
id: request.params.id,
},
],
});
return response.ok({ body: newComment });
} catch (error) {
return response.customError(wrapError(error));
}
}
);
}