88 lines
5.1 KiB
JavaScript
88 lines
5.1 KiB
JavaScript
const MODULE = 'merchant-qualification-fixture'
|
|
const REQUIRED_TYPES = new Set(['BUSINESS_LICENSE', 'LEGAL_PERSON_ID'])
|
|
const QUALIFICATION_IMAGE = Uint8Array.from(Buffer.from(
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
|
|
'base64',
|
|
))
|
|
|
|
function id(value) {
|
|
return value === undefined || value === null ? '' : String(value)
|
|
}
|
|
|
|
export async function run(context) {
|
|
const suffix = context.marker.replace(/[^0-9A-Z]/gi, '').slice(-10).padStart(10, '0')
|
|
const phone = await context.findAvailablePhone(MODULE)
|
|
const createResponse = await context.request('POST', '/api/platform/v1/iam/tenants', {
|
|
tenantName: `全量验收下单客户-${suffix}`,
|
|
legalName: `全量验收下单有限公司-${suffix}`,
|
|
legalRepresentative: '全量验收法人',
|
|
contactName: '全量验收管理员',
|
|
contactPhone: phone,
|
|
contactEmail: `merchant-fixture-${suffix.toLowerCase()}@example.com`,
|
|
servicePackage: '全量验收',
|
|
salesOwnerName: '全量验收',
|
|
paymentTermDays: 30,
|
|
enterpriseType: 'LIMITED_COMPANY',
|
|
establishedDate: '2020-01-02',
|
|
registeredCapital: 1000000,
|
|
registeredCapitalCurrency: 'CNY',
|
|
registeredAddress: '深圳市全量验收地址',
|
|
internalRemark: `${context.marker}-MERCHANT-QUALIFICATION`,
|
|
})
|
|
const created = context.apiSuccess(MODULE, '创建隔离下单客户', createResponse)
|
|
const customer = created?.tenant || created?.customer || created
|
|
const tenantId = id(customer?.id || created?.tenantId)
|
|
const username = created?.administratorUsername
|
|
const password = created?.temporaryPassword
|
|
context.assert(MODULE, '下单客户和一次性管理员凭据完整', createResponse,
|
|
Boolean(tenantId && username && password),
|
|
`tenantId=${tenantId || 'missing'}, username=${username || 'missing'}, passwordGenerated=${Boolean(password)}`,
|
|
{ deploymentBlocker: true })
|
|
if (!tenantId || !username || !password) throw new Error('创建下单客户未返回完整管理员凭据')
|
|
|
|
const packageForm = new FormData()
|
|
packageForm.append('businessLicense', new Blob([QUALIFICATION_IMAGE], { type: 'image/png' }), `business-${suffix}.png`)
|
|
packageForm.append('legalPersonId', new Blob([QUALIFICATION_IMAGE], { type: 'image/png' }), `legal-person-${suffix}.png`)
|
|
packageForm.append('unifiedSocialCreditCode', `91440300${suffix}`)
|
|
const packageResponse = await context.requestWithBody('POST',
|
|
`/api/platform/v1/files/qualifications/${encodeURIComponent(tenantId)}/required-package`, packageForm)
|
|
context.apiSuccess(MODULE, '通过 File 上传真实必填资质包', packageResponse,
|
|
`tenantId=${tenantId}`, { deploymentBlocker: true })
|
|
|
|
const submitResponse = await context.request('POST',
|
|
`/api/platform/v1/iam/tenants/${encodeURIComponent(tenantId)}/submit-review`)
|
|
const submitted = context.apiSuccess(MODULE, '提交企业资质审核', submitResponse,
|
|
`tenantId=${tenantId}`, { deploymentBlocker: true })
|
|
context.assert(MODULE, '企业进入待审核状态', submitResponse, submitted?.status === 'PENDING',
|
|
`status=${submitted?.status ?? 'missing'}`, { deploymentBlocker: true })
|
|
|
|
const reviewsResponse = await context.request('GET', '/api/platform/v1/iam/reviews')
|
|
const reviews = context.apiSuccess(MODULE, '查询待审核企业', reviewsResponse, '', { deploymentBlocker: true })
|
|
const review = Array.isArray(reviews) ? reviews.find((item) => id(item.tenantId) === tenantId) : null
|
|
context.assert(MODULE, '待审核列表包含隔离下单客户', reviewsResponse, Boolean(review?.id),
|
|
`tenantId=${tenantId}, reviewId=${id(review?.id) || 'missing'}`, { deploymentBlocker: true })
|
|
if (!review?.id) throw new Error('未找到隔离下单客户的待审核单')
|
|
|
|
const approveResponse = await context.request('POST',
|
|
`/api/platform/v1/iam/reviews/${encodeURIComponent(review.id)}/approve`,
|
|
{ comment: `${context.marker} 全量验收正式资质审核通过` })
|
|
context.apiSuccess(MODULE, '平台审核通过企业资质', approveResponse,
|
|
`tenantId=${tenantId}`, { deploymentBlocker: true })
|
|
|
|
const qualificationsResponse = await context.request('GET',
|
|
`/api/platform/v1/iam/tenants/${encodeURIComponent(tenantId)}/qualifications`)
|
|
const qualifications = context.apiSuccess(MODULE, '回查企业资质审核结果', qualificationsResponse)
|
|
const approvedTypes = new Set((Array.isArray(qualifications) ? qualifications : [])
|
|
.filter((item) => item.status === 'APPROVED')
|
|
.map((item) => item.qualificationType))
|
|
const eligible = [...REQUIRED_TYPES].every((type) => approvedTypes.has(type))
|
|
context.assert(MODULE, '全部必填企业资质已审核通过', qualificationsResponse, eligible,
|
|
`approved=${[...approvedTypes].join(',') || 'missing'}`, { deploymentBlocker: true })
|
|
if (!eligible) throw new Error('隔离下单客户必填资质未全部审核通过')
|
|
|
|
process.env.MERCHANT_TEST_USERNAME = username
|
|
process.env.MERCHANT_TEST_PASSWORD = password
|
|
context.context.merchantQualificationFixture = { tenantId, username, marker: context.marker }
|
|
context.createdResources.merchantQualificationFixture = { tenantId, username, marker: context.marker }
|
|
}
|