|
|
|
@ -1,5 +1,5 @@
|
|
|
|
import type { Database } from 'bun:sqlite';
|
|
|
|
import type { Database } from 'bun:sqlite';
|
|
|
|
import { db } from '../db';
|
|
|
|
import { db, ensureUserExists } from '../db';
|
|
|
|
|
|
|
|
|
|
|
|
type CreateTaskInput = {
|
|
|
|
type CreateTaskInput = {
|
|
|
|
description: string;
|
|
|
|
description: string;
|
|
|
|
@ -23,11 +23,16 @@ export class TaskService {
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
`);
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ensuredCreator = ensureUserExists(task.created_by, this.dbInstance);
|
|
|
|
|
|
|
|
if (!ensuredCreator) {
|
|
|
|
|
|
|
|
throw new Error('No se pudo asegurar created_by');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const runResult = insertTask.run(
|
|
|
|
const runResult = insertTask.run(
|
|
|
|
task.description,
|
|
|
|
task.description,
|
|
|
|
task.due_date ?? null,
|
|
|
|
task.due_date ?? null,
|
|
|
|
task.group_id ?? null,
|
|
|
|
task.group_id ?? null,
|
|
|
|
task.created_by
|
|
|
|
ensuredCreator
|
|
|
|
);
|
|
|
|
);
|
|
|
|
const taskId = Number((runResult as any).lastInsertRowid);
|
|
|
|
const taskId = Number((runResult as any).lastInsertRowid);
|
|
|
|
|
|
|
|
|
|
|
|
@ -37,12 +42,18 @@ export class TaskService {
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
`);
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
// Evitar duplicados por (task_id, user_id)
|
|
|
|
// Evitar duplicados por (task_id, user_id) tras asegurar usuarios
|
|
|
|
const seen = new Set<string>();
|
|
|
|
const seen = new Set<string>();
|
|
|
|
for (const a of assignments) {
|
|
|
|
for (const a of assignments) {
|
|
|
|
if (seen.has(a.user_id)) continue;
|
|
|
|
const ensuredUser = ensureUserExists(a.user_id, this.dbInstance);
|
|
|
|
seen.add(a.user_id);
|
|
|
|
if (!ensuredUser) continue;
|
|
|
|
insertAssignment.run(taskId, a.user_id, a.assigned_by);
|
|
|
|
if (seen.has(ensuredUser)) continue;
|
|
|
|
|
|
|
|
seen.add(ensuredUser);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ensuredAssigner =
|
|
|
|
|
|
|
|
ensureUserExists(a.assigned_by || ensuredCreator, this.dbInstance) || ensuredCreator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
insertAssignment.run(taskId, ensuredUser, ensuredAssigner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|