main
ts 227 lines 6.42 KB
Raw
1 import type { KeysOfUnion, UnionToIntersection } from "type-fest"
2 import type { FlaskBaseResponse } from "@/types/flask.d"
3 import type {
4 Alert,
5 AlertComment,
6 AlertContext,
7 AlertDetails,
8 AlertsFilter,
9 AlertStatus,
10 AlertTag,
11 AlertTimeline
12 } from "@/types/incidentManagement/alerts.d"
13 import _castArray from "lodash/castArray"
14 import { HttpClient } from "../../httpClient"
15
16 export type AlertsListFilterValue = string | string[] | AlertStatus | null
17 export type AlertsFilterTypes = KeysOfUnion<AlertsFilter>
18
19 export interface AlertsQuery {
20 page: number
21 pageSize: number
22 sort: "asc" | "desc"
23 filter: Partial<UnionToIntersection<AlertsFilter>>
24 filters: {
25 type: AlertsFilterTypes
26 value: AlertsListFilterValue
27 }[]
28 }
29
30 export type AlertCommentPayload = Omit<AlertComment, "id">
31
32 export type AlertCommentUpdatePayload = Omit<AlertComment, "id"> & { comment_id: number }
33
34 export interface AlertIocPayload {
35 alert_id: number
36 ioc_value: string
37 ioc_type: string
38 ioc_description: string
39 }
40
41 // TODO-FE: refactor
42 export default {
43 getAlertsList(args: Partial<AlertsQuery>, signal?: AbortSignal) {
44 let url = `/incidents/db_operations/alerts`
45
46 if (args?.filter?.status) {
47 url = `/incidents/db_operations/alerts/status/${args.filter.status}`
48 }
49 if (args?.filter?.assetName) {
50 url = `/incidents/db_operations/alerts/asset/${args.filter.assetName}`
51 }
52 if (args?.filter?.assignedTo) {
53 url = `/incidents/db_operations/alerts/assigned-to/${args.filter.assignedTo}`
54 }
55 if (args?.filter?.tag) {
56 url = `/incidents/db_operations/alert/tag/${_castArray(args.filter.tag).join(",")}`
57 }
58 if (args?.filter?.title) {
59 url = `/incidents/db_operations/alerts/title/${args.filter.title}`
60 }
61 if (args?.filter?.customerCode) {
62 url = `/incidents/db_operations/alerts/customer/${args.filter.customerCode}`
63 }
64 if (args?.filter?.source) {
65 url = `/incidents/db_operations/alerts/source/${args.filter.source}`
66 }
67
68 // TODO-FE: remove any
69 const params: any = {
70 page: args.page || 1,
71 page_size: args.pageSize || 25,
72 order: args.sort || "desc"
73 }
74
75 if (args.filters?.length) {
76 for (const filter of args.filters) {
77 if (filter.value?.length) {
78 switch (filter.type) {
79 case "assignedTo":
80 params.assigned_to = filter.value
81 break
82 case "title":
83 params.alert_title = filter.value
84 break
85 case "customerCode":
86 params.customer_code = filter.value
87 break
88 case "source":
89 params.source = filter.value
90 break
91 case "assetName":
92 params.asset_name = filter.value
93 break
94 case "iocValue":
95 params.ioc_value = filter.value
96 break
97 case "status":
98 params.status = filter.value
99 break
100 case "tag":
101 params.tags = _castArray(filter.value)
102 break
103 default:
104 params[filter.type] = filter.value
105 break
106 }
107 }
108 }
109
110 url = `/incidents/db_operations/alerts/filter`
111 }
112
113 return HttpClient.get<
114 FlaskBaseResponse & {
115 alerts: Alert[]
116 closed: number
117 in_progress: number
118 open: number
119 total: number
120 total_filtered: number
121 }
122 >(url, {
123 params,
124 paramsSerializer: {
125 indexes: null // remove brackets in array types
126 },
127 signal
128 })
129 },
130 getAlert(alertId: number) {
131 return HttpClient.get<FlaskBaseResponse & { alerts: Alert[] }>(`/incidents/db_operations/alert/${alertId}`)
132 },
133 getAlertDetails(indexId: string, indexName: string) {
134 return HttpClient.post<FlaskBaseResponse & { alert_details: AlertDetails }>(`/incidents/alerts/alert/details`, {
135 index_id: indexId,
136 index_name: indexName
137 })
138 },
139 getAlertTimeline(indexId: string, indexName: string) {
140 return HttpClient.post<FlaskBaseResponse & { alert_timeline: AlertTimeline[] }>(
141 `/incidents/alerts/alert/timeline`,
142 {
143 index_id: indexId,
144 index_name: indexName
145 }
146 )
147 },
148 getAvailableUsers() {
149 return HttpClient.get<FlaskBaseResponse & { available_users: string[] }>(
150 `/incidents/db_operations/alert/available-users`
151 )
152 },
153 updateAlertStatus(alertId: number, status: AlertStatus) {
154 return HttpClient.put<FlaskBaseResponse>(`/incidents/db_operations/alert/status`, {
155 alert_id: alertId,
156 status
157 })
158 },
159 updateAlertAssignedUser(alertId: number, user: string) {
160 return HttpClient.put<FlaskBaseResponse>(`/incidents/db_operations/alert/assigned-to`, {
161 alert_id: alertId,
162 assigned_to: user
163 })
164 },
165 deleteAlertTag(alertId: number, tagId: number) {
166 return HttpClient.delete<FlaskBaseResponse & { alert_tag: AlertTag }>(`/incidents/db_operations/alert/tag`, {
167 data: { alert_id: alertId, tag_id: tagId }
168 })
169 },
170 deleteAlert(alertId: number) {
171 return HttpClient.delete<FlaskBaseResponse>(`/incidents/db_operations/alert/${alertId}`)
172 },
173 deleteAlerts(alertIds: number[]) {
174 return HttpClient.delete<FlaskBaseResponse & { deleted_alert_ids: number[]; not_deleted_alert_ids: [] }>(
175 `/incidents/db_operations/alerts`,
176 {
177 data: { alert_ids: alertIds }
178 }
179 )
180 },
181 deleteAlertsByTitle(titleFilter: string) {
182 return HttpClient.delete<
183 FlaskBaseResponse & {
184 deleted_alert_ids: number[]
185 not_deleted_alert_ids: number[]
186 message: string
187 }
188 >(`/incidents/db_operations/alerts/by-title/${encodeURIComponent(titleFilter)}`)
189 },
190 getAlertContext(alertContextId: number) {
191 return HttpClient.get<FlaskBaseResponse & { alert_context: AlertContext }>(
192 `/incidents/db_operations/alert/context/${alertContextId}`
193 )
194 },
195 newAlertComment(payload: AlertCommentPayload) {
196 return HttpClient.post<FlaskBaseResponse & { comment: AlertComment }>(
197 `/incidents/db_operations/alert/comment`,
198 payload
199 )
200 },
201 updateAlertComment(payload: AlertCommentUpdatePayload) {
202 return HttpClient.put<FlaskBaseResponse & { comment: AlertComment }>(
203 `/incidents/db_operations/alert/comment`,
204 payload
205 )
206 },
207 deleteAlertComment(commentId: number) {
208 return HttpClient.delete<FlaskBaseResponse>(`/incidents/db_operations/alert/comment/${commentId}`)
209 },
210 newAlertTag(alertId: number, tag: string) {
211 return HttpClient.post<FlaskBaseResponse & { alert_tag: AlertTag }>(`/incidents/db_operations/alert/tag`, {
212 alert_id: alertId,
213 tag
214 })
215 },
216 createAlertIoc(payload: AlertIocPayload) {
217 return HttpClient.post<FlaskBaseResponse & { alert_ioc: { alert_id: number; ioc_id: number } }>(
218 `/incidents/db_operations/alert/ioc`,
219 payload
220 )
221 },
222 deleteAlertIoc(alertId: number, iocId: number) {
223 return HttpClient.delete<FlaskBaseResponse>(`/incidents/db_operations/alert/ioc`, {
224 data: { alert_id: alertId, ioc_id: iocId }
225 })
226 }
227 }