| 1 | import type { KeysOfUnion, UnionToIntersection } from "type-fest" |
| 2 | import type { FlaskBaseResponse } from "@/types/flask.d" |
| 3 | import type { |
| 4 | Case, |
| 5 | CaseComment, |
| 6 | CaseDataStore, |
| 7 | CasePayload, |
| 8 | CaseReportTemplateDataStore, |
| 9 | CasesListResponse, |
| 10 | CaseStatus |
| 11 | } from "@/types/incidentManagement/cases.d" |
| 12 | import { HttpClient } from "../../httpClient" |
| 13 | |
| 14 | export type CasesFilter = |
| 15 | | { status: CaseStatus } |
| 16 | | { assignedTo: string } |
| 17 | | { hostname: string } |
| 18 | | { customerCode: string } |
| 19 | |
| 20 | export type CasesFilterTypes = KeysOfUnion<CasesFilter> |
| 21 | |
| 22 | export interface CasesPaginationParams { |
| 23 | page?: number |
| 24 | pageSize?: number |
| 25 | order?: "asc" | "desc" |
| 26 | } |
| 27 | |
| 28 | export interface CaseReportPayload { |
| 29 | case_id: number |
| 30 | file_name: string |
| 31 | template_name: string |
| 32 | } |
| 33 | |
| 34 | export type CaseCommentPayload = Omit<CaseComment, "id"> |
| 35 | |
| 36 | export type CaseCommentUpdatePayload = Omit<CaseComment, "id"> & { comment_id: number } |
| 37 | |
| 38 | export default { |
| 39 | getCasesList(filters?: Partial<UnionToIntersection<CasesFilter>>, pagination?: CasesPaginationParams) { |
| 40 | let url = `/incidents/db_operations/cases` |
| 41 | |
| 42 | if (filters?.status) { |
| 43 | url = `/incidents/db_operations/case/status/${filters.status}` |
| 44 | } |
| 45 | if (filters?.assignedTo) { |
| 46 | url = `/incidents/db_operations/case/assigned-to/${filters.assignedTo}` |
| 47 | } |
| 48 | if (filters?.customerCode) { |
| 49 | url = `/incidents/db_operations/case/customer/${filters.customerCode}` |
| 50 | } |
| 51 | if (filters?.hostname) { |
| 52 | url = `/agents/${filters.hostname}/cases` |
| 53 | } |
| 54 | |
| 55 | const params: Record<string, number | string> = {} |
| 56 | if (pagination?.page !== undefined) { |
| 57 | params.page = pagination.page |
| 58 | } |
| 59 | if (pagination?.pageSize !== undefined) { |
| 60 | params.page_size = pagination.pageSize |
| 61 | } |
| 62 | if (pagination?.order !== undefined) { |
| 63 | params.order = pagination.order |
| 64 | } |
| 65 | |
| 66 | return HttpClient.get<CasesListResponse>(url, { params }) |
| 67 | }, |
| 68 | getCase(caseId: number) { |
| 69 | return HttpClient.get<FlaskBaseResponse & { cases: Case[] }>(`/incidents/db_operations/case/${caseId}`) |
| 70 | }, |
| 71 | createCase(payload: CasePayload, params: Record<string, number | string | boolean> = {}) { |
| 72 | return HttpClient.post<FlaskBaseResponse & { case: Case }>(`/incidents/db_operations/case/create`, payload, { |
| 73 | params |
| 74 | }) |
| 75 | }, |
| 76 | createCaseFromAlert(alertId: number, params: Record<string, number | string | boolean> = {}) { |
| 77 | return HttpClient.post<FlaskBaseResponse & { case_alert_link: { case_id: number; alert_id: number } }>( |
| 78 | `/incidents/db_operations/case/from-alert`, |
| 79 | { |
| 80 | alert_id: alertId |
| 81 | }, |
| 82 | { params } |
| 83 | ) |
| 84 | }, |
| 85 | /** @deprecated in favor of multiLinkCase */ |
| 86 | linkCase(alertId: number, caseId: number) { |
| 87 | return HttpClient.post<FlaskBaseResponse & { case_alert_link: { case_id: number; alert_id: number } }>( |
| 88 | `/incidents/db_operations/case/alert-link`, |
| 89 | { |
| 90 | alert_id: alertId, |
| 91 | case_id: caseId |
| 92 | } |
| 93 | ) |
| 94 | }, |
| 95 | multiLinkCase(alertIds: number[], caseId: number) { |
| 96 | return HttpClient.post<FlaskBaseResponse & { case_alert_links: { case_id: number; alert_id: number }[] }>( |
| 97 | `/incidents/db_operations/case/alert-links`, |
| 98 | { |
| 99 | alert_ids: alertIds, |
| 100 | case_id: caseId |
| 101 | } |
| 102 | ) |
| 103 | }, |
| 104 | unlinkCase(alertId: number, caseId: number) { |
| 105 | return HttpClient.post<FlaskBaseResponse>(`/incidents/db_operations/case/alert-unlink`, { |
| 106 | alert_id: alertId, |
| 107 | case_id: caseId |
| 108 | }) |
| 109 | }, |
| 110 | updateCaseStatus(caseId: number, status: CaseStatus, force = false) { |
| 111 | return HttpClient.put<FlaskBaseResponse>( |
| 112 | `/incidents/db_operations/case/status`, |
| 113 | { |
| 114 | case_id: caseId, |
| 115 | status |
| 116 | }, |
| 117 | { params: force ? { force: true } : {} } |
| 118 | ) |
| 119 | }, |
| 120 | updateCaseAssignedUser(caseId: number, user: string) { |
| 121 | return HttpClient.put<FlaskBaseResponse>(`/incidents/db_operations/case/assigned-to`, { |
| 122 | case_id: caseId, |
| 123 | assigned_to: user |
| 124 | }) |
| 125 | }, |
| 126 | deleteCase(caseId: number) { |
| 127 | return HttpClient.delete<FlaskBaseResponse>(`/incidents/db_operations/case/${caseId}`) |
| 128 | }, |
| 129 | exportCases(customerCode?: string, year?: number, month?: number) { |
| 130 | let url = `/incidents/report/generate-report-csv` |
| 131 | |
| 132 | if (customerCode) { |
| 133 | url = `/incidents/report/generate-report-csv/${customerCode}` |
| 134 | } |
| 135 | |
| 136 | return HttpClient.post<Blob>(url, null, { |
| 137 | responseType: "blob", |
| 138 | params: { |
| 139 | ...(year !== undefined && month !== undefined ? { year, month } : {}) |
| 140 | } |
| 141 | }) |
| 142 | }, |
| 143 | getCaseDataStoreFiles(caseId: number) { |
| 144 | return HttpClient.get<FlaskBaseResponse & { case_data_store: CaseDataStore[] }>( |
| 145 | `/incidents/db_operations/case/data-store/${caseId}` |
| 146 | ) |
| 147 | }, |
| 148 | downloadCaseDataStoreFile(caseId: number, fileName: string) { |
| 149 | return HttpClient.get<Blob>(`/incidents/db_operations/case/data-store/download/${caseId}/${fileName}`, { |
| 150 | responseType: "blob" |
| 151 | }) |
| 152 | }, |
| 153 | uploadCaseDataStoreFile(caseId: number, file: File) { |
| 154 | const form = new FormData() |
| 155 | form.append("file", new Blob([file], { type: file.type }), file.name) |
| 156 | |
| 157 | return HttpClient.post<FlaskBaseResponse & { case_data_store: CaseDataStore }>( |
| 158 | `/incidents/db_operations/case/data-store/upload`, |
| 159 | form, |
| 160 | { |
| 161 | params: { |
| 162 | case_id: caseId |
| 163 | } |
| 164 | } |
| 165 | ) |
| 166 | }, |
| 167 | deleteCaseDataStoreFile(caseId: number, fileName: string) { |
| 168 | return HttpClient.delete<FlaskBaseResponse>(`/incidents/db_operations/case/data-store/${caseId}/${fileName}`) |
| 169 | }, |
| 170 | getCaseReportTemplate() { |
| 171 | return HttpClient.get<FlaskBaseResponse & { case_report_template_data_store: string[] }>( |
| 172 | `/incidents/db_operations/case-report-template` |
| 173 | ) |
| 174 | }, |
| 175 | uploadDefaultCaseReportTemplate() { |
| 176 | return HttpClient.post<FlaskBaseResponse & { case_report_template_data_store: string[] }>( |
| 177 | `/incidents/db_operations/case-report-template/default-template` |
| 178 | ) |
| 179 | }, |
| 180 | uploadCustomCaseReportTemplate(file: File) { |
| 181 | const form = new FormData() |
| 182 | form.append("file", new Blob([file], { type: file.type }), file.name) |
| 183 | |
| 184 | return HttpClient.post<FlaskBaseResponse & { case_report_template_data_store: CaseReportTemplateDataStore }>( |
| 185 | `/incidents/db_operations/case-report-template/upload`, |
| 186 | form |
| 187 | ) |
| 188 | }, |
| 189 | downloadCaseReportTemplate(fileName: string) { |
| 190 | return HttpClient.get<Blob>(`/incidents/db_operations/case-report-template/download/${fileName}`, { |
| 191 | responseType: "blob" |
| 192 | }) |
| 193 | }, |
| 194 | deleteCaseReportTemplate(fileName: string) { |
| 195 | return HttpClient.delete<FlaskBaseResponse>(`/incidents/db_operations/case-report-template/${fileName}`) |
| 196 | }, |
| 197 | checkDefaultCaseReportTemplateExists() { |
| 198 | return HttpClient.get<FlaskBaseResponse & { default_template_exists: boolean }>( |
| 199 | `/incidents/db_operations/case-report-template/do-default-template-exists` |
| 200 | ) |
| 201 | }, |
| 202 | generateCaseReport(payload: CaseReportPayload, type: "docx" | "pdf") { |
| 203 | const url = type === "docx" ? `/incidents/report/generate-report-docx` : `/incidents/report/generate-report-pdf` |
| 204 | return HttpClient.post<Blob>(url, payload, { |
| 205 | responseType: "blob" |
| 206 | }) |
| 207 | }, |
| 208 | createCaseNotification(caseId: number) { |
| 209 | return HttpClient.post<FlaskBaseResponse>(`/incidents/db_operations/case/notification`, { case_id: caseId }) |
| 210 | }, |
| 211 | newCaseComment(payload: CaseCommentPayload) { |
| 212 | return HttpClient.post<FlaskBaseResponse & { comment: CaseComment }>( |
| 213 | `/incidents/db_operations/case/comment`, |
| 214 | payload |
| 215 | ) |
| 216 | }, |
| 217 | updateCaseComment(payload: CaseCommentUpdatePayload) { |
| 218 | return HttpClient.put<FlaskBaseResponse & { comment: CaseComment }>( |
| 219 | `/incidents/db_operations/case/comment`, |
| 220 | payload |
| 221 | ) |
| 222 | }, |
| 223 | deleteCaseComment(commentId: number) { |
| 224 | return HttpClient.delete<FlaskBaseResponse>(`/incidents/db_operations/case/comment/${commentId}`) |
| 225 | } |
| 226 | } |