From 6d2008e8d39e98030d2e1e0216e6af1e8df59869 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 20 Mar 2026 10:59:34 -0700 Subject: [PATCH] geo checks service --- server/src/controllers/geoCheckController.ts | 28 ++++++++++++--- .../src/service/business/geoChecksService.ts | 35 +++++++++++++++---- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/server/src/controllers/geoCheckController.ts b/server/src/controllers/geoCheckController.ts index cfa7fe056..aef0d7e7c 100644 --- a/server/src/controllers/geoCheckController.ts +++ b/server/src/controllers/geoCheckController.ts @@ -1,6 +1,8 @@ import { Request, Response, NextFunction } from "express"; import { getChecksParamValidation, getChecksQueryValidation } from "@/validation/checkValidation.js"; import type { IGeoChecksService } from "@/service/business/geoChecksService.js"; +import { requireTeamId } from "./controllerUtils.js"; +import { AppError } from "@/utils/AppError.js"; const SERVICE_NAME = "geoCheckController"; @@ -18,13 +20,29 @@ class GeoCheckController { getGeoChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => { try { - getChecksParamValidation.parse(req.params); - getChecksQueryValidation.parse(req.query); + const validatedParams = getChecksParamValidation.parse(req.params); + const validatedQuery = getChecksQueryValidation.parse(req.query); + + if (!validatedQuery.continent || validatedQuery.continent.length === 0) { + throw new AppError({ + message: "At least one continent must be specified", + service: SERVICE_NAME, + method: "getGeoChecksByMonitor", + details: { monitorId: validatedParams.monitorId }, + status: 400, + }); + } + + const teamId = requireTeamId(req.user?.teamId); const result = await this.geoChecksService.getGeoChecksByMonitor({ - monitorId: req?.params?.monitorId as string, - query: req?.query, - teamId: req?.user?.teamId as string, + monitorId: validatedParams.monitorId, + teamId: teamId, + sortOrder: validatedQuery.sortOrder, + dateRange: validatedQuery.dateRange, + page: validatedQuery.page, + rowsPerPage: validatedQuery.rowsPerPage, + continent: validatedQuery.continent, }); return res.status(200).json({ diff --git a/server/src/service/business/geoChecksService.ts b/server/src/service/business/geoChecksService.ts index f35826a8f..2793cc542 100644 --- a/server/src/service/business/geoChecksService.ts +++ b/server/src/service/business/geoChecksService.ts @@ -1,5 +1,5 @@ import type { Monitor, GeoCheck } from "@/types/index.js"; -import type { GeoCheckResult } from "@/types/geoCheck.js"; +import type { GeoCheckResult, GeoContinent } from "@/types/geoCheck.js"; import { Types } from "mongoose"; import type { IGeoChecksRepository } from "@/repositories/index.js"; import type { IMonitorsRepository } from "@/repositories/index.js"; @@ -13,7 +13,15 @@ export interface IGeoChecksService { readonly serviceName: string; buildGeoCheck(monitor: Monitor): Promise; createGeoChecks(geoChecks: GeoCheck[]): Promise; - getGeoChecksByMonitor(args: { monitorId: string; query: any; teamId: string }): Promise; + getGeoChecksByMonitor(args: { + monitorId: string; + teamId: string; + sortOrder: string; + dateRange: string; + page?: number; + rowsPerPage?: number; + continent: GeoContinent | GeoContinent[]; + }): Promise; } export class GeoChecksService implements IGeoChecksService { @@ -139,7 +147,23 @@ export class GeoChecksService implements IGeoChecksService { return this.geoChecksRepository.createGeoChecks(geoChecks); }; - getGeoChecksByMonitor = async ({ monitorId, query, teamId }: { monitorId: string; query: any; teamId: string }) => { + getGeoChecksByMonitor = async ({ + monitorId, + teamId, + sortOrder, + dateRange, + page, + rowsPerPage, + continent, + }: { + monitorId: string; + teamId: string; + sortOrder: string; + dateRange: string; + page?: number; + rowsPerPage?: number; + continent: GeoContinent | GeoContinent[]; + }) => { if (!monitorId) { throw new AppError({ message: "No monitor ID in request", @@ -167,10 +191,9 @@ export class GeoChecksService implements IGeoChecksService { }); } - const { sortOrder, dateRange, page, rowsPerPage, continent } = query; const continents = continent ? (Array.isArray(continent) ? continent : [continent]) : undefined; - const parsedPage = page ? parseInt(page) : page; - const parsedRowsPerPage = rowsPerPage ? parseInt(rowsPerPage) : rowsPerPage; + const parsedPage = page || 0; + const parsedRowsPerPage = rowsPerPage || 5; const result = await this.geoChecksRepository.findByMonitorId(monitorId, sortOrder, dateRange, parsedPage, parsedRowsPerPage, continents); return result; };