增加取消订单重新可以发布
This commit is contained in:
@@ -319,6 +319,7 @@ export type WorkOrderCapabilities = {
|
||||
canAccept: boolean
|
||||
canMarkProblem: boolean
|
||||
canDelete: boolean
|
||||
canReopen: boolean
|
||||
canPublish: boolean
|
||||
canCancel: boolean
|
||||
canResolveProblem: boolean
|
||||
|
||||
@@ -1981,6 +1981,84 @@ export async function cancelAssignedWorkOrder(input: {
|
||||
})
|
||||
}
|
||||
|
||||
/** 将已取消工单恢复为未分配状态,不变更历史押金流水。 */
|
||||
export async function reopenCancelledWorkOrder(input: {
|
||||
workOrderId: number
|
||||
now: string
|
||||
actorName?: string
|
||||
}): Promise<{
|
||||
order: WorkOrderRow | null
|
||||
failureReason: 'work_order_not_cancelled' | 'work_order_has_active_shares' | null
|
||||
}> {
|
||||
return withTransaction(async (client) => {
|
||||
const currentResult = await client.query<WorkOrderRow>(
|
||||
`
|
||||
SELECT *
|
||||
FROM work_orders
|
||||
WHERE id = $1
|
||||
FOR UPDATE
|
||||
`,
|
||||
[input.workOrderId],
|
||||
)
|
||||
const workOrder = currentResult.rows[0] || null
|
||||
if (!workOrder || workOrder.status !== 'cancelled') {
|
||||
return { order: null, failureReason: 'work_order_not_cancelled' }
|
||||
}
|
||||
|
||||
const activeShareResult = await client.query<{ total: number }>(
|
||||
`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM work_order_shares
|
||||
WHERE work_order_id = $1
|
||||
AND status != 'cancelled'
|
||||
`,
|
||||
[input.workOrderId],
|
||||
)
|
||||
if (Number(activeShareResult.rows[0]?.total || 0) > 0) {
|
||||
return { order: null, failureReason: 'work_order_has_active_shares' }
|
||||
}
|
||||
|
||||
await client.query(
|
||||
`
|
||||
UPDATE work_orders
|
||||
SET
|
||||
status = 'unassigned',
|
||||
assigned_worker_id = NULL,
|
||||
assigned_at = NULL,
|
||||
deadline_at = NULL,
|
||||
acceptance_json = '{}'::jsonb,
|
||||
draft_acceptance_json = '{}'::jsonb,
|
||||
submitted_at = NULL,
|
||||
accepted_at = NULL,
|
||||
problem_note = '',
|
||||
published_at = NULL,
|
||||
pinned_at = NULL,
|
||||
updated_at = $1
|
||||
WHERE id = $2
|
||||
`,
|
||||
[input.now, input.workOrderId],
|
||||
)
|
||||
|
||||
await createWorkOrderEventWithClient(client, {
|
||||
workOrderId: input.workOrderId,
|
||||
actorType: 'admin',
|
||||
actorId: input.actorName || '',
|
||||
eventType: 'reopened_from_cancelled',
|
||||
fromStatus: 'cancelled',
|
||||
toStatus: 'unassigned',
|
||||
payloadJson: JSON.stringify({
|
||||
previousAssignedWorkerId: Number(workOrder.last_assigned_worker_id || 0) || null,
|
||||
}),
|
||||
now: input.now,
|
||||
})
|
||||
|
||||
return {
|
||||
order: await getWorkOrderByIdWithClient(client, input.workOrderId),
|
||||
failureReason: null,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function countWorkerCancellationsSince(
|
||||
workerId: number | string,
|
||||
sinceIso: string,
|
||||
|
||||
Reference in New Issue
Block a user