11const path = require ( "path" )
2- const { Address, Customer } = require ( "@medusajs/medusa" )
2+ const { Address, Customer, Order , Region } = require ( "@medusajs/medusa" )
33
44const setupServer = require ( "../../../helpers/setup-server" )
55const { useApi } = require ( "../../../helpers/use-api" )
@@ -19,7 +19,7 @@ describe("/store/customers", () => {
1919 beforeAll ( async ( ) => {
2020 const cwd = path . resolve ( path . join ( __dirname , ".." , ".." ) )
2121 dbConnection = await initDb ( { cwd } )
22- medusaProcess = await setupServer ( { cwd } )
22+ medusaProcess = await setupServer ( { cwd, verbose : false } )
2323 } )
2424
2525 afterAll ( async ( ) => {
@@ -89,6 +89,150 @@ describe("/store/customers", () => {
8989 } )
9090 } )
9191
92+ describe ( "GET /store/customers/me/orders" , ( ) => {
93+ beforeEach ( async ( ) => {
94+ const manager = dbConnection . manager
95+ await manager . query ( `ALTER SEQUENCE order_display_id_seq RESTART WITH 1` )
96+
97+ await manager . insert ( Address , {
98+ id : "addr_test" ,
99+ first_name : "String" ,
100+ last_name : "Stringson" ,
101+ address_1 : "String st" ,
102+ city : "Stringville" ,
103+ postal_code : "1236" ,
104+ province : "ca" ,
105+ country_code : "us" ,
106+ } )
107+
108+ await manager . insert ( Region , {
109+ id : "region" ,
110+ name : "Test Region" ,
111+ currency_code : "usd" ,
112+ tax_rate : 0 ,
113+ } )
114+
115+ await manager . insert ( Customer , {
116+ id : "test_customer" ,
117+ first_name : "John" ,
118+ last_name : "Deere" ,
119+ email : "john@deere.com" ,
120+ password_hash :
121+ "c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN" , // password matching "test"
122+ has_account : true ,
123+ } )
124+
125+ await manager . insert ( Customer , {
126+ id : "test_customer1" ,
127+ first_name : "John" ,
128+ last_name : "Deere" ,
129+ email : "joh1n@deere.com" ,
130+ password_hash :
131+ "c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN" , // password matching "test"
132+ has_account : true ,
133+ } )
134+
135+ await manager . insert ( Order , {
136+ id : "order_test_completed" ,
137+ email : "test1@email.com" ,
138+ display_id : 1 ,
139+ customer_id : "test_customer" ,
140+ region_id : "region" ,
141+ status : "completed" ,
142+ tax_rate : 0 ,
143+ currency_code : "usd" ,
144+ } )
145+
146+ await manager . insert ( Order , {
147+ id : "order_test_completed1" ,
148+ email : "test1@email.com" ,
149+ display_id : 2 ,
150+ customer_id : "test_customer1" ,
151+ region_id : "region" ,
152+ status : "completed" ,
153+ tax_rate : 0 ,
154+ currency_code : "usd" ,
155+ } )
156+
157+ await manager . insert ( Order , {
158+ id : "order_test_canceled" ,
159+ email : "test1@email.com" ,
160+ display_id : 3 ,
161+ customer_id : "test_customer" ,
162+ region_id : "region" ,
163+ status : "canceled" ,
164+ tax_rate : 0 ,
165+ currency_code : "usd" ,
166+ } )
167+ } )
168+
169+ afterEach ( async ( ) => {
170+ await doAfterEach ( )
171+ } )
172+
173+ it ( "looks up completed orders" , async ( ) => {
174+ const api = useApi ( )
175+
176+ const authResponse = await api . post ( "/store/auth" , {
177+ email : "john@deere.com" ,
178+ password : "test" ,
179+ } )
180+
181+ const [ authCookie ] = authResponse . headers [ "set-cookie" ] [ 0 ] . split ( ";" )
182+
183+ const response = await api
184+ . get ( "/store/customers/me/orders?status[]=completed" , {
185+ headers : {
186+ Cookie : authCookie ,
187+ } ,
188+ } )
189+ . catch ( ( err ) => {
190+ return err . response
191+ } )
192+ expect ( response . status ) . toEqual ( 200 )
193+ expect ( response . data . orders [ 0 ] . display_id ) . toEqual ( 1 )
194+ expect ( response . data . orders [ 0 ] . email ) . toEqual ( "test1@email.com" )
195+ expect ( response . data . orders . length ) . toEqual ( 1 )
196+ } )
197+
198+ it ( "looks up cancelled and completed orders" , async ( ) => {
199+ const api = useApi ( )
200+
201+ const authResponse = await api . post ( "/store/auth" , {
202+ email : "john@deere.com" ,
203+ password : "test" ,
204+ } )
205+
206+ const [ authCookie ] = authResponse . headers [ "set-cookie" ] [ 0 ] . split ( ";" )
207+
208+ const response = await api
209+ . get (
210+ "/store/customers/me/orders?status[]=completed&status[]=canceled" ,
211+ {
212+ headers : {
213+ Cookie : authCookie ,
214+ } ,
215+ }
216+ )
217+ . catch ( ( err ) => {
218+ return console . log ( err . response . data . message )
219+ } )
220+
221+ expect ( response . status ) . toEqual ( 200 )
222+ expect ( response . data . orders ) . toEqual ( [
223+ expect . objectContaining ( {
224+ display_id : 3 ,
225+ status : "canceled" ,
226+ } ) ,
227+ expect . objectContaining ( {
228+ display_id : 1 ,
229+ status : "completed" ,
230+ } ) ,
231+ ] )
232+ expect ( response . data . orders . length ) . toEqual ( 2 )
233+ } )
234+ } )
235+
92236 describe ( "POST /store/customers/me" , ( ) => {
93237 beforeEach ( async ( ) => {
94238 const manager = dbConnection . manager
0 commit comments