diff --git a/src/controllers/api-status.controller.ts b/src/controllers/api-status.controller.ts index 78a1c90b..d9aeaf94 100644 --- a/src/controllers/api-status.controller.ts +++ b/src/controllers/api-status.controller.ts @@ -1,6 +1,9 @@ import { Request, Response } from "express"; import { asyncHandler } from "../middleware/asyncHandler"; -import { parseJsonResponse } from "../services/api-status.services"; +import { + fetchApiStatusService, + parseJsonResponse, +} from "../services/api-status.services"; import { sendJsonResponse } from "../utils/sendJsonResponse"; export const createApiStatus = asyncHandler( @@ -11,3 +14,11 @@ export const createApiStatus = asyncHandler( sendJsonResponse(res, 201, "API status updated successfully"); }, ); + +export const getApiStatus = asyncHandler( + async (_req: Request, res: Response) => { + const response = await fetchApiStatusService(); + + sendJsonResponse(res, 200, "Api status", { response }); + }, +); diff --git a/src/routes/api-status.ts b/src/routes/api-status.ts index c699ff13..4b7e3cfd 100644 --- a/src/routes/api-status.ts +++ b/src/routes/api-status.ts @@ -1,8 +1,12 @@ import { Router } from "express"; -import { createApiStatus } from "../controllers/api-status.controller"; +import { + createApiStatus, + getApiStatus, +} from "../controllers/api-status.controller"; const apiStatusRouter = Router(); apiStatusRouter.post("/api-status", createApiStatus); +apiStatusRouter.get("/api-status", getApiStatus); export { apiStatusRouter }; diff --git a/src/services/api-status.services.ts b/src/services/api-status.services.ts index feba7f27..d738f3d5 100644 --- a/src/services/api-status.services.ts +++ b/src/services/api-status.services.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import AppDataSource from "../data-source"; import { API_STATUS, ApiStatus } from "../models/api-model"; +import { GroupedApi } from "../types"; const apiStatusRepository = AppDataSource.getRepository(ApiStatus); const MAX_ALLOWED_RESPONSE_TIME = 2000; @@ -47,4 +48,53 @@ const parseJsonResponse = async (resultJson: any): Promise => { } }; -export { parseJsonResponse }; +const fetchApiStatusService = async () => { + const api_status_response = await apiStatusRepository.find(); + + const groupedAPIs = api_status_response.reduce((acc, current) => { + const existingGroup = acc.find( + (group) => group.api_group === current.api_group, + ); + + if (existingGroup) { + existingGroup.collection.push({ + api_name: current.api_name, + is_operational: current.status, + details: current.details, + last_checked: current.updated_at, + }); + } else { + acc.push({ + api_group: current.api_group, + is_operational: API_STATUS.OPERATIONAL, + collection: [ + { + api_name: current.api_name, + is_operational: current.status, + details: current.details, + last_checked: current.updated_at, + }, + ], + }); + } + + return acc; + }, [] as GroupedApi[]); + + const response_dto = groupedAPIs.map((api) => { + const hasDegraded = api.collection.some( + (apiCollection) => + apiCollection.is_operational !== API_STATUS.OPERATIONAL, + ); + + if (hasDegraded) { + api.is_operational = API_STATUS.DEGRADED; + } + + return api; + }); + + return response_dto; +}; + +export { fetchApiStatusService, parseJsonResponse }; diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 81e8e02a..647e0c02 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,5 +1,4 @@ import { User } from "../models"; -import { Permissions } from "../models/permissions.entity"; export interface IUserService { getUserById(id: string): Promise; @@ -123,3 +122,14 @@ export type UpdateUserRecordOption = { export interface IBillingPlanService { createBillingPlan(planData: Partial): Promise; } + +export type GroupedApi = { + api_group: string; + is_operational: API_STATUS; + collection: { + api_name: string; + is_operational: string; + details: string; + last_checked: Date; + }[]; +};