gongxiangp2/lib/external-auto-listing.ts

1346 lines
41 KiB
TypeScript

import { EXTERNAL_MATERIAL_SCOPE_WHERE } from '@/lib/material-access-scope'
import { randomUUID } from 'crypto'
import {
AutoListingCallbackError,
normalizeAutoListingCallbackUrl,
postAutoListingCallback,
summarizeAutoListingCallbackError,
validateAutoListingCallbackUrl,
validateCallbackAuth,
} from '@/lib/external-auto-listing-callback'
import { decryptCallbackSecret, encryptCallbackSecret } from '@/lib/external-auto-listing-crypto'
import { prisma } from '@/lib/prisma'
import { getPublicAppOrigin } from '@/lib/public-app-url'
import { XIAOYI_DELIVERY_MATERIAL_TYPES, type XiaoyiDeliveryKind } from '@/lib/xiaoyi-delivery-material'
export const AUTO_LISTING_EVENT_TYPE = 'task-detail.full-set.ready'
export const TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE = 'task-detail.barcode-images.updated'
export const XIAOYI_AUTO_LISTING_EVENT_TYPE = 'xiaoyi.auto-listing.material.ready'
export const AUTO_LISTING_AUTH_TYPE = {
NONE: 'none',
BEARER: 'bearer',
HEADER: 'header',
} as const
export const AUTO_LISTING_DELIVERY_STATUS = {
PENDING: 'pending',
PROCESSING: 'processing',
RETRYING: 'retrying',
SUCCEEDED: 'succeeded',
EXHAUSTED: 'exhausted',
} as const
export interface ExternalNotificationTarget {
id: string
name: string
callbackUrl: string
isActive: boolean
authType: string
authConfig: Record<string, string>
eventTypes: string[]
createdAt: string
updatedAt: string
}
export interface ExternalNotificationTargetAdminView
extends Omit<ExternalNotificationTarget, 'authConfig'> {
authConfig: Record<string, string>
authConfigured: boolean
authMasked: string
}
export interface ExternalNotificationDelivery {
id: string
targetId: string
targetName: string
eventType: string
materialId: string
materialType: string
taskNo: string | null
status: string
attemptCount: number
maxAttempts: number
nextRetryAt: string | null
lastStatusCode: number | null
lastResponseBody: string | null
lastError: string | null
payload: Record<string, unknown> | null
createdAt: string
updatedAt: string
completedAt: string | null
}
export type ExternalNotificationDeliverySummary =
Omit<ExternalNotificationDelivery, 'lastResponseBody' | 'payload'>
export interface ExternalNotificationTargetInput {
name: string
callbackUrl: string
isActive?: boolean
authType?: string
authConfig?: Record<string, string>
eventTypes?: string[]
}
const TARGET_TABLE = 'external_notification_targets'
const DELIVERY_TABLE = 'external_notification_deliveries'
const XIAOYI_EVENT_TABLE = 'xiaoyi_auto_listing_events'
const OLD_DELIVERY_UNIQUE_INDEX = 'external_notification_deliveries_target_event_material_key'
const DELIVERY_DEDUPE_UNIQUE_INDEX = 'external_notification_deliveries_target_event_dedupe_key'
const DELIVERY_MAX_ATTEMPTS = 5
const RECOVERY_INTERVAL_MS = 60_000
const STALE_PROCESSING_MS = 10 * 60 * 1000
const RETRY_DELAYS_MS = [0, 60_000, 5 * 60_000, 15 * 60_000, 30 * 60_000, 60 * 60_000]
export const SUPPORTED_AUTO_LISTING_EVENT_TYPES = [
AUTO_LISTING_EVENT_TYPE,
TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE,
XIAOYI_AUTO_LISTING_EVENT_TYPE,
] as const
let isTableInitialized = false
let isWorkerStarted = false
let isXiaoyiWorkerStarted = false
let recoveryTimer: NodeJS.Timeout | null = null
const deliveryTimers = new Map<string, NodeJS.Timeout>()
function asJson(value: unknown, fallback: string) {
try {
return JSON.stringify(value ?? JSON.parse(fallback))
} catch {
return fallback
}
}
function toIsoString(value: unknown): string | null {
if (!value) {
return null
}
if (value instanceof Date) {
return value.toISOString()
}
const date = new Date(String(value))
return Number.isNaN(date.getTime()) ? null : date.toISOString()
}
function normalizeAuthType(value: string | null | undefined) {
if (value === AUTO_LISTING_AUTH_TYPE.BEARER || value === AUTO_LISTING_AUTH_TYPE.HEADER) {
return value
}
return AUTO_LISTING_AUTH_TYPE.NONE
}
function normalizeAuthConfig(input: unknown): Record<string, string> {
if (!input || typeof input !== 'object' || Array.isArray(input)) {
return {}
}
return Object.entries(input as Record<string, unknown>).reduce<Record<string, string>>((acc, [key, value]) => {
const normalizedKey = String(key || '').trim()
const normalizedValue = typeof value === 'string' ? value.trim() : ''
if (normalizedKey && normalizedValue) {
acc[normalizedKey] = normalizedValue
}
return acc
}, {})
}
function normalizeEventTypes(eventTypes?: string[] | null): string[] {
const normalized = Array.isArray(eventTypes)
? eventTypes
.map((item) => String(item || '').trim())
.filter((item): item is (typeof SUPPORTED_AUTO_LISTING_EVENT_TYPES)[number] =>
Boolean(item) && (SUPPORTED_AUTO_LISTING_EVENT_TYPES as readonly string[]).includes(item),
)
: []
return normalized.length > 0 ? Array.from(new Set(normalized)) : [AUTO_LISTING_EVENT_TYPE]
}
function buildBarcodeLookupUrl(barcode: string) {
return `${getPublicAppOrigin()}/api/external/product-barcode-images?barcode=${encodeURIComponent(barcode)}`
}
function compareBarcodeTimeline(
left: { createdAt: Date; sortOrder: number },
right: { createdAt: Date; sortOrder: number },
) {
const createdAtDiff = right.createdAt.getTime() - left.createdAt.getTime()
if (createdAtDiff !== 0) {
return createdAtDiff
}
return left.sortOrder - right.sortOrder
}
async function listActiveTargetsByEventType(eventType: string) {
return queryRaw<any>(
`
SELECT
id,
name,
callback_url AS "callbackUrl",
is_active AS "isActive",
auth_type AS "authType",
auth_config AS "authConfig",
event_types AS "eventTypes",
created_at AS "createdAt",
updated_at AS "updatedAt"
FROM "${TARGET_TABLE}"
WHERE is_active = TRUE
AND event_types ? $1
ORDER BY created_at ASC
`,
eventType,
)
}
async function enqueueNotificationDeliveries(options: {
eventType: string
materialId: string
materialType: string
taskNo?: string | null
dedupeKey: string
payload: Record<string, unknown>
}) {
const targets = await listActiveTargetsByEventType(options.eventType)
const createdIds: string[] = []
for (const row of targets) {
const deliveryId = randomUUID()
const insertedRows = await queryRaw<{ id: string }>(
`
INSERT INTO "${DELIVERY_TABLE}" (
id,
target_id,
event_type,
material_id,
material_type,
task_no,
dedupe_key,
status,
payload,
attempt_count,
max_attempts,
next_retry_at,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::jsonb, 0, $10, NOW(), NOW(), NOW())
ON CONFLICT ("target_id", "event_type", "dedupe_key") DO NOTHING
RETURNING id
`,
deliveryId,
String(row.id),
options.eventType,
options.materialId,
options.materialType,
options.taskNo || null,
options.dedupeKey,
AUTO_LISTING_DELIVERY_STATUS.PENDING,
asJson(options.payload, '{}'),
DELIVERY_MAX_ATTEMPTS,
)
const inserted = insertedRows[0]
if (inserted?.id) {
createdIds.push(String(inserted.id))
scheduleDeliveryProcessing(String(inserted.id), 0)
}
}
return createdIds
}
function normalizeTargetRow(row: any): ExternalNotificationTarget {
return {
id: String(row.id),
name: String(row.name || ''),
callbackUrl: String(row.callbackUrl || ''),
isActive: Boolean(row.isActive),
authType: normalizeAuthType(row.authType),
authConfig: normalizeAuthConfig(row.authConfig),
eventTypes: normalizeEventTypes(Array.isArray(row.eventTypes) ? row.eventTypes : []),
createdAt: toIsoString(row.createdAt) || new Date().toISOString(),
updatedAt: toIsoString(row.updatedAt) || new Date().toISOString(),
}
}
export function buildExternalNotificationTargetAdminView(
target: ExternalNotificationTarget,
): ExternalNotificationTargetAdminView {
const { authConfig, ...summary } = target
// Older rows can contain URL credentials or query parameters. Keep them out of responses.
const url = URL.canParse(target.callbackUrl) ? new URL(target.callbackUrl) : null
const callbackUrl = url && (url.protocol === 'http:' || url.protocol === 'https:')
? url.origin + url.pathname
: ''
const hasEncryptedSecret = Boolean(authConfig.secretCiphertext)
const isBearer = target.authType === AUTO_LISTING_AUTH_TYPE.BEARER
const isHeader = target.authType === AUTO_LISTING_AUTH_TYPE.HEADER
const authConfigured = isBearer
? hasEncryptedSecret || Boolean(authConfig.token)
: isHeader && Boolean(authConfig.headerName && (hasEncryptedSecret || authConfig.headerValue))
return {
...summary,
callbackUrl,
authConfig: isHeader && authConfig.headerName ? { headerName: authConfig.headerName } : {},
authConfigured,
authMasked: isBearer || isHeader
? `${isBearer ? 'Bearer' : '自定义请求头'} · ${authConfigured ? '已配置' : '未配置'}`
: '无鉴权',
}
}
export function summarizeExternalNotificationDelivery(
{ lastResponseBody: _lastResponseBody, payload: _payload, ...delivery }: ExternalNotificationDelivery,
): ExternalNotificationDeliverySummary {
return {
...delivery,
lastError: summarizeAutoListingCallbackError(delivery.lastError, delivery.lastStatusCode),
}
}
function normalizeDeliveryRow(row: any): ExternalNotificationDelivery {
return {
id: String(row.id),
targetId: String(row.targetId || ''),
targetName: String(row.targetName || ''),
eventType: String(row.eventType || ''),
materialId: String(row.materialId || ''),
materialType: String(row.materialType || ''),
taskNo: row.taskNo ? String(row.taskNo) : null,
status: String(row.status || AUTO_LISTING_DELIVERY_STATUS.PENDING),
attemptCount: Number(row.attemptCount || 0),
maxAttempts: Number(row.maxAttempts || DELIVERY_MAX_ATTEMPTS),
nextRetryAt: toIsoString(row.nextRetryAt),
lastStatusCode:
typeof row.lastStatusCode === 'number'
? row.lastStatusCode
: row.lastStatusCode != null
? Number(row.lastStatusCode)
: null,
lastResponseBody: row.lastResponseBody ? String(row.lastResponseBody) : null,
lastError: row.lastError ? String(row.lastError) : null,
payload:
row.payload && typeof row.payload === 'object' && !Array.isArray(row.payload)
? (row.payload as Record<string, unknown>)
: null,
createdAt: toIsoString(row.createdAt) || new Date().toISOString(),
updatedAt: toIsoString(row.updatedAt) || new Date().toISOString(),
completedAt: toIsoString(row.completedAt),
}
}
async function normalizeTargetInput(
input: Partial<ExternalNotificationTargetInput>,
existing?: ExternalNotificationTarget,
) {
const name = (input.name ?? existing?.name ?? '').trim()
if (!name) throw new AutoListingCallbackError('目标系统名称不能为空')
const inputUrl = normalizeAutoListingCallbackUrl(input.callbackUrl ?? existing?.callbackUrl ?? '').href
const callbackUrl = inputUrl === existing?.callbackUrl
? inputUrl
: await validateAutoListingCallbackUrl(inputUrl)
const authType = normalizeAuthType(input.authType ?? existing?.authType)
const config = normalizeAuthConfig(input.authConfig)
const headerName = authType === AUTO_LISTING_AUTH_TYPE.BEARER
? 'Authorization'
: config.headerName || existing?.authConfig.headerName || ''
const canKeepSecret = existing && authType === existing.authType
&& callbackUrl === existing.callbackUrl
&& (authType !== AUTO_LISTING_AUTH_TYPE.HEADER || headerName === existing.authConfig.headerName)
let authConfig: Record<string, string> = {}
if (authType !== AUTO_LISTING_AUTH_TYPE.NONE) {
const secret = authType === AUTO_LISTING_AUTH_TYPE.BEARER ? config.token : config.headerValue
if (!secret && canKeepSecret && existing.authConfig.secretCiphertext) {
authConfig = existing.authConfig
} else {
const savedSecret = canKeepSecret
? (authType === AUTO_LISTING_AUTH_TYPE.BEARER ? existing.authConfig.token : existing.authConfig.headerValue)
: undefined
const nextSecret = secret || savedSecret
if (!nextSecret || !headerName) {
throw new AutoListingCallbackError('请填写回调鉴权凭据,更改地址或鉴权方式后需重新填写')
}
validateCallbackAuth(headerName, authType === AUTO_LISTING_AUTH_TYPE.BEARER ? `Bearer ${nextSecret}` : nextSecret)
authConfig = encryptCallbackSecret(nextSecret)
if (authType === AUTO_LISTING_AUTH_TYPE.HEADER) authConfig.headerName = headerName
}
}
return {
name,
callbackUrl,
isActive: input.isActive ?? existing?.isActive ?? true,
authType,
authConfig,
eventTypes: normalizeEventTypes(input.eventTypes ?? existing?.eventTypes),
}
}
export async function getExternalNotificationTargetById(targetId: string) {
const rows = await queryRaw<any>(
`
SELECT
id,
name,
callback_url AS "callbackUrl",
is_active AS "isActive",
auth_type AS "authType",
auth_config AS "authConfig",
event_types AS "eventTypes",
created_at AS "createdAt",
updated_at AS "updatedAt"
FROM "${TARGET_TABLE}"
WHERE id = $1
LIMIT 1
`,
targetId,
)
return rows[0] ? normalizeTargetRow(rows[0]) : null
}
export async function createExternalNotificationTarget(input: ExternalNotificationTargetInput) {
const normalized = await normalizeTargetInput(input)
await ensureExternalNotificationTables()
ensureExternalNotificationWorkerStarted()
const targetId = randomUUID()
await executeRaw(
`
INSERT INTO "${TARGET_TABLE}" (
id,
name,
callback_url,
is_active,
auth_type,
auth_config,
event_types,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7::jsonb, NOW(), NOW())
`,
targetId,
normalized.name,
normalized.callbackUrl,
normalized.isActive,
normalized.authType,
asJson(normalized.authConfig, '{}'),
asJson(normalized.eventTypes, '[]'),
)
const created = await getExternalNotificationTargetById(targetId)
if (!created) {
throw new Error('创建通知目标失败')
}
return created
}
export async function listExternalNotificationTargets() {
await ensureExternalNotificationTables()
const rows = await queryRaw<any>(
`
SELECT
id,
name,
callback_url AS "callbackUrl",
is_active AS "isActive",
auth_type AS "authType",
auth_config AS "authConfig",
event_types AS "eventTypes",
created_at AS "createdAt",
updated_at AS "updatedAt"
FROM "${TARGET_TABLE}"
ORDER BY created_at ASC, name ASC
`,
)
return rows.map((row) => normalizeTargetRow(row))
}
export async function updateExternalNotificationTarget(
targetId: string,
input: Partial<ExternalNotificationTargetInput>,
) {
await ensureExternalNotificationTables()
ensureExternalNotificationWorkerStarted()
const existing = await getExternalNotificationTargetById(targetId)
if (!existing) return null
const normalized = await normalizeTargetInput(input, existing)
await executeRaw(
`
UPDATE "${TARGET_TABLE}"
SET
name = $2,
callback_url = $3,
is_active = COALESCE($4, is_active),
auth_type = $5,
auth_config = $6::jsonb,
event_types = $7::jsonb,
updated_at = NOW()
WHERE id = $1
`,
targetId,
normalized.name,
normalized.callbackUrl,
input.isActive ?? null,
normalized.authType,
asJson(normalized.authConfig, '{}'),
asJson(normalized.eventTypes, '[]'),
)
return getExternalNotificationTargetById(targetId)
}
export async function setExternalNotificationTargetActive(targetId: string, isActive: boolean) {
await ensureExternalNotificationTables()
const updated = await executeRaw(
`UPDATE "${TARGET_TABLE}" SET is_active = $2, updated_at = NOW() WHERE id = $1`,
targetId,
isActive,
)
if (!updated) return null
ensureExternalNotificationWorkerStarted()
return getExternalNotificationTargetById(targetId)
}
export async function testExternalNotificationTarget(targetId: string) {
await ensureExternalNotificationTables()
const target = await getExternalNotificationTargetById(targetId)
if (!target) return null
const statusCode = await postAutoListingCallback(target.callbackUrl, buildNotificationHeaders(target), {
eventId: randomUUID(),
eventType: 'auto-listing.callback.test',
test: true,
occurredAt: new Date().toISOString(),
})
return {
statusCode,
error: summarizeAutoListingCallbackError(null, statusCode),
}
}
export async function listExternalNotificationDeliveries(options?: {
canAccessOperationsG?: boolean
status?: string
targetId?: string
materialId?: string
limit?: number
}) {
await ensureExternalNotificationTables()
const conditions = ['1 = 1']
const params: any[] = []
if (!options?.canAccessOperationsG) {
conditions.push(`NOT EXISTS (SELECT 1 FROM materials m WHERE m.id = d.material_id AND m.access_scope = 'operations_g')`)
conditions.push(`NOT EXISTS (SELECT 1 FROM material_purge_jobs p WHERE p.material_id = d.material_id AND p.material_snapshot->>'accessScope' = 'operations_g')`)
}
if (options?.status) {
params.push(String(options.status).trim())
conditions.push(`d.status = $${params.length}`)
}
if (options?.targetId) {
params.push(String(options.targetId).trim())
conditions.push(`d.target_id = $${params.length}`)
}
if (options?.materialId) {
params.push(String(options.materialId).trim())
conditions.push(`d.material_id = $${params.length}`)
}
const limit = Number.isFinite(options?.limit) && Number(options?.limit) > 0
? Math.min(Number(options?.limit), 200)
: 100
params.push(limit)
const rows = await queryRaw<any>(
`
SELECT
d.id,
d.target_id AS "targetId",
t.name AS "targetName",
d.event_type AS "eventType",
d.material_id AS "materialId",
d.material_type AS "materialType",
d.task_no AS "taskNo",
d.status,
d.attempt_count AS "attemptCount",
d.max_attempts AS "maxAttempts",
d.next_retry_at AS "nextRetryAt",
d.last_status_code AS "lastStatusCode",
d.last_response_body AS "lastResponseBody",
d.last_error AS "lastError",
d.payload,
d.created_at AS "createdAt",
d.updated_at AS "updatedAt",
d.completed_at AS "completedAt"
FROM "${DELIVERY_TABLE}" d
LEFT JOIN "${TARGET_TABLE}" t ON t.id = d.target_id
WHERE ${conditions.join(' AND ')}
ORDER BY d.created_at DESC
LIMIT $${params.length}
`,
...params,
)
return rows.map((row) => normalizeDeliveryRow(row))
}
async function executeRaw(sql: string, ...params: any[]) {
return prisma.$executeRawUnsafe(sql, ...params)
}
async function queryRaw<T>(sql: string, ...params: any[]) {
return prisma.$queryRawUnsafe(sql, ...params) as Promise<T[]>
}
export type XiaoyiOutcomeKind = XiaoyiDeliveryKind
export interface XiaoyiAutoListingEvent {
materialId: string
materialType: string
outcomeKind: XiaoyiOutcomeKind
projectId: string
replaceExisting?: boolean
}
/** Called inside the same transaction that creates the material and its files. */
export async function queueXiaoyiAutoListingNotification(
tx: { $executeRawUnsafe: (sql: string, ...args: any[]) => Promise<unknown> },
event: XiaoyiAutoListingEvent,
) {
if (!event.materialId || !event.projectId || XIAOYI_DELIVERY_MATERIAL_TYPES[event.outcomeKind] !== event.materialType) {
throw new Error('小壹交付通知参数无效')
}
const conflictClause = event.replaceExisting
? `ON CONFLICT (material_id) DO UPDATE SET
id = EXCLUDED.id,
material_type = EXCLUDED.material_type,
outcome_kind = EXCLUDED.outcome_kind,
project_id = EXCLUDED.project_id,
created_at = CURRENT_TIMESTAMP`
: 'ON CONFLICT (material_id) DO NOTHING'
await tx.$executeRawUnsafe(
`INSERT INTO "${XIAOYI_EVENT_TABLE}" (id, material_id, material_type, outcome_kind, project_id)
VALUES ($1, $2, $3, $4, $5) ${conflictClause}`,
randomUUID(), event.materialId, event.materialType, event.outcomeKind, event.projectId,
)
}
/** Call after commit so the worker never delivers uncommitted material. */
export function wakeXiaoyiAutoListingNotifications() {
ensureXiaoyiAutoListingWorkerStarted()
void recoverDueDeliveries(20, XIAOYI_AUTO_LISTING_EVENT_TYPE)
.catch((error) => console.error('[external-notification] 小壹通知入队失败:', error))
}
export async function queueXiaoyiDeliveries(limit = 20) {
const pending = await queryRaw<{
eventId: string; materialId: string; materialType: string
outcomeKind: XiaoyiOutcomeKind; projectId: string; createdAt: Date; targetId: string
}>(`
SELECT e.id AS "eventId",e.material_id AS "materialId",e.material_type AS "materialType",
e.outcome_kind AS "outcomeKind",e.project_id AS "projectId",e.created_at AS "createdAt",
t.id AS "targetId"
FROM "${XIAOYI_EVENT_TABLE}" e
JOIN "${TARGET_TABLE}" t ON t.is_active = TRUE AND t.event_types ? $1
WHERE NOT EXISTS (
SELECT 1 FROM "${DELIVERY_TABLE}" d
WHERE d.target_id=t.id AND d.event_type=$1
AND (d.dedupe_key=e.id OR (d.dedupe_key=e.material_id AND d.created_at>=e.created_at))
)
ORDER BY e.created_at,t.created_at
LIMIT $2
`, XIAOYI_AUTO_LISTING_EVENT_TYPE, limit)
for (const event of pending) {
const materialUrl = `${getPublicAppOrigin()}/api/external/product-assets?materialId=${encodeURIComponent(event.materialId)}`
const payload = {
eventId: event.eventId,
eventType: XIAOYI_AUTO_LISTING_EVENT_TYPE,
materialId: event.materialId,
materialType: event.materialType,
outcomeKind: event.outcomeKind,
projectId: event.projectId,
occurredAt: new Date(event.createdAt).toISOString(),
pullUrl: materialUrl,
statusUrl: materialUrl,
}
const deliveryId = randomUUID()
const inserted = await queryRaw<{ id: string }>(`
INSERT INTO "${DELIVERY_TABLE}" (
id,target_id,event_type,material_id,material_type,dedupe_key,status,payload,
attempt_count,max_attempts,next_retry_at,created_at,updated_at
) VALUES ($1,$2,$3,$4,$5,$9,$6,$7::jsonb,0,$8,NOW(),NOW(),NOW())
ON CONFLICT ("target_id","event_type","dedupe_key") DO NOTHING RETURNING id
`, deliveryId, event.targetId, XIAOYI_AUTO_LISTING_EVENT_TYPE, event.materialId,
event.materialType, AUTO_LISTING_DELIVERY_STATUS.PENDING, JSON.stringify(payload), DELIVERY_MAX_ATTEMPTS, event.eventId)
if (inserted[0]) scheduleDeliveryProcessing(String(inserted[0].id), 0)
}
}
export async function ensureExternalNotificationTables() {
if (isTableInitialized) {
return
}
await executeRaw(`
CREATE TABLE IF NOT EXISTS "${TARGET_TABLE}" (
"id" TEXT PRIMARY KEY,
"name" TEXT NOT NULL,
"callback_url" TEXT NOT NULL,
"is_active" BOOLEAN NOT NULL DEFAULT TRUE,
"auth_type" TEXT NOT NULL DEFAULT 'none',
"auth_config" JSONB NOT NULL DEFAULT '{}'::jsonb,
"event_types" JSONB NOT NULL DEFAULT '[]'::jsonb,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`)
await executeRaw(`
CREATE TABLE IF NOT EXISTS "${DELIVERY_TABLE}" (
"id" TEXT PRIMARY KEY,
"target_id" TEXT NOT NULL,
"event_type" TEXT NOT NULL,
"material_id" TEXT NOT NULL,
"material_type" TEXT NOT NULL,
"task_no" TEXT,
"dedupe_key" TEXT,
"status" TEXT NOT NULL,
"payload" JSONB NOT NULL,
"attempt_count" INT NOT NULL DEFAULT 0,
"max_attempts" INT NOT NULL DEFAULT ${DELIVERY_MAX_ATTEMPTS},
"next_retry_at" TIMESTAMP(3),
"last_status_code" INT,
"last_response_body" TEXT,
"last_error" TEXT,
"dispatched_at" TIMESTAMP(3),
"completed_at" TIMESTAMP(3),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`)
await executeRaw(`
CREATE UNIQUE INDEX IF NOT EXISTS "external_notification_targets_name_key"
ON "${TARGET_TABLE}" ("name")
`)
await executeRaw(`
ALTER TABLE "${DELIVERY_TABLE}"
ADD COLUMN IF NOT EXISTS "dedupe_key" TEXT
`)
await executeRaw(`
UPDATE "${DELIVERY_TABLE}"
SET "dedupe_key" = COALESCE("dedupe_key", "material_id")
WHERE "dedupe_key" IS NULL
`)
await executeRaw(`
DROP INDEX IF EXISTS "${OLD_DELIVERY_UNIQUE_INDEX}"
`)
await executeRaw(`
CREATE UNIQUE INDEX IF NOT EXISTS "${DELIVERY_DEDUPE_UNIQUE_INDEX}"
ON "${DELIVERY_TABLE}" ("target_id", "event_type", "dedupe_key")
`)
await executeRaw(`
CREATE INDEX IF NOT EXISTS "external_notification_deliveries_status_next_retry_idx"
ON "${DELIVERY_TABLE}" ("status", "next_retry_at")
`)
await executeRaw(`
CREATE TABLE IF NOT EXISTS "${XIAOYI_EVENT_TABLE}" (
"id" TEXT PRIMARY KEY,
"material_id" TEXT NOT NULL UNIQUE,
"material_type" TEXT NOT NULL,
"outcome_kind" TEXT NOT NULL,
"project_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`)
isTableInitialized = true
}
async function recoverDueDeliveries(limit = 20, eventType?: string) {
await ensureExternalNotificationTables()
await queueXiaoyiDeliveries(limit)
const rows = await queryRaw<{ id: string }>(
`
SELECT d.id
FROM "${DELIVERY_TABLE}" d
INNER JOIN "${TARGET_TABLE}" t ON t.id = d.target_id
WHERE t.is_active = TRUE
AND ($2::text IS NULL OR d.event_type = $2)
AND (
(d.status IN ('pending', 'retrying') AND (d.next_retry_at IS NULL OR d.next_retry_at <= NOW()))
OR
(d.status = 'processing' AND d.updated_at < NOW() - INTERVAL '10 minutes')
)
ORDER BY d.created_at ASC
LIMIT $1
`,
limit,
eventType ?? null,
)
rows.forEach((row) => scheduleDeliveryProcessing(String(row.id), 0))
}
export function ensureXiaoyiAutoListingWorkerStarted() {
if (isXiaoyiWorkerStarted) return
isXiaoyiWorkerStarted = true
const recover = () => {
void recoverDueDeliveries(20, XIAOYI_AUTO_LISTING_EVENT_TYPE)
.catch((error) => console.error('[external-notification] 小壹通知恢复失败:', error))
}
const bootstrap = setTimeout(recover, 1000)
bootstrap.unref?.()
const interval = setInterval(recover, RECOVERY_INTERVAL_MS)
interval.unref?.()
}
function ensureExternalNotificationWorkerStarted() {
if (isWorkerStarted) {
return
}
isWorkerStarted = true
const bootstrapTimer = setTimeout(() => {
void recoverDueDeliveries().catch((error) => console.error('[external-notification] 恢复失败:', error))
}, 1000)
bootstrapTimer.unref?.()
recoveryTimer = setInterval(() => {
void recoverDueDeliveries().catch((error) => console.error('[external-notification] 恢复失败:', error))
}, RECOVERY_INTERVAL_MS)
recoveryTimer.unref?.()
}
function scheduleDeliveryProcessing(deliveryId: string, delayMs: number) {
const existingTimer = deliveryTimers.get(deliveryId)
if (existingTimer) {
clearTimeout(existingTimer)
}
const timer = setTimeout(() => {
deliveryTimers.delete(deliveryId)
void processExternalNotificationDelivery(deliveryId).catch((error) => {
console.error('[external-notification] 投递结果保存失败:', error)
})
}, Math.max(0, delayMs))
timer.unref?.()
deliveryTimers.set(deliveryId, timer)
}
function buildNotificationHeaders(target: ExternalNotificationTarget) {
const headers = { 'Content-Type': 'application/json' }
if (target.authType === AUTO_LISTING_AUTH_TYPE.NONE) return headers
const isBearer = target.authType === AUTO_LISTING_AUTH_TYPE.BEARER
const { authConfig } = target
const headerName = isBearer ? 'Authorization' : authConfig.headerName
const secret = authConfig.secretCiphertext
? decryptCallbackSecret(authConfig)
: (isBearer ? authConfig.token : authConfig.headerValue)
if (!secret || !headerName) throw new AutoListingCallbackError('回调鉴权凭据不可用,请重新填写', 503)
const headerValue = isBearer ? `Bearer ${secret}` : secret
validateCallbackAuth(headerName, headerValue)
return { ...headers, [headerName]: headerValue }
}
async function buildTaskDetailNotificationPayload(materialId: string) {
const material = await prisma.material.findFirst({
where: {
id: materialId,
deletedAt: null,
materialType: '任务-详情',
...EXTERNAL_MATERIAL_SCOPE_WHERE,
isFullSet: true,
},
select: {
id: true,
taskNo: true,
productModel: true,
categoryName: true,
sceneName: true,
materialType: true,
previewUrl: true,
isFullSet: true,
createdAt: true,
assets: {
where: {
assetType: {
in: ['main-preview', 'sku-preview', 'detail-preview'],
},
},
orderBy: { sortOrder: 'asc' },
select: {
id: true,
assetType: true,
fileUrl: true,
fileName: true,
sortOrder: true,
},
},
},
})
if (!material) {
return null
}
const mapPreviewAssets = (assetType: 'main-preview' | 'sku-preview' | 'detail-preview') =>
material.assets
.filter((asset: any) => asset.assetType === assetType)
.sort((left: any, right: any) => Number(left.sortOrder || 0) - Number(right.sortOrder || 0))
.map((asset: any) => ({
id: String(asset.id),
url: String(asset.fileUrl || ''),
fileName: String(asset.fileName || ''),
sortOrder: Number(asset.sortOrder || 0),
}))
return {
eventId: randomUUID(),
eventType: AUTO_LISTING_EVENT_TYPE,
materialId: material.id,
materialType: material.materialType,
taskNo: material.taskNo,
productModel: material.productModel,
categoryName: material.categoryName,
shopName: material.categoryName,
sceneName: material.sceneName,
isFullSet: Boolean(material.isFullSet),
occurredAt: material.createdAt.toISOString(),
pullUrl: `${getPublicAppOrigin()}/api/external/product-assets?materialId=${material.id}`,
coverPreviewUrl: material.previewUrl || null,
previewAssets: {
main: mapPreviewAssets('main-preview'),
sku: mapPreviewAssets('sku-preview'),
detail: mapPreviewAssets('detail-preview'),
},
}
}
async function buildTaskDetailBarcodeNotificationPayload(materialId: string, assetIds: string[]) {
const normalizedAssetIds = Array.from(new Set(assetIds.map((assetId) => String(assetId || '').trim()).filter(Boolean)))
if (normalizedAssetIds.length === 0) {
return null
}
const material = await prisma.material.findFirst({
where: {
id: materialId,
deletedAt: null,
materialType: '任务-详情',
...EXTERNAL_MATERIAL_SCOPE_WHERE,
},
select: {
id: true,
taskNo: true,
productModel: true,
categoryName: true,
sceneName: true,
materialType: true,
authorName: true,
createdAt: true,
user: {
select: {
realName: true,
username: true,
},
},
assets: {
where: {
id: { in: normalizedAssetIds },
assetType: 'sku-preview',
},
orderBy: [
{ createdAt: 'desc' },
{ sortOrder: 'asc' },
],
select: {
id: true,
fileUrl: true,
fileName: true,
sortOrder: true,
skuCode: true,
createdAt: true,
updatedAt: true,
barcodes: {
select: {
barcode: true,
},
},
},
},
},
})
if (!material || material.assets.length === 0) {
return null
}
const barcodes = Array.from(
new Set(
material.assets.flatMap((asset: any) =>
(asset.barcodes || []).map((item: any) => String(item.barcode || '').trim()).filter(Boolean),
),
),
)
const latestBarcodeRecords = await prisma.materialAssetBarcode.findMany({
where: {
barcode: { in: barcodes },
asset: {
assetType: 'sku-preview',
material: {
deletedAt: null,
materialType: '任务-详情',
...EXTERNAL_MATERIAL_SCOPE_WHERE,
},
},
},
select: {
barcode: true,
assetId: true,
asset: {
select: {
createdAt: true,
sortOrder: true,
},
},
},
})
const latestAssetByBarcode = new Map<string, string>()
const latestMetaByBarcode = new Map<string, { createdAt: Date; sortOrder: number }>()
for (const record of latestBarcodeRecords) {
const barcode = String(record.barcode || '').trim()
if (!barcode || !record.asset) {
continue
}
const nextMeta = {
createdAt: record.asset.createdAt,
sortOrder: Number(record.asset.sortOrder || 0),
}
const currentMeta = latestMetaByBarcode.get(barcode)
if (!currentMeta || compareBarcodeTimeline(nextMeta, currentMeta) < 0) {
latestMetaByBarcode.set(barcode, nextMeta)
latestAssetByBarcode.set(barcode, String(record.assetId))
}
}
const uploaderName =
material.user?.realName || material.user?.username || material.authorName || ''
const items = material.assets.flatMap((asset: any) => {
const assetBarcodes: string[] = Array.from(
new Set((asset.barcodes || []).map((item: any) => String(item.barcode || '').trim()).filter(Boolean)),
)
return assetBarcodes.map((barcode) => ({
materialId: material.id,
assetId: String(asset.id),
skuCode: asset.skuCode || null,
barcode,
previewUrl: String(asset.fileUrl || ''),
fileName: String(asset.fileName || ''),
sortOrder: Number(asset.sortOrder || 0),
uploadedAt: asset.createdAt.toISOString(),
updatedAt: asset.updatedAt.toISOString(),
uploaderName,
isLatest: latestAssetByBarcode.get(barcode) === String(asset.id),
lookupUrl: buildBarcodeLookupUrl(barcode),
}))
})
return {
eventId: randomUUID(),
eventType: TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE,
materialId: material.id,
materialType: material.materialType,
taskNo: material.taskNo,
productModel: material.productModel,
categoryName: material.categoryName,
shopName: material.categoryName,
sceneName: material.sceneName,
occurredAt: new Date().toISOString(),
items,
}
}
export async function queueTaskDetailFullSetNotification(materialId: string) {
await ensureExternalNotificationTables()
ensureExternalNotificationWorkerStarted()
const payload = await buildTaskDetailNotificationPayload(materialId)
if (!payload) {
return []
}
return enqueueNotificationDeliveries({
eventType: AUTO_LISTING_EVENT_TYPE,
materialId: payload.materialId,
materialType: payload.materialType,
taskNo: payload.taskNo,
dedupeKey: payload.materialId,
payload,
})
}
export async function queueTaskDetailBarcodeImagesUpdatedNotification(
materialId: string,
assetIds: string[],
) {
await ensureExternalNotificationTables()
ensureExternalNotificationWorkerStarted()
const normalizedAssetIds = Array.from(new Set(assetIds.map((assetId) => String(assetId || '').trim()).filter(Boolean)))
const payload = await buildTaskDetailBarcodeNotificationPayload(materialId, normalizedAssetIds)
if (!payload || payload.items.length === 0) {
return []
}
return enqueueNotificationDeliveries({
eventType: TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE,
materialId: payload.materialId,
materialType: payload.materialType,
taskNo: payload.taskNo,
dedupeKey: payload.eventId,
payload,
})
}
export async function retryExternalNotificationDelivery(deliveryId: string) {
await ensureExternalNotificationTables()
ensureExternalNotificationWorkerStarted()
await executeRaw(
`
UPDATE "${DELIVERY_TABLE}"
SET
status = $2,
next_retry_at = NOW(),
completed_at = NULL,
updated_at = NOW()
WHERE id = $1
`,
deliveryId,
AUTO_LISTING_DELIVERY_STATUS.PENDING,
)
scheduleDeliveryProcessing(deliveryId, 0)
}
export async function processExternalNotificationDelivery(
deliveryId: string,
options?: { force?: boolean },
) {
await ensureExternalNotificationTables()
const claimed = await prisma.$transaction(async (tx: any) => {
await tx.$executeRawUnsafe('SELECT pg_advisory_xact_lock(hashtext($1))', deliveryId)
const rows = await tx.$queryRawUnsafe(
`
SELECT
d.id,
d.status,
d.payload,
d.attempt_count AS "attemptCount",
d.max_attempts AS "maxAttempts",
d.updated_at AS "updatedAt",
t.id AS "targetId",
t.name,
t.callback_url AS "callbackUrl",
t.is_active AS "isActive",
t.auth_type AS "authType",
t.auth_config AS "authConfig",
t.event_types AS "eventTypes",
t.created_at AS "createdAt",
t.updated_at AS "targetUpdatedAt"
FROM "${DELIVERY_TABLE}" d
INNER JOIN "${TARGET_TABLE}" t ON t.id = d.target_id
WHERE d.id = $1
LIMIT 1
`,
deliveryId,
)
const row = rows[0]
if (!row || !Boolean(row.isActive)) {
return null
}
const updatedAt = new Date(String(row.updatedAt || ''))
const isStaleProcessing =
row.status === AUTO_LISTING_DELIVERY_STATUS.PROCESSING &&
!Number.isNaN(updatedAt.getTime()) &&
Date.now() - updatedAt.getTime() > STALE_PROCESSING_MS
if (
!options?.force &&
row.status === AUTO_LISTING_DELIVERY_STATUS.PROCESSING &&
!isStaleProcessing
) {
return null
}
if (
!options?.force &&
row.status !== AUTO_LISTING_DELIVERY_STATUS.PENDING &&
row.status !== AUTO_LISTING_DELIVERY_STATUS.RETRYING &&
!isStaleProcessing
) {
return null
}
await tx.$executeRawUnsafe(
`
UPDATE "${DELIVERY_TABLE}"
SET
status = $2,
updated_at = NOW()
WHERE id = $1
`,
deliveryId,
AUTO_LISTING_DELIVERY_STATUS.PROCESSING,
)
return {
attemptCount: Number(row.attemptCount || 0),
maxAttempts: Number(row.maxAttempts || DELIVERY_MAX_ATTEMPTS),
payload: row.payload,
target: normalizeTargetRow({
id: row.targetId,
name: row.name,
callbackUrl: row.callbackUrl,
isActive: row.isActive,
authType: row.authType,
authConfig: row.authConfig,
eventTypes: row.eventTypes,
createdAt: row.createdAt,
updatedAt: row.targetUpdatedAt,
}),
}
})
if (!claimed) {
return null
}
let responseStatus: number | null = null
let requestError: string | null = null
try {
const materialId = (claimed.payload as { materialId?: string } | null)?.materialId
if (materialId && !(await prisma.material.findFirst({
where: { id: materialId, deletedAt: null, ...EXTERNAL_MATERIAL_SCOPE_WHERE },
select: { id: true },
}))) {
await executeRaw(
`UPDATE "${DELIVERY_TABLE}" SET status = $2, last_error = $3, next_retry_at = NULL, completed_at = NOW(), updated_at = NOW() WHERE id = $1`,
deliveryId, AUTO_LISTING_DELIVERY_STATUS.EXHAUSTED, '素材已不可对外提供',
)
return { success: false, status: AUTO_LISTING_DELIVERY_STATUS.EXHAUSTED }
}
responseStatus = await postAutoListingCallback(
claimed.target.callbackUrl,
buildNotificationHeaders(claimed.target),
claimed.payload,
)
requestError = summarizeAutoListingCallbackError(null, responseStatus)
if (!requestError) {
await executeRaw(
`
UPDATE "${DELIVERY_TABLE}"
SET
status = $2,
attempt_count = attempt_count + 1,
last_status_code = $3,
last_response_body = $4,
last_error = NULL,
next_retry_at = NULL,
dispatched_at = COALESCE(dispatched_at, NOW()),
completed_at = NOW(),
updated_at = NOW()
WHERE id = $1
`,
deliveryId,
AUTO_LISTING_DELIVERY_STATUS.SUCCEEDED,
responseStatus,
null,
)
return {
success: true,
status: AUTO_LISTING_DELIVERY_STATUS.SUCCEEDED,
}
}
} catch (error) {
requestError = error instanceof AutoListingCallbackError ? error.message : '通知请求失败'
}
const nextAttempt = claimed.attemptCount + 1
const isExhausted = nextAttempt >= claimed.maxAttempts
const retryDelayMs = RETRY_DELAYS_MS[Math.min(nextAttempt, RETRY_DELAYS_MS.length - 1)]
await executeRaw(
`
UPDATE "${DELIVERY_TABLE}"
SET
status = $2,
attempt_count = attempt_count + 1,
last_status_code = $3,
last_response_body = $4,
last_error = $5,
next_retry_at = CASE WHEN $6::bigint IS NULL THEN NULL ELSE NOW() + ($6::bigint * INTERVAL '1 millisecond') END,
completed_at = CASE WHEN $7::boolean THEN NOW() ELSE NULL END,
updated_at = NOW()
WHERE id = $1
`,
deliveryId,
isExhausted ? AUTO_LISTING_DELIVERY_STATUS.EXHAUSTED : AUTO_LISTING_DELIVERY_STATUS.RETRYING,
responseStatus,
null,
requestError,
isExhausted ? null : retryDelayMs,
isExhausted,
)
if (!isExhausted) {
scheduleDeliveryProcessing(deliveryId, retryDelayMs)
}
return {
success: false,
status: isExhausted ? AUTO_LISTING_DELIVERY_STATUS.EXHAUSTED : AUTO_LISTING_DELIVERY_STATUS.RETRYING,
}
}