All files / src/utils apiStatusService.ts

85.29% Statements 87/102
93.93% Branches 31/33
100% Functions 7/7
85.29% Lines 87/102

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 1681x   1x                     1x 1x 1x 1x 1x 1x   1x             18x 18x 2x 2x   18x 4x 4x   18x 2x 2x   18x 5x 5x   18x 5x 5x                   10x 10x 10x 10x 10x 10x 10x   10x 1x 1x   8x 10x 10x 1x 1x 10x             1x 31x 31x 31x   31x 10x 10x 10x   31x 10x             10x 10x 31x           1x 59x 59x           1x 84x 84x           1x 80x 80x               1x 100x 100x 100x 100x                 100x 100x 10x 10x 100x 10x 10x   10x 1x 1x   9x 10x 8x 8x 8x 1x 1x   90x 90x  
import { ref, Ref } from "vue";
 
import { fetchServicesAndProviders } from "@/api/ZMSAppointmentAPI";
 
export type ApiStatusType = "normal" | "maintenance" | "systemFailure";
 
export interface ApiStatusState {
  status: Ref<ApiStatusType>;
  lastCheckTime: Ref<number | null>;
  checkInterval: Ref<number | null>;
  baseUrl: Ref<string | undefined>;
}
 
const apiStatusState: ApiStatusState = {
  status: ref<ApiStatusType>("normal"),
  lastCheckTime: ref(null),
  checkInterval: ref(null),
  baseUrl: ref(undefined),
};
 
const CHECK_INTERVAL_MS = 30000;
 
/**
 * Determines the API status based on error information
 * @param firstError - The first error from the response
 * @returns ApiStatusType - The determined status
 */
function determineStatusFromError(firstError: any): ApiStatusType {
  if (firstError?.errorCode === "rateLimitExceeded") {
    return "normal";
  }
 
  if (firstError?.statusCode === 503) {
    return "maintenance";
  }
 
  if (firstError?.errorCode === "serviceUnavailable") {
    return "maintenance";
  }
 
  if (firstError?.statusCode >= 500 || firstError?.statusCode === 0) {
    return "systemFailure";
  }
 
  if (firstError?.statusCode >= 400 && firstError?.statusCode < 500) {
    return "maintenance";
  }
 
  return "normal";
}
 
/**
 * Checks if the offices-and-services API is available and determines the appropriate status
 * @param baseUrl - Optional base URL for the API
 * @returns Promise<ApiStatusType> - The current API status
 */
export async function checkApiStatus(baseUrl?: string): Promise<ApiStatusType> {
  try {
    const response = await fetchServicesAndProviders(
      undefined,
      undefined,
      baseUrl
    );
 
    if (response && !(response as any).errors) {
      return "normal";
    }
 
    const firstError = (response as any)?.errors?.[0];
    return determineStatusFromError(firstError);
  } catch (error) {
    return "systemFailure";
  }
}
 
/**
 * Sets the API status and starts automatic recovery checking if needed
 * @param status - The new API status
 * @param baseUrl - Optional base URL for the API
 */
export function setApiStatus(status: ApiStatusType, baseUrl?: string): void {
  apiStatusState.status.value = status;
  apiStatusState.lastCheckTime.value = Date.now();
  apiStatusState.baseUrl.value = baseUrl;
 
  if (apiStatusState.checkInterval.value) {
    clearInterval(apiStatusState.checkInterval.value);
    apiStatusState.checkInterval.value = null;
  }
 
  if (status !== "normal") {
    apiStatusState.checkInterval.value = setInterval(async () => {
      const currentStatus = await checkApiStatus(baseUrl);
      if (currentStatus === "normal") {
        setApiStatus("normal", baseUrl);
      } else if (currentStatus !== status) {
        setApiStatus(currentStatus, baseUrl);
      }
    }, CHECK_INTERVAL_MS) as any;
  }
}
 
/**
 * Gets the current API status state
 * @returns ApiStatusState - The current API status state
 */
export function getApiStatusState(): ApiStatusState {
  return apiStatusState;
}
 
/**
 * Checks if the application is currently in maintenance mode
 * @returns boolean - true if in maintenance mode
 */
export function isInMaintenanceMode(): boolean {
  return apiStatusState.status.value === "maintenance";
}
 
/**
 * Checks if the application is currently in system failure mode
 * @returns boolean - true if in system failure mode
 */
export function isInSystemFailureMode(): boolean {
  return apiStatusState.status.value === "systemFailure";
}
 
/**
 * Handles API response and determines the appropriate status
 * @param response - The API response
 * @param baseUrl - Optional base URL for the API
 * @returns boolean - true if status was changed from normal
 */
export function handleApiResponseForDownTime(
  response: any,
  baseUrl?: string
): boolean {
  if (!response || typeof response !== "object" || Array.isArray(response)) {
    const prev = apiStatusState.status.value;
    if (prev !== "systemFailure") {
      setApiStatus("systemFailure", baseUrl);
      return true;
    }
    return false;
  }
 
  if (
    response.errors &&
    Array.isArray(response.errors) &&
    response.errors.length > 0
  ) {
    const firstError = response.errors[0];
    const status = determineStatusFromError(firstError);
 
    if (status === "normal") {
      return false;
    }
 
    const prev = apiStatusState.status.value;
    if (prev !== status) {
      setApiStatus(status, baseUrl);
      return true;
    }
    return false;
  }
 
  return false;
}