@@ -9,10 +9,15 @@ import {
99 PromotionStatus ,
1010 PromotionType ,
1111} from "@medusajs/utils"
12- import { createAdminUser , generatePublishableKey , generateStoreHeaders , } from "../../../../helpers/create-admin-user"
12+ import {
13+ createAdminUser ,
14+ generatePublishableKey ,
15+ generateStoreHeaders ,
16+ } from "../../../../helpers/create-admin-user"
1317import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
1418import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer"
1519import { medusaTshirtProduct } from "../../../__fixtures__/product"
20+ import { setTimeout } from "timers/promises"
1621
1722jest . setTimeout ( 100000 )
1823
@@ -150,10 +155,9 @@ medusaIntegrationTestRunner({
150155
151156 describe ( "GET /store/carts/[id]" , ( ) => {
152157 it ( "should return 404 when trying to fetch a cart that does not exist" , async ( ) => {
153- const response = await api . get (
154- `/store/carts/fake` ,
155- storeHeadersWithCustomer
156- ) . catch ( ( e ) => e )
158+ const response = await api
159+ . get ( `/store/carts/fake` , storeHeadersWithCustomer )
160+ . catch ( ( e ) => e )
157161
158162 expect ( response . response . status ) . toEqual ( 404 )
159163 } )
@@ -1868,6 +1872,80 @@ medusaIntegrationTestRunner({
18681872 )
18691873 } )
18701874
1875+ it ( "should successfully complete cart and fail on concurrent complete" , async ( ) => {
1876+ const paymentCollection = (
1877+ await api . post (
1878+ `/store/payment-collections` ,
1879+ { cart_id : cart . id } ,
1880+ storeHeaders
1881+ )
1882+ ) . data . payment_collection
1883+
1884+ await api . post (
1885+ `/store/payment-collections/${ paymentCollection . id } /payment-sessions` ,
1886+ { provider_id : "pp_system_default" } ,
1887+ storeHeaders
1888+ )
1889+
1890+ await createCartCreditLinesWorkflow . run ( {
1891+ input : [
1892+ {
1893+ cart_id : cart . id ,
1894+ amount : 100 ,
1895+ currency_code : "usd" ,
1896+ reference : "test" ,
1897+ reference_id : "test" ,
1898+ } ,
1899+ ] ,
1900+ container : appContainer ,
1901+ } )
1902+
1903+ // Concurrently complete the cart
1904+ let completedCart : any [ ] = [ ]
1905+ for ( let i = 0 ; i < 5 ; i ++ ) {
1906+ completedCart . push (
1907+ api
1908+ . post ( `/store/carts/${ cart . id } /complete` , { } , storeHeaders )
1909+ . catch ( ( e ) => e )
1910+ )
1911+
1912+ await setTimeout ( 25 )
1913+ }
1914+
1915+ let all = await Promise . all ( completedCart )
1916+
1917+ let success = all . filter ( ( res ) => res . status === 200 )
1918+ let failure = all . filter ( ( res ) => res . status !== 200 )
1919+
1920+ const successData = success [ 0 ] . data . order
1921+ for ( const res of success ) {
1922+ expect ( res . data . order ) . toEqual ( successData )
1923+ }
1924+
1925+ expect ( failure . length ) . toBeGreaterThan ( 0 )
1926+
1927+ expect ( successData ) . toEqual (
1928+ expect . objectContaining ( {
1929+ id : expect . any ( String ) ,
1930+ currency_code : "usd" ,
1931+ credit_lines : [
1932+ expect . objectContaining ( {
1933+ amount : 100 ,
1934+ reference : "test" ,
1935+ reference_id : "test" ,
1936+ } ) ,
1937+ ] ,
1938+ items : expect . arrayContaining ( [
1939+ expect . objectContaining ( {
1940+ unit_price : 1500 ,
1941+ compare_at_unit_price : null ,
1942+ quantity : 1 ,
1943+ } ) ,
1944+ ] ) ,
1945+ } )
1946+ )
1947+ } )
1948+
18711949 it ( "should successfully complete cart" , async ( ) => {
18721950 const paymentCollection = (
18731951 await api . post (
@@ -1883,7 +1961,7 @@ medusaIntegrationTestRunner({
18831961 storeHeaders
18841962 )
18851963
1886- createCartCreditLinesWorkflow . run ( {
1964+ await createCartCreditLinesWorkflow . run ( {
18871965 input : [
18881966 {
18891967 cart_id : cart . id ,
0 commit comments