增强关键接口幂等与限流保护
This commit is contained in:
@@ -213,6 +213,55 @@ export async function updateTask(taskId: number | string, patch: TaskUpdatePatch
|
||||
})
|
||||
}
|
||||
|
||||
export async function updateTaskStatusIfCurrent(
|
||||
taskId: number | string,
|
||||
currentStatus: string,
|
||||
patch: TaskUpdatePatch,
|
||||
): Promise<TaskRow | null> {
|
||||
const now = String(patch.updated_at || '').trim()
|
||||
const result = await query<{ id: number }>(
|
||||
`
|
||||
UPDATE fulfillment_tasks
|
||||
SET
|
||||
task_status = $1,
|
||||
delivery_status = COALESCE($2, delivery_status),
|
||||
result_code = COALESCE($3, result_code),
|
||||
result_message = COALESCE($4, result_message),
|
||||
user_action_status = COALESCE($5, user_action_status),
|
||||
last_error = COALESCE($6, last_error),
|
||||
context_json = COALESCE($7::jsonb, context_json),
|
||||
redeemed_at = COALESCE($8, redeemed_at),
|
||||
updated_at = COALESCE($9, updated_at)
|
||||
WHERE id = $10
|
||||
AND task_status = $11
|
||||
RETURNING id
|
||||
`,
|
||||
[
|
||||
patch.task_status,
|
||||
patch.delivery_status ?? null,
|
||||
patch.result_code ?? null,
|
||||
patch.result_message ?? null,
|
||||
patch.user_action_status ?? null,
|
||||
patch.last_error ?? null,
|
||||
typeof patch.context_json === 'undefined'
|
||||
? null
|
||||
: typeof patch.context_json === 'string'
|
||||
? patch.context_json
|
||||
: JSON.stringify(patch.context_json || {}),
|
||||
patch.redeemed_at ?? null,
|
||||
now || null,
|
||||
Number(taskId),
|
||||
currentStatus,
|
||||
],
|
||||
)
|
||||
|
||||
if (!result.rows[0]) {
|
||||
return null
|
||||
}
|
||||
|
||||
return getTaskById(taskId)
|
||||
}
|
||||
|
||||
export async function getTaskById(taskId: number | string): Promise<TaskRow | null> {
|
||||
return getTaskByIdWithExecutor(query, taskId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user