@@ -2,9 +2,13 @@ import {
22 createPaymentSessionsWorkflow ,
33 createPaymentSessionsWorkflowId ,
44} from "@medusajs/core-flows"
5- import { IPaymentModuleService , IRegionModuleService } from "@medusajs/types"
6- import { Modules } from "@medusajs/utils"
75import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
6+ import {
7+ ICustomerModuleService ,
8+ IPaymentModuleService ,
9+ IRegionModuleService ,
10+ } from "@medusajs/types"
11+ import { ContainerRegistrationKeys , Modules } from "@medusajs/utils"
812
913jest . setTimeout ( 50000 )
1014
@@ -17,18 +21,21 @@ medusaIntegrationTestRunner({
1721 let appContainer
1822 let paymentModule : IPaymentModuleService
1923 let regionModule : IRegionModuleService
20- let remoteLink
24+ let customerModule : ICustomerModuleService
25+ let query
2126
2227 beforeAll ( async ( ) => {
2328 appContainer = getContainer ( )
2429 paymentModule = appContainer . resolve ( Modules . PAYMENT )
2530 regionModule = appContainer . resolve ( Modules . REGION )
26- remoteLink = appContainer . resolve ( "remoteLink" )
31+ customerModule = appContainer . resolve ( Modules . CUSTOMER )
32+ query = appContainer . resolve ( ContainerRegistrationKeys . QUERY )
2733 } )
2834
2935 describe ( "createPaymentSessionWorkflow" , ( ) => {
3036 let region
3137 let paymentCollection
38+ let customer
3239
3340 beforeEach ( async ( ) => {
3441 region = await regionModule . createRegions ( {
@@ -40,6 +47,12 @@ medusaIntegrationTestRunner({
4047 currency_code : "usd" ,
4148 amount : 1000 ,
4249 } )
50+
51+ customer = await customerModule . createCustomers ( {
52+ email : "test@test.com" ,
53+ first_name : "Test" ,
54+ last_name : "Test" ,
55+ } )
4356 } )
4457
4558 it ( "should create payment sessions" , async ( ) => {
@@ -75,6 +88,47 @@ medusaIntegrationTestRunner({
7588 )
7689 } )
7790
91+ it ( "should create payment sessions with customer" , async ( ) => {
92+ await createPaymentSessionsWorkflow ( appContainer ) . run ( {
93+ input : {
94+ payment_collection_id : paymentCollection . id ,
95+ provider_id : "pp_system_default" ,
96+ customer_id : customer . id ,
97+ } ,
98+ } )
99+
100+ const {
101+ data : [ updatedPaymentCollection ] ,
102+ } = await query . graph ( {
103+ entity : "payment_collection" ,
104+ filters : {
105+ id : paymentCollection . id ,
106+ } ,
107+ fields : [ "id" , "currency_code" , "amount" , "payment_sessions.*" ] ,
108+ } )
109+
110+ expect ( updatedPaymentCollection . payment_sessions ) . toHaveLength ( 1 )
111+ expect ( updatedPaymentCollection ) . toEqual (
112+ expect . objectContaining ( {
113+ id : paymentCollection . id ,
114+ currency_code : "usd" ,
115+ amount : 1000 ,
116+ payment_sessions : expect . arrayContaining ( [
117+ expect . objectContaining ( {
118+ context : expect . objectContaining ( {
119+ customer : expect . objectContaining ( {
120+ id : customer . id ,
121+ } ) ,
122+ account_holder : expect . objectContaining ( {
123+ email : customer . email ,
124+ } ) ,
125+ } ) ,
126+ } ) ,
127+ ] ) ,
128+ } )
129+ )
130+ } )
131+
78132 it ( "should delete existing sessions when create payment sessions" , async ( ) => {
79133 await createPaymentSessionsWorkflow ( appContainer ) . run ( {
80134 input : {
@@ -164,6 +218,89 @@ medusaIntegrationTestRunner({
164218
165219 expect ( sessions ) . toHaveLength ( 0 )
166220 } )
221+
222+ it . only ( "should not delete account holder if it exists before creating payment sessions" , async ( ) => {
223+ await createPaymentSessionsWorkflow ( appContainer ) . run ( {
224+ input : {
225+ payment_collection_id : paymentCollection . id ,
226+ provider_id : "pp_system_default" ,
227+ customer_id : customer . id ,
228+ } ,
229+ } )
230+
231+ const {
232+ data : [ updatedCustomer1 ] ,
233+ } = await query . graph ( {
234+ entity : "customer" ,
235+ filters : {
236+ id : customer . id ,
237+ } ,
238+ fields : [ "id" , "account_holders.*" ] ,
239+ } )
240+
241+ expect ( updatedCustomer1 . account_holders ) . toEqual (
242+ expect . arrayContaining ( [
243+ expect . objectContaining ( {
244+ email : customer . email ,
245+ } ) ,
246+ ] )
247+ )
248+
249+ const newPaymentCollection =
250+ await paymentModule . createPaymentCollections ( {
251+ currency_code : "usd" ,
252+ amount : 2000 ,
253+ } )
254+
255+ const workflow = createPaymentSessionsWorkflow ( appContainer )
256+
257+ workflow . appendAction ( "throw" , createPaymentSessionsWorkflowId , {
258+ invoke : async function failStep ( ) {
259+ throw new Error (
260+ `Failed to do something after creating payment sessions`
261+ )
262+ } ,
263+ } )
264+
265+ const { errors } = await workflow . run ( {
266+ input : {
267+ payment_collection_id : newPaymentCollection . id ,
268+ provider_id : "pp_system_default" ,
269+ customer_id : customer . id ,
270+ context : { } ,
271+ data : { } ,
272+ } ,
273+ throwOnError : false ,
274+ } )
275+
276+ expect ( errors ) . toEqual ( [
277+ {
278+ action : "throw" ,
279+ handlerType : "invoke" ,
280+ error : expect . objectContaining ( {
281+ message : `Failed to do something after creating payment sessions` ,
282+ } ) ,
283+ } ,
284+ ] )
285+
286+ const {
287+ data : [ updatedCustomer2 ] ,
288+ } = await query . graph ( {
289+ entity : "customer" ,
290+ filters : {
291+ id : customer . id ,
292+ } ,
293+ fields : [ "id" , "account_holders.*" ] ,
294+ } )
295+
296+ expect ( updatedCustomer2 . account_holders ) . toEqual (
297+ expect . arrayContaining ( [
298+ expect . objectContaining ( {
299+ email : customer . email ,
300+ } ) ,
301+ ] )
302+ )
303+ } )
167304 } )
168305 } )
169306 } )
0 commit comments