feat(tests): 添加结算相关功能的测试用例,增强代码覆盖率
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Models\SettlementBatch;
|
||||
use App\Lottery\SettlementBatchStatus;
|
||||
use App\Services\AuditLogger;
|
||||
use App\Services\LotterySettings;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* draw tick 在自动结算后,按系统设置自动审核并派彩入账。
|
||||
@@ -16,14 +17,15 @@ final class SettlementTickFinalizer
|
||||
private readonly SettlementBatchWorkflowService $workflow,
|
||||
) {}
|
||||
|
||||
/** @return array{approved: int, paid: int} */
|
||||
/** @return array{approved: int, paid: int, payout_failed: int} */
|
||||
public function finalizePendingBatches(): array
|
||||
{
|
||||
$approved = 0;
|
||||
$paid = 0;
|
||||
$payoutFailed = 0;
|
||||
|
||||
if (! (bool) LotterySettings::get('settlement.auto_approve_on_tick', true)) {
|
||||
return ['approved' => 0, 'paid' => 0];
|
||||
return ['approved' => 0, 'paid' => 0, 'payout_failed' => 0];
|
||||
}
|
||||
|
||||
$pending = SettlementBatch::query()
|
||||
@@ -58,9 +60,39 @@ final class SettlementTickFinalizer
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
$this->markAutoPayoutFailed($batch, $e);
|
||||
$payoutFailed++;
|
||||
}
|
||||
}
|
||||
|
||||
return ['approved' => $approved, 'paid' => $paid];
|
||||
return ['approved' => $approved, 'paid' => $paid, 'payout_failed' => $payoutFailed];
|
||||
}
|
||||
}
|
||||
|
||||
private function markAutoPayoutFailed(SettlementBatch $batch, \Throwable $e): void
|
||||
{
|
||||
$message = mb_substr($e->getMessage(), 0, 200);
|
||||
|
||||
DB::transaction(function () use ($batch, $message): void {
|
||||
$locked = SettlementBatch::query()->whereKey($batch->id)->lockForUpdate()->first();
|
||||
if ($locked === null || $locked->status !== SettlementBatchStatus::Approved->value) {
|
||||
return;
|
||||
}
|
||||
|
||||
$locked->forceFill([
|
||||
'status' => SettlementBatchStatus::Failed->value,
|
||||
'review_remark' => 'auto_payout_failed: '.$message,
|
||||
])->save();
|
||||
|
||||
AuditLogger::recordForSystem(
|
||||
moduleCode: 'settlement',
|
||||
actionCode: 'auto_payout_failed',
|
||||
targetType: 'settlement_batch',
|
||||
targetId: (string) $locked->id,
|
||||
afterJson: [
|
||||
'draw_id' => (int) $locked->draw_id,
|
||||
'error' => $message,
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user