All files / src/store/app query.ts

23.63% Statements 13/55
0% Branches 0/28
0% Functions 0/3
22.22% Lines 12/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1241x 1x 1x       1x 1x 1x 1x 1x 1x         1x                       1x                                                 1x                                                                                                                                            
import { fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { API_URL } from "../../constants/index.ts";
import {
  rangeSelector,
  updateProperty,
} from "../features/timeRange/timeRangeSlice.ts";
import { showSnackbar } from "../features/snackbar/snackbarSlice.ts";
import { CustomError, ErrorType } from "../../utils/common/Error.ts";
import { refreshToken } from "./refreshTokenService.ts";
import { logOut } from "../features/auth/authSlice.ts";
import { isUnAuth } from "../../utils/auth/unauthenticatedRoutes.ts";
import {
  PaginationKeys,
  paginationSelector,
} from "../features/pagination/paginationSlice.ts";
 
const baseQuery = fetchBaseQuery({
  baseUrl: API_URL,
  prepareHeaders: (headers, { getState }) => {
    const token = (getState() as any).auth.accessToken;
    Iif (token) {
      headers.set("Authorization", `Bearer ${token}`);
    }
    return headers;
  },
  credentials: "include",
});
 
const modifyRequestBody = (originalArgs, api) => {
  Iif (
    originalArgs.method?.toUpperCase() === "POST" &&
    typeof originalArgs.body === "object"
  ) {
    const modifiedArgs = { ...originalArgs };
    const timeRange = rangeSelector(api.getState());
    const pagination = paginationSelector(api.getState());
    modifiedArgs.body = {
      ...originalArgs.body,
      meta: {
        time_range: timeRange,
        page: {
          limit: pagination[PaginationKeys.LIMIT],
          offset:
            pagination[PaginationKeys.LIMIT] * pagination[PaginationKeys.PAGE],
        },
        ...originalArgs.body.meta,
      },
    };
    return modifiedArgs;
  }
  return originalArgs;
};
 
export const baseQueryWithReauthAndModify = async (args, api, extraOptions) => {
  const modifiedArgs = modifyRequestBody(args, api);
  let result: any = await baseQuery(modifiedArgs, api, extraOptions);
 
  Iif (result.error?.status === 401 && !isUnAuth(location.pathname)) {
    try {
      const refreshResult = await refreshToken();
      const newAccessToken = refreshResult.data?.access;
 
      Iif (newAccessToken) {
        modifiedArgs.headers.set("Authorization", `Bearer ${newAccessToken}`);
        result = await baseQuery(modifiedArgs, api, extraOptions);
      }
    } catch (error) {
      api.dispatch(showSnackbar("Session expired. Please log in again."));
      api.dispatch(logOut());
      throw error;
    }
  }
 
  Iif (result?.error?.originalStatus === 502) {
    const retryCount = extraOptions?.retryCount || 0;
    if (retryCount < 3) {
      return baseQueryWithReauthAndModify(args, api, {
        ...extraOptions,
        retryCount: retryCount + 1,
      });
    } else {
      const message = result.data?.message?.description || "There was an error";
      api.dispatch(showSnackbar(message));
      throw new CustomError(message, ErrorType.GENERAL);
    }
  }
 
  Iif (result.data?.hasOwnProperty("success") && !result.data?.success) {
    const message =
      result.data?.message?.description ||
      result?.data?.task_execution_result?.error ||
      result?.data?.message?.title ||
      "There was an error";
    api.dispatch(showSnackbar(message));
    if (result?.data?.task_execution_result?.error) {
      throw new CustomError(message, ErrorType.TASK_FAILED);
    } else throw new CustomError(message, ErrorType.GENERAL);
  }
 
  const timeRange = result.data?.meta?.time_range;
  Iif (timeRange && timeRange.time_geq && timeRange.time_lt) {
    api.dispatch(
      updateProperty({
        key: "timeRange",
        value: undefined,
      }),
    );
    api.dispatch(
      updateProperty({
        key: "startTime",
        value: new Date(timeRange?.time_geq * 1000),
      }),
    );
    api.dispatch(
      updateProperty({
        key: "endTime",
        value: new Date(timeRange?.time_lt * 1000),
      }),
    );
  }
 
  return result;
};