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.
This commit is contained in:
themodrnhakr 2025-09-28 15:41:13 -05:00
parent 136b58c44f
commit 0c002edefa

View File

@ -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<string>) {
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<number>) {
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;