From 32b9a8098179c561a82b4814599bdc168a82118d Mon Sep 17 00:00:00 2001 From: solvhold Date: Mon, 9 Mar 2026 16:23:14 +0100 Subject: [PATCH] feat: :sparkles: fix error message issue: #126 --- src/db-access/teams.ts | 18 +++++++++++++++++- src/routers/teams.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/db-access/teams.ts b/src/db-access/teams.ts index 13c4637..8990d1c 100644 --- a/src/db-access/teams.ts +++ b/src/db-access/teams.ts @@ -2,7 +2,8 @@ import { database } from "@/db/setup/query-postgres"; import { teamsTable } from "@/db/tables/teams"; import { type OrmResult, ormError } from "@/src/error/orm-error"; import type { Team, TeamKey } from "@/src/response-handling/teams"; -import { inArray } from "drizzle-orm"; +import { eq, inArray } from "drizzle-orm"; +import type { QueryParameters } from "../request-handling/common"; import { newDatabaseTransaction } from "./common"; export async function selectTeamsById( @@ -19,3 +20,18 @@ export async function selectTeamsById( return selectResult; }); } + +export const selectActiveTeams = async ( + parameters: QueryParameters, +): Promise> => { + return await newDatabaseTransaction(database, async (tx) => { + const teams = await tx + .select() + .from(teamsTable) + .where(eq(teamsTable.active, true)) + .limit(parameters.limit) + .offset(parameters.offset); + + return teams; + }); +}; diff --git a/src/routers/teams.ts b/src/routers/teams.ts index 97e4a34..e1ca51f 100644 --- a/src/routers/teams.ts +++ b/src/routers/teams.ts @@ -1,7 +1,8 @@ -import { selectTeamsById } from "@/src/db-access/teams"; +import { selectActiveTeams, selectTeamsById } from "@/src/db-access/teams"; import { clientError } from "@/src/error/http-errors"; import { listQueryParser, + toListQueryParser, toSerialIdParser, } from "@/src/request-handling/common"; import { Router, json } from "express"; @@ -9,6 +10,44 @@ import { Router, json } from "express"; export const teamsRouter = Router(); teamsRouter.use(json()); +/** + * @openapi + * /teams/active/: + * get: + * tags: [teams] + * summary: Get all active teams + * description: Return all active teams + * parameters: + * - $ref: "#/components/parameters/offset" + * - $ref: "#/components/parameters/limit" + * responses: + * 200: + * description: Successfull response + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/teams" + */ +teamsRouter.get("/active", async (req, res, next) => { + const queryParametersResult = toListQueryParser.safeParse(req.query); + if (!queryParametersResult.success) { + return next( + clientError(400, "Invalid request format", queryParametersResult.error), + ); + } + + const results = await selectActiveTeams(queryParametersResult.data); + if (!results.success) { + return next( + clientError( + 400, + "Failed to retrieve data from the database", + results.error, + ), + ); + } + res.json(results.data); +}); /** * @openapi * /teams/{teamId}/: