init
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import { getDb } from '../db/client.js'
|
||||
|
||||
export function createMessageDelivery(input) {
|
||||
const result = getDb().prepare(`
|
||||
INSERT INTO message_deliveries (
|
||||
platform,
|
||||
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
input.platform,
|
||||
input.channel,
|
||||
input.orderId,
|
||||
input.taskId,
|
||||
input.platformOrderId,
|
||||
input.recipientKey,
|
||||
input.messageContent,
|
||||
input.claimUrl,
|
||||
input.status,
|
||||
input.requestUrl,
|
||||
input.requestHeadersJson,
|
||||
input.requestBodyJson,
|
||||
input.responseStatus,
|
||||
input.responseJson,
|
||||
input.errorMessage,
|
||||
input.sentAt,
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
)
|
||||
|
||||
return getMessageDeliveryById(Number(result.lastInsertRowid))
|
||||
}
|
||||
|
||||
export function getMessageDeliveryById(deliveryId) {
|
||||
return getDb().prepare('SELECT * FROM message_deliveries WHERE id = ? LIMIT 1').get(deliveryId) || null
|
||||
}
|
||||
|
||||
export function updateMessageDelivery(deliveryId, patch = {}) {
|
||||
const current = getMessageDeliveryById(deliveryId)
|
||||
|
||||
if (!current) {
|
||||
return null
|
||||
}
|
||||
|
||||
const next = { ...current, ...patch }
|
||||
|
||||
getDb().prepare(`
|
||||
UPDATE message_deliveries
|
||||
SET
|
||||
status = ?,
|
||||
request_url = ?,
|
||||
request_headers_json = ?,
|
||||
request_body_json = ?,
|
||||
response_status = ?,
|
||||
response_json = ?,
|
||||
error_message = ?,
|
||||
sent_at = ?,
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
`).run(
|
||||
next.status,
|
||||
next.request_url,
|
||||
next.request_headers_json,
|
||||
next.request_body_json,
|
||||
next.response_status,
|
||||
next.response_json,
|
||||
next.error_message,
|
||||
next.sent_at,
|
||||
next.updated_at,
|
||||
deliveryId,
|
||||
)
|
||||
|
||||
return getMessageDeliveryById(deliveryId)
|
||||
}
|
||||
|
||||
export function findLatestSuccessfulMessageDeliveryByTask(taskId, channel) {
|
||||
return getDb().prepare(`
|
||||
SELECT *
|
||||
FROM message_deliveries
|
||||
WHERE task_id = ? AND channel = ? AND status = 'success'
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`).get(taskId, channel) || null
|
||||
}
|
||||
Reference in New Issue
Block a user