优化订单流程与数据库关闭
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import type { QueryResult, QueryResultRow } from 'pg'
|
||||
|
||||
import { query } from '../db/client.js'
|
||||
import type {
|
||||
OrderCreateInput,
|
||||
@@ -6,6 +8,11 @@ import type {
|
||||
} from '../types/repository/inputs.js'
|
||||
import type { OrderListQueryResult, OrderListRow, OrderRow } from '../types/repository/rows.js'
|
||||
|
||||
export type OrderQueryExecutor = <T extends QueryResultRow = QueryResultRow>(
|
||||
text: string,
|
||||
params?: unknown[],
|
||||
) => Promise<QueryResult<T>>
|
||||
|
||||
type OrderPlatformLookupInput = {
|
||||
provider?: string
|
||||
platform: string
|
||||
@@ -37,7 +44,18 @@ export async function findLatestOrderByPlatformOrderId({
|
||||
platform,
|
||||
platformOrderId,
|
||||
}: Omit<OrderPlatformLookupInput, 'shopId'>): Promise<OrderRow | null> {
|
||||
const result = await query<OrderRow>(
|
||||
return findLatestOrderByPlatformOrderIdWithExecutor(query, {
|
||||
provider,
|
||||
platform,
|
||||
platformOrderId,
|
||||
})
|
||||
}
|
||||
|
||||
export async function findLatestOrderByPlatformOrderIdWithExecutor(
|
||||
executor: OrderQueryExecutor,
|
||||
{ provider = '91kaquan', platform, platformOrderId }: Omit<OrderPlatformLookupInput, 'shopId'>,
|
||||
): Promise<OrderRow | null> {
|
||||
const result = await executor<OrderRow>(
|
||||
`
|
||||
SELECT *
|
||||
FROM orders
|
||||
@@ -69,7 +87,14 @@ export async function findLatestOrderByAnyPlatformOrderId(
|
||||
}
|
||||
|
||||
export async function createOrder(input: OrderCreateInput): Promise<OrderRow | null> {
|
||||
const result = await query<OrderRow>(
|
||||
return createOrderWithExecutor(query, input)
|
||||
}
|
||||
|
||||
export async function createOrderWithExecutor(
|
||||
executor: OrderQueryExecutor,
|
||||
input: OrderCreateInput,
|
||||
): Promise<OrderRow | null> {
|
||||
const result = await executor<OrderRow>(
|
||||
`
|
||||
INSERT INTO orders (
|
||||
provider,
|
||||
@@ -118,7 +143,15 @@ export async function updateOrder(
|
||||
orderId: number | string,
|
||||
input: OrderUpdateInput,
|
||||
): Promise<OrderRow | null> {
|
||||
const result = await query<OrderRow>(
|
||||
return updateOrderWithExecutor(query, orderId, input)
|
||||
}
|
||||
|
||||
export async function updateOrderWithExecutor(
|
||||
executor: OrderQueryExecutor,
|
||||
orderId: number | string,
|
||||
input: OrderUpdateInput,
|
||||
): Promise<OrderRow | null> {
|
||||
const result = await executor<OrderRow>(
|
||||
`
|
||||
UPDATE orders
|
||||
SET
|
||||
|
||||
Reference in New Issue
Block a user