Spaces:
Runtime error
Runtime error
Nyk commited on
Commit ·
3fd1706
1
Parent(s): c90f3b4
fix(tasks): auto-normalize assignment status transitions
Browse files- src/app/api/tasks/[id]/route.ts +15 -8
- src/app/api/tasks/route.ts +5 -3
- src/lib/__tests__/task-status.test.ts +47 -0
- src/lib/task-status.ts +40 -0
src/app/api/tasks/[id]/route.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { mutationLimiter } from '@/lib/rate-limit';
|
|
| 6 |
import { logger } from '@/lib/logger';
|
| 7 |
import { validateBody, updateTaskSchema } from '@/lib/validation';
|
| 8 |
import { resolveMentionRecipients } from '@/lib/mentions';
|
|
|
|
| 9 |
|
| 10 |
function formatTicketRef(prefix?: string | null, num?: number | null): string | undefined {
|
| 11 |
if (!prefix || typeof num !== 'number' || !Number.isFinite(num) || num <= 0) return undefined
|
|
@@ -115,7 +116,7 @@ export async function PUT(
|
|
| 115 |
const {
|
| 116 |
title,
|
| 117 |
description,
|
| 118 |
-
status,
|
| 119 |
priority,
|
| 120 |
project_id,
|
| 121 |
assigned_to,
|
|
@@ -125,6 +126,12 @@ export async function PUT(
|
|
| 125 |
tags,
|
| 126 |
metadata
|
| 127 |
} = body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
const now = Math.floor(Date.now() / 1000);
|
| 130 |
const descriptionMentionResolution = description !== undefined
|
|
@@ -152,15 +159,15 @@ export async function PUT(
|
|
| 152 |
fieldsToUpdate.push('description = ?');
|
| 153 |
updateParams.push(description);
|
| 154 |
}
|
| 155 |
-
if (
|
| 156 |
-
if (
|
| 157 |
return NextResponse.json(
|
| 158 |
{ error: 'Aegis approval is required to move task to done.' },
|
| 159 |
{ status: 403 }
|
| 160 |
)
|
| 161 |
}
|
| 162 |
fieldsToUpdate.push('status = ?');
|
| 163 |
-
updateParams.push(
|
| 164 |
}
|
| 165 |
if (priority !== undefined) {
|
| 166 |
fieldsToUpdate.push('priority = ?');
|
|
@@ -240,8 +247,8 @@ export async function PUT(
|
|
| 240 |
// Track changes and log activities
|
| 241 |
const changes: string[] = [];
|
| 242 |
|
| 243 |
-
if (
|
| 244 |
-
changes.push(`status: ${currentTask.status} → ${
|
| 245 |
|
| 246 |
// Create notification for status change if assigned
|
| 247 |
if (currentTask.assigned_to) {
|
|
@@ -249,7 +256,7 @@ export async function PUT(
|
|
| 249 |
currentTask.assigned_to,
|
| 250 |
'status_change',
|
| 251 |
'Task Status Updated',
|
| 252 |
-
`Task "${currentTask.title}" status changed to ${
|
| 253 |
'task',
|
| 254 |
taskId,
|
| 255 |
workspaceId
|
|
@@ -322,7 +329,7 @@ export async function PUT(
|
|
| 322 |
priority: currentTask.priority,
|
| 323 |
assigned_to: currentTask.assigned_to
|
| 324 |
},
|
| 325 |
-
newValues: { title, status, priority, assigned_to }
|
| 326 |
},
|
| 327 |
workspaceId
|
| 328 |
);
|
|
|
|
| 6 |
import { logger } from '@/lib/logger';
|
| 7 |
import { validateBody, updateTaskSchema } from '@/lib/validation';
|
| 8 |
import { resolveMentionRecipients } from '@/lib/mentions';
|
| 9 |
+
import { normalizeTaskUpdateStatus } from '@/lib/task-status';
|
| 10 |
|
| 11 |
function formatTicketRef(prefix?: string | null, num?: number | null): string | undefined {
|
| 12 |
if (!prefix || typeof num !== 'number' || !Number.isFinite(num) || num <= 0) return undefined
|
|
|
|
| 116 |
const {
|
| 117 |
title,
|
| 118 |
description,
|
| 119 |
+
status: requestedStatus,
|
| 120 |
priority,
|
| 121 |
project_id,
|
| 122 |
assigned_to,
|
|
|
|
| 126 |
tags,
|
| 127 |
metadata
|
| 128 |
} = body;
|
| 129 |
+
const normalizedStatus = normalizeTaskUpdateStatus({
|
| 130 |
+
currentStatus: currentTask.status,
|
| 131 |
+
requestedStatus,
|
| 132 |
+
assignedTo: assigned_to,
|
| 133 |
+
assignedToProvided: assigned_to !== undefined,
|
| 134 |
+
})
|
| 135 |
|
| 136 |
const now = Math.floor(Date.now() / 1000);
|
| 137 |
const descriptionMentionResolution = description !== undefined
|
|
|
|
| 159 |
fieldsToUpdate.push('description = ?');
|
| 160 |
updateParams.push(description);
|
| 161 |
}
|
| 162 |
+
if (normalizedStatus !== undefined) {
|
| 163 |
+
if (normalizedStatus === 'done' && !hasAegisApproval(db, taskId, workspaceId)) {
|
| 164 |
return NextResponse.json(
|
| 165 |
{ error: 'Aegis approval is required to move task to done.' },
|
| 166 |
{ status: 403 }
|
| 167 |
)
|
| 168 |
}
|
| 169 |
fieldsToUpdate.push('status = ?');
|
| 170 |
+
updateParams.push(normalizedStatus);
|
| 171 |
}
|
| 172 |
if (priority !== undefined) {
|
| 173 |
fieldsToUpdate.push('priority = ?');
|
|
|
|
| 247 |
// Track changes and log activities
|
| 248 |
const changes: string[] = [];
|
| 249 |
|
| 250 |
+
if (normalizedStatus !== undefined && normalizedStatus !== currentTask.status) {
|
| 251 |
+
changes.push(`status: ${currentTask.status} → ${normalizedStatus}`);
|
| 252 |
|
| 253 |
// Create notification for status change if assigned
|
| 254 |
if (currentTask.assigned_to) {
|
|
|
|
| 256 |
currentTask.assigned_to,
|
| 257 |
'status_change',
|
| 258 |
'Task Status Updated',
|
| 259 |
+
`Task "${currentTask.title}" status changed to ${normalizedStatus}`,
|
| 260 |
'task',
|
| 261 |
taskId,
|
| 262 |
workspaceId
|
|
|
|
| 329 |
priority: currentTask.priority,
|
| 330 |
assigned_to: currentTask.assigned_to
|
| 331 |
},
|
| 332 |
+
newValues: { title, status: normalizedStatus ?? currentTask.status, priority, assigned_to }
|
| 333 |
},
|
| 334 |
workspaceId
|
| 335 |
);
|
src/app/api/tasks/route.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { mutationLimiter } from '@/lib/rate-limit';
|
|
| 6 |
import { logger } from '@/lib/logger';
|
| 7 |
import { validateBody, createTaskSchema, bulkUpdateTaskStatusSchema } from '@/lib/validation';
|
| 8 |
import { resolveMentionRecipients } from '@/lib/mentions';
|
|
|
|
| 9 |
|
| 10 |
function formatTicketRef(prefix?: string | null, num?: number | null): string | undefined {
|
| 11 |
if (!prefix || typeof num !== 'number' || !Number.isFinite(num) || num <= 0) return undefined
|
|
@@ -163,7 +164,7 @@ export async function POST(request: NextRequest) {
|
|
| 163 |
const {
|
| 164 |
title,
|
| 165 |
description,
|
| 166 |
-
status
|
| 167 |
priority = 'medium',
|
| 168 |
project_id,
|
| 169 |
assigned_to,
|
|
@@ -173,6 +174,7 @@ export async function POST(request: NextRequest) {
|
|
| 173 |
tags = [],
|
| 174 |
metadata = {}
|
| 175 |
} = body;
|
|
|
|
| 176 |
|
| 177 |
// Check for duplicate title
|
| 178 |
const existingTask = db.prepare('SELECT id FROM tasks WHERE title = ? AND workspace_id = ?').get(title, workspaceId);
|
|
@@ -212,7 +214,7 @@ export async function POST(request: NextRequest) {
|
|
| 212 |
const dbResult = insertStmt.run(
|
| 213 |
title,
|
| 214 |
description,
|
| 215 |
-
|
| 216 |
priority,
|
| 217 |
resolvedProjectId,
|
| 218 |
row.ticket_counter,
|
|
@@ -234,7 +236,7 @@ export async function POST(request: NextRequest) {
|
|
| 234 |
// Log activity
|
| 235 |
db_helpers.logActivity('task_created', 'task', taskId, created_by, `Created task: ${title}`, {
|
| 236 |
title,
|
| 237 |
-
status,
|
| 238 |
priority,
|
| 239 |
assigned_to
|
| 240 |
}, workspaceId);
|
|
|
|
| 6 |
import { logger } from '@/lib/logger';
|
| 7 |
import { validateBody, createTaskSchema, bulkUpdateTaskStatusSchema } from '@/lib/validation';
|
| 8 |
import { resolveMentionRecipients } from '@/lib/mentions';
|
| 9 |
+
import { normalizeTaskCreateStatus } from '@/lib/task-status';
|
| 10 |
|
| 11 |
function formatTicketRef(prefix?: string | null, num?: number | null): string | undefined {
|
| 12 |
if (!prefix || typeof num !== 'number' || !Number.isFinite(num) || num <= 0) return undefined
|
|
|
|
| 164 |
const {
|
| 165 |
title,
|
| 166 |
description,
|
| 167 |
+
status,
|
| 168 |
priority = 'medium',
|
| 169 |
project_id,
|
| 170 |
assigned_to,
|
|
|
|
| 174 |
tags = [],
|
| 175 |
metadata = {}
|
| 176 |
} = body;
|
| 177 |
+
const normalizedStatus = normalizeTaskCreateStatus(status, assigned_to)
|
| 178 |
|
| 179 |
// Check for duplicate title
|
| 180 |
const existingTask = db.prepare('SELECT id FROM tasks WHERE title = ? AND workspace_id = ?').get(title, workspaceId);
|
|
|
|
| 214 |
const dbResult = insertStmt.run(
|
| 215 |
title,
|
| 216 |
description,
|
| 217 |
+
normalizedStatus,
|
| 218 |
priority,
|
| 219 |
resolvedProjectId,
|
| 220 |
row.ticket_counter,
|
|
|
|
| 236 |
// Log activity
|
| 237 |
db_helpers.logActivity('task_created', 'task', taskId, created_by, `Created task: ${title}`, {
|
| 238 |
title,
|
| 239 |
+
status: normalizedStatus,
|
| 240 |
priority,
|
| 241 |
assigned_to
|
| 242 |
}, workspaceId);
|
src/lib/__tests__/task-status.test.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest'
|
| 2 |
+
import { normalizeTaskCreateStatus, normalizeTaskUpdateStatus } from '../task-status'
|
| 3 |
+
|
| 4 |
+
describe('task status normalization', () => {
|
| 5 |
+
it('sets assigned status on create when assignee is present', () => {
|
| 6 |
+
expect(normalizeTaskCreateStatus(undefined, 'main')).toBe('assigned')
|
| 7 |
+
expect(normalizeTaskCreateStatus('inbox', 'main')).toBe('assigned')
|
| 8 |
+
})
|
| 9 |
+
|
| 10 |
+
it('keeps explicit non-inbox status on create', () => {
|
| 11 |
+
expect(normalizeTaskCreateStatus('in_progress', 'main')).toBe('in_progress')
|
| 12 |
+
})
|
| 13 |
+
|
| 14 |
+
it('auto-promotes inbox to assigned when assignment is added via update', () => {
|
| 15 |
+
expect(
|
| 16 |
+
normalizeTaskUpdateStatus({
|
| 17 |
+
currentStatus: 'inbox',
|
| 18 |
+
requestedStatus: undefined,
|
| 19 |
+
assignedTo: 'main',
|
| 20 |
+
assignedToProvided: true,
|
| 21 |
+
})
|
| 22 |
+
).toBe('assigned')
|
| 23 |
+
})
|
| 24 |
+
|
| 25 |
+
it('auto-demotes assigned to inbox when assignment is removed via update', () => {
|
| 26 |
+
expect(
|
| 27 |
+
normalizeTaskUpdateStatus({
|
| 28 |
+
currentStatus: 'assigned',
|
| 29 |
+
requestedStatus: undefined,
|
| 30 |
+
assignedTo: '',
|
| 31 |
+
assignedToProvided: true,
|
| 32 |
+
})
|
| 33 |
+
).toBe('inbox')
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
it('does not override explicit status changes on update', () => {
|
| 37 |
+
expect(
|
| 38 |
+
normalizeTaskUpdateStatus({
|
| 39 |
+
currentStatus: 'inbox',
|
| 40 |
+
requestedStatus: 'in_progress',
|
| 41 |
+
assignedTo: 'main',
|
| 42 |
+
assignedToProvided: true,
|
| 43 |
+
})
|
| 44 |
+
).toBe('in_progress')
|
| 45 |
+
})
|
| 46 |
+
})
|
| 47 |
+
|
src/lib/task-status.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { Task } from './db'
|
| 2 |
+
|
| 3 |
+
export type TaskStatus = Task['status']
|
| 4 |
+
|
| 5 |
+
function hasAssignee(assignedTo: string | null | undefined): boolean {
|
| 6 |
+
return Boolean(assignedTo && assignedTo.trim())
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* Keep task state coherent when a task is created with an assignee.
|
| 11 |
+
* If caller asks for `inbox` but also sets `assigned_to`, normalize to `assigned`.
|
| 12 |
+
*/
|
| 13 |
+
export function normalizeTaskCreateStatus(
|
| 14 |
+
requestedStatus: TaskStatus | undefined,
|
| 15 |
+
assignedTo: string | undefined
|
| 16 |
+
): TaskStatus {
|
| 17 |
+
const status = requestedStatus ?? 'inbox'
|
| 18 |
+
if (status === 'inbox' && hasAssignee(assignedTo)) return 'assigned'
|
| 19 |
+
return status
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
/**
|
| 23 |
+
* Auto-adjust status for assignment-only updates when caller does not
|
| 24 |
+
* explicitly request a status transition.
|
| 25 |
+
*/
|
| 26 |
+
export function normalizeTaskUpdateStatus(args: {
|
| 27 |
+
currentStatus: TaskStatus
|
| 28 |
+
requestedStatus: TaskStatus | undefined
|
| 29 |
+
assignedTo: string | null | undefined
|
| 30 |
+
assignedToProvided: boolean
|
| 31 |
+
}): TaskStatus | undefined {
|
| 32 |
+
const { currentStatus, requestedStatus, assignedTo, assignedToProvided } = args
|
| 33 |
+
if (requestedStatus !== undefined) return requestedStatus
|
| 34 |
+
if (!assignedToProvided) return undefined
|
| 35 |
+
|
| 36 |
+
if (hasAssignee(assignedTo) && currentStatus === 'inbox') return 'assigned'
|
| 37 |
+
if (!hasAssignee(assignedTo) && currentStatus === 'assigned') return 'inbox'
|
| 38 |
+
return undefined
|
| 39 |
+
}
|
| 40 |
+
|