feat(order-service): 重构分类资料收集与订单状态链路
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import type { PoolClient } from 'pg'
|
||||
|
||||
import { query } from '../db/client.js'
|
||||
|
||||
export type FileAssetRow = {
|
||||
@@ -87,8 +89,12 @@ export async function createFileAsset(input: CreateFileAssetInput): Promise<File
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function getFileAssetByObjectKey(objectKey: string): Promise<FileAssetRow | null> {
|
||||
const result = await query<FileAssetRow>(
|
||||
export async function getFileAssetByObjectKey(
|
||||
objectKey: string,
|
||||
client?: Pick<PoolClient, 'query'>,
|
||||
): Promise<FileAssetRow | null> {
|
||||
const db = client || { query }
|
||||
const result = await db.query<FileAssetRow>(
|
||||
'SELECT * FROM file_assets WHERE object_key = $1 AND status = $2 LIMIT 1',
|
||||
[String(objectKey || '').trim(), 'active'],
|
||||
)
|
||||
|
||||
@@ -29,3 +29,4 @@ export * from './product-match-repo.js'
|
||||
export * from './sms-code-repo.js'
|
||||
export * from './consignment-repo.js'
|
||||
export * from './consignment-contact-repo.js'
|
||||
export * from './order-service-profile-repo.js'
|
||||
|
||||
@@ -0,0 +1,294 @@
|
||||
import type { PoolClient } from 'pg'
|
||||
|
||||
import { query } from '../../db/client.js'
|
||||
|
||||
export type OrderServiceProfileRow = {
|
||||
id: number
|
||||
category_id: number
|
||||
name: string
|
||||
title: string
|
||||
description: string
|
||||
guide_image_json: Record<string, unknown>
|
||||
fields_json: Array<Record<string, unknown>>
|
||||
screenshots_json: Record<string, unknown>
|
||||
auto_publish_json: Record<string, unknown>
|
||||
submit_text: string
|
||||
success_text: string
|
||||
status: string
|
||||
current_version_id: number | null
|
||||
current_version_no: number | null
|
||||
created_by_admin_user_id: number | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export type OrderServicePublishedProfileRow = {
|
||||
profile_id: number
|
||||
category_id: number
|
||||
version_id: number
|
||||
version_no: number
|
||||
config_json: Record<string, unknown>
|
||||
}
|
||||
|
||||
const PROFILE_SELECT = `
|
||||
SELECT
|
||||
p.*,
|
||||
v.version_no AS current_version_no
|
||||
FROM order_service_profiles p
|
||||
LEFT JOIN order_service_profile_versions v ON v.id = p.current_version_id
|
||||
`
|
||||
|
||||
function mapProfile(row: OrderServiceProfileRow): OrderServiceProfileRow {
|
||||
return {
|
||||
...row,
|
||||
id: Number(row.id),
|
||||
category_id: Number(row.category_id),
|
||||
current_version_id:
|
||||
row.current_version_id === null || row.current_version_id === undefined
|
||||
? null
|
||||
: Number(row.current_version_id),
|
||||
current_version_no:
|
||||
row.current_version_no === null || row.current_version_no === undefined
|
||||
? null
|
||||
: Number(row.current_version_no),
|
||||
created_by_admin_user_id:
|
||||
row.created_by_admin_user_id === null || row.created_by_admin_user_id === undefined
|
||||
? null
|
||||
: Number(row.created_by_admin_user_id),
|
||||
}
|
||||
}
|
||||
|
||||
export async function getOrderServiceProfileByCategory(
|
||||
categoryId: number | string,
|
||||
client?: Pick<PoolClient, 'query'>,
|
||||
): Promise<OrderServiceProfileRow | null> {
|
||||
const db = client || { query }
|
||||
const result = await db.query<OrderServiceProfileRow>(
|
||||
`${PROFILE_SELECT} WHERE p.category_id = $1 LIMIT 1`,
|
||||
[Number(categoryId)],
|
||||
)
|
||||
return result.rows[0] ? mapProfile(result.rows[0]) : null
|
||||
}
|
||||
|
||||
export async function getOrderServiceProfileById(
|
||||
profileId: number | string,
|
||||
client?: Pick<PoolClient, 'query'>,
|
||||
): Promise<OrderServiceProfileRow | null> {
|
||||
const db = client || { query }
|
||||
const result = await db.query<OrderServiceProfileRow>(
|
||||
`${PROFILE_SELECT} WHERE p.id = $1 LIMIT 1`,
|
||||
[Number(profileId)],
|
||||
)
|
||||
return result.rows[0] ? mapProfile(result.rows[0]) : null
|
||||
}
|
||||
|
||||
export async function listOrderServiceProfiles(): Promise<OrderServiceProfileRow[]> {
|
||||
const result = await query<OrderServiceProfileRow>(
|
||||
`${PROFILE_SELECT} ORDER BY p.updated_at DESC, p.id DESC`,
|
||||
)
|
||||
return result.rows.map(mapProfile)
|
||||
}
|
||||
|
||||
export async function upsertOrderServiceProfile(input: {
|
||||
categoryId: number
|
||||
name: string
|
||||
title: string
|
||||
description: string
|
||||
guideImage: Record<string, unknown>
|
||||
fields: Array<Record<string, unknown>>
|
||||
screenshots: Record<string, unknown>
|
||||
autoPublish: Record<string, unknown>
|
||||
submitText: string
|
||||
successText: string
|
||||
adminUserId: number | null
|
||||
}): Promise<OrderServiceProfileRow> {
|
||||
const result = await query<OrderServiceProfileRow>(
|
||||
`
|
||||
INSERT INTO order_service_profiles (
|
||||
category_id, name, title, description,
|
||||
guide_image_json, fields_json, screenshots_json, auto_publish_json,
|
||||
submit_text, success_text, status, created_by_admin_user_id
|
||||
) VALUES (
|
||||
$1, $2, $3, $4,
|
||||
$5::jsonb, $6::jsonb, $7::jsonb, $8::jsonb,
|
||||
$9, $10, 'draft', $11
|
||||
)
|
||||
ON CONFLICT (category_id) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
title = EXCLUDED.title,
|
||||
description = EXCLUDED.description,
|
||||
guide_image_json = EXCLUDED.guide_image_json,
|
||||
fields_json = EXCLUDED.fields_json,
|
||||
screenshots_json = EXCLUDED.screenshots_json,
|
||||
auto_publish_json = EXCLUDED.auto_publish_json,
|
||||
submit_text = EXCLUDED.submit_text,
|
||||
success_text = EXCLUDED.success_text,
|
||||
current_version_id = CASE
|
||||
WHEN order_service_profiles.status = 'archived' THEN NULL
|
||||
ELSE order_service_profiles.current_version_id
|
||||
END,
|
||||
status = 'draft',
|
||||
updated_at = NOW()
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.categoryId,
|
||||
input.name,
|
||||
input.title,
|
||||
input.description,
|
||||
JSON.stringify(input.guideImage),
|
||||
JSON.stringify(input.fields),
|
||||
JSON.stringify(input.screenshots),
|
||||
JSON.stringify(input.autoPublish),
|
||||
input.submitText,
|
||||
input.successText,
|
||||
input.adminUserId,
|
||||
],
|
||||
)
|
||||
const row = result.rows[0]
|
||||
if (!row) throw new Error('order service profile upsert returned no row')
|
||||
return mapProfile(row)
|
||||
}
|
||||
|
||||
export async function archiveOrderServiceProfile(
|
||||
profileId: number | string,
|
||||
): Promise<OrderServiceProfileRow | null> {
|
||||
const result = await query<OrderServiceProfileRow>(
|
||||
`UPDATE order_service_profiles SET status = 'archived', updated_at = NOW() WHERE id = $1 RETURNING *`,
|
||||
[Number(profileId)],
|
||||
)
|
||||
return result.rows[0] ? mapProfile(result.rows[0]) : null
|
||||
}
|
||||
|
||||
export async function createOrderServiceProfileVersion(
|
||||
client: PoolClient,
|
||||
input: {
|
||||
profileId: number
|
||||
config: Record<string, unknown>
|
||||
adminUserId: number | null
|
||||
},
|
||||
): Promise<{ versionId: number; versionNo: number }> {
|
||||
const versionResult = await client.query<{ next_version_no: number }>(
|
||||
`
|
||||
SELECT COALESCE(MAX(version_no), 0) + 1 AS next_version_no
|
||||
FROM order_service_profile_versions
|
||||
WHERE profile_id = $1
|
||||
`,
|
||||
[input.profileId],
|
||||
)
|
||||
const versionNo = Math.max(1, Number(versionResult.rows[0]?.next_version_no || 1))
|
||||
const inserted = await client.query<{ id: number }>(
|
||||
`
|
||||
INSERT INTO order_service_profile_versions (
|
||||
profile_id, version_no, config_json, published_by_admin_user_id
|
||||
) VALUES ($1, $2, $3::jsonb, $4)
|
||||
RETURNING id
|
||||
`,
|
||||
[input.profileId, versionNo, JSON.stringify(input.config), input.adminUserId],
|
||||
)
|
||||
return {
|
||||
versionId: Number(inserted.rows[0]?.id || 0),
|
||||
versionNo,
|
||||
}
|
||||
}
|
||||
|
||||
export async function setOrderServiceProfileCurrentVersion(
|
||||
client: PoolClient,
|
||||
input: { profileId: number; versionId: number },
|
||||
): Promise<void> {
|
||||
await client.query(
|
||||
`UPDATE order_service_profiles SET current_version_id = $2, status = 'published', updated_at = NOW() WHERE id = $1`,
|
||||
[input.profileId, input.versionId],
|
||||
)
|
||||
}
|
||||
|
||||
export async function getOrderServiceProfileVersion(
|
||||
versionId: number | string,
|
||||
client?: Pick<PoolClient, 'query'>,
|
||||
): Promise<{
|
||||
id: number
|
||||
profile_id: number
|
||||
version_no: number
|
||||
config_json: Record<string, unknown>
|
||||
} | null> {
|
||||
const db = client || { query }
|
||||
const result = await db.query<{
|
||||
id: number
|
||||
profile_id: number
|
||||
version_no: number
|
||||
config_json: Record<string, unknown>
|
||||
}>(
|
||||
`SELECT id, profile_id, version_no, config_json FROM order_service_profile_versions WHERE id = $1 LIMIT 1`,
|
||||
[Number(versionId)],
|
||||
)
|
||||
const row = result.rows[0]
|
||||
return row
|
||||
? {
|
||||
id: Number(row.id),
|
||||
profile_id: Number(row.profile_id),
|
||||
version_no: Number(row.version_no),
|
||||
config_json: row.config_json,
|
||||
}
|
||||
: null
|
||||
}
|
||||
|
||||
export async function getPublishedOrderServiceProfileByCategory(
|
||||
categoryId: number | string,
|
||||
client?: Pick<PoolClient, 'query'>,
|
||||
): Promise<OrderServicePublishedProfileRow | null> {
|
||||
const db = client || { query }
|
||||
const result = await db.query<OrderServicePublishedProfileRow>(
|
||||
`
|
||||
SELECT
|
||||
p.id AS profile_id,
|
||||
p.category_id,
|
||||
v.id AS version_id,
|
||||
v.version_no,
|
||||
v.config_json
|
||||
FROM order_service_profiles p
|
||||
JOIN order_service_profile_versions v ON v.id = p.current_version_id
|
||||
WHERE p.category_id = $1
|
||||
AND p.status <> 'archived'
|
||||
LIMIT 1
|
||||
`,
|
||||
[Number(categoryId)],
|
||||
)
|
||||
const row = result.rows[0]
|
||||
return row
|
||||
? {
|
||||
profile_id: Number(row.profile_id),
|
||||
category_id: Number(row.category_id),
|
||||
version_id: Number(row.version_id),
|
||||
version_no: Number(row.version_no),
|
||||
config_json: row.config_json,
|
||||
}
|
||||
: null
|
||||
}
|
||||
|
||||
export async function updateWorkOrderServiceProfile(input: {
|
||||
workOrderId: number | string
|
||||
profileId: number | null
|
||||
versionId: number | null
|
||||
snapshot: Record<string, unknown> | null
|
||||
now: string
|
||||
client?: Pick<PoolClient, 'query'>
|
||||
}): Promise<void> {
|
||||
const db = input.client || { query }
|
||||
await db.query(
|
||||
`
|
||||
UPDATE work_orders
|
||||
SET service_profile_id = $2,
|
||||
service_profile_version_id = $3,
|
||||
service_profile_snapshot_json = $4::jsonb,
|
||||
updated_at = $5
|
||||
WHERE id = $1
|
||||
`,
|
||||
[
|
||||
Number(input.workOrderId),
|
||||
input.profileId,
|
||||
input.versionId,
|
||||
input.snapshot === null ? null : JSON.stringify(input.snapshot),
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
}
|
||||
@@ -408,6 +408,9 @@ export type WorkOrderRow = {
|
||||
user_payment_amount_updated_by?: string | null
|
||||
material_json: string | Record<string, unknown>
|
||||
requirement_json: string | Record<string, unknown>
|
||||
service_profile_id?: number | null
|
||||
service_profile_version_id?: number | null
|
||||
service_profile_snapshot_json?: string | Record<string, unknown> | null
|
||||
acceptance_json: string | Record<string, unknown>
|
||||
draft_acceptance_json: string | Record<string, unknown>
|
||||
admin_note: string
|
||||
@@ -634,5 +637,8 @@ export type CreateWorkOrderInput = {
|
||||
giftCooldownMinutes?: number
|
||||
materialJson: string
|
||||
requirementJson: string
|
||||
serviceProfileId?: number | null
|
||||
serviceProfileVersionId?: number | null
|
||||
serviceProfileSnapshotJson?: string | null
|
||||
now: string
|
||||
}
|
||||
|
||||
@@ -23,12 +23,16 @@ export async function createWorkOrder(input: CreateWorkOrderInput): Promise<Work
|
||||
deposit_threshold_amount, sharing_enabled, sharing_total_quantity,
|
||||
sharing_unit_reward, timeout_minutes, timeout_policy,
|
||||
acceptance_mode, gift_phase, gift_cooldown_hours, gift_cooldown_minutes,
|
||||
material_json, requirement_json, source_order_amount_fen, created_at, updated_at
|
||||
material_json, requirement_json,
|
||||
service_profile_id, service_profile_version_id, service_profile_snapshot_json,
|
||||
source_order_amount_fen, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
|
||||
$12, $13, $14, $15, $16, $17,
|
||||
$18, $19, $20, $21,
|
||||
$22::jsonb, $23::jsonb, $24, $25, $26
|
||||
$22::jsonb, $23::jsonb,
|
||||
$24, $25, $26::jsonb,
|
||||
$27, $28, $29
|
||||
)
|
||||
RETURNING id
|
||||
`,
|
||||
@@ -58,6 +62,9 @@ export async function createWorkOrder(input: CreateWorkOrderInput): Promise<Work
|
||||
normalizeWorkOrderGiftCooldownMinutes(input.giftCooldownMinutes),
|
||||
input.materialJson,
|
||||
input.requirementJson,
|
||||
input.serviceProfileId || null,
|
||||
input.serviceProfileVersionId || null,
|
||||
input.serviceProfileSnapshotJson || null,
|
||||
// 与 material_json 同源:统计要用的付款金额落到列上,避免每次刷新解析全表 JSON
|
||||
resolveSourceOrderAmountFen(input.materialJson),
|
||||
input.now,
|
||||
|
||||
Reference in New Issue
Block a user