优化
This commit is contained in:
@@ -38,7 +38,46 @@ async function waitForDimensionInitialization(context, tenantId) {
|
||||
if (status !== 'READY') throw new Error(`客户默认尺寸初始化未就绪: ${status}`)
|
||||
}
|
||||
|
||||
export async function run(context, { publishEnvironment = true } = {}) {
|
||||
export function normalizeFixedCustomerId(value) {
|
||||
const text = String(value || '').trim().replace(/^TEN/, '')
|
||||
if (!/^[1-9]\d{0,19}$/.test(text)) throw new Error('固定客户编号必须为 TEN 编号或数字 ID')
|
||||
return text
|
||||
}
|
||||
|
||||
async function reuseFixedCustomer(context, publishEnvironment, requireDistinctCustomer) {
|
||||
if (requireDistinctCustomer) throw new Error('固定客户模式未配置第二个客户,跨客户场景阻断;禁止自动建客或用同一客户替代隔离验证')
|
||||
const tenantId = normalizeFixedCustomerId(context.options.fixedCustomerId)
|
||||
const username = context.options.merchantUsername || process.env.MERCHANT_TEST_USERNAME
|
||||
const password = context.options.merchantPassword || process.env.MERCHANT_TEST_PASSWORD
|
||||
if (!username || !password) throw new Error('请配置固定客户的 MERCHANT_TEST_USERNAME / MERCHANT_TEST_PASSWORD;不会新建客户或重置密码')
|
||||
const loginResponse = await context.request('POST', '/api/auth/login', {loginId:username,password}, '')
|
||||
const login = context.apiSuccess(MODULE, '登录已配置的固定客户账号', loginResponse)
|
||||
if (!login?.accessToken) throw new Error('固定客户账号登录失败')
|
||||
let profile
|
||||
try {
|
||||
const response = await context.request('GET', '/api/merchant/v1/iam/tenant/profile', undefined, login.accessToken)
|
||||
profile = context.apiSuccess(MODULE, '核对固定客户账号实际归属和状态', response)
|
||||
const valid = id(profile?.id) === tenantId && profile?.status === 'ACTIVE'
|
||||
context.assert(MODULE, '固定客户账号属于指定已审核客户', response, valid,
|
||||
`expected=${tenantId}, actual=${id(profile?.id)}, status=${profile?.status}`, {deploymentBlocker:true})
|
||||
if (!valid) throw new Error('固定客户账号归属或状态不匹配,停止造数;不会新建客户')
|
||||
await waitForDimensionInitialization(context, tenantId)
|
||||
} finally {
|
||||
await context.request('POST', '/api/auth/logout', {refreshToken:login.refreshToken}, login.accessToken)
|
||||
}
|
||||
if (publishEnvironment) {
|
||||
process.env.MERCHANT_TEST_USERNAME = username
|
||||
process.env.MERCHANT_TEST_PASSWORD = password
|
||||
}
|
||||
const fixture = {tenantId,tenantName:profile.tenantName,username,marker:context.marker,reused:true}
|
||||
context.context.merchantQualificationFixture = {...fixture,password}
|
||||
context.context.customerId = tenantId
|
||||
context.createdResources.merchantQualificationFixture = fixture
|
||||
}
|
||||
|
||||
export async function run(context, { publishEnvironment = true, requireDistinctCustomer = !publishEnvironment } = {}) {
|
||||
if (context.options?.fixedCustomerId) return reuseFixedCustomer(context, publishEnvironment, requireDistinctCustomer)
|
||||
if (context.options?.environment === 'test') throw new Error('TEST 必须配置固定客户,禁止自动建客')
|
||||
const suffix = context.marker.replace(/[^0-9A-Z]/gi, '').slice(-10).padStart(10, '0')
|
||||
const customerName = String(context.options?.customerName || '').trim()
|
||||
const tenantName = customerName || `全量验收下单客户-${suffix}`
|
||||
|
||||
Reference in New Issue
Block a user