diff --git a/package-lock.json b/package-lock.json index 77e814107..666d35cd7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@mui/x-date-pickers": "^7.11.0", "@redux-devtools/extension": "^3.3.0", "@reduxjs/toolkit": "^2.2.6", + "async-mutex": "^0.5.0", "browser-image-compression": "^2.0.2", "chart.js": "^3.9.1", "classnames": "^2.2.6", @@ -11853,6 +11854,21 @@ "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==", "dev": true }, + "node_modules/async-mutex": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/async-mutex/-/async-mutex-0.5.0.tgz", + "integrity": "sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==", + "license": "MIT", + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/async-mutex/node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "license": "0BSD" + }, "node_modules/asynciterator.prototype": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/asynciterator.prototype/-/asynciterator.prototype-1.0.0.tgz", diff --git a/package.json b/package.json index 46eef8123..5b852b501 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@mui/x-date-pickers": "^7.11.0", "@redux-devtools/extension": "^3.3.0", "@reduxjs/toolkit": "^2.2.6", + "async-mutex": "^0.5.0", "browser-image-compression": "^2.0.2", "chart.js": "^3.9.1", "classnames": "^2.2.6", diff --git a/src/consts.ts b/src/consts.ts index f9a522ec1..753d620e9 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -2,6 +2,7 @@ export const AUTH_KEY = "auth"; export const PERMISSION_KEY = "permission"; export const MOBILE_BREAKPOINT = 768; export const TOKEN_EXPIRATION_TIMEOUT = 60000; +export const REFRESH_TOKEN_EXPIRATION_TIMEOUT = 0; export const PATHS = { home: "/", diff --git a/src/libraries/apiUtils/applyTokenMiddleware.ts b/src/libraries/apiUtils/applyTokenMiddleware.ts index 937c89e87..a7761f888 100644 --- a/src/libraries/apiUtils/applyTokenMiddleware.ts +++ b/src/libraries/apiUtils/applyTokenMiddleware.ts @@ -2,16 +2,20 @@ import produce from "immer"; import { set } from "lodash"; import { AUTH_KEY } from "../../consts"; import { Middleware, RequestArgs } from "../../generated"; +import { refreshTokenHasExpired } from "../authUtils/tokenHasExpired"; import { SessionStorage } from "../storage/storage"; -import { redirect } from "react-router"; -import { tokenHasExpired } from "../authUtils/tokenHasExpired"; export const applyTokenMiddleware: Middleware = { pre(request: RequestArgs): RequestArgs { const userCredentials = SessionStorage.read(AUTH_KEY); - if (userCredentials.token && tokenHasExpired(userCredentials.token)) { + if ( + userCredentials?.refreshToken && + refreshTokenHasExpired(userCredentials.refreshToken) + ) { SessionStorage.clear(); - return redirect("/login"); + } + if (!userCredentials) { + window.location.reload(); } return produce(request, (draft) => { if (userCredentials.token) { diff --git a/src/libraries/apiUtils/wrapper.ts b/src/libraries/apiUtils/wrapper.ts new file mode 100644 index 000000000..66038fe22 --- /dev/null +++ b/src/libraries/apiUtils/wrapper.ts @@ -0,0 +1,47 @@ +import { Mutex } from "async-mutex"; +import { AUTH_KEY } from "consts"; +import { LoginApi } from "generated"; +import { saveAuthenticationDataToSession } from "libraries/authUtils/saveAuthenticationDataToSession"; +import { + refreshTokenHasExpired, + tokenHasExpired, +} from "libraries/authUtils/tokenHasExpired"; +import { SessionStorage } from "libraries/storage/storage"; +import { Observable, from, throwError } from "rxjs"; +import { catchError, delay, switchMap, tap } from "rxjs/operators"; +import { customConfiguration } from "./configuration"; + +const mutex = new Mutex(); + +const loginApi = new LoginApi(customConfiguration(false)); + +export function wrapper(callback: () => Observable): Observable { + return callback().pipe( + catchError((error) => { + if (error.status === 401) { + const refreshToken = SessionStorage.read(AUTH_KEY)?.refreshToken; + if (refreshToken && !refreshTokenHasExpired(refreshToken)) { + return from( + mutex.runExclusive(async () => { + const token = SessionStorage.read(AUTH_KEY)?.token; + if (token && !tokenHasExpired(token)) { + return callback().toPromise(); + } + return loginApi + .refreshToken({ + tokenRefreshRequest: { refreshToken }, + }) + .pipe( + tap(saveAuthenticationDataToSession), + delay(500), + switchMap(() => callback()) + ) + .toPromise(); + }) + ); + } + } + return throwError(error); + }) + ); +} diff --git a/src/libraries/authUtils/tokenHasExpired.ts b/src/libraries/authUtils/tokenHasExpired.ts index 64f1671ed..e5d6d998b 100644 --- a/src/libraries/authUtils/tokenHasExpired.ts +++ b/src/libraries/authUtils/tokenHasExpired.ts @@ -1,5 +1,8 @@ import jwtDecode from "jwt-decode"; -import { TOKEN_EXPIRATION_TIMEOUT } from "../../consts"; +import { + REFRESH_TOKEN_EXPIRATION_TIMEOUT, + TOKEN_EXPIRATION_TIMEOUT, +} from "../../consts"; export interface JwtTokenModel { exp: number; @@ -12,3 +15,9 @@ export const tokenHasExpired = (token: any): boolean => { const expirationTime = exp * 1000 - TOKEN_EXPIRATION_TIMEOUT; return Date.now() >= expirationTime; }; + +export const refreshTokenHasExpired = (token: any): boolean => { + const { exp } = jwtDecode(token); + const expirationTime = exp * 1000 - REFRESH_TOKEN_EXPIRATION_TIMEOUT; + return Date.now() >= expirationTime; +}; diff --git a/src/routes/MainRouter.tsx b/src/routes/MainRouter.tsx index 475b82bef..1eaec9331 100644 --- a/src/routes/MainRouter.tsx +++ b/src/routes/MainRouter.tsx @@ -2,15 +2,15 @@ import { useAppDispatch, useAppSelector } from "libraries/hooks/redux"; import React, { useEffect } from "react"; import { Navigate, Route, Routes } from "react-router"; import { BrowserRouter } from "react-router-dom"; +import { Private } from "../components/Private"; import Dashboard from "../components/accessories/dashboard/Dashboard"; +import PermissionDenied from "../components/activities/PermissionDenied/PermissionDenied"; import ForgotActivity from "../components/activities/forgotActivity/ForgotActivity"; import LaboratoryActivity from "../components/activities/laboratoryActivity/LaboratoryActivity"; import LoginActivity from "../components/activities/loginActivity/LoginActivity"; import { RedirectAfterLogin } from "../components/activities/loginActivity/RedirectAfterLogin"; import NotFound from "../components/activities/notFound/NotFound"; -import PermissionDenied from "../components/activities/PermissionDenied/PermissionDenied"; import VisitsActivity from "../components/activities/visitsActivity/VisitsActivity"; -import { Private } from "../components/Private"; import { PATHS } from "../consts"; import { withPermission } from "../libraries/permissionUtils/withPermission"; import { getUserSettings } from "../state/main"; diff --git a/src/state/admissions/thunk.ts b/src/state/admissions/thunk.ts index be6478de6..9efda4636 100644 --- a/src/state/admissions/thunk.ts +++ b/src/state/admissions/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { AdmissionDTO, AdmissionsApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new AdmissionsApi(customConfiguration()); export const createAdmission = createAsyncThunk( "admissions/CREATE_ADMISSION", async (admissionDTO: AdmissionDTO, thunkApi) => - api - .newAdmissions({ admissionDTO }) + wrapper(() => api.newAdmissions({ admissionDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const createAdmission = createAsyncThunk( export const updateAdmission = createAsyncThunk( "admissions/UPDATE_ADMISSION", async (admissionDTO: AdmissionDTO, thunkApi) => - api - .updateAdmissions({ admissionDTO }) + wrapper(() => api.updateAdmissions({ admissionDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -28,8 +27,7 @@ export const dischargePatient = createAsyncThunk( payload: { patientCode: number; admissionDTO: AdmissionDTO }, thunkApi ) => - api - .dischargePatient(payload) + wrapper(() => api.dischargePatient(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -40,12 +38,13 @@ export const getDischarges = createAsyncThunk( payload: { dischargerange: string[]; page?: number; size?: number }, thunkApi ) => - api - .getDischarges({ + wrapper(() => + api.getDischarges({ ...payload, page: payload.page ?? 0, size: payload.size ?? 80, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -56,12 +55,13 @@ export const getAdmissions = createAsyncThunk( payload: { admissionrange: string[]; page?: number; size?: number }, thunkApi ) => - api - .getAdmissions({ + wrapper(() => + api.getAdmissions({ ...payload, page: payload.page ?? 0, size: payload.size ?? 80, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -69,8 +69,7 @@ export const getAdmissions = createAsyncThunk( export const getPatientAdmissions = createAsyncThunk( "admissions/GET_PATIENT_ADMISSIONS", async (payload: { patientCode: number }, thunkApi) => - api - .getAdmissions1(payload) + wrapper(() => api.getAdmissions1(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -85,8 +84,7 @@ export const getAdmittedPatients = createAsyncThunk( }, thunkApi ) => - api - .getAdmittedPatients(payload) + wrapper(() => api.getAdmittedPatients(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -94,8 +92,7 @@ export const getAdmittedPatients = createAsyncThunk( export const getCurrentAdmission = createAsyncThunk( "admissions/getCurrentAdmission", async (patientCode: number | undefined, thunkApi) => - api - .getCurrentAdmission({ patientCode: patientCode ?? -1 }) + wrapper(() => api.getCurrentAdmission({ patientCode: patientCode ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/bills/thunk.ts b/src/state/bills/thunk.ts index 845b4b655..06a57496a 100644 --- a/src/state/bills/thunk.ts +++ b/src/state/bills/thunk.ts @@ -1,4 +1,8 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; +import { Observable, forkJoin, of } from "rxjs"; +import { catchError, map, switchMap } from "rxjs/operators"; +import { TFilterValues } from "../../components/accessories/billTable/types"; import { BillDTO, BillItemsDTO, @@ -8,9 +12,6 @@ import { UpdateBillRequest, } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; -import { TFilterValues } from "../../components/accessories/billTable/types"; -import { forkJoin, Observable, of } from "rxjs"; -import { catchError, map, switchMap } from "rxjs/operators"; const api = new BillsApi(customConfiguration()); @@ -18,9 +19,11 @@ const getPayments = (bills: BillDTO[]): Observable => { if (bills.length === 0) return of([]); const fbills = forkJoin( bills.map((bill: BillDTO) => { - const obs = api.getPaymentsByBillId({ - billId: bill.id ? bill.id : 0, - }); + const obs = wrapper(() => + api.getPaymentsByBillId({ + billId: bill.id ? bill.id : 0, + }) + ); return obs.pipe( map((payments) => { return { @@ -40,9 +43,11 @@ const getItems = (bills: FullBillDTO[]): Observable => { if (bills.length === 0) return of([]); const fbills = forkJoin( bills.map((fbill: FullBillDTO) => { - const obs = api.getItems({ - billId: fbill?.bill?.id ? fbill.bill.id : 0, - }); + const obs = wrapper(() => + api.getItems({ + billId: fbill?.bill?.id ? fbill.bill.id : 0, + }) + ); return obs.pipe( map((items) => { return { @@ -62,8 +67,7 @@ const getItems = (bills: FullBillDTO[]): Observable => { export const newBill = createAsyncThunk( "bills/newBill", async (fullBillDTO: FullBillDTO, thunkApi) => - api - .newBill({ fullBillDTO }) + wrapper(() => api.newBill({ fullBillDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -71,8 +75,7 @@ export const newBill = createAsyncThunk( export const updateBill = createAsyncThunk( "bills/updateBill", async (payload: UpdateBillRequest, thunkApi) => - api - .updateBill(payload) + wrapper(() => api.updateBill(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -80,8 +83,7 @@ export const updateBill = createAsyncThunk( export const getBill = createAsyncThunk( "bills/getBill", async (id: number, thunkApi) => - api - .getBill({ id }) + wrapper(() => api.getBill({ id })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -89,8 +91,7 @@ export const getBill = createAsyncThunk( export const getPendingBills = createAsyncThunk( "bills/getPendingBills", async (patientCode: number, thunkApi) => - api - .getPendingBills({ patientCode }) + wrapper(() => api.getPendingBills({ patientCode })) .pipe( switchMap((bills) => getPayments(bills)), catchError(() => of([])) @@ -106,12 +107,13 @@ export const getPendingBills = createAsyncThunk( export const searchBills = createAsyncThunk( "bills/searchBills", async (filter: TFilterValues, thunkApi) => - api - .searchBills1({ + wrapper(() => + api.searchBills1({ datefrom: filter.fromDate, dateto: filter.toDate, patientCode: filter.patientCode, }) + ) .pipe( switchMap((bills) => getPayments(bills)), catchError(() => of([])) @@ -127,12 +129,13 @@ export const searchBills = createAsyncThunk( export const searchPayments = createAsyncThunk( "bills/searchPayments", async (filter: TFilterValues, thunkApi) => - api - .searchBillsPayments({ + wrapper(() => + api.searchBillsPayments({ datefrom: filter.fromDate, dateto: filter.toDate, patientCode: filter.patientCode, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -140,10 +143,11 @@ export const searchPayments = createAsyncThunk( export const deleteBill = createAsyncThunk( "bills/deleteBill", async (id: number | undefined, thunkApi) => - api - .deleteBill({ + wrapper(() => + api.deleteBill({ id: id ?? -1, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -151,11 +155,12 @@ export const deleteBill = createAsyncThunk( export const payBill = createAsyncThunk( "bills/payBill", async (payload: BillPaymentsDTO, thunkApi) => - api - .updateBill({ + wrapper(() => + api.updateBill({ id: payload.billId, fullBillDTO: { billPayments: [payload] } as any, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -163,8 +168,7 @@ export const payBill = createAsyncThunk( export const closeBill = createAsyncThunk( "bills/closeBill", async ({ id, bill }: { id: number; bill: BillDTO }, thunkApi) => - api - .updateBill({ id, fullBillDTO: { bill: bill } as any }) + wrapper(() => api.updateBill({ id, fullBillDTO: { bill: bill } as any })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -172,12 +176,13 @@ export const closeBill = createAsyncThunk( export const getBillsByYear = createAsyncThunk( "bills/getBillsByYear", async (year: number, thunkApi) => - api - .searchBills1({ + wrapper(() => + api.searchBills1({ datefrom: new Date(year, 0, 1).toISOString(), dateto: new Date(year, 11, 31).toISOString(), patientCode: 0, }) + ) .pipe( switchMap((bills) => getPayments(bills)), catchError(() => of([])) diff --git a/src/state/diseases/thunk.ts b/src/state/diseases/thunk.ts index 8b7079950..8abffbf16 100644 --- a/src/state/diseases/thunk.ts +++ b/src/state/diseases/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { DiseaseDTO, DiseasesApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new DiseasesApi(customConfiguration()); export const getAllDiseases = createAsyncThunk( "diseases/getAllDiseases", async (_, thunkApi) => - api - .getAllDiseases() + wrapper(() => api.getAllDiseases()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getAllDiseases = createAsyncThunk( export const getDiseasesOpd = createAsyncThunk( "diseases/getDiseasesOpd", async (_, thunkApi) => - api - .getDiseasesOpd() + wrapper(() => api.getDiseasesOpd()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const getDiseasesOpd = createAsyncThunk( export const getDiseasesIpdIn = createAsyncThunk( "diseases/getDiseasesIpdIn", async (_, thunkApi) => - api - .getDiseasesIpdIn() + wrapper(() => api.getDiseasesIpdIn()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const getDiseasesIpdIn = createAsyncThunk( export const getDiseasesIpdOut = createAsyncThunk( "diseases/getDiseasesIpdOut", async (_, thunkApi) => - api - .getDiseasesIpdOut() + wrapper(() => api.getDiseasesIpdOut()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -43,8 +40,7 @@ export const getDiseasesIpdOut = createAsyncThunk( export const createDisease = createAsyncThunk( "diseases/createDisease", async (diseaseDTO: DiseaseDTO, thunkApi) => - api - .newDisease({ diseaseDTO }) + wrapper(() => api.newDisease({ diseaseDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -52,8 +48,7 @@ export const createDisease = createAsyncThunk( export const updateDisease = createAsyncThunk( "diseases/updateDisease", async (diseaseDTO: DiseaseDTO, thunkApi) => - api - .updateDisease({ diseaseDTO }) + wrapper(() => api.updateDisease({ diseaseDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/examinations/thunk.ts b/src/state/examinations/thunk.ts index 7fb6dd0cc..47855ddb2 100644 --- a/src/state/examinations/thunk.ts +++ b/src/state/examinations/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { ExaminationsApi, PatientExaminationDTO } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new ExaminationsApi(customConfiguration()); export const examinationsByPatientId = createAsyncThunk( "examinations/examinationsByPatientId", async (patId: number | undefined, thunkApi) => - api - .getByPatientId({ patId: patId ?? -1 }) + wrapper(() => api.getByPatientId({ patId: patId ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const examinationsByPatientId = createAsyncThunk( export const getDefaultPatientExamination = createAsyncThunk( "examinations/getDefaultPatientExamination", async (patId: number, thunkApi) => - api - .getDefaultPatientExamination({ patId }) + wrapper(() => api.getDefaultPatientExamination({ patId })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const getDefaultPatientExamination = createAsyncThunk( export const getLastByPatientId = createAsyncThunk( "examinations/getLastByPatientId", async (patId: number, thunkApi) => - api - .getLastByPatientId({ patId }) + wrapper(() => api.getLastByPatientId({ patId })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const getLastByPatientId = createAsyncThunk( export const createExamination = createAsyncThunk( "examinations/createExamination", async (patientExaminationDTO: PatientExaminationDTO, thunkApi) => - api - .newPatientExamination({ patientExaminationDTO }) + wrapper(() => api.newPatientExamination({ patientExaminationDTO })) .toPromise() .then(() => patientExaminationDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -47,8 +44,7 @@ export const updateExamination = createAsyncThunk( payload: { id: number; patientExaminationDTO: PatientExaminationDTO }, thunkApi ) => - api - .updateExamination(payload) + wrapper(() => api.updateExamination(payload)) .toPromise() .then(() => ({ ...payload.patientExaminationDTO, id: payload.id })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/exams/thunk.ts b/src/state/exams/thunk.ts index bc1c58231..1eaf44d62 100644 --- a/src/state/exams/thunk.ts +++ b/src/state/exams/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { ExamRowsApi, ExamsApi, @@ -14,8 +15,7 @@ const examRowsApi = new ExamRowsApi(customConfiguration()); export const getExams = createAsyncThunk( "exams/getExams", async (_, thunkApi) => - api - .getExams() + wrapper(() => api.getExams()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -23,8 +23,7 @@ export const getExams = createAsyncThunk( export const getExamRows = createAsyncThunk( "exams/getExamRows", async (examCode: string, thunkApi) => - examRowsApi - .getExamRowsByExamCode({ examCode }) + wrapper(() => examRowsApi.getExamRowsByExamCode({ examCode })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -32,8 +31,7 @@ export const getExamRows = createAsyncThunk( export const createExam = createAsyncThunk( "exams/createExam", async (payload: NewExamRequest, thunkApi) => - api - .newExam(payload) + wrapper(() => api.newExam(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -41,8 +39,7 @@ export const createExam = createAsyncThunk( export const updateExam = createAsyncThunk( "exams/updateExam", async (payload: UpdateExamRequest, thunkApi) => - api - .updateExam(payload) + wrapper(() => api.updateExam(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -50,8 +47,7 @@ export const updateExam = createAsyncThunk( export const deleteExam = createAsyncThunk( "exams/deleteExam", async (code: string, thunkApi) => - api - .deleteExam1({ code }) + wrapper(() => api.deleteExam1({ code })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/hospital/thunk.ts b/src/state/hospital/thunk.ts index 5bc077173..4692ef926 100644 --- a/src/state/hospital/thunk.ts +++ b/src/state/hospital/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { HospitalDTO, HospitalsApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -18,8 +19,7 @@ export const getHospital = createAsyncThunk( export const updateHospital = createAsyncThunk( "hospitals/updateHospital", async (payload: { code: string; hospitalDTO: HospitalDTO }, thunkApi) => - securedApi - .updateHospital(payload) + wrapper(() => securedApi.updateHospital(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/laboratories/thunk.ts b/src/state/laboratories/thunk.ts index 782a34eab..94980595c 100644 --- a/src/state/laboratories/thunk.ts +++ b/src/state/laboratories/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import moment from "moment"; import { LaboratoriesApi, @@ -13,8 +14,8 @@ const api = new LaboratoriesApi(customConfiguration()); export const searchLabs = createAsyncThunk( "laboratories/searchLabs", async (query: any, thunkApi) => - api - .getLaboratoryForPrint({ + wrapper(() => + api.getLaboratoryForPrint({ dateFrom: query.dateFrom ?? moment().add("-30", "days").toISOString(), dateTo: query.dateTo ?? moment().toISOString(), examName: query.examName, @@ -24,6 +25,7 @@ export const searchLabs = createAsyncThunk( page: query.page, size: query.size, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -31,8 +33,7 @@ export const searchLabs = createAsyncThunk( export const getLabsByPatientId = createAsyncThunk( "laboratories/getLabsByPatientId", async (patId: number | undefined, thunkApi) => - api - .getLaboratory1({ patId: patId ?? -1 }) + wrapper(() => api.getLaboratory1({ patId: patId ?? -1 })) .toPromise() .then((result) => (result ?? []).map((item) => @@ -45,8 +46,7 @@ export const getLabsByPatientId = createAsyncThunk( export const getLabsRequestByPatientId = createAsyncThunk( "laboratories/getLabsRequestByPatientId", async (patId: number | undefined, thunkApi) => - api - .getLaboratoryExamRequest1({ patId: patId ?? -1 }) + wrapper(() => api.getLaboratoryExamRequest1({ patId: patId ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -54,8 +54,7 @@ export const getLabsRequestByPatientId = createAsyncThunk( export const getLabByCode = createAsyncThunk( "laboratories/getLabByCode", async (patId: number | undefined, thunkApi) => - api - .getLaboratory1({ patId: patId ?? -1 }) + wrapper(() => api.getLaboratory1({ patId: patId ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -63,8 +62,7 @@ export const getLabByCode = createAsyncThunk( export const getLabWithRowsByCode = createAsyncThunk( "laboratories/getLabWithRowsByCode", async (code: number | undefined, thunkApi) => - api - .getExamWithRowsById({ code: code ?? -1 }) + wrapper(() => api.getExamWithRowsById({ code: code ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -72,8 +70,7 @@ export const getLabWithRowsByCode = createAsyncThunk( export const getMaterials = createAsyncThunk( "laboratories/getMaterials", async (_, thunkApi) => - api - .getMaterials() + wrapper(() => api.getMaterials()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -81,8 +78,7 @@ export const getMaterials = createAsyncThunk( export const createLab = createAsyncThunk( "laboratories/createLab", async (labWithRowsDTO: LabWithRowsDTO, thunkApi) => - api - .newLaboratory({ labWithRowsDTO }) + wrapper(() => api.newLaboratory({ labWithRowsDTO })) .toPromise() .then(() => labWithRowsDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -91,8 +87,7 @@ export const createLab = createAsyncThunk( export const createLabRequest = createAsyncThunk( "laboratories/createLabRequest", async (laboratoryDTO: LaboratoryDTO, thunkApi) => - api - .newExamRequest({ laboratoryDTO }) + wrapper(() => api.newExamRequest({ laboratoryDTO })) .toPromise() .then(() => laboratoryDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -101,8 +96,7 @@ export const createLabRequest = createAsyncThunk( export const updateLab = createAsyncThunk( "laboratories/updateLab", async (payload: UpdateLaboratoryRequest, thunkApi) => - api - .updateLaboratory(payload) + wrapper(() => api.updateLaboratory(payload)) .toPromise() .then(() => ({ ...payload.labWithRowsDTO })) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -111,8 +105,7 @@ export const updateLab = createAsyncThunk( export const updateLabStatus = createAsyncThunk( "laboratories/updateLabStatus", async (payload: { code: number; status: string }, thunkApi) => - api - .updateExamRequest(payload) + wrapper(() => api.updateExamRequest(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -120,8 +113,7 @@ export const updateLabStatus = createAsyncThunk( export const deleteLab = createAsyncThunk( "laboratories/deleteLab", async (code: number | undefined, thunkApi) => - api - .deleteExam({ code: code ?? -1 }) + wrapper(() => api.deleteExam({ code: code ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -129,8 +121,7 @@ export const deleteLab = createAsyncThunk( export const cancelLab = createAsyncThunk( "laboratories/cancelLab", async (code: number | undefined, thunkApi) => - api - .deleteExamRequest({ code: code ?? -1 }) + wrapper(() => api.deleteExamRequest({ code: code ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/layouts/thunk.ts b/src/state/layouts/thunk.ts index bfcca14e8..eb67414f7 100644 --- a/src/state/layouts/thunk.ts +++ b/src/state/layouts/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import createDebouncedAsyncThunk from "libraries/reduxUtils/createDebounceAsyncThunk"; import { decodeLayoutConfig } from "../../components/accessories/dashboard/layouts/consts"; import { UserSettingDTO, UserSettingsApi } from "../../generated"; @@ -9,8 +10,7 @@ const api = new UserSettingsApi(customConfiguration()); export const getLayouts = createAsyncThunk( "layouts/getLayouts", async (userName: string, thunkApi) => - api - .getUserSettingByUser({ configName: "dashboard" }) + wrapper(() => api.getUserSettingByUser({ configName: "dashboard" })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -18,14 +18,15 @@ export const getLayouts = createAsyncThunk( export const saveLayouts = createDebouncedAsyncThunk( "layouts/saveLayouts", async (setting: UserSettingDTO, thunkApi) => - (setting.id > 0 - ? api.updateUserSettings({ - id: setting.id, - userSettingDTO: setting, - }) - : api.newUserSettings({ - userSettingDTO: setting, - }) + wrapper(() => + setting.id > 0 + ? api.updateUserSettings({ + id: setting.id, + userSettingDTO: setting, + }) + : api.newUserSettings({ + userSettingDTO: setting, + }) ) .toPromise() .then((data) => ({ ...decodeLayoutConfig(setting.configValue), data })) diff --git a/src/state/main/slice.ts b/src/state/main/slice.ts index 9d3614f86..345de86e5 100644 --- a/src/state/main/slice.ts +++ b/src/state/main/slice.ts @@ -47,6 +47,18 @@ export const mainSlice = createSlice({ .addCase(thunks.setAuthentication.rejected, (state, action) => { state.authentication = ApiResponse.error(action.payload); }) + // Refresh token + .addCase(thunks.refreshToken.pending, (state) => { + state.authentication = ApiResponse.loading(); + }) + .addCase(thunks.refreshToken.fulfilled, (state, action) => { + state.authentication = ApiResponse.value( + action.payload as IAuthentication + ); + }) + .addCase(thunks.refreshToken.rejected, (state, action) => { + state.authentication = ApiResponse.error(action.payload); + }) // Forgot password .addCase(thunks.setForgotPasswordThunk.pending, (state) => { state.forgotpassword = ApiResponse.loading(); diff --git a/src/state/main/thunk.ts b/src/state/main/thunk.ts index 04f5b85e9..95ab6eb4a 100644 --- a/src/state/main/thunk.ts +++ b/src/state/main/thunk.ts @@ -77,3 +77,23 @@ export const getUserSettings = createAsyncThunk( .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); + +export const refreshToken = createAsyncThunk( + "main/refreshToken", + async (value: string, thunkApi) => + concat( + loginApi + .refreshToken({ tokenRefreshRequest: { refreshToken: value } }) + .pipe(tap(saveAuthenticationDataToSession)), + usersApi + .retrieveProfileByCurrentLoggedInUser() + .pipe(tap(savePermissionDataToSession)) + ) + .pipe(toArray()) + .toPromise() + .then(([userCredentials, me]) => ({ + ...userCredentials, + ...me, + })) + .catch((error) => thunkApi.rejectWithValue(error.response)) +); diff --git a/src/state/medicals/thunk.ts b/src/state/medicals/thunk.ts index 684d81736..040dc4822 100644 --- a/src/state/medicals/thunk.ts +++ b/src/state/medicals/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { GetMedicalsSortByEnum, MedicalDTO, @@ -11,10 +12,11 @@ const api = new MedicalsApi(customConfiguration()); export const getMedicals = createAsyncThunk( "medicals/getMedicals", async (_, thunkApi) => - api - .getMedicals({ + wrapper(() => + api.getMedicals({ sortBy: GetMedicalsSortByEnum.NAME, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -22,8 +24,7 @@ export const getMedicals = createAsyncThunk( export const createMedical = createAsyncThunk( "medicals/createMedical", async (medicalDTO: MedicalDTO, thunkApi) => - api - .newMedical({ medicalDTO }) + wrapper(() => api.newMedical({ medicalDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -31,8 +32,7 @@ export const createMedical = createAsyncThunk( export const updateMedical = createAsyncThunk( "medicals/updateMedical", async (medicalDTO: MedicalDTO, thunkApi) => - api - .updateMedical({ medicalDTO }) + wrapper(() => api.updateMedical({ medicalDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -40,8 +40,7 @@ export const updateMedical = createAsyncThunk( export const deleteMedical = createAsyncThunk( "medicals/deleteMedical", async (code: number, thunkApi) => - api - .deleteMedical({ code }) + wrapper(() => api.deleteMedical({ code })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/opds/thunk.ts b/src/state/opds/thunk.ts index 20843e630..b319aece8 100644 --- a/src/state/opds/thunk.ts +++ b/src/state/opds/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { isEmpty } from "lodash"; import moment from "moment"; import { @@ -14,10 +15,11 @@ const api = new OpdsApi(customConfiguration()); export const getOpds = createAsyncThunk( "opds/getOpds", async (patientCode: number | undefined, thunkApi) => - api - .getOpdByPatient({ + wrapper(() => + api.getOpdByPatient({ pcode: patientCode ?? -1, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,10 +27,11 @@ export const getOpds = createAsyncThunk( export const getOpdsWithOperationRows = createAsyncThunk( "opds/getOpdsWithOperationRows", async (patientCode: number | undefined, thunkApi) => - api - .getOpdByPatient({ + wrapper(() => + api.getOpdByPatient({ pcode: patientCode ?? -1, }) + ) .toPromise() .then((result) => (result ?? []).map((item) => @@ -41,8 +44,8 @@ export const getOpdsWithOperationRows = createAsyncThunk( export const searchOpds = createAsyncThunk( "opds/searchOpds", async (query: any, thunkApi) => - api - .getOpdByDates({ + wrapper(() => + api.getOpdByDates({ sex: isEmpty(query.sex) ? null : query.sex, newPatient: isEmpty(query.newPatient) ? null : query.newPatient, dateFrom: query.dateFrom ?? moment().add("-30", "days").toISOString(), @@ -59,6 +62,7 @@ export const searchOpds = createAsyncThunk( page: isNaN(query.page) ? 0 : query.page, size: isNaN(query.size) ? 80 : query.size, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -66,10 +70,11 @@ export const searchOpds = createAsyncThunk( export const getLastOpd = createAsyncThunk( "opds/getLastOpd", async (patientCode: number | undefined, thunkApi) => - api - .getLastOpd({ + wrapper(() => + api.getLastOpd({ patientCode: patientCode ?? -1, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -77,8 +82,7 @@ export const getLastOpd = createAsyncThunk( export const createOpd = createAsyncThunk( "opds/createOpd", async (opdDTO: OpdDTO, thunkApi) => - api - .newOpd({ opdDTO }) + wrapper(() => api.newOpd({ opdDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -86,8 +90,7 @@ export const createOpd = createAsyncThunk( export const createOpdWithOperationsRow = createAsyncThunk( "opds/createOpdWithOperationsRow", async (opdWithOperationRowDTO: OpdWithOperationRowDTO, thunkApi) => - api - .newOpdWithOperationRow({ opdWithOperationRowDTO }) + wrapper(() => api.newOpdWithOperationRow({ opdWithOperationRowDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -101,8 +104,7 @@ export const createOpdWithOperationsRow = createAsyncThunk( export const updateOpd = createAsyncThunk( "opds/updateOpd", async (payload: { code: number; opdDTO: OpdDTO }, thunkApi) => - api - .updateOpd(payload) + wrapper(() => api.updateOpd(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -110,8 +112,7 @@ export const updateOpd = createAsyncThunk( export const updateOpdWithOperationRow = createAsyncThunk( "opds/updateOpdWithOperationRow", async (payload: UpdateOpdWithOperationRowRequest, thunkApi) => - api - .updateOpdWithOperationRow(payload) + wrapper(() => api.updateOpdWithOperationRow(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -119,8 +120,7 @@ export const updateOpdWithOperationRow = createAsyncThunk( export const deleteOpd = createAsyncThunk( "opds/deleteOpd", async (code: number, thunkApi) => - api - .deleteOpd({ code }) + wrapper(() => api.deleteOpd({ code })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/operations/thunk.ts b/src/state/operations/thunk.ts index 04e5f6cbe..77c83ac3e 100644 --- a/src/state/operations/thunk.ts +++ b/src/state/operations/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { OperationDTO, OperationRowDTO, OperationsApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new OperationsApi(customConfiguration()); export const getOperations = createAsyncThunk( "operations/getOperations", async (_, thunkApi) => - api - .getOperations() + wrapper(() => api.getOperations()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getOperations = createAsyncThunk( export const getOperationsByAdmissionId = createAsyncThunk( "operations/getOperationsByAdmissionId", async (admissionId: number, thunkApi) => - api - .getOperationRowsByAdmt({ admissionId }) + wrapper(() => api.getOperationRowsByAdmt({ admissionId })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const getOperationsByAdmissionId = createAsyncThunk( export const createOperationRow = createAsyncThunk( "operations/createOperationRow", async (operationRowDTO: OperationRowDTO, thunkApi) => - api - .newOperationRow({ operationRowDTO }) + wrapper(() => api.newOperationRow({ operationRowDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const createOperationRow = createAsyncThunk( export const updateOperationRow = createAsyncThunk( "operations/updateOperationRow", async (operationRowDTO: OperationRowDTO, thunkApi) => - api - .updateOperationRow({ operationRowDTO }) + wrapper(() => api.updateOperationRow({ operationRowDTO })) .toPromise() .then(() => operationRowDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -44,8 +41,7 @@ export const updateOperationRow = createAsyncThunk( export const deleteOperationRow = createAsyncThunk( "operations/deleteOperationRow", async (code: number, thunkApi) => - api - .deleteOperationRow({ code }) + wrapper(() => api.deleteOperationRow({ code })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -53,8 +49,7 @@ export const deleteOperationRow = createAsyncThunk( export const createOperation = createAsyncThunk( "operations/createOperation", async (operationDTO: OperationDTO, thunkApi) => - api - .newOperation({ operationDTO }) + wrapper(() => api.newOperation({ operationDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -62,8 +57,7 @@ export const createOperation = createAsyncThunk( export const updateOperation = createAsyncThunk( "operations/updateOperation", async (payload: { code: string; operationDTO: OperationDTO }, thunkApi) => - api - .updateOperation(payload) + wrapper(() => api.updateOperation(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -71,8 +65,7 @@ export const updateOperation = createAsyncThunk( export const deleteOperation = createAsyncThunk( "operations/deleteOperation", async (code: string, thunkApi) => - api - .deleteOperation({ code }) + wrapper(() => api.deleteOperation({ code })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/patients/thunk.ts b/src/state/patients/thunk.ts index 3e759204c..8044480de 100644 --- a/src/state/patients/thunk.ts +++ b/src/state/patients/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import moment from "moment"; import { TValues } from "../../components/activities/searchPatientActivity/types"; import { PatientDTO, PatientsApi, UpdatePatientRequest } from "../../generated"; @@ -10,19 +11,19 @@ export const searchPatient = createAsyncThunk( "patients/searchPatient", async (values: TValues, thunkApi) => { if (values.id) { - return api - .getPatient({ code: parseInt(values.id) }) + return wrapper(() => api.getPatient({ code: parseInt(values.id) })) .toPromise() .then((result) => (result ? [result] : [])) .catch((error) => thunkApi.rejectWithValue(error.response)); } - return api - .searchPatient({ + return wrapper(() => + api.searchPatient({ ...values, birthDate: moment(values.birthDate).isValid() ? values.birthDate : undefined, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)); } @@ -31,8 +32,7 @@ export const searchPatient = createAsyncThunk( export const getCities = createAsyncThunk( "patients/getCities", async (_, thunkApi) => - api - .getPatientCities() + wrapper(() => api.getPatientCities()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -40,11 +40,12 @@ export const getCities = createAsyncThunk( export const getPatients = createAsyncThunk( "patients/getPatients", async ({ page, size }: { page?: number; size?: number }, thunkApi) => - api - .getPatients({ + wrapper(() => + api.getPatients({ page: page ?? 0, size: size ?? 80, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -52,8 +53,7 @@ export const getPatients = createAsyncThunk( export const getPatient = createAsyncThunk( "patients/getPatient", async (id: string, thunkApi) => - api - .getPatient({ code: parseInt(id) }) + wrapper(() => api.getPatient({ code: parseInt(id) })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -61,8 +61,7 @@ export const getPatient = createAsyncThunk( export const createPatient = createAsyncThunk( "patients/createPatient", async (patientDTO: PatientDTO, thunkApi) => - api - .newPatient({ patientDTO }) + wrapper(() => api.newPatient({ patientDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -70,8 +69,7 @@ export const createPatient = createAsyncThunk( export const updatePatient = createAsyncThunk( "patients/updatePatient", async (payload: UpdatePatientRequest, thunkApi) => - api - .updatePatient(payload) + wrapper(() => api.updatePatient(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/permissions/thunk.ts b/src/state/permissions/thunk.ts index 5f0547a1c..2a71f91f7 100644 --- a/src/state/permissions/thunk.ts +++ b/src/state/permissions/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { PermissionDTO, PermissionsApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new PermissionsApi(customConfiguration()); export const getAllPermissions = createAsyncThunk( "permissions/getPermissions", async (_, thunkApi) => - api - .retrieveAllPermissions() + wrapper(() => api.retrieveAllPermissions()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/prices/thunk.ts b/src/state/prices/thunk.ts index f056c8c1e..bbd0b6784 100644 --- a/src/state/prices/thunk.ts +++ b/src/state/prices/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { PriceListDTO, PriceListsApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new PriceListsApi(customConfiguration()); export const getPrices = createAsyncThunk( "prices/getPrices", async (_, thunkApi) => - api - .getPrices() + wrapper(() => api.getPrices()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getPrices = createAsyncThunk( export const getPriceLists = createAsyncThunk( "prices/getPriceLists", async (_, thunkApi) => - api - .getPriceLists() + wrapper(() => api.getPriceLists()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const getPriceLists = createAsyncThunk( export const createPriceList = createAsyncThunk( "prices/createPriceList", async (priceListDTO: PriceListDTO, thunkApi) => - api - .newPriceList({ priceListDTO }) + wrapper(() => api.newPriceList({ priceListDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const createPriceList = createAsyncThunk( export const updatePriceList = createAsyncThunk( "prices/updatePriceList", async (payload: { id: number; priceListDTO: PriceListDTO }, thunkApi) => - api - .updatePriceLists(payload) + wrapper(() => api.updatePriceLists(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -43,8 +40,7 @@ export const updatePriceList = createAsyncThunk( export const deletePriceList = createAsyncThunk( "prices/deletePriceList", async (id: number, thunkApi) => - api - .deletePriceList({ id }) + wrapper(() => api.deletePriceList({ id })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/summary/thunk.ts b/src/state/summary/thunk.ts index 0b2d4de39..aeea5c648 100644 --- a/src/state/summary/thunk.ts +++ b/src/state/summary/thunk.ts @@ -1,4 +1,7 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; +import { concat, of } from "rxjs"; +import { catchError, map, toArray } from "rxjs/operators"; import { AdmissionsApi, ExaminationsApi, @@ -9,8 +12,6 @@ import { VisitApi, } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; -import { of, concat } from "rxjs"; -import { catchError, map, toArray } from "rxjs/operators"; import { convertToSummaryData } from "../../libraries/reduxUtils/convert"; import { SummaryField } from "./consts"; @@ -29,11 +30,11 @@ export const loadSummaryData = createAsyncThunk( "summary/loadSummaryData", async (code: number, thunkApi) => concat( - examinationsApi.getByPatientId({ patId: code }).pipe( + wrapper(() => examinationsApi.getByPatientId({ patId: code })).pipe( map((res) => convertToSummaryData(res, SummaryField.triage)), catchError(() => of([])) ), - opdControllerrApi.getOpdByPatient({ pcode: code }).pipe( + wrapper(() => opdControllerrApi.getOpdByPatient({ pcode: code })).pipe( map((res) => convertToSummaryData( res.map((e) => e.opdDTO), @@ -42,7 +43,7 @@ export const loadSummaryData = createAsyncThunk( ), catchError(() => of([])) ), - laboratoriesApi.getLaboratory1({ patId: code }).pipe( + wrapper(() => laboratoriesApi.getLaboratory1({ patId: code })).pipe( map((res) => convertToSummaryData( res.map((e) => e.laboratoryDTO), @@ -51,19 +52,21 @@ export const loadSummaryData = createAsyncThunk( ), catchError(() => of([])) ), - admissionsApi.getAdmissions1({ patientCode: code }).pipe( + wrapper(() => admissionsApi.getAdmissions1({ patientCode: code })).pipe( map((res) => convertToSummaryData(res, SummaryField.admission)), catchError(() => of([])) ), - visitControllerrApi.getVisit({ patID: code }).pipe( + wrapper(() => visitControllerrApi.getVisit({ patID: code })).pipe( map((res) => convertToSummaryData(res, SummaryField.visit)), catchError(() => of([])) ), - operationsApi.getOperationRowsByPatient({ patientCode: code }).pipe( + wrapper(() => + operationsApi.getOperationRowsByPatient({ patientCode: code }) + ).pipe( map((res) => convertToSummaryData(res, SummaryField.operation)), catchError(() => of([])) ), - therapiesApi.getTherapyRows({ codePatient: code }).pipe( + wrapper(() => therapiesApi.getTherapyRows({ codePatient: code })).pipe( map((res) => convertToSummaryData(res, SummaryField.therapy)), catchError(() => of([])) ) diff --git a/src/state/suppliers/thunk.ts b/src/state/suppliers/thunk.ts index 98a634ca9..176e80e51 100644 --- a/src/state/suppliers/thunk.ts +++ b/src/state/suppliers/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { SupplierDTO, SuppliersApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new SuppliersApi(customConfiguration()); export const getSuppliers = createAsyncThunk( "suppliers/getSuppliers", async (excludeDeleted: boolean | undefined, thunkApi) => - api - .getSuppliers({ excludeDeleted }) + wrapper(() => api.getSuppliers({ excludeDeleted })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getSuppliers = createAsyncThunk( export const createSupplier = createAsyncThunk( "suppliers/createSupplier", async (supplierDTO: SupplierDTO, thunkApi) => - api - .saveSupplier({ supplierDTO }) + wrapper(() => api.saveSupplier({ supplierDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createSupplier = createAsyncThunk( export const updateSupplier = createAsyncThunk( "suppliers/updateSupplier", async (supplierDTO: SupplierDTO, thunkApi) => - api - .updateSupplier({ supplierDTO }) + wrapper(() => api.updateSupplier({ supplierDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateSupplier = createAsyncThunk( export const deleteSupplier = createAsyncThunk( "suppliers/deleteSupplier", async (id: number, thunkApi) => - api - .deleteSupplier({ id }) + wrapper(() => api.deleteSupplier({ id })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/therapies/thunk.ts b/src/state/therapies/thunk.ts index ed77ea8a0..5b92b1566 100644 --- a/src/state/therapies/thunk.ts +++ b/src/state/therapies/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { TherapiesApi, TherapyRowDTO } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new TherapiesApi(customConfiguration()); export const getTherapiesByPatientId = createAsyncThunk( "therapies/getTherapiesByPatientId", async (codePatient: number | undefined, thunkApi) => - api - .getTherapyRows({ codePatient: codePatient ?? -1 }) + wrapper(() => api.getTherapyRows({ codePatient: codePatient ?? -1 })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getTherapiesByPatientId = createAsyncThunk( export const createTherapy = createAsyncThunk( "therapies/createTherapy", async (therapyRowDTO: TherapyRowDTO, thunkApi) => - api - .newTherapy({ therapyRowDTO }) + wrapper(() => api.newTherapy({ therapyRowDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createTherapy = createAsyncThunk( export const updateTherapy = createAsyncThunk( "therapies/updateTherapy", async (therapyRowDTO: TherapyRowDTO, thunkApi) => - api - .replaceTherapies({ therapyRowDTO: [therapyRowDTO] }) + wrapper(() => api.replaceTherapies({ therapyRowDTO: [therapyRowDTO] })) .toPromise() .then(() => therapyRowDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/admissions/thunk.ts b/src/state/types/admissions/thunk.ts index f50e7e07a..ff020bf2f 100644 --- a/src/state/types/admissions/thunk.ts +++ b/src/state/types/admissions/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { AdmissionTypeDTO, AdmissionTypesApi } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new AdmissionTypesApi(customConfiguration()); export const getAdmissionTypes = createAsyncThunk( "admissionTypes/getAdmissionTypes", async (_, thunkApi) => - api - .getAdmissionTypes() + wrapper(() => api.getAdmissionTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getAdmissionTypes = createAsyncThunk( export const createAdmissionType = createAsyncThunk( "admissionTypes/createAdmissionType", async (admissionTypeDTO: AdmissionTypeDTO, thunkApi) => - api - .newAdmissionType({ admissionTypeDTO }) + wrapper(() => api.newAdmissionType({ admissionTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createAdmissionType = createAsyncThunk( export const updateAdmissionType = createAsyncThunk( "admissionTypes/updateAdmissionType", async (admissionTypeDTO: AdmissionTypeDTO, thunkApi) => - api - .updateAdmissionTypes({ admissionTypeDTO }) + wrapper(() => api.updateAdmissionTypes({ admissionTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateAdmissionType = createAsyncThunk( export const deleteAdmissionType = createAsyncThunk( "admissionTypes/deleteAdmissionType", async (code: string, thunkApi) => - api - .deleteAdmissionType({ code }) + wrapper(() => api.deleteAdmissionType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/ageTypes/thunk.ts b/src/state/types/ageTypes/thunk.ts index d220efa07..26d67cc48 100644 --- a/src/state/types/ageTypes/thunk.ts +++ b/src/state/types/ageTypes/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { AgeTypeDTO, AgeTypesApi } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new AgeTypesApi(customConfiguration()); export const getAgeTypes = createAsyncThunk( "ageTypes/getAll", async (_, thunkApi) => - api - .getAllAgeTypes() + wrapper(() => api.getAllAgeTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getAgeTypes = createAsyncThunk( export const updateAgeTypes = createAsyncThunk( "ageTypes/update", async (ageTypeDTO: AgeTypeDTO[], thunkApi) => - api - .updateAgeType({ ageTypeDTO }) + wrapper(() => api.updateAgeType({ ageTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/types/deliveries/thunk.ts b/src/state/types/deliveries/thunk.ts index 62f9cee55..27508b8e7 100644 --- a/src/state/types/deliveries/thunk.ts +++ b/src/state/types/deliveries/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { DeliveryTypeApi, DeliveryTypeDTO } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new DeliveryTypeApi(customConfiguration()); export const getDeliveryTypes = createAsyncThunk( "deliveryTypes/getDeliveryTypes", async (_, thunkApi) => - api - .getDeliveryTypes() + wrapper(() => api.getDeliveryTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getDeliveryTypes = createAsyncThunk( export const createDeliveryType = createAsyncThunk( "deliveryTypes/createDeliveryType", async (deliveryTypeDTO: DeliveryTypeDTO, thunkApi) => - api - .newDeliveryType({ deliveryTypeDTO }) + wrapper(() => api.newDeliveryType({ deliveryTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createDeliveryType = createAsyncThunk( export const updateDeliveryType = createAsyncThunk( "deliveryTypes/updateDeliveryType", async (deliveryTypeDTO: DeliveryTypeDTO, thunkApi) => - api - .updateDeliveryTypes({ deliveryTypeDTO }) + wrapper(() => api.updateDeliveryTypes({ deliveryTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateDeliveryType = createAsyncThunk( export const deleteDeliveryType = createAsyncThunk( "deliveryTypes/deleteDeliveryType", async (code: string, thunkApi) => - api - .deleteDeliveryType({ code }) + wrapper(() => api.deleteDeliveryType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/deliveryResults/thunk.ts b/src/state/types/deliveryResults/thunk.ts index 7a5fffbbe..e1c31bcac 100644 --- a/src/state/types/deliveryResults/thunk.ts +++ b/src/state/types/deliveryResults/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { DeliveryResultTypeApi, DeliveryResultTypeDTO, @@ -10,8 +11,7 @@ const api = new DeliveryResultTypeApi(customConfiguration()); export const getDeliveryResultTypes = createAsyncThunk( "deliveryResultTypes/getDeliveryResultTypes", async (_, thunkApi) => - api - .getDeliveryResultTypes() + wrapper(() => api.getDeliveryResultTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -19,8 +19,7 @@ export const getDeliveryResultTypes = createAsyncThunk( export const createDeliveryResultType = createAsyncThunk( "deliveryResultTypes/createDeliveryResultType", async (deliveryResultTypeDTO: DeliveryResultTypeDTO, thunkApi) => - api - .newDeliveryResultType({ deliveryResultTypeDTO }) + wrapper(() => api.newDeliveryResultType({ deliveryResultTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -28,8 +27,7 @@ export const createDeliveryResultType = createAsyncThunk( export const updateDeliveryResultType = createAsyncThunk( "deliveryResultTypes/updateDeliveryResultType", async (deliveryResultTypeDTO: DeliveryResultTypeDTO, thunkApi) => - api - .updateDeliveryResultTypes({ deliveryResultTypeDTO }) + wrapper(() => api.updateDeliveryResultTypes({ deliveryResultTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -37,8 +35,7 @@ export const updateDeliveryResultType = createAsyncThunk( export const deleteDeliveryResultType = createAsyncThunk( "deliveryResultTypes/deleteDeliveryResultType", async (code: string, thunkApi) => - api - .deleteDeliveryResultType({ code }) + wrapper(() => api.deleteDeliveryResultType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/discharges/thunk.ts b/src/state/types/discharges/thunk.ts index f04eb7803..b750ea25f 100644 --- a/src/state/types/discharges/thunk.ts +++ b/src/state/types/discharges/thunk.ts @@ -1,5 +1,6 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; import { DischargeTypeDTO } from "generated/models/DischargeTypeDTO"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { DischargeTypeApi } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -8,8 +9,7 @@ const api = new DischargeTypeApi(customConfiguration()); export const getDischargeTypes = createAsyncThunk( "dischargeTypes/getDischargeTypes", async (_, thunkApi) => - api - .getDischargeTypes() + wrapper(() => api.getDischargeTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -17,8 +17,7 @@ export const getDischargeTypes = createAsyncThunk( export const createDischargeType = createAsyncThunk( "dischargeTypes/createDischargeType", async (dischargeTypeDTO: DischargeTypeDTO, thunkApi) => - api - .newDischargeType({ dischargeTypeDTO }) + wrapper(() => api.newDischargeType({ dischargeTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -26,8 +25,7 @@ export const createDischargeType = createAsyncThunk( export const updateDischargeType = createAsyncThunk( "dischargeTypes/updateDischargeType", async (dischargeTypeDTO: DischargeTypeDTO, thunkApi) => - api - .updateDischargeType({ dischargeTypeDTO }) + wrapper(() => api.updateDischargeType({ dischargeTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -35,8 +33,7 @@ export const updateDischargeType = createAsyncThunk( export const deleteDischargeType = createAsyncThunk( "dischargeTypes/deleteDischargeType", async (code: string, thunkApi) => - api - .deleteDischargeType({ code }) + wrapper(() => api.deleteDischargeType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/diseases/thunk.ts b/src/state/types/diseases/thunk.ts index 9ffe804f7..1aeb0a3a4 100644 --- a/src/state/types/diseases/thunk.ts +++ b/src/state/types/diseases/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { DiseaseTypeDTO, DiseaseTypesApi } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new DiseaseTypesApi(customConfiguration()); export const getDiseaseTypes = createAsyncThunk( "diseaseTypes/getDiseaseTypes", async (_, thunkApi) => - api - .getAllDiseaseTypes() + wrapper(() => api.getAllDiseaseTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getDiseaseTypes = createAsyncThunk( export const createDiseaseType = createAsyncThunk( "diseaseTypes/createDiseaseType", async (diseaseTypeDTO: DiseaseTypeDTO, thunkApi) => - api - .newDiseaseType({ diseaseTypeDTO }) + wrapper(() => api.newDiseaseType({ diseaseTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createDiseaseType = createAsyncThunk( export const updateDiseaseType = createAsyncThunk( "diseaseTypes/updateDiseaseType", async (diseaseTypeDTO: DiseaseTypeDTO, thunkApi) => - api - .updateDiseaseType({ diseaseTypeDTO }) + wrapper(() => api.updateDiseaseType({ diseaseTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateDiseaseType = createAsyncThunk( export const deleteDiseaseType = createAsyncThunk( "diseaseTypes/deleteDiseaseType", async (code: string, thunkApi) => - api - .deleteDiseaseType({ code }) + wrapper(() => api.deleteDiseaseType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/exams/thunk.ts b/src/state/types/exams/thunk.ts index 02fb30127..bef4385d3 100644 --- a/src/state/types/exams/thunk.ts +++ b/src/state/types/exams/thunk.ts @@ -1,5 +1,6 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; -import { ExamTypesApi, ExamTypeDTO } from "../../../generated"; +import { wrapper } from "libraries/apiUtils/wrapper"; +import { ExamTypeDTO, ExamTypesApi } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; const api = new ExamTypesApi(customConfiguration()); @@ -7,8 +8,7 @@ const api = new ExamTypesApi(customConfiguration()); export const getExamTypes = createAsyncThunk( "examTypes/getExamTypes", async (_, thunkApi) => - api - .getExamTypes() + wrapper(() => api.getExamTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getExamTypes = createAsyncThunk( export const createExamType = createAsyncThunk( "examTypes/createExamType", async (examTypeDTO: ExamTypeDTO, thunkApi) => - api - .newExamType({ examTypeDTO }) + wrapper(() => api.newExamType({ examTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createExamType = createAsyncThunk( export const updateExamType = createAsyncThunk( "examTypes/updateExamType", async (payload: { code: string; examTypeDTO: ExamTypeDTO }, thunkApi) => - api - .updateExamType(payload) + wrapper(() => api.updateExamType(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateExamType = createAsyncThunk( export const deleteExamType = createAsyncThunk( "examTypes/deleteExamType", async (code: string, thunkApi) => - api - .deleteExamType({ code }) + wrapper(() => api.deleteExamType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/medicals/thunk.ts b/src/state/types/medicals/thunk.ts index cb11a0ac9..c7bbc1ff3 100644 --- a/src/state/types/medicals/thunk.ts +++ b/src/state/types/medicals/thunk.ts @@ -1,5 +1,6 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; -import { MedicalTypesApi, MedicalTypeDTO } from "../../../generated"; +import { wrapper } from "libraries/apiUtils/wrapper"; +import { MedicalTypeDTO, MedicalTypesApi } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; const api = new MedicalTypesApi(customConfiguration()); @@ -7,8 +8,7 @@ const api = new MedicalTypesApi(customConfiguration()); export const getMedicalTypes = createAsyncThunk( "medicalTypes/getMedicalTypes", async (_, thunkApi) => - api - .getMedicalTypes() + wrapper(() => api.getMedicalTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getMedicalTypes = createAsyncThunk( export const createMedicalType = createAsyncThunk( "medicalTypes/createMedicalType", async (medicalTypeDTO: MedicalTypeDTO, thunkApi) => - api - .createMedicalType({ medicalTypeDTO }) + wrapper(() => api.createMedicalType({ medicalTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createMedicalType = createAsyncThunk( export const updateMedicalType = createAsyncThunk( "medicalTypes/updateMedicalType", async (medicalTypeDTO: MedicalTypeDTO, thunkApi) => - api - .updateMedicalType({ medicalTypeDTO }) + wrapper(() => api.updateMedicalType({ medicalTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateMedicalType = createAsyncThunk( export const deleteMedicalType = createAsyncThunk( "medicalTypes/deleteMedicalType", async (code: string, thunkApi) => - api - .deleteMedicalType({ code }) + wrapper(() => api.deleteMedicalType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/operations/thunk.ts b/src/state/types/operations/thunk.ts index a1d9b69d9..37235bbe9 100644 --- a/src/state/types/operations/thunk.ts +++ b/src/state/types/operations/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { OperationTypeDTO, OperationsTypesApi } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new OperationsTypesApi(customConfiguration()); export const getOperationTypes = createAsyncThunk( "operationTypes/getOperationTypes", async (_, thunkApi) => - api - .getOperationTypes() + wrapper(() => api.getOperationTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getOperationTypes = createAsyncThunk( export const createOperationType = createAsyncThunk( "operationTypes/createOperationType", async (operationTypeDTO: OperationTypeDTO, thunkApi) => - api - .newOperationType({ operationTypeDTO }) + wrapper(() => api.newOperationType({ operationTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -28,8 +27,7 @@ export const updateOperationType = createAsyncThunk( payload: { code: string; operationTypeDTO: OperationTypeDTO }, thunkApi ) => - api - .updateOperationTypes(payload) + wrapper(() => api.updateOperationTypes(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -37,8 +35,7 @@ export const updateOperationType = createAsyncThunk( export const deleteOperationType = createAsyncThunk( "operationTypes/deleteOperationType", async (code: string, thunkApi) => - api - .deleteOperationType({ code }) + wrapper(() => api.deleteOperationType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/pregnantTreatment/thunk.ts b/src/state/types/pregnantTreatment/thunk.ts index 91b2d54c3..3e6c26876 100644 --- a/src/state/types/pregnantTreatment/thunk.ts +++ b/src/state/types/pregnantTreatment/thunk.ts @@ -1,7 +1,8 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { - PregnantTreatmentTypesApi, PregnantTreatmentTypeDTO, + PregnantTreatmentTypesApi, } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -10,8 +11,7 @@ const api = new PregnantTreatmentTypesApi(customConfiguration()); export const getPregnantTreatmentTypes = createAsyncThunk( "pregnantTreatmentTypes/getPregnantTreatmentTypes", async (_, thunkApi) => - api - .getPregnantTreatmentTypes() + wrapper(() => api.getPregnantTreatmentTypes()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -19,8 +19,7 @@ export const getPregnantTreatmentTypes = createAsyncThunk( export const createPregnantTreatmentType = createAsyncThunk( "pregnantTreatmentTypes/createPregnantTreatmentType", async (pregnantTreatmentTypeDTO: PregnantTreatmentTypeDTO, thunkApi) => - api - .newPregnantTreatmentType({ pregnantTreatmentTypeDTO }) + wrapper(() => api.newPregnantTreatmentType({ pregnantTreatmentTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +33,7 @@ export const updatePregnantTreatmentType = createAsyncThunk( }, thunkApi ) => - api - .updatePregnantTreatmentTypes(payload) + wrapper(() => api.updatePregnantTreatmentTypes(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -43,8 +41,7 @@ export const updatePregnantTreatmentType = createAsyncThunk( export const deletePregnantTreatmentType = createAsyncThunk( "pregnantTreatmentTypes/deletePregnantTreatmentType", async (code: string, thunkApi) => - api - .deletePregnantTreatmentType({ code }) + wrapper(() => api.deletePregnantTreatmentType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/types/vaccines/thunk.ts b/src/state/types/vaccines/thunk.ts index 2cf883ac6..38d3204bd 100644 --- a/src/state/types/vaccines/thunk.ts +++ b/src/state/types/vaccines/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { VaccineTypeApi, VaccineTypeDTO } from "../../../generated"; import { customConfiguration } from "../../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new VaccineTypeApi(customConfiguration()); export const getVaccineTypes = createAsyncThunk( "vaccineTypes/getVaccineTypes", async (_, thunkApi) => - api - .getVaccineType() + wrapper(() => api.getVaccineType()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getVaccineTypes = createAsyncThunk( export const createVaccineType = createAsyncThunk( "vaccineTypes/createVaccineType", async (vaccineTypeDTO: VaccineTypeDTO, thunkApi) => - api - .newVaccineType({ vaccineTypeDTO }) + wrapper(() => api.newVaccineType({ vaccineTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createVaccineType = createAsyncThunk( export const updateVaccineType = createAsyncThunk( "vaccineTypes/updateVaccineType", async (vaccineTypeDTO: VaccineTypeDTO, thunkApi) => - api - .updateVaccineType({ vaccineTypeDTO }) + wrapper(() => api.updateVaccineType({ vaccineTypeDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateVaccineType = createAsyncThunk( export const deleteVaccineType = createAsyncThunk( "vaccineTypes/deleteVaccineType", async (code: string, thunkApi) => - api - .deleteVaccineType({ code }) + wrapper(() => api.deleteVaccineType({ code })) .toPromise() .then(() => ({ code })) .catch((error) => thunkApi.rejectWithValue(error.response)) diff --git a/src/state/usergroups/thunk.ts b/src/state/usergroups/thunk.ts index 8c08ec016..6adfc318d 100644 --- a/src/state/usergroups/thunk.ts +++ b/src/state/usergroups/thunk.ts @@ -1,5 +1,6 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; import { UserGroupDTO } from "generated/models/UserGroupDTO"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { UserGroupsApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -8,8 +9,7 @@ const api = new UserGroupsApi(customConfiguration()); export const getUserGroups = createAsyncThunk( "userGroups/getUserGroups", async (_, thunkApi) => - api - .getUserGroups() + wrapper(() => api.getUserGroups()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -17,8 +17,7 @@ export const getUserGroups = createAsyncThunk( export const createUserGroup = createAsyncThunk( "userGroups/createUserGroup", async (userGroupDTO: UserGroupDTO, thunkApi) => - api - .newUserGroup({ userGroupDTO }) + wrapper(() => api.newUserGroup({ userGroupDTO })) .toPromise() .then(() => userGroupDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -27,8 +26,9 @@ export const createUserGroup = createAsyncThunk( export const updateUserGroup = createAsyncThunk( "userGroups/updateUserGroup", async (userGroupDTO: UserGroupDTO, thunkApi) => - api - .updateUserGroup({ groupCode: userGroupDTO.code, userGroupDTO }) + wrapper(() => + api.updateUserGroup({ groupCode: userGroupDTO.code, userGroupDTO }) + ) .toPromise() .then(() => userGroupDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -37,8 +37,7 @@ export const updateUserGroup = createAsyncThunk( export const deleteUserGroup = createAsyncThunk( "userGroups/deleteUserGroup", async (groupCode: string, thunkApi) => - api - .deleteGroup({ groupCode }) + wrapper(() => api.deleteGroup({ groupCode })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -46,9 +45,7 @@ export const deleteUserGroup = createAsyncThunk( export const getUserGroup = createAsyncThunk( "userGroups/getUserGroup", async (groupCode: string, thunkApi) => - api - // GET /users/groups/{group_code} - .getUserGroup({ groupCode }) + wrapper(() => api.getUserGroup({ groupCode })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -59,8 +56,7 @@ export const assignPermission = createAsyncThunk( { permissionId, groupCode }: { permissionId: number; groupCode: string }, thunkApi ) => - api - .assignPermission({ groupCode, id: permissionId }) + wrapper(() => api.assignPermission({ groupCode, id: permissionId })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -71,8 +67,7 @@ export const revokePermission = createAsyncThunk( { permissionId, groupCode }: { permissionId: number; groupCode: string }, thunkApi ) => - api - .revokePermission({ groupCode, id: permissionId }) + wrapper(() => api.revokePermission({ groupCode, id: permissionId })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/users/thunk.ts b/src/state/users/thunk.ts index 1a5e60326..b6526a040 100644 --- a/src/state/users/thunk.ts +++ b/src/state/users/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { GetUserRequest, UserDTO, UsersApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new UsersApi(customConfiguration()); export const getUsers = createAsyncThunk( "users/getUsers", async (payload: GetUserRequest, thunkApi) => - api - .getUser(payload) + wrapper(() => api.getUser(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getUsers = createAsyncThunk( export const getUserById = createAsyncThunk( "users/getUserById", async (username: string, thunkApi) => - api - .getUserByName({ username }) + wrapper(() => api.getUserByName({ username })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const getUserById = createAsyncThunk( export const createUser = createAsyncThunk( "users/createUser", async (userDTO: UserDTO, thunkApi) => - api - .newUser({ userDTO }) + wrapper(() => api.newUser({ userDTO })) .toPromise() .then(() => userDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -35,8 +33,7 @@ export const createUser = createAsyncThunk( export const updateUser = createAsyncThunk( "users/updateUser", async (userDTO: UserDTO, thunkApi) => - api - .updateUser({ username: userDTO.userName, userDTO }) + wrapper(() => api.updateUser({ username: userDTO.userName, userDTO })) .toPromise() .then(() => userDTO) .catch((error) => thunkApi.rejectWithValue(error.response)) @@ -45,8 +42,7 @@ export const updateUser = createAsyncThunk( export const deleteUser = createAsyncThunk( "users/deleteUser", async (username: string, thunkApi) => - api - .deleteUser({ username }) + wrapper(() => api.deleteUser({ username })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/vaccines/thunk.ts b/src/state/vaccines/thunk.ts index 56609335b..bb02d8ae1 100644 --- a/src/state/vaccines/thunk.ts +++ b/src/state/vaccines/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { VaccineDTO, VaccinesApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new VaccinesApi(customConfiguration()); export const getVaccines = createAsyncThunk( "vaccines/getVaccines", async (_, thunkApi) => - api - .getVaccines() + wrapper(() => api.getVaccines()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getVaccines = createAsyncThunk( export const createVaccine = createAsyncThunk( "vaccines/createVaccine", async (vaccineDTO: VaccineDTO, thunkApi) => - api - .newVaccine({ vaccineDTO }) + wrapper(() => api.newVaccine({ vaccineDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createVaccine = createAsyncThunk( export const updateVaccine = createAsyncThunk( "vaccines/updateVaccine", async (vaccineDTO: VaccineDTO, thunkApi) => - api - .updateVaccine({ vaccineDTO }) + wrapper(() => api.updateVaccine({ vaccineDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateVaccine = createAsyncThunk( export const deleteVaccine = createAsyncThunk( "vaccines/deleteVaccine", async (code: string, thunkApi) => - api - .deleteVaccine({ code }) + wrapper(() => api.deleteVaccine({ code })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/visits/thunk.ts b/src/state/visits/thunk.ts index b0e4de6b0..b64828bff 100644 --- a/src/state/visits/thunk.ts +++ b/src/state/visits/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { UpdateVisitRequest, VisitApi, VisitDTO } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,10 +8,11 @@ const api = new VisitApi(customConfiguration()); export const getVisits = createAsyncThunk( "visits/getVisits", async (patientCode: number, thunkApi) => - api - .getVisit({ + wrapper(() => + api.getVisit({ patID: patientCode, }) + ) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -18,8 +20,7 @@ export const getVisits = createAsyncThunk( export const createVisit = createAsyncThunk( "visits/createVisit", async (visitDTO: VisitDTO, thunkApi) => - api - .newVisit({ visitDTO }) + wrapper(() => api.newVisit({ visitDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -27,8 +28,7 @@ export const createVisit = createAsyncThunk( export const updateVisit = createAsyncThunk( "visits/updateVisit", async (payload: UpdateVisitRequest, thunkApi) => - api - .updateVisit(payload) + wrapper(() => api.updateVisit(payload)) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -36,8 +36,7 @@ export const updateVisit = createAsyncThunk( export const deleteVisit = createAsyncThunk( "visits/deleteVisit", async (patientCode: number, thunkApi) => - api - .deleteVisitsRelatedToPatient({ patID: patientCode }) + wrapper(() => api.deleteVisitsRelatedToPatient({ patID: patientCode })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); diff --git a/src/state/ward/thunk.ts b/src/state/ward/thunk.ts index 5251bfac1..64f66d66e 100644 --- a/src/state/ward/thunk.ts +++ b/src/state/ward/thunk.ts @@ -1,4 +1,5 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import { wrapper } from "libraries/apiUtils/wrapper"; import { WardDTO, WardsApi } from "../../generated"; import { customConfiguration } from "../../libraries/apiUtils/configuration"; @@ -7,8 +8,7 @@ const api = new WardsApi(customConfiguration()); export const getWards = createAsyncThunk( "wards/getWards", async (_, thunkApi) => - api - .getWards() + wrapper(() => api.getWards()) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -16,8 +16,7 @@ export const getWards = createAsyncThunk( export const createWard = createAsyncThunk( "wards/createWard", async (wardDTO: WardDTO, thunkApi) => - api - .newWard({ wardDTO }) + wrapper(() => api.newWard({ wardDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -25,8 +24,7 @@ export const createWard = createAsyncThunk( export const updateWard = createAsyncThunk( "wards/updateWard", async (wardDTO: WardDTO, thunkApi) => - api - .updateWard({ wardDTO }) + wrapper(() => api.updateWard({ wardDTO })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) ); @@ -34,8 +32,7 @@ export const updateWard = createAsyncThunk( export const deleteWard = createAsyncThunk( "wards/deleteWard", async (code: string, thunkApi) => - api - .deleteWard({ code }) + wrapper(() => api.deleteWard({ code })) .toPromise() .catch((error) => thunkApi.rejectWithValue(error.response)) );