You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB
TypeScript

import type { Database } from 'bun:sqlite';
import { db } from '../db';
type CreateTaskInput = {
description: string;
due_date?: string | null; // Expect 'YYYY-MM-DD' or null
group_id?: string | null; // Full JID (e.g., 'xxx@g.us') or null
created_by: string; // Normalized user ID
};
type CreateAssignmentInput = {
user_id: string; // Normalized user ID
assigned_by: string; // Normalized user ID (typically created_by)
};
export class TaskService {
static dbInstance: Database = db;
static createTask(task: CreateTaskInput, assignments: CreateAssignmentInput[] = []): number {
const runTx = this.dbInstance.transaction(() => {
const insertTask = this.dbInstance.prepare(`
INSERT INTO tasks (description, due_date, group_id, created_by)
VALUES (?, ?, ?, ?)
`);
const runResult = insertTask.run(
task.description,
task.due_date ?? null,
task.group_id ?? null,
task.created_by
);
const taskId = Number((runResult as any).lastInsertRowid);
if (assignments.length > 0) {
const insertAssignment = this.dbInstance.prepare(`
INSERT INTO task_assignments (task_id, user_id, assigned_by)
VALUES (?, ?, ?)
`);
// Evitar duplicados por (task_id, user_id)
const seen = new Set<string>();
for (const a of assignments) {
if (seen.has(a.user_id)) continue;
seen.add(a.user_id);
insertAssignment.run(taskId, a.user_id, a.assigned_by);
}
}
return taskId;
});
return runTx();
}
// We'll add more methods here as we progress
}