Merge branch 'main' into dev_match
This commit is contained in:
@@ -94,8 +94,43 @@ export async function listTasksByOrderIds(orderIds: number[]): Promise<TaskRow[]
|
||||
|
||||
export async function createTask(input: TaskCreateInput): Promise<TaskRow | null> {
|
||||
return withTransaction(async (client) => {
|
||||
const taskResult = await client.query<{ id: number }>(
|
||||
`
|
||||
// 同一订单项单元的创建必须串行,否则并发调度会同时算出同一尝试号,
|
||||
// 再一起绕过“仅一个当前尝试”的唯一索引。
|
||||
await client.query(`SELECT id FROM order_items WHERE id = $1 FOR UPDATE`, [input.orderItemId])
|
||||
const active = await client.query<{ id: number }>(
|
||||
`SELECT id
|
||||
FROM fulfillment_tasks
|
||||
WHERE order_item_id = $1
|
||||
AND unit_index = $2
|
||||
AND task_status NOT IN (
|
||||
'closed', 'expired', 'failed', 'manual_review', 'retry_pending', 'cancelled'
|
||||
)
|
||||
ORDER BY is_current DESC, id DESC
|
||||
LIMIT 1`,
|
||||
[input.orderItemId, input.unitIndex],
|
||||
)
|
||||
const activeTaskId = Number(active.rows[0]?.id || 0)
|
||||
if (activeTaskId > 0) {
|
||||
return getTaskByIdWithExecutor(client.query.bind(client) as TaskQueryExecutor, activeTaskId)
|
||||
}
|
||||
|
||||
const nextAttempt = await client.query<{ attempt_no: number }>(
|
||||
`SELECT COALESCE(MAX(attempt_no), 0) + 1 AS attempt_no
|
||||
FROM fulfillment_tasks
|
||||
WHERE order_item_id = $1 AND unit_index = $2`,
|
||||
[input.orderItemId, input.unitIndex],
|
||||
)
|
||||
await client.query(
|
||||
`UPDATE fulfillment_tasks
|
||||
SET is_current = FALSE
|
||||
WHERE order_item_id = $1 AND unit_index = $2 AND is_current = TRUE`,
|
||||
[input.orderItemId, input.unitIndex],
|
||||
)
|
||||
const nextAttemptNo = Number(nextAttempt.rows[0]?.attempt_no || 1)
|
||||
let taskResult: { rows: Array<{ id: number }> }
|
||||
try {
|
||||
taskResult = await client.query<{ id: number }>(
|
||||
`
|
||||
INSERT INTO fulfillment_tasks (
|
||||
order_id,
|
||||
order_item_id,
|
||||
@@ -121,42 +156,54 @@ export async function createTask(input: TaskCreateInput): Promise<TaskRow | null
|
||||
last_error,
|
||||
context_json,
|
||||
created_at,
|
||||
updated_at
|
||||
updated_at,
|
||||
attempt_no,
|
||||
is_current
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
|
||||
$11, $12, $13, $14, $15, $16, $17, $18, $19,
|
||||
$20, $21, $22, $23::jsonb, $24, $25
|
||||
$20, $21, $22, $23::jsonb, $24, $25, $26, TRUE
|
||||
)
|
||||
RETURNING id
|
||||
`,
|
||||
[
|
||||
input.orderId,
|
||||
input.orderItemId,
|
||||
input.unitIndex,
|
||||
input.taskNo,
|
||||
input.provider,
|
||||
input.platform,
|
||||
input.shopId || '',
|
||||
input.shopName || '',
|
||||
input.platformOrderId,
|
||||
input.profileId,
|
||||
input.executorKey,
|
||||
input.taskStatus || 'pending_payment',
|
||||
input.deliveryStatus || 'pending',
|
||||
input.resultCode || '',
|
||||
input.resultMessage || '',
|
||||
input.claimToken || '',
|
||||
input.claimExpiresAt || null,
|
||||
input.automationMode || 'manual',
|
||||
Boolean(input.requiresClaim),
|
||||
input.userActionStatus || 'not_required',
|
||||
input.attemptCount || 0,
|
||||
input.lastError || '',
|
||||
input.contextJson || '{}',
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
],
|
||||
)
|
||||
[
|
||||
input.orderId,
|
||||
input.orderItemId,
|
||||
input.unitIndex,
|
||||
input.taskNo,
|
||||
input.provider,
|
||||
input.platform,
|
||||
input.shopId || '',
|
||||
input.shopName || '',
|
||||
input.platformOrderId,
|
||||
input.profileId,
|
||||
input.executorKey,
|
||||
input.taskStatus || 'pending_payment',
|
||||
input.deliveryStatus || 'pending',
|
||||
input.resultCode || '',
|
||||
input.resultMessage || '',
|
||||
input.claimToken || '',
|
||||
input.claimExpiresAt || null,
|
||||
input.automationMode || 'manual',
|
||||
Boolean(input.requiresClaim),
|
||||
input.userActionStatus || 'not_required',
|
||||
input.attemptCount || 0,
|
||||
input.lastError || '',
|
||||
input.contextJson || '{}',
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
nextAttemptNo,
|
||||
],
|
||||
)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
throw new Error(
|
||||
`履约任务创建失败 orderItemId=${input.orderItemId} ` +
|
||||
`unitIndex=${input.unitIndex} taskStatus=${input.taskStatus || 'pending_payment'} ` +
|
||||
`attemptNo=${nextAttemptNo}: ${message}`,
|
||||
{ cause: error },
|
||||
)
|
||||
}
|
||||
|
||||
const taskId = Number(taskResult.rows[0]?.id || 0)
|
||||
if (input.runtimeContext) {
|
||||
@@ -492,10 +539,15 @@ export function buildTaskListFilters({
|
||||
roleId = '',
|
||||
dateFrom = '',
|
||||
dateTo = '',
|
||||
currentOnly = false,
|
||||
}: TaskListQueryInput = {}): { whereClause: string; params: unknown[] } {
|
||||
const filters: string[] = []
|
||||
const params: unknown[] = []
|
||||
|
||||
if (currentOnly) {
|
||||
filters.push('ft.is_current = TRUE')
|
||||
}
|
||||
|
||||
if (keyword) {
|
||||
const trimmedKeyword = String(keyword).trim()
|
||||
const isOrderNoLookup = isPlatformOrderIdLookupKeyword(trimmedKeyword)
|
||||
|
||||
@@ -54,6 +54,28 @@ test('寄卖仓库搜索必须覆盖供货打手姓名、打手电话与页面
|
||||
)
|
||||
})
|
||||
|
||||
test('寄卖仓库按通过审核入库时间筛选,结束日期必须包含当天全天', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const inventoryListStart = source.indexOf('export async function listConsignmentInventories')
|
||||
const inventoryListBody = source.slice(
|
||||
inventoryListStart,
|
||||
source.indexOf('\n}\n', inventoryListStart),
|
||||
)
|
||||
|
||||
assert.ok(
|
||||
inventoryListBody.includes(
|
||||
"ci.inbound_at >= ($${values.length}::date::timestamp AT TIME ZONE 'Asia/Shanghai')",
|
||||
),
|
||||
'开始日期必须按上海时区当天零点筛选 inbound_at',
|
||||
)
|
||||
assert.ok(
|
||||
inventoryListBody.includes(
|
||||
"ci.inbound_at < (($${values.length}::date + INTERVAL '1 day')::timestamp AT TIME ZONE 'Asia/Shanghai')",
|
||||
),
|
||||
'结束日期必须筛选到次日零点之前,不能漏掉结束日当天',
|
||||
)
|
||||
})
|
||||
|
||||
test('匹配候选要带出寄卖单号,并且 join 之后所有列名都必须加 ci. 前缀', async () => {
|
||||
const source = await readFile(new URL('./consignment-repo.ts', import.meta.url), 'utf8')
|
||||
const start = source.indexOf('export async function listActiveConsignmentInventoriesForMatching')
|
||||
|
||||
@@ -158,6 +158,8 @@ export async function listConsignmentInventories(filters: {
|
||||
allowMatching?: boolean | undefined
|
||||
workerId?: number | undefined
|
||||
keyword?: string | undefined
|
||||
dateFrom?: string | undefined
|
||||
dateTo?: string | undefined
|
||||
page?: number | undefined
|
||||
pageSize?: number | undefined
|
||||
}): Promise<{ items: ConsignmentInventoryRow[]; total: number }> {
|
||||
@@ -231,6 +233,18 @@ export async function listConsignmentInventories(filters: {
|
||||
OR ccs.name ILIKE $${values.length})`,
|
||||
)
|
||||
}
|
||||
if (filters.dateFrom) {
|
||||
values.push(filters.dateFrom)
|
||||
conditions.push(
|
||||
`ci.inbound_at >= ($${values.length}::date::timestamp AT TIME ZONE 'Asia/Shanghai')`,
|
||||
)
|
||||
}
|
||||
if (filters.dateTo) {
|
||||
values.push(filters.dateTo)
|
||||
conditions.push(
|
||||
`ci.inbound_at < (($${values.length}::date + INTERVAL '1 day')::timestamp AT TIME ZONE 'Asia/Shanghai')`,
|
||||
)
|
||||
}
|
||||
|
||||
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
|
||||
// 计数与列表必须使用同一组 join,否则新增的提报单 / 供货打手关键字会直接报错。
|
||||
|
||||
Reference in New Issue
Block a user