统一前后端代码格式化配置
This commit is contained in:
@@ -23,7 +23,8 @@ export function getOpen91Config(overrides: Partial<Open91RuntimeConfig> = {}) {
|
||||
shopId: String(config.shopId || OPEN_91_PROVIDER).trim() || OPEN_91_PROVIDER,
|
||||
shopName: String(config.shopName || '91卡券').trim() || '91卡券',
|
||||
timestampToleranceSeconds: Number(config.timestampToleranceSeconds || 600) || 600,
|
||||
cardsEncoding: String(config.cardsEncoding || 'aes-256-ecb-base64').trim() || 'aes-256-ecb-base64',
|
||||
cardsEncoding:
|
||||
String(config.cardsEncoding || 'aes-256-ecb-base64').trim() || 'aes-256-ecb-base64',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,11 @@ import test from 'node:test'
|
||||
import { resolveIndustryVoucherForTask } from './consume-on-deliver.js'
|
||||
import type { KuaishouIndustryVoucherRow } from '../../types/repository/rows.js'
|
||||
|
||||
function voucher(partial: Partial<KuaishouIndustryVoucherRow> & {
|
||||
voucher_code: string
|
||||
}): KuaishouIndustryVoucherRow {
|
||||
function voucher(
|
||||
partial: Partial<KuaishouIndustryVoucherRow> & {
|
||||
voucher_code: string
|
||||
},
|
||||
): KuaishouIndustryVoucherRow {
|
||||
return {
|
||||
id: partial.id || 1,
|
||||
oid: partial.oid || 'OID-1',
|
||||
@@ -43,18 +45,14 @@ test('resolveIndustryVoucherForTask prefers task_id over unit_index', () => {
|
||||
})
|
||||
|
||||
test('resolveIndustryVoucherForTask falls back to unit_index when task_id missing', () => {
|
||||
const vouchers = [
|
||||
voucher({ id: 1, voucher_code: 'A', task_id: null, unit_index: 2 }),
|
||||
]
|
||||
const vouchers = [voucher({ id: 1, voucher_code: 'A', task_id: null, unit_index: 2 })]
|
||||
|
||||
const matched = resolveIndustryVoucherForTask({ id: 99, unit_index: 2 }, vouchers)
|
||||
assert.equal(matched?.voucher_code, 'A')
|
||||
})
|
||||
|
||||
test('resolveIndustryVoucherForTask returns null when no match', () => {
|
||||
const vouchers = [
|
||||
voucher({ id: 1, voucher_code: 'A', task_id: 1, unit_index: 1 }),
|
||||
]
|
||||
const vouchers = [voucher({ id: 1, voucher_code: 'A', task_id: 1, unit_index: 1 })]
|
||||
|
||||
assert.equal(resolveIndustryVoucherForTask({ id: 2, unit_index: 3 }, vouchers), null)
|
||||
})
|
||||
|
||||
@@ -8,22 +8,14 @@
|
||||
* 交付后会 best-effort 自动准备 Cloud 绑定资源(取号/绑链),避免
|
||||
* 「已核销却停在待准备资源」只能靠后台手动点「准备资源」。
|
||||
*/
|
||||
import {
|
||||
attachKuaishouIndustryVoucherToTask,
|
||||
} from '../platforms/kuaishou-industry/voucher-binding-service.js'
|
||||
import {
|
||||
consumeKuaishouIndustryVoucher,
|
||||
} from '../platforms/kuaishou-industry/voucher-service.js'
|
||||
import { attachKuaishouIndustryVoucherToTask } from '../platforms/kuaishou-industry/voucher-binding-service.js'
|
||||
import { consumeKuaishouIndustryVoucher } from '../platforms/kuaishou-industry/voucher-service.js'
|
||||
import { prepareFulfillmentBinding } from '../fulfillment/executors/registry.js'
|
||||
import { normalizeKuaishouCloudFlow } from '../fulfillment/kuaishou-cloud/index.js'
|
||||
import { listKuaishouIndustryVouchersByOid } from '../../repositories/kuaishou-industry-voucher-repo.js'
|
||||
import { logIntegration, logWarn } from '../../utils/logger.js'
|
||||
import { parseTaskContext } from '../../utils/task-json.js'
|
||||
import type {
|
||||
KuaishouIndustryVoucherRow,
|
||||
OrderRow,
|
||||
TaskRow,
|
||||
} from '../../types/repository/rows.js'
|
||||
import type { KuaishouIndustryVoucherRow, OrderRow, TaskRow } from '../../types/repository/rows.js'
|
||||
|
||||
export const OPEN_91_DELIVER_CONSUME_SOURCE = 'open91_query_deliver'
|
||||
|
||||
@@ -69,7 +61,11 @@ export function resolveIndustryVoucherForTask(
|
||||
}
|
||||
|
||||
function isVoucherConsumed(voucher: Pick<KuaishouIndustryVoucherRow, 'status'>): boolean {
|
||||
return String(voucher.status || '').trim().toUpperCase() === 'CONSUMED'
|
||||
return (
|
||||
String(voucher.status || '')
|
||||
.trim()
|
||||
.toUpperCase() === 'CONSUMED'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,8 +79,9 @@ export async function consumeIndustryVouchersBeforeOpen91Deliver(options: {
|
||||
requestId?: string
|
||||
source?: string
|
||||
}): Promise<Open91DeliverConsumeResult> {
|
||||
const source = String(options.source || OPEN_91_DELIVER_CONSUME_SOURCE).trim()
|
||||
|| OPEN_91_DELIVER_CONSUME_SOURCE
|
||||
const source =
|
||||
String(options.source || OPEN_91_DELIVER_CONSUME_SOURCE).trim() ||
|
||||
OPEN_91_DELIVER_CONSUME_SOURCE
|
||||
const readySet = new Set(
|
||||
(Array.isArray(options.readyTaskIds) ? options.readyTaskIds : [])
|
||||
.map((id) => Number(id))
|
||||
@@ -213,9 +210,7 @@ async function ensureBindingPreparedAfterDeliver(options: {
|
||||
return
|
||||
}
|
||||
|
||||
const flow = normalizeKuaishouCloudFlow(
|
||||
parseTaskContext(options.task).kuaishouCloudFulfillment,
|
||||
)
|
||||
const flow = normalizeKuaishouCloudFlow(parseTaskContext(options.task).kuaishouCloudFulfillment)
|
||||
if (flow.binding.prepareStatus === 'ready' && String(flow.binding.bindUrl || '').trim()) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { listKuaishouIndustryVouchersByOid } from '../../repositories/kuaishou-i
|
||||
import { listTasksByOrderId } from '../../repositories/task-repo.js'
|
||||
import { resolveTaskDeliveryLink } from '../fulfillment/delivery-link-service.js'
|
||||
import { logIntegration } from '../../utils/logger.js'
|
||||
import { asJsonObject, type JsonObject } from '../../types/json.js'
|
||||
import { asJsonObject, type JsonObject } from '../../types/json.js'
|
||||
import {
|
||||
OPEN_91_MANUAL_FAILED_STATUS,
|
||||
OPEN_91_PENDING_CONFIG_STATUS,
|
||||
@@ -114,10 +114,12 @@ export async function queryOpen91Order(payload: JsonObject = {}, { requestId = '
|
||||
}
|
||||
|
||||
readyTaskIds.push(Number(task.id))
|
||||
cardItems.push(buildOpen91CardItem({
|
||||
claimUrl,
|
||||
expireTime: deliveryLink?.expireTime || '',
|
||||
}))
|
||||
cardItems.push(
|
||||
buildOpen91CardItem({
|
||||
claimUrl,
|
||||
expireTime: deliveryLink?.expireTime || '',
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const queryState = resolveOpen91QueryState({
|
||||
|
||||
@@ -64,7 +64,10 @@ export function assertOpen91QueryPayload(payload: JsonObject, config = assertOpe
|
||||
assertOpen91CommonPayload(payload, config)
|
||||
}
|
||||
|
||||
export function assertOpen91Timestamp(timestamp: unknown, toleranceSeconds = assertOpen91Config().timestampToleranceSeconds) {
|
||||
export function assertOpen91Timestamp(
|
||||
timestamp: unknown,
|
||||
toleranceSeconds = assertOpen91Config().timestampToleranceSeconds,
|
||||
) {
|
||||
const nowSeconds = Math.floor(Date.now() / 1000)
|
||||
if (Math.abs(nowSeconds - Number(timestamp)) > Math.max(1, Number(toleranceSeconds) || 600)) {
|
||||
throw createHttpError('timestamp 已过期', {
|
||||
|
||||
@@ -7,11 +7,7 @@ import {
|
||||
signOpen91Payload,
|
||||
verifyOpen91Signature,
|
||||
} from './crypto.js'
|
||||
import {
|
||||
buildOpen91CardItem,
|
||||
formatOpen91ExpireTime,
|
||||
resolveOpen91QueryState,
|
||||
} from './response.js'
|
||||
import { buildOpen91CardItem, formatOpen91ExpireTime, resolveOpen91QueryState } from './response.js'
|
||||
|
||||
test('signOpen91Payload signs only request body fields', () => {
|
||||
const config = {
|
||||
@@ -77,7 +73,10 @@ test('encryptOpen91Cards matches documented AES ECB base64 sample', () => {
|
||||
test('resolveOpen91QueryState returns success only when every task is ready', () => {
|
||||
assert.deepEqual(
|
||||
resolveOpen91QueryState({
|
||||
tasks: [{ id: 1, task_status: 'pending_binding_prepare' }, { id: 2, task_status: 'waiting_binding' }],
|
||||
tasks: [
|
||||
{ id: 1, task_status: 'pending_binding_prepare' },
|
||||
{ id: 2, task_status: 'waiting_binding' },
|
||||
],
|
||||
readyTaskIds: [1, 2],
|
||||
}),
|
||||
{
|
||||
@@ -89,7 +88,10 @@ test('resolveOpen91QueryState returns success only when every task is ready', ()
|
||||
|
||||
assert.deepEqual(
|
||||
resolveOpen91QueryState({
|
||||
tasks: [{ id: 1, task_status: 'pending_binding_prepare' }, { id: 2, task_status: 'waiting_binding' }],
|
||||
tasks: [
|
||||
{ id: 1, task_status: 'pending_binding_prepare' },
|
||||
{ id: 2, task_status: 'waiting_binding' },
|
||||
],
|
||||
readyTaskIds: [1],
|
||||
}),
|
||||
{
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import type { JsonObject } from '../../types/json.js'
|
||||
import {
|
||||
OPEN_91_DEFAULT_FAIL_CODE,
|
||||
OPEN_91_SUCCESS_MESSAGE,
|
||||
} from './config.js'
|
||||
import { OPEN_91_DEFAULT_FAIL_CODE, OPEN_91_SUCCESS_MESSAGE } from './config.js'
|
||||
import { isOpen91FailedTaskStatus } from '../../domain/task-status.js'
|
||||
import { normalizeOpen91String } from './payload.js'
|
||||
|
||||
@@ -32,7 +29,10 @@ export function buildOpen91OrderCost(value = 0) {
|
||||
return Number.isFinite(amount) ? amount.toFixed(4) : '0.0000'
|
||||
}
|
||||
|
||||
export function buildOpen91CardItem({ claimUrl = '', expireTime = '' }: { claimUrl?: unknown, expireTime?: unknown } = {}) {
|
||||
export function buildOpen91CardItem({
|
||||
claimUrl = '',
|
||||
expireTime = '',
|
||||
}: { claimUrl?: unknown; expireTime?: unknown } = {}) {
|
||||
const normalizedClaimUrl = normalizeOpen91String(claimUrl)
|
||||
return {
|
||||
cardNo: normalizedClaimUrl,
|
||||
@@ -72,11 +72,14 @@ export function formatOpen91ExpireTime(value: unknown = '') {
|
||||
return `${byType.year}-${byType.month}-${byType.day} ${byType.hour}:${byType.minute}:${byType.second}`
|
||||
}
|
||||
|
||||
export function resolveOpen91QueryState(
|
||||
{ tasks = [], readyTaskIds = [] }: { tasks?: JsonObject[], readyTaskIds?: unknown[] } = {},
|
||||
) {
|
||||
export function resolveOpen91QueryState({
|
||||
tasks = [],
|
||||
readyTaskIds = [],
|
||||
}: { tasks?: JsonObject[]; readyTaskIds?: unknown[] } = {}) {
|
||||
const normalizedTasks = Array.isArray(tasks) ? tasks : []
|
||||
const readyTaskIdSet = new Set((Array.isArray(readyTaskIds) ? readyTaskIds : []).map((item) => Number(item)))
|
||||
const readyTaskIdSet = new Set(
|
||||
(Array.isArray(readyTaskIds) ? readyTaskIds : []).map((item) => Number(item)),
|
||||
)
|
||||
|
||||
if (normalizedTasks.length === 0) {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user