保证接单注册事务一致性
This commit is contained in:
@@ -359,6 +359,8 @@ export type CreateWorkerInput = {
|
||||
workerType?: string
|
||||
inviterId?: number | null
|
||||
inviteCode: string
|
||||
/** 注册场景下与账号、钱包在同一事务内消费验证码。 */
|
||||
smsCodeId?: number
|
||||
now: string
|
||||
}
|
||||
|
||||
|
||||
@@ -351,6 +351,19 @@ export async function createWorkerUser(input: CreateWorkerInput): Promise<Worker
|
||||
)
|
||||
const workerId = Number(result.rows[0]?.id || 0)
|
||||
await ensureWorkerWalletWithClient(client, workerId, input.now)
|
||||
if (input.smsCodeId) {
|
||||
const consumed = await client.query(
|
||||
`
|
||||
UPDATE worker_sms_codes
|
||||
SET consumed_at = $2
|
||||
WHERE id = $1 AND consumed_at IS NULL
|
||||
`,
|
||||
[input.smsCodeId, input.now],
|
||||
)
|
||||
if (consumed.rowCount !== 1) {
|
||||
throw new Error('worker_sms_code_consume_conflict')
|
||||
}
|
||||
}
|
||||
return getWorkerUserByIdWithClient(client, workerId)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import {
|
||||
countWorkerWithdrawRequestsOnDay,
|
||||
countWorkOrderPendingSharingSubmissions,
|
||||
countWorkerSmsCodesOnDay,
|
||||
consumeWorkerSmsCode,
|
||||
createWorkerSmsCode,
|
||||
createWorkOrderEvent,
|
||||
createWorkerFinanceRequest,
|
||||
@@ -331,6 +330,7 @@ export async function registerWorker(payload: JsonObject = {}) {
|
||||
status: 'pending_review',
|
||||
inviterId: inviter ? Number(inviter.id) : null,
|
||||
inviteCode: await generateInviteCode(now),
|
||||
smsCodeId: latest!.id,
|
||||
now,
|
||||
})
|
||||
|
||||
@@ -341,9 +341,6 @@ export async function registerWorker(payload: JsonObject = {}) {
|
||||
})
|
||||
}
|
||||
|
||||
// 账号创建成功后再消费验证码,避免邀请码、昵称或数据库异常导致验证码提前失效。
|
||||
await consumeWorkerSmsCode(latest!.id, nowIso())
|
||||
|
||||
return {
|
||||
worker: mapWorkerUser(created),
|
||||
reviewRequired: true,
|
||||
|
||||
Reference in New Issue
Block a user