重构后台管理服务并推进渐进式类型化

This commit is contained in:
yml2213
2026-04-14 09:21:20 +08:00
parent fa36be6539
commit 335eb44104
35 changed files with 4143 additions and 2738 deletions
@@ -1,5 +1,15 @@
// @ts-check
import { query } from '../db/client.js'
/** @typedef {import('../types/repository-inputs.js').WebhookEventCreateInput} WebhookEventCreateInput */
/** @typedef {import('../types/repository-inputs.js').WebhookEventListQueryInput} WebhookEventListQueryInput */
/** @typedef {import('../types/repository-inputs.js').WebhookEventUpdatePatch} WebhookEventUpdatePatch */
/** @typedef {import('../types/repository-rows.js').WebhookEventListQueryResult} WebhookEventListQueryResult */
/** @typedef {import('../types/repository-rows.js').WebhookEventRow} WebhookEventRow */
/** @returns {Promise<WebhookEventRow | null>} */
/** @param {WebhookEventCreateInput} input */
export async function createWebhookEvent(input) {
const result = await query(
`
@@ -39,9 +49,11 @@ export async function createWebhookEvent(input) {
],
)
return result.rows[0] || null
return /** @type {WebhookEventRow | null} */ (result.rows[0] || null)
}
/** @returns {Promise<WebhookEventRow | null>} */
/** @param {WebhookEventUpdatePatch} patch */
export async function updateWebhookEvent(eventId, patch) {
const current = await getWebhookEventById(eventId)
if (!current) {
@@ -62,14 +74,17 @@ export async function updateWebhookEvent(eventId, patch) {
[Boolean(next.processed), next.process_error || '', next.related_order_id || null, Number(eventId)],
)
return result.rows[0] || null
return /** @type {WebhookEventRow | null} */ (result.rows[0] || null)
}
/** @returns {Promise<WebhookEventRow | null>} */
export async function getWebhookEventById(eventId) {
const result = await query('SELECT * FROM webhook_events WHERE id = $1 LIMIT 1', [Number(eventId)])
return result.rows[0] || null
return /** @type {WebhookEventRow | null} */ (result.rows[0] || null)
}
/** @returns {Promise<WebhookEventListQueryResult>} */
/** @param {WebhookEventListQueryInput} [queryInput] */
export async function listWebhookEvents({
page = 1,
pageSize = 20,
@@ -80,7 +95,7 @@ export async function listWebhookEvents({
relatedOrderId = '',
dateFrom = '',
dateTo = '',
} = {}) {
} = /** @type {WebhookEventListQueryInput} */ ({})) {
const offset = (page - 1) * pageSize
const filters = []
const params = []
@@ -137,11 +152,12 @@ export async function listWebhookEvents({
)
return {
items: itemsResult.rows,
items: /** @type {WebhookEventRow[]} */ (itemsResult.rows),
total: Number(totalResult.rows[0]?.total || 0),
}
}
/** @returns {Promise<WebhookEventRow[]>} */
export async function listWebhookEventsByOrderId(orderId) {
const result = await query(
`
@@ -153,5 +169,5 @@ export async function listWebhookEventsByOrderId(orderId) {
[Number(orderId)],
)
return result.rows
return /** @type {WebhookEventRow[]} */ (result.rows)
}