11import 'server-only' ;
22
3- import { and , eq , sql } from 'drizzle-orm' ;
3+ import { and , eq , inArray , sql } from 'drizzle-orm' ;
44
55import { db } from '@/db' ;
66import { orderItems , orders , paymentAttempts } from '@/db/schema' ;
@@ -49,7 +49,7 @@ async function getActiveAttempt(
4949 and (
5050 eq ( paymentAttempts . orderId , orderId ) ,
5151 eq ( paymentAttempts . provider , 'monobank' ) ,
52- sql ` ${ paymentAttempts . status } in ( 'creating','active')`
52+ inArray ( paymentAttempts . status , [ 'creating' , 'active' ] )
5353 )
5454 )
5555 . limit ( 1 ) ;
@@ -230,7 +230,7 @@ async function cancelOrderAndRelease(orderId: string, reason: string) {
230230 and (
231231 eq ( orders . id , orderId ) ,
232232 eq ( orders . paymentProvider , 'monobank' ) ,
233- sql ` ${ orders . paymentStatus } in ( 'pending','requires_payment')`
233+ inArray ( orders . paymentStatus , [ 'pending' , 'requires_payment' ] )
234234 )
235235 )
236236 . returning ( { id : orders . id } ) ;
@@ -458,6 +458,7 @@ async function createMonoAttemptAndInvoiceImpl(
458458 }
459459) : Promise < {
460460 attemptId : string ;
461+ attemptNumber : number ;
461462 invoiceId : string ;
462463 pageUrl : string ;
463464 currency : 'UAH' ;
@@ -473,6 +474,7 @@ async function createMonoAttemptAndInvoiceImpl(
473474 invoiceId : existing . providerPaymentIntentId ,
474475 pageUrl,
475476 attemptId : existing . id ,
477+ attemptNumber : existing . attemptNumber ,
476478 currency : MONO_CURRENCY ,
477479 totalAmountMinor : snapshot . amountMinor ,
478480 } ;
@@ -535,6 +537,7 @@ async function createMonoAttemptAndInvoiceImpl(
535537 invoiceId : reused . providerPaymentIntentId ,
536538 pageUrl,
537539 attemptId : reused . id ,
540+ attemptNumber : reused . attemptNumber ,
538541 currency : MONO_CURRENCY ,
539542 totalAmountMinor : snapshot . amountMinor ,
540543 } ;
@@ -579,10 +582,18 @@ async function createMonoAttemptAndInvoiceImpl(
579582 } ) ;
580583 }
581584
582- await deps . cancelOrderAndRelease (
583- args . orderId ,
584- 'Monobank snapshot validation failed.'
585- ) ;
585+ try {
586+ await deps . cancelOrderAndRelease (
587+ args . orderId ,
588+ 'Monobank snapshot validation failed.'
589+ ) ;
590+ } catch ( cancelErr ) {
591+ logError ( 'monobank_cancel_order_failed' , cancelErr , {
592+ orderId : args . orderId ,
593+ attemptId : attempt . id ,
594+ requestId : args . requestId ,
595+ } ) ;
596+ }
586597
587598 throw error ;
588599 }
@@ -673,6 +684,7 @@ async function createMonoAttemptAndInvoiceImpl(
673684 invoiceId : invoice . invoiceId ,
674685 pageUrl : invoice . pageUrl ,
675686 attemptId : attempt . id ,
687+ attemptNumber : attempt . attemptNumber ,
676688 currency : MONO_CURRENCY ,
677689 totalAmountMinor : snapshot . amountMinor ,
678690 } ;
@@ -686,6 +698,7 @@ export async function createMonoAttemptAndInvoice(args: {
686698 maxAttempts ?: number ;
687699} ) : Promise < {
688700 attemptId : string ;
701+ attemptNumber : number ;
689702 invoiceId : string ;
690703 pageUrl : string ;
691704 currency : 'UAH' ;
@@ -738,14 +751,5 @@ export async function createMonobankAttemptAndInvoice(args: {
738751 maxAttempts : args . maxAttempts ,
739752 } ) ;
740753
741- const [ row ] = await db
742- . select ( { attemptNumber : paymentAttempts . attemptNumber } )
743- . from ( paymentAttempts )
744- . where ( eq ( paymentAttempts . id , result . attemptId ) )
745- . limit ( 1 ) ;
746-
747- return {
748- ...result ,
749- attemptNumber : row ?. attemptNumber ?? 1 ,
750- } ;
754+ return result ;
751755}
0 commit comments