清理旧消息和历史命名
This commit is contained in:
@@ -1,314 +0,0 @@
|
||||
import { query } from '../db/client.js'
|
||||
import type {
|
||||
MessageDeliveryListQueryResult,
|
||||
MessageDeliveryRow,
|
||||
} from '../types/repository-rows.js'
|
||||
|
||||
type MessageDeliveryCreateInput = {
|
||||
provider: string
|
||||
platform: string
|
||||
shopId?: string
|
||||
shopName?: string
|
||||
channel: string
|
||||
orderId?: number | string | null
|
||||
taskId?: number | string | null
|
||||
platformOrderId?: string
|
||||
recipientKey?: string
|
||||
messageContent?: string
|
||||
claimUrl?: string
|
||||
status?: string
|
||||
requestUrl?: string
|
||||
requestHeadersJson?: string | Record<string, unknown>
|
||||
requestBodyJson?: string | Record<string, unknown>
|
||||
responseStatus?: number | string
|
||||
responseJson?: string | Record<string, unknown>
|
||||
errorMessage?: string
|
||||
sentAt?: string | null
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
type MessageDeliveryPatch = Partial<Pick<
|
||||
MessageDeliveryRow,
|
||||
| 'status'
|
||||
| 'request_url'
|
||||
| 'request_headers_json'
|
||||
| 'request_body_json'
|
||||
| 'response_status'
|
||||
| 'response_json'
|
||||
| 'error_message'
|
||||
| 'sent_at'
|
||||
| 'updated_at'
|
||||
>>
|
||||
|
||||
type LatestSuccessfulMessageDeliveryQuery = {
|
||||
provider?: string
|
||||
platform?: string
|
||||
shopId?: string
|
||||
platformOrderId?: string
|
||||
channel?: string
|
||||
claimUrl?: string
|
||||
}
|
||||
|
||||
type MessageDeliveryListQuery = {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
provider?: string
|
||||
platform?: string
|
||||
status?: string
|
||||
shopId?: string
|
||||
platformOrderId?: string
|
||||
taskNo?: string
|
||||
dateFrom?: string
|
||||
dateTo?: string
|
||||
}
|
||||
|
||||
export async function createMessageDelivery(
|
||||
input: MessageDeliveryCreateInput,
|
||||
): Promise<MessageDeliveryRow | null> {
|
||||
const result = await query<MessageDeliveryRow>(
|
||||
`
|
||||
INSERT INTO message_deliveries (
|
||||
provider,
|
||||
platform,
|
||||
shop_id,
|
||||
shop_name,
|
||||
channel,
|
||||
order_id,
|
||||
task_id,
|
||||
platform_order_id,
|
||||
recipient_key,
|
||||
message_content,
|
||||
claim_url,
|
||||
status,
|
||||
request_url,
|
||||
request_headers_json,
|
||||
request_body_json,
|
||||
response_status,
|
||||
response_json,
|
||||
error_message,
|
||||
sent_at,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14::jsonb, $15::jsonb, $16, $17::jsonb, $18, $19, $20, $21)
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.provider,
|
||||
input.platform,
|
||||
input.shopId || '',
|
||||
input.shopName || '',
|
||||
input.channel,
|
||||
input.orderId || null,
|
||||
input.taskId || null,
|
||||
input.platformOrderId || '',
|
||||
input.recipientKey || '',
|
||||
input.messageContent || '',
|
||||
input.claimUrl || '',
|
||||
input.status || 'pending',
|
||||
input.requestUrl || '',
|
||||
input.requestHeadersJson || '{}',
|
||||
input.requestBodyJson || '{}',
|
||||
Number(input.responseStatus || 0),
|
||||
input.responseJson || '{}',
|
||||
input.errorMessage || '',
|
||||
input.sentAt || null,
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
],
|
||||
)
|
||||
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function getMessageDeliveryById(deliveryId: number | string): Promise<MessageDeliveryRow | null> {
|
||||
const result = await query<MessageDeliveryRow>('SELECT * FROM message_deliveries WHERE id = $1 LIMIT 1', [
|
||||
Number(deliveryId),
|
||||
])
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function updateMessageDelivery(
|
||||
deliveryId: number | string,
|
||||
patch: MessageDeliveryPatch = {},
|
||||
): Promise<MessageDeliveryRow | null> {
|
||||
const current = await getMessageDeliveryById(deliveryId)
|
||||
if (!current) {
|
||||
return null
|
||||
}
|
||||
|
||||
const next = { ...current, ...patch }
|
||||
const result = await query<MessageDeliveryRow>(
|
||||
`
|
||||
UPDATE message_deliveries
|
||||
SET
|
||||
status = $1,
|
||||
request_url = $2,
|
||||
request_headers_json = $3::jsonb,
|
||||
request_body_json = $4::jsonb,
|
||||
response_status = $5,
|
||||
response_json = $6::jsonb,
|
||||
error_message = $7,
|
||||
sent_at = $8,
|
||||
updated_at = $9
|
||||
WHERE id = $10
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
next.status,
|
||||
next.request_url || '',
|
||||
next.request_headers_json || '{}',
|
||||
next.request_body_json || '{}',
|
||||
Number(next.response_status || 0),
|
||||
next.response_json || '{}',
|
||||
next.error_message || '',
|
||||
next.sent_at || null,
|
||||
next.updated_at,
|
||||
Number(deliveryId),
|
||||
],
|
||||
)
|
||||
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function findLatestSuccessfulMessageDeliveryByTask(
|
||||
taskId: number | string,
|
||||
channel: string,
|
||||
): Promise<MessageDeliveryRow | null> {
|
||||
const result = await query<MessageDeliveryRow>(
|
||||
`
|
||||
SELECT *
|
||||
FROM message_deliveries
|
||||
WHERE task_id = $1 AND channel = $2 AND status = 'success'
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`,
|
||||
[Number(taskId), channel],
|
||||
)
|
||||
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function findLatestSuccessfulMessageDelivery({
|
||||
provider = '',
|
||||
platform = '',
|
||||
shopId = '',
|
||||
platformOrderId = '',
|
||||
channel = '',
|
||||
claimUrl = '',
|
||||
}: LatestSuccessfulMessageDeliveryQuery = {}): Promise<MessageDeliveryRow | null> {
|
||||
const result = await query<MessageDeliveryRow>(
|
||||
`
|
||||
SELECT *
|
||||
FROM message_deliveries
|
||||
WHERE provider = $1
|
||||
AND platform = $2
|
||||
AND shop_id = $3
|
||||
AND platform_order_id = $4
|
||||
AND channel = $5
|
||||
AND claim_url = $6
|
||||
AND status = 'success'
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`,
|
||||
[
|
||||
provider,
|
||||
platform,
|
||||
shopId,
|
||||
platformOrderId,
|
||||
channel,
|
||||
claimUrl,
|
||||
],
|
||||
)
|
||||
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function listMessageDeliveries({
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
provider = '',
|
||||
platform = '',
|
||||
status = '',
|
||||
shopId = '',
|
||||
platformOrderId = '',
|
||||
taskNo = '',
|
||||
dateFrom = '',
|
||||
dateTo = '',
|
||||
}: MessageDeliveryListQuery = {}): Promise<MessageDeliveryListQueryResult> {
|
||||
const offset = (page - 1) * pageSize
|
||||
const filters: string[] = []
|
||||
const params: unknown[] = []
|
||||
|
||||
if (provider) {
|
||||
params.push(provider)
|
||||
filters.push(`md.provider = $${params.length}`)
|
||||
}
|
||||
|
||||
if (platform) {
|
||||
params.push(platform)
|
||||
filters.push(`md.platform = $${params.length}`)
|
||||
}
|
||||
|
||||
if (status) {
|
||||
params.push(status)
|
||||
filters.push(`md.status = $${params.length}`)
|
||||
}
|
||||
|
||||
if (shopId) {
|
||||
params.push(shopId)
|
||||
filters.push(`md.shop_id = $${params.length}`)
|
||||
}
|
||||
|
||||
if (platformOrderId) {
|
||||
params.push(`%${platformOrderId}%`)
|
||||
filters.push(`md.platform_order_id ILIKE $${params.length}`)
|
||||
}
|
||||
|
||||
if (taskNo) {
|
||||
params.push(`%${taskNo}%`)
|
||||
filters.push(`ft.task_no ILIKE $${params.length}`)
|
||||
}
|
||||
|
||||
if (dateFrom) {
|
||||
params.push(dateFrom)
|
||||
filters.push(`md.created_at >= $${params.length}`)
|
||||
}
|
||||
|
||||
if (dateTo) {
|
||||
params.push(dateTo)
|
||||
filters.push(`md.created_at <= $${params.length}`)
|
||||
}
|
||||
|
||||
const whereClause = filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : ''
|
||||
const fromClause = `
|
||||
FROM message_deliveries md
|
||||
LEFT JOIN fulfillment_tasks ft ON ft.id = md.task_id
|
||||
`
|
||||
|
||||
const totalResult = await query<{ [column: string]: unknown, total: number }>(
|
||||
`SELECT COUNT(*)::int AS total ${fromClause} ${whereClause}`,
|
||||
params,
|
||||
)
|
||||
|
||||
params.push(pageSize)
|
||||
params.push(offset)
|
||||
const itemsResult = await query<MessageDeliveryRow>(
|
||||
`
|
||||
SELECT
|
||||
md.*,
|
||||
ft.task_no,
|
||||
ft.task_status
|
||||
${fromClause}
|
||||
${whereClause}
|
||||
ORDER BY md.id DESC
|
||||
LIMIT $${params.length - 1} OFFSET $${params.length}
|
||||
`,
|
||||
params,
|
||||
)
|
||||
|
||||
return {
|
||||
items: itemsResult.rows,
|
||||
total: Number(totalResult.rows[0]?.total || 0),
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ type OrderPlatformCandidateLookupInput = {
|
||||
}
|
||||
|
||||
export async function findOrderByPlatformOrderId({
|
||||
provider = 'agiso',
|
||||
provider = '91kaquan',
|
||||
platform,
|
||||
shopId = '',
|
||||
platformOrderId,
|
||||
@@ -44,7 +44,7 @@ export async function findOrderByPlatformOrderId({
|
||||
}
|
||||
|
||||
export async function findOrderByPlatformOrderIdCandidates({
|
||||
provider = 'agiso',
|
||||
provider = '91kaquan',
|
||||
platform,
|
||||
shopIds = [],
|
||||
platformOrderId,
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { PoolClient } from 'pg'
|
||||
import type {
|
||||
TaskCreateInput,
|
||||
TaskListQueryInput,
|
||||
TaskTencentContextPatch,
|
||||
TaskRuntimeContextPatch,
|
||||
TaskUpdatePatch,
|
||||
} from '../types/repository-inputs.js'
|
||||
import type {
|
||||
@@ -14,7 +14,7 @@ import type {
|
||||
|
||||
type TaskQueryExecutor = (text: string, params?: unknown[]) => Promise<{ rows: unknown[] }>
|
||||
|
||||
type TencentBrowserContextRow = {
|
||||
type TaskRuntimeContextRow = {
|
||||
browser_session_id: string
|
||||
login_type: string
|
||||
nickname: string
|
||||
@@ -52,7 +52,7 @@ const TASK_FIELDS = `
|
||||
const TASK_JOINS = `
|
||||
FROM fulfillment_tasks ft
|
||||
LEFT JOIN claim_tokens ct ON ct.task_id = ft.id
|
||||
LEFT JOIN tencent_browser_contexts ctx ON ctx.task_id = ft.id
|
||||
LEFT JOIN task_runtime_contexts ctx ON ctx.task_id = ft.id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
tib.inventory_item_id,
|
||||
@@ -158,8 +158,8 @@ export async function createTask(input: TaskCreateInput): Promise<TaskRow | null
|
||||
)
|
||||
|
||||
const taskId = Number(taskResult.rows[0]?.id || 0)
|
||||
if (input.tencentContext) {
|
||||
await upsertTencentBrowserContextWithClient(client, taskId, input.tencentContext, input.createdAt)
|
||||
if (input.runtimeContext) {
|
||||
await upsertTaskRuntimeContextWithClient(client, taskId, input.runtimeContext, input.createdAt)
|
||||
}
|
||||
|
||||
return getTaskByIdWithExecutor(client.query.bind(client) as TaskQueryExecutor, taskId)
|
||||
@@ -220,8 +220,8 @@ export async function updateTask(taskId: number | string, patch: TaskUpdatePatch
|
||||
],
|
||||
)
|
||||
|
||||
if (containsTencentPatch(patch)) {
|
||||
await upsertTencentBrowserContextWithClient(client, Number(taskId), {
|
||||
if (containsRuntimeContextPatch(patch)) {
|
||||
await upsertTaskRuntimeContextWithClient(client, Number(taskId), {
|
||||
browserSessionId: patch.browser_session_id,
|
||||
loginType: patch.login_type,
|
||||
nickname: patch.nickname,
|
||||
@@ -310,7 +310,7 @@ export async function listTasks({
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM fulfillment_tasks ft
|
||||
LEFT JOIN order_items oi ON oi.id = ft.order_item_id
|
||||
LEFT JOIN tencent_browser_contexts ctx ON ctx.task_id = ft.id
|
||||
LEFT JOIN task_runtime_contexts ctx ON ctx.task_id = ft.id
|
||||
${whereClause}
|
||||
`,
|
||||
params,
|
||||
@@ -333,14 +333,14 @@ export async function listTasks({
|
||||
}
|
||||
}
|
||||
|
||||
async function upsertTencentBrowserContextWithClient(
|
||||
async function upsertTaskRuntimeContextWithClient(
|
||||
client: PoolClient,
|
||||
taskId: number | string,
|
||||
patch: TaskTencentContextPatch = {},
|
||||
patch: TaskRuntimeContextPatch = {},
|
||||
timestamp: string | undefined,
|
||||
): Promise<void> {
|
||||
const currentResult = await client.query<TencentBrowserContextRow>(
|
||||
'SELECT * FROM tencent_browser_contexts WHERE task_id = $1 LIMIT 1',
|
||||
const currentResult = await client.query<TaskRuntimeContextRow>(
|
||||
'SELECT * FROM task_runtime_contexts WHERE task_id = $1 LIMIT 1',
|
||||
[Number(taskId)],
|
||||
)
|
||||
const current = currentResult.rows[0] || null
|
||||
@@ -362,7 +362,7 @@ async function upsertTencentBrowserContextWithClient(
|
||||
if (!current) {
|
||||
await client.query(
|
||||
`
|
||||
INSERT INTO tencent_browser_contexts (
|
||||
INSERT INTO task_runtime_contexts (
|
||||
task_id,
|
||||
browser_session_id,
|
||||
login_type,
|
||||
@@ -399,7 +399,7 @@ async function upsertTencentBrowserContextWithClient(
|
||||
|
||||
await client.query(
|
||||
`
|
||||
UPDATE tencent_browser_contexts
|
||||
UPDATE task_runtime_contexts
|
||||
SET
|
||||
browser_session_id = $1,
|
||||
login_type = $2,
|
||||
@@ -444,7 +444,7 @@ async function getTaskByIdWithExecutor(
|
||||
return (result.rows[0] as TaskRow | undefined) || null
|
||||
}
|
||||
|
||||
function containsTencentPatch(patch: TaskUpdatePatch = {}): boolean {
|
||||
function containsRuntimeContextPatch(patch: TaskUpdatePatch = {}): boolean {
|
||||
return [
|
||||
'browser_session_id',
|
||||
'login_type',
|
||||
|
||||
Reference in New Issue
Block a user