Compare commits

..

3 Commits

Author SHA1 Message Date
8c0970a19a Update calls to TaskService. 2025-09-28 16:16:49 -05:00
fca17a796d Update getAll() to return status codes. 2025-09-28 16:12:07 -05:00
0c002edefa 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.
2025-09-28 16:08:15 -05:00
2 changed files with 51 additions and 9 deletions

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";
@ -12,16 +13,57 @@ class TasksService {
public async getAll() {
logger.info("Fetching all task records...");
try {
const result = await this.db.query.tasks.findMany({
const tasks = await this.db.query.tasks.findMany({
with: {
type: true,
},
});
logger.debug(`Found ${result.length} records.`);
return result;
} catch (e) {
logger.error({ msg: "Error querying the database.", error: e });
return false as const;
logger.debug(`Found ${tasks.length} records.`);
return { tasks, status: "ok" };
} catch (error) {
logger.error({ msg: "Error querying the database.", error });
return { status: "failed", error };
}
}
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 };
}
}
}

View File

@ -4,8 +4,8 @@
let { data }: PageProps = $props();
</script>
{#if data.tasks}
<p>{data.tasks.length} total records.</p>
{#if data.tasks.status === "ok" && data.tasks.tasks !== undefined}
<p>{data.tasks.tasks.length} total records.</p>
<table>
<thead>
<tr>
@ -15,7 +15,7 @@
</tr>
</thead>
<tbody>
{#each data.tasks as task (task.id)}
{#each data.tasks.tasks as task (task.id)}
<tr>
<td
>{task.type?.prefix +