From 0c002edefa16f7d3f5b97d86854ac93d00ae66dd Mon Sep 17 00:00:00 2001 From: themodrnhakr Date: Sun, 28 Sep 2025 15:41:13 -0500 Subject: [PATCH] Add getByTaskId() and getByDbId() methods. `getByTaskId()` takes an array of strings. It is parsed into task prefix (e.g. "TA") and task_id. The database is then queried on an array of task_ids. The result is wrapped in an object with a status code. Currently, the code is hardcoded as "ok", but can be fleshed out when additional features are implemented. There are a few features missing: - Validation that each string is the right length - Validation that syntactically valid prefixes and ids were passed - A check that all requested records are returned - A check that task prefixes actually exist in the config (requires implementing the config module) - Proper status codes `getByDbId()` takes and array of id numbers. The database is then queried with this array. The results are wrapped as above. Missing features: - A check that all requested records are returned - Proper status codes Both methods return `{status: "failed", error}` when the database request throws an error. --- src/lib/server/services/tasks.ts | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/lib/server/services/tasks.ts b/src/lib/server/services/tasks.ts index 1416c0e..90b0760 100644 --- a/src/lib/server/services/tasks.ts +++ b/src/lib/server/services/tasks.ts @@ -1,5 +1,6 @@ import { type DB, db } from "$lib/server/db/db"; import logger from "../logger"; + class TasksService { private db: DB; private caller: "internal" | "api"; @@ -24,6 +25,47 @@ class TasksService { return false as const; } } + + public async getByTaskId(taskIds: Array) { + const mappedTasks = taskIds.map(x => { + const prefix = x.slice(0, 2); + const task_id = x.slice(2); + return { prefix, task_id }; + }); + logger.info( + `Fetching ${ + taskIds.length === 0 + ? "0 records" + : taskIds.length < 10 + ? taskIds.join(", ") + : `${taskIds.length} records` + }.`, + ); + try { + const tasks = await db.query.tasks.findMany({ + with: { type: true }, + where: (tasks, { inArray }) => inArray(tasks.taskId, mappedTasks.map(x => x.task_id)), + }); + return { tasks, status: "ok" }; + } catch (error) { + logger.error({ msg: "Error querying the database.", error }); + return { status: "failed", error }; + } + } + + public async getByDbId(ids: Array) { + logger.info(`Fetching ${ids.length} records.`); + try { + const tasks = await db.query.tasks.findMany({ + with: { type: true }, + where: (tasks, { inArray }) => inArray(tasks.id, ids), + }); + return { tasks, status: "ok" }; + } catch (error) { + logger.error({ msg: "Error querying the database.", error }); + return { status: "failed", error }; + } + } } export default TasksService;