后端迁移订单仓储模块

This commit is contained in:
yml
2026-05-21 13:53:17 +08:00
parent 79e586387c
commit 173da43fdf
3 changed files with 75 additions and 56 deletions
@@ -1,17 +1,36 @@
// @ts-check
import { query } from '../db/client.js'
import type {
OrderCreateInput,
OrderListQueryInput,
OrderUpdateInput,
} from '../types/repository-inputs.js'
import type {
OrderListQueryResult,
OrderListRow,
OrderRow,
} from '../types/repository-rows.js'
/** @typedef {import('../types/repository-inputs.js').OrderCreateInput} OrderCreateInput */
/** @typedef {import('../types/repository-inputs.js').OrderListQueryInput} OrderListQueryInput */
/** @typedef {import('../types/repository-inputs.js').OrderUpdateInput} OrderUpdateInput */
/** @typedef {import('../types/repository-rows.js').OrderListQueryResult} OrderListQueryResult */
/** @typedef {import('../types/repository-rows.js').OrderListRow} OrderListRow */
/** @typedef {import('../types/repository-rows.js').OrderRow} OrderRow */
type OrderPlatformLookupInput = {
provider?: string
platform: string
shopId?: string
platformOrderId: string
}
/** @returns {Promise<OrderRow | null>} */
export async function findOrderByPlatformOrderId({ provider = 'agiso', platform, shopId = '', platformOrderId }) {
const result = await query(
type OrderPlatformCandidateLookupInput = {
provider?: string
platform?: string
shopIds?: unknown[]
platformOrderId?: string
}
export async function findOrderByPlatformOrderId({
provider = 'agiso',
platform,
shopId = '',
platformOrderId,
}: OrderPlatformLookupInput): Promise<OrderRow | null> {
const result = await query<OrderRow>(
`
SELECT *
FROM orders
@@ -21,16 +40,15 @@ export async function findOrderByPlatformOrderId({ provider = 'agiso', platform,
[provider, platform, shopId, platformOrderId],
)
return /** @type {OrderRow | null} */ (result.rows[0] || null)
return result.rows[0] || null
}
/** @returns {Promise<OrderRow | null>} */
export async function findOrderByPlatformOrderIdCandidates({
provider = 'agiso',
platform,
shopIds = [],
platformOrderId,
}) {
}: OrderPlatformCandidateLookupInput): Promise<OrderRow | null> {
const normalizedShopIds = [...new Set((Array.isArray(shopIds) ? shopIds : [])
.map((item) => String(item || '').trim())
.filter(Boolean))]
@@ -39,7 +57,7 @@ export async function findOrderByPlatformOrderIdCandidates({
return null
}
const result = await query(
const result = await query<OrderRow>(
`
SELECT *
FROM orders
@@ -50,13 +68,11 @@ export async function findOrderByPlatformOrderIdCandidates({
[provider, platform, normalizedShopIds, platformOrderId],
)
return /** @type {OrderRow | null} */ (result.rows[0] || null)
return result.rows[0] || null
}
/** @returns {Promise<OrderRow | null>} */
/** @param {OrderCreateInput} input */
export async function createOrder(input) {
const result = await query(
export async function createOrder(input: OrderCreateInput): Promise<OrderRow | null> {
const result = await query<OrderRow>(
`
INSERT INTO orders (
provider,
@@ -98,13 +114,11 @@ export async function createOrder(input) {
],
)
return /** @type {OrderRow | null} */ (result.rows[0] || null)
return result.rows[0] || null
}
/** @returns {Promise<OrderRow | null>} */
/** @param {OrderUpdateInput} input */
export async function updateOrder(orderId, input) {
const result = await query(
export async function updateOrder(orderId: number | string, input: OrderUpdateInput): Promise<OrderRow | null> {
const result = await query<OrderRow>(
`
UPDATE orders
SET
@@ -144,17 +158,14 @@ export async function updateOrder(orderId, input) {
],
)
return /** @type {OrderRow | null} */ (result.rows[0] || null)
return result.rows[0] || null
}
/** @returns {Promise<OrderRow | null>} */
export async function getOrderById(orderId) {
const result = await query('SELECT * FROM orders WHERE id = $1 LIMIT 1', [Number(orderId)])
return /** @type {OrderRow | null} */ (result.rows[0] || null)
export async function getOrderById(orderId: number | string): Promise<OrderRow | null> {
const result = await query<OrderRow>('SELECT * FROM orders WHERE id = $1 LIMIT 1', [Number(orderId)])
return result.rows[0] || null
}
/** @returns {Promise<OrderListQueryResult>} */
/** @param {OrderListQueryInput} [queryInput] */
export async function listOrders({
page = 1,
pageSize = 20,
@@ -163,10 +174,10 @@ export async function listOrders({
skuCode = '',
dateFrom = '',
dateTo = '',
} = /** @type {OrderListQueryInput} */ ({})) {
}: OrderListQueryInput = {}): Promise<OrderListQueryResult> {
const offset = (page - 1) * pageSize
const filters = []
const params = []
const filters: string[] = []
const params: unknown[] = []
if (platformOrderId) {
params.push(`%${platformOrderId}%`)
@@ -194,11 +205,14 @@ export async function listOrders({
}
const whereClause = filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : ''
const totalResult = await query(`SELECT COUNT(*)::int AS total FROM orders o ${whereClause}`, params)
const totalResult = await query<{ [column: string]: unknown, total: number }>(
`SELECT COUNT(*)::int AS total FROM orders o ${whereClause}`,
params,
)
params.push(pageSize)
params.push(offset)
const itemsResult = await query(
const itemsResult = await query<OrderListRow>(
`
SELECT
o.*,
@@ -212,7 +226,7 @@ export async function listOrders({
)
return {
items: /** @type {OrderListRow[]} */ (itemsResult.rows),
items: itemsResult.rows,
total: Number(totalResult.rows[0]?.total || 0),
}
}
@@ -1,13 +1,13 @@
// @ts-check
import { query } from '../db/client.js'
import type {
TaskInventoryBindingRow,
TaskInventoryBindingSummaryRow,
} from '../types/repository-rows.js'
/** @typedef {import('../types/repository-rows.js').TaskInventoryBindingRow} TaskInventoryBindingRow */
/** @typedef {import('../types/repository-rows.js').TaskInventoryBindingSummaryRow} TaskInventoryBindingSummaryRow */
/** @returns {Promise<TaskInventoryBindingRow[]>} */
export async function listTaskInventoryBindingsByTaskId(taskId) {
const result = await query(
export async function listTaskInventoryBindingsByTaskId(
taskId: number | string,
): Promise<TaskInventoryBindingRow[]> {
const result = await query<TaskInventoryBindingRow>(
`
SELECT
tib.id,
@@ -36,12 +36,13 @@ export async function listTaskInventoryBindingsByTaskId(taskId) {
[Number(taskId)],
)
return /** @type {TaskInventoryBindingRow[]} */ (result.rows)
return result.rows
}
/** @returns {Promise<TaskInventoryBindingRow | null>} */
export async function getTaskInventoryBindingById(bindingId) {
const result = await query(
export async function getTaskInventoryBindingById(
bindingId: number | string,
): Promise<TaskInventoryBindingRow | null> {
const result = await query<TaskInventoryBindingRow>(
`
SELECT
tib.id,
@@ -70,11 +71,12 @@ export async function getTaskInventoryBindingById(bindingId) {
[Number(bindingId)],
)
return /** @type {TaskInventoryBindingRow | null} */ (result.rows[0] || null)
return result.rows[0] || null
}
/** @returns {Promise<TaskInventoryBindingSummaryRow[]>} */
export async function listTaskInventoryBindingSummariesByTaskIds(taskIds = []) {
export async function listTaskInventoryBindingSummariesByTaskIds(
taskIds: unknown[] = [],
): Promise<TaskInventoryBindingSummaryRow[]> {
const normalizedTaskIds = Array.from(new Set((Array.isArray(taskIds) ? taskIds : [])
.map((value) => Number(value))
.filter((value) => Number.isFinite(value) && value > 0)))
@@ -83,7 +85,7 @@ export async function listTaskInventoryBindingSummariesByTaskIds(taskIds = []) {
return []
}
const result = await query(
const result = await query<TaskInventoryBindingSummaryRow>(
`
SELECT
tib.task_id,
@@ -99,5 +101,5 @@ export async function listTaskInventoryBindingSummariesByTaskIds(taskIds = []) {
[normalizedTaskIds],
)
return /** @type {TaskInventoryBindingSummaryRow[]} */ (result.rows)
return result.rows
}
+4 -1
View File
@@ -228,12 +228,15 @@
- `src/repositories/admin-user-repo.ts`
- `src/repositories/product-match-rule-repo.ts`
- `src/repositories/fulfillment-profile-repo.ts`
15. repository 第四批已迁移到 `.ts`
- `src/repositories/order-repo.ts`
- `src/repositories/task-inventory-binding-repo.ts`
## 下一步建议
第一批继续推进时,建议按这个顺序:
1. 继续迁移剩余核心 repository`inventory-repo``order-repo``task-repo`
1. 继续迁移剩余核心 repository`inventory-repo``task-repo`
2. 拆分并迁移 `runtime.js`,把 env 解析、默认配置加载、配置合并分开
3. 为 webhook、库存换码、自动发货补测试