|
|
|
|
@ -1,36 +1,52 @@
|
|
|
|
|
import { db } from '../db';
|
|
|
|
|
import type { Task, TaskAssignment } from './model';
|
|
|
|
|
|
|
|
|
|
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 createTask(task: Omit<Task, 'id'>, assignments: Omit<TaskAssignment, 'task_id'>[]) {
|
|
|
|
|
return db.transaction(() => {
|
|
|
|
|
static createTask(task: CreateTaskInput, assignments: CreateAssignmentInput[] = []): number {
|
|
|
|
|
const runTx = db.transaction(() => {
|
|
|
|
|
const insertTask = db.prepare(`
|
|
|
|
|
INSERT INTO tasks (description, due_date, group_id)
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
|
INSERT INTO tasks (description, due_date, group_id, created_by)
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
|
RETURNING id
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
const taskResult = insertTask.get(
|
|
|
|
|
task.description,
|
|
|
|
|
task.due_date?.toISOString(),
|
|
|
|
|
task.group_id
|
|
|
|
|
task.due_date ?? null,
|
|
|
|
|
task.group_id ?? null,
|
|
|
|
|
task.created_by
|
|
|
|
|
) as { id: number };
|
|
|
|
|
|
|
|
|
|
if (assignments.length > 0) {
|
|
|
|
|
const insertAssignment = db.prepare(`
|
|
|
|
|
INSERT INTO task_assignments (task_id, user_id, assigned_by)
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
assignments.forEach(assignment => {
|
|
|
|
|
insertAssignment.run(
|
|
|
|
|
taskResult.id,
|
|
|
|
|
assignment.user_id,
|
|
|
|
|
assignment.assigned_by
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
// 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(taskResult.id, a.user_id, a.assigned_by);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return taskResult.id;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return runTx();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We'll add more methods here as we progress
|
|
|
|
|
|