This commit is contained in:
yml
2026-04-08 16:30:42 +08:00
commit 313c036845
131 changed files with 22393 additions and 0 deletions
@@ -0,0 +1,127 @@
import { getDb } from '../db/client.js'
export function createWebhookEvent(input) {
const result = getDb().prepare(`
INSERT INTO webhook_events (
platform,
event_type,
event_key,
signature_valid,
headers_json,
query_json,
body_json,
processed,
process_error,
related_order_id,
created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
input.platform,
input.eventType,
input.eventKey,
input.signatureValid ? 1 : 0,
input.headersJson,
input.queryJson,
input.bodyJson,
input.processed ? 1 : 0,
input.processError,
input.relatedOrderId,
input.createdAt,
)
return getWebhookEventById(Number(result.lastInsertRowid))
}
export function updateWebhookEvent(eventId, patch) {
const current = getWebhookEventById(eventId)
if (!current) {
return null
}
const next = { ...current, ...patch }
getDb().prepare(`
UPDATE webhook_events
SET
processed = ?,
process_error = ?,
related_order_id = ?
WHERE id = ?
`).run(next.processed ? 1 : 0, next.process_error, next.related_order_id, eventId)
return getWebhookEventById(eventId)
}
export function getWebhookEventById(eventId) {
return getDb().prepare('SELECT * FROM webhook_events WHERE id = ? LIMIT 1').get(eventId) || null
}
export function listWebhookEvents({
page = 1,
pageSize = 20,
platform = '',
processed = '',
relatedOrderId = '',
dateFrom = '',
dateTo = '',
} = {}) {
const offset = (page - 1) * pageSize
const filters = []
const params = []
if (platform) {
filters.push('platform = ?')
params.push(platform)
}
if (processed === '0' || processed === '1') {
filters.push('processed = ?')
params.push(Number(processed))
}
if (relatedOrderId) {
filters.push('related_order_id = ?')
params.push(Number(relatedOrderId))
}
if (dateFrom) {
filters.push('created_at >= ?')
params.push(dateFrom)
}
if (dateTo) {
filters.push('created_at <= ?')
params.push(dateTo)
}
const whereClause = filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : ''
const db = getDb()
const totalRow = db.prepare(`
SELECT COUNT(*) AS total
FROM webhook_events
${whereClause}
`).get(...params)
const items = db.prepare(`
SELECT *
FROM webhook_events
${whereClause}
ORDER BY id DESC
LIMIT ? OFFSET ?
`).all(...params, pageSize, offset)
return {
items,
total: Number(totalRow?.total || 0),
}
}
export function listWebhookEventsByOrderId(orderId) {
return getDb().prepare(`
SELECT *
FROM webhook_events
WHERE related_order_id = ?
ORDER BY id DESC
`).all(orderId)
}