Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/app/api/sprints/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,21 @@ export async function PATCH(request: NextRequest, context: RouteContext) {

const sprint = db.updateSprint(params.id, data);

let rolledOver = 0;
if (data.reviewed_at && data.status === 'completed') {
const activeSprint = db.getActiveSprint() ?? db.rotateSprintIfNeeded();
if (activeSprint && activeSprint.id !== params.id) {
rolledOver = db.rolloverIncompleteTodos(params.id, activeSprint.id);
}
}

eventBus.publish({
type: 'sprint:updated',
payload: { sprint },
timestamp: Date.now(),
});

return NextResponse.json({ sprint }, { status: 200, headers: corsHeaders(request) });
return NextResponse.json({ sprint, rolled_over: rolledOver }, { status: 200, headers: corsHeaders(request) });
} catch (error) {
if (error instanceof z.ZodError) {
return NextResponse.json({ error: 'Invalid request body', details: error.issues }, { status: 400, headers: corsHeaders(request) });
Expand Down
20 changes: 20 additions & 0 deletions src/lib/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,26 @@ const db: DatabaseOperations = {
stmt.run(todoId, sprintId);
},

rolloverIncompleteTodos(fromSprintId: string, toSprintId: string): number {
const database = getDatabase();
const todos = db.getSprintTodos(fromSprintId);
const incomplete = todos.filter(
(t) => (t.status === 'pending' || t.status === 'in_progress' || t.status === 'blocked') && !t.archived_at
);

const insertStmt = database.prepare(`
INSERT INTO todo_sprints (todo_id, sprint_id)
VALUES (?, ?)
ON CONFLICT(todo_id, sprint_id) DO NOTHING
`);

for (const todo of incomplete) {
insertStmt.run(todo.id, toSprintId);
}

return incomplete.length;
},

removeTodoFromSprint(todoId: string, sprintId: string): void {
const database = getDatabase();
const stmt = database.prepare('DELETE FROM todo_sprints WHERE todo_id = ? AND sprint_id = ?');
Expand Down
1 change: 1 addition & 0 deletions src/lib/db/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export interface DatabaseOperations {
getSprint(id: string): Sprint | null;
getAllSprints(): Sprint[];
updateSprint(id: string, updates: Partial<Omit<Sprint, 'id' | 'created_at'>>): Sprint;
rolloverIncompleteTodos(fromSprintId: string, toSprintId: string): number;
assignTodoToSprint(todoId: string, sprintId: string): void;
removeTodoFromSprint(todoId: string, sprintId: string): void;
getSprintTodos(sprintId: string): Todo[];
Expand Down