Files
homepage/src/routes/api/tasks/[id]/+server.ts
T
Alexander c2007aaa4a tasks: add refresh mode toggle (completion date vs planned date)
Recurring tasks can now calculate next due date from either the
completion time (default) or the planned due date, catching up
if overdue.
2026-04-02 17:29:08 +02:00

50 lines
1.7 KiB
TypeScript

import type { RequestHandler } from '@sveltejs/kit';
import { Task } from '$models/Task';
import { dbConnect } from '$utils/db';
import { error, json } from '@sveltejs/kit';
export const PUT: RequestHandler = async ({ params, request, locals }) => {
const auth = await locals.auth();
if (!auth?.user?.nickname) throw error(401, 'Not logged in');
const data = await request.json();
const { title, description, assignees, tags, difficulty, refreshMode, isRecurring, frequency, nextDueDate, active } = data;
await dbConnect();
const task = await Task.findById(params.id);
if (!task) throw error(404, 'Task not found');
if (title !== undefined) task.title = title.trim();
if (description !== undefined) task.description = description?.trim();
if (assignees !== undefined) task.assignees = assignees;
if (tags !== undefined) task.tags = tags;
if (difficulty !== undefined) task.difficulty = difficulty || undefined;
if (refreshMode !== undefined) task.refreshMode = refreshMode || 'completion';
if (isRecurring !== undefined) {
task.isRecurring = isRecurring;
task.frequency = isRecurring ? frequency : undefined;
}
if (nextDueDate !== undefined) task.nextDueDate = new Date(nextDueDate);
if (active !== undefined) task.active = active;
await task.save();
return json({ task });
};
export const DELETE: RequestHandler = async ({ params, locals }) => {
const auth = await locals.auth();
if (!auth?.user?.nickname) throw error(401, 'Not logged in');
await dbConnect();
const task = await Task.findById(params.id);
if (!task) throw error(404, 'Task not found');
// Soft delete
task.active = false;
await task.save();
return json({ success: true });
};