后端迁移订单仓储模块
This commit is contained in:
+52
-38
@@ -1,17 +1,36 @@
|
|||||||
// @ts-check
|
|
||||||
|
|
||||||
import { query } from '../db/client.js'
|
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 */
|
type OrderPlatformLookupInput = {
|
||||||
/** @typedef {import('../types/repository-inputs.js').OrderListQueryInput} OrderListQueryInput */
|
provider?: string
|
||||||
/** @typedef {import('../types/repository-inputs.js').OrderUpdateInput} OrderUpdateInput */
|
platform: string
|
||||||
/** @typedef {import('../types/repository-rows.js').OrderListQueryResult} OrderListQueryResult */
|
shopId?: string
|
||||||
/** @typedef {import('../types/repository-rows.js').OrderListRow} OrderListRow */
|
platformOrderId: string
|
||||||
/** @typedef {import('../types/repository-rows.js').OrderRow} OrderRow */
|
}
|
||||||
|
|
||||||
/** @returns {Promise<OrderRow | null>} */
|
type OrderPlatformCandidateLookupInput = {
|
||||||
export async function findOrderByPlatformOrderId({ provider = 'agiso', platform, shopId = '', platformOrderId }) {
|
provider?: string
|
||||||
const result = await query(
|
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 *
|
SELECT *
|
||||||
FROM orders
|
FROM orders
|
||||||
@@ -21,16 +40,15 @@ export async function findOrderByPlatformOrderId({ provider = 'agiso', platform,
|
|||||||
[provider, platform, shopId, platformOrderId],
|
[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({
|
export async function findOrderByPlatformOrderIdCandidates({
|
||||||
provider = 'agiso',
|
provider = 'agiso',
|
||||||
platform,
|
platform,
|
||||||
shopIds = [],
|
shopIds = [],
|
||||||
platformOrderId,
|
platformOrderId,
|
||||||
}) {
|
}: OrderPlatformCandidateLookupInput): Promise<OrderRow | null> {
|
||||||
const normalizedShopIds = [...new Set((Array.isArray(shopIds) ? shopIds : [])
|
const normalizedShopIds = [...new Set((Array.isArray(shopIds) ? shopIds : [])
|
||||||
.map((item) => String(item || '').trim())
|
.map((item) => String(item || '').trim())
|
||||||
.filter(Boolean))]
|
.filter(Boolean))]
|
||||||
@@ -39,7 +57,7 @@ export async function findOrderByPlatformOrderIdCandidates({
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await query(
|
const result = await query<OrderRow>(
|
||||||
`
|
`
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM orders
|
FROM orders
|
||||||
@@ -50,13 +68,11 @@ export async function findOrderByPlatformOrderIdCandidates({
|
|||||||
[provider, platform, normalizedShopIds, platformOrderId],
|
[provider, platform, normalizedShopIds, platformOrderId],
|
||||||
)
|
)
|
||||||
|
|
||||||
return /** @type {OrderRow | null} */ (result.rows[0] || null)
|
return result.rows[0] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {Promise<OrderRow | null>} */
|
export async function createOrder(input: OrderCreateInput): Promise<OrderRow | null> {
|
||||||
/** @param {OrderCreateInput} input */
|
const result = await query<OrderRow>(
|
||||||
export async function createOrder(input) {
|
|
||||||
const result = await query(
|
|
||||||
`
|
`
|
||||||
INSERT INTO orders (
|
INSERT INTO orders (
|
||||||
provider,
|
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>} */
|
export async function updateOrder(orderId: number | string, input: OrderUpdateInput): Promise<OrderRow | null> {
|
||||||
/** @param {OrderUpdateInput} input */
|
const result = await query<OrderRow>(
|
||||||
export async function updateOrder(orderId, input) {
|
|
||||||
const result = await query(
|
|
||||||
`
|
`
|
||||||
UPDATE orders
|
UPDATE orders
|
||||||
SET
|
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: number | string): Promise<OrderRow | null> {
|
||||||
export async function getOrderById(orderId) {
|
const result = await query<OrderRow>('SELECT * FROM orders WHERE id = $1 LIMIT 1', [Number(orderId)])
|
||||||
const result = await query('SELECT * FROM orders WHERE id = $1 LIMIT 1', [Number(orderId)])
|
return result.rows[0] || null
|
||||||
return /** @type {OrderRow | null} */ (result.rows[0] || null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {Promise<OrderListQueryResult>} */
|
|
||||||
/** @param {OrderListQueryInput} [queryInput] */
|
|
||||||
export async function listOrders({
|
export async function listOrders({
|
||||||
page = 1,
|
page = 1,
|
||||||
pageSize = 20,
|
pageSize = 20,
|
||||||
@@ -163,10 +174,10 @@ export async function listOrders({
|
|||||||
skuCode = '',
|
skuCode = '',
|
||||||
dateFrom = '',
|
dateFrom = '',
|
||||||
dateTo = '',
|
dateTo = '',
|
||||||
} = /** @type {OrderListQueryInput} */ ({})) {
|
}: OrderListQueryInput = {}): Promise<OrderListQueryResult> {
|
||||||
const offset = (page - 1) * pageSize
|
const offset = (page - 1) * pageSize
|
||||||
const filters = []
|
const filters: string[] = []
|
||||||
const params = []
|
const params: unknown[] = []
|
||||||
|
|
||||||
if (platformOrderId) {
|
if (platformOrderId) {
|
||||||
params.push(`%${platformOrderId}%`)
|
params.push(`%${platformOrderId}%`)
|
||||||
@@ -194,11 +205,14 @@ export async function listOrders({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const whereClause = filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : ''
|
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(pageSize)
|
||||||
params.push(offset)
|
params.push(offset)
|
||||||
const itemsResult = await query(
|
const itemsResult = await query<OrderListRow>(
|
||||||
`
|
`
|
||||||
SELECT
|
SELECT
|
||||||
o.*,
|
o.*,
|
||||||
@@ -212,7 +226,7 @@ export async function listOrders({
|
|||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
items: /** @type {OrderListRow[]} */ (itemsResult.rows),
|
items: itemsResult.rows,
|
||||||
total: Number(totalResult.rows[0]?.total || 0),
|
total: Number(totalResult.rows[0]?.total || 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+19
-17
@@ -1,13 +1,13 @@
|
|||||||
// @ts-check
|
|
||||||
|
|
||||||
import { query } from '../db/client.js'
|
import { query } from '../db/client.js'
|
||||||
|
import type {
|
||||||
|
TaskInventoryBindingRow,
|
||||||
|
TaskInventoryBindingSummaryRow,
|
||||||
|
} from '../types/repository-rows.js'
|
||||||
|
|
||||||
/** @typedef {import('../types/repository-rows.js').TaskInventoryBindingRow} TaskInventoryBindingRow */
|
export async function listTaskInventoryBindingsByTaskId(
|
||||||
/** @typedef {import('../types/repository-rows.js').TaskInventoryBindingSummaryRow} TaskInventoryBindingSummaryRow */
|
taskId: number | string,
|
||||||
|
): Promise<TaskInventoryBindingRow[]> {
|
||||||
/** @returns {Promise<TaskInventoryBindingRow[]>} */
|
const result = await query<TaskInventoryBindingRow>(
|
||||||
export async function listTaskInventoryBindingsByTaskId(taskId) {
|
|
||||||
const result = await query(
|
|
||||||
`
|
`
|
||||||
SELECT
|
SELECT
|
||||||
tib.id,
|
tib.id,
|
||||||
@@ -36,12 +36,13 @@ export async function listTaskInventoryBindingsByTaskId(taskId) {
|
|||||||
[Number(taskId)],
|
[Number(taskId)],
|
||||||
)
|
)
|
||||||
|
|
||||||
return /** @type {TaskInventoryBindingRow[]} */ (result.rows)
|
return result.rows
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {Promise<TaskInventoryBindingRow | null>} */
|
export async function getTaskInventoryBindingById(
|
||||||
export async function getTaskInventoryBindingById(bindingId) {
|
bindingId: number | string,
|
||||||
const result = await query(
|
): Promise<TaskInventoryBindingRow | null> {
|
||||||
|
const result = await query<TaskInventoryBindingRow>(
|
||||||
`
|
`
|
||||||
SELECT
|
SELECT
|
||||||
tib.id,
|
tib.id,
|
||||||
@@ -70,11 +71,12 @@ export async function getTaskInventoryBindingById(bindingId) {
|
|||||||
[Number(bindingId)],
|
[Number(bindingId)],
|
||||||
)
|
)
|
||||||
|
|
||||||
return /** @type {TaskInventoryBindingRow | null} */ (result.rows[0] || null)
|
return result.rows[0] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {Promise<TaskInventoryBindingSummaryRow[]>} */
|
export async function listTaskInventoryBindingSummariesByTaskIds(
|
||||||
export async function listTaskInventoryBindingSummariesByTaskIds(taskIds = []) {
|
taskIds: unknown[] = [],
|
||||||
|
): Promise<TaskInventoryBindingSummaryRow[]> {
|
||||||
const normalizedTaskIds = Array.from(new Set((Array.isArray(taskIds) ? taskIds : [])
|
const normalizedTaskIds = Array.from(new Set((Array.isArray(taskIds) ? taskIds : [])
|
||||||
.map((value) => Number(value))
|
.map((value) => Number(value))
|
||||||
.filter((value) => Number.isFinite(value) && value > 0)))
|
.filter((value) => Number.isFinite(value) && value > 0)))
|
||||||
@@ -83,7 +85,7 @@ export async function listTaskInventoryBindingSummariesByTaskIds(taskIds = []) {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await query(
|
const result = await query<TaskInventoryBindingSummaryRow>(
|
||||||
`
|
`
|
||||||
SELECT
|
SELECT
|
||||||
tib.task_id,
|
tib.task_id,
|
||||||
@@ -99,5 +101,5 @@ export async function listTaskInventoryBindingSummariesByTaskIds(taskIds = []) {
|
|||||||
[normalizedTaskIds],
|
[normalizedTaskIds],
|
||||||
)
|
)
|
||||||
|
|
||||||
return /** @type {TaskInventoryBindingSummaryRow[]} */ (result.rows)
|
return result.rows
|
||||||
}
|
}
|
||||||
@@ -228,12 +228,15 @@
|
|||||||
- `src/repositories/admin-user-repo.ts`
|
- `src/repositories/admin-user-repo.ts`
|
||||||
- `src/repositories/product-match-rule-repo.ts`
|
- `src/repositories/product-match-rule-repo.ts`
|
||||||
- `src/repositories/fulfillment-profile-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 解析、默认配置加载、配置合并分开
|
2. 拆分并迁移 `runtime.js`,把 env 解析、默认配置加载、配置合并分开
|
||||||
3. 为 webhook、库存换码、自动发货补测试
|
3. 为 webhook、库存换码、自动发货补测试
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user