-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
index.ts
77 lines (70 loc) · 2.39 KB
/
index.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
/*
* 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 {
SavedObjectsFindResponse,
Logger,
SavedObjectsBulkResponse,
SavedObjectReference,
} from 'kibana/server';
import { CaseUserActionAttributes } from '../../../common/api';
import { CASE_USER_ACTION_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../saved_object_types';
import { ClientArgs } from '..';
interface GetCaseUserActionArgs extends ClientArgs {
caseId: string;
}
export interface UserActionItem {
attributes: CaseUserActionAttributes;
references: SavedObjectReference[];
}
interface PostCaseUserActionArgs extends ClientArgs {
actions: UserActionItem[];
}
export interface CaseUserActionServiceSetup {
getUserActions(
args: GetCaseUserActionArgs
): Promise<SavedObjectsFindResponse<CaseUserActionAttributes>>;
postUserActions(
args: PostCaseUserActionArgs
): Promise<SavedObjectsBulkResponse<CaseUserActionAttributes>>;
}
export class CaseUserActionService {
constructor(private readonly log: Logger) {}
public setup = async (): Promise<CaseUserActionServiceSetup> => ({
getUserActions: async ({ client, caseId }: GetCaseUserActionArgs) => {
try {
const caseUserActionInfo = await client.find({
type: CASE_USER_ACTION_SAVED_OBJECT,
fields: [],
hasReference: { type: CASE_SAVED_OBJECT, id: caseId },
page: 1,
perPage: 1,
});
return await client.find({
type: CASE_USER_ACTION_SAVED_OBJECT,
hasReference: { type: CASE_SAVED_OBJECT, id: caseId },
page: 1,
perPage: caseUserActionInfo.total,
sortField: 'action_at',
sortOrder: 'asc',
});
} catch (error) {
this.log.debug(`Error on GET case user action: ${error}`);
throw error;
}
},
postUserActions: async ({ client, actions }: PostCaseUserActionArgs) => {
try {
this.log.debug(`Attempting to POST a new case user action`);
return await client.bulkCreate(
actions.map((action) => ({ type: CASE_USER_ACTION_SAVED_OBJECT, ...action }))
);
} catch (error) {
this.log.debug(`Error on POST a new case user action: ${error}`);
throw error;
}
},
});
}