|
|
|
|
@ -83,15 +83,38 @@ final class CloudInvoiceDetailPageService
|
|
|
|
|
->orderByDesc('updated_at')->orderByDesc('id')
|
|
|
|
|
->sharedLock()
|
|
|
|
|
->first();
|
|
|
|
|
$jobQuery = CloudJobService::blockingQueryForInvoice($invoiceId)
|
|
|
|
|
->where('user_id', $userId);
|
|
|
|
|
if ($payment !== null) {
|
|
|
|
|
$jobQuery->where('request_idempotency_key',
|
|
|
|
|
(string) $payment->idempotency_key);
|
|
|
|
|
$jobFields = ['id', 'user_id', 'invoice_id', 'kind',
|
|
|
|
|
'request_idempotency_key', 'phase', 'outcome',
|
|
|
|
|
'effect_resolution', 'safe_to_resubmit', 'updated_at'];
|
|
|
|
|
$latestBlockingJob = CloudJobService::blockingQueryForInvoice($invoiceId)
|
|
|
|
|
->where('user_id', $userId)->orderByDesc('id')->sharedLock()
|
|
|
|
|
->first($jobFields);
|
|
|
|
|
$paymentJob = $payment === null ? null
|
|
|
|
|
: \App\Models\CloudJob::where('invoice_id', $invoiceId)
|
|
|
|
|
->where('user_id', $userId)->where('kind', 'payment_create')
|
|
|
|
|
->where('request_idempotency_key',
|
|
|
|
|
(string) $payment->idempotency_key)
|
|
|
|
|
->orderByDesc('id')->sharedLock()->first($jobFields);
|
|
|
|
|
$paymentBlockingJob = $payment === null ? null
|
|
|
|
|
: CloudJobService::blockingQueryForInvoice($invoiceId)
|
|
|
|
|
->where('user_id', $userId)
|
|
|
|
|
->where('request_idempotency_key',
|
|
|
|
|
(string) $payment->idempotency_key)
|
|
|
|
|
->orderByDesc('id')->sharedLock()->first($jobFields);
|
|
|
|
|
$jobIsNewer = $latestBlockingJob !== null && (
|
|
|
|
|
$payment === null
|
|
|
|
|
|| ($paymentJob !== null
|
|
|
|
|
&& (int) $latestBlockingJob->id > (int) $paymentJob->id)
|
|
|
|
|
|| ($paymentJob === null
|
|
|
|
|
&& (int) $latestBlockingJob->updated_at
|
|
|
|
|
> (int) $payment->updated_at)
|
|
|
|
|
);
|
|
|
|
|
if ($jobIsNewer) {
|
|
|
|
|
$payment = null;
|
|
|
|
|
$job = $latestBlockingJob;
|
|
|
|
|
} else {
|
|
|
|
|
$job = $paymentBlockingJob;
|
|
|
|
|
}
|
|
|
|
|
$job = $jobQuery->orderByDesc('id')->sharedLock()
|
|
|
|
|
->first(['id', 'user_id', 'invoice_id', 'kind', 'phase',
|
|
|
|
|
'outcome', 'effect_resolution', 'safe_to_resubmit']);
|
|
|
|
|
if ($job === null && $payment === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|