|
1
|
import { addShippingMethodToCartWorkflow } from "@medusajs/core-flows" |
|
2
|
import { |
|
3
|
AdminInventoryItem, |
|
4
|
AdminProduct, |
|
5
|
AdminShippingProfile, |
|
6
|
AdminStockLocation, |
|
7
|
MedusaContainer, |
|
8
|
} from "@medusajs/types" |
|
9
|
import { |
|
10
|
ContainerRegistrationKeys, |
|
11
|
Modules, |
|
12
|
ProductStatus, |
|
13
|
} from "@medusajs/utils" |
|
14
|
import { |
|
15
|
adminHeaders, |
|
16
|
generatePublishableKey, |
|
17
|
generateStoreHeaders, |
|
18
|
} from "../../../helpers/create-admin-user" |
|
19
|
|
|
20
|
export async function createOrderSeeder({ |
|
21
|
api, |
|
22
|
container, |
|
23
|
storeHeaderOverride, |
|
24
|
productOverride, |
|
25
|
additionalProducts, |
|
26
|
stockChannelOverride, |
|
27
|
inventoryItemOverride, |
|
28
|
shippingProfileOverride, |
|
29
|
withoutShipping, |
|
30
|
}: { |
|
31
|
api: any |
|
32
|
container: MedusaContainer |
|
33
|
storeHeaderOverride?: any |
|
34
|
productOverride?: AdminProduct |
|
35
|
stockChannelOverride?: AdminStockLocation |
|
36
|
additionalProducts?: { variant_id: string; quantity: number }[] |
|
37
|
inventoryItemOverride?: AdminInventoryItem |
|
38
|
shippingProfileOverride?: AdminShippingProfile | AdminShippingProfile[] |
|
39
|
withoutShipping?: boolean |
|
40
|
}) { |
|
41
|
const publishableKey = await generatePublishableKey(container) |
|
42
|
|
|
43
|
const shippingProfileOverrideArray = !shippingProfileOverride |
|
44
|
? undefined |
|
45
|
: Array.isArray(shippingProfileOverride) |
|
46
|
? shippingProfileOverride |
|
47
|
: [shippingProfileOverride] |
|
48
|
|
|
49
|
const storeHeaders = |
|
50
|
storeHeaderOverride ?? |
|
51
|
generateStoreHeaders({ |
|
52
|
publishableKey, |
|
53
|
}) |
|
54
|
|
|
55
|
const region = ( |
|
56
|
await api.post( |
|
57
|
"/admin/regions", |
|
58
|
{ |
|
59
|
payment_providers: ["pp_system_default"], |
|
60
|
name: "Test region", |
|
61
|
currency_code: "usd", |
|
62
|
}, |
|
63
|
adminHeaders |
|
64
|
) |
|
65
|
).data.region |
|
66
|
|
|
67
|
const salesChannel = ( |
|
68
|
await api.post( |
|
69
|
"/admin/sales-channels", |
|
70
|
{ name: "first channel", description: "channel" }, |
|
71
|
adminHeaders |
|
72
|
) |
|
73
|
).data.sales_channel |
|
74
|
|
|
75
|
const stockLocation = |
|
76
|
stockChannelOverride ?? |
|
77
|
( |
|
78
|
await api.post( |
|
79
|
`/admin/stock-locations`, |
|
80
|
{ name: "test location" }, |
|
81
|
adminHeaders |
|
82
|
) |
|
83
|
).data.stock_location |
|
84
|
|
|
85
|
const inventoryItem = |
|
86
|
inventoryItemOverride ?? |
|
87
|
( |
|
88
|
await api.post( |
|
89
|
`/admin/inventory-items`, |
|
90
|
{ sku: "test-variant" }, |
|
91
|
adminHeaders |
|
92
|
) |
|
93
|
).data.inventory_item |
|
94
|
|
|
95
|
await api.post( |
|
96
|
`/admin/inventory-items/${inventoryItem.id}/location-levels`, |
|
97
|
{ |
|
98
|
location_id: stockLocation.id, |
|
99
|
stocked_quantity: 10, |
|
100
|
}, |
|
101
|
adminHeaders |
|
102
|
) |
|
103
|
|
|
104
|
await api.post( |
|
105
|
`/admin/stock-locations/${stockLocation.id}/sales-channels`, |
|
106
|
{ add: [salesChannel.id] }, |
|
107
|
adminHeaders |
|
108
|
) |
|
109
|
|
|
110
|
const shippingProfile = |
|
111
|
shippingProfileOverrideArray?.[0] ?? |
|
112
|
( |
|
113
|
await api.post( |
|
114
|
`/admin/shipping-profiles`, |
|
115
|
{ name: `test-${stockLocation.id}`, type: "default" }, |
|
116
|
adminHeaders |
|
117
|
) |
|
118
|
).data.shipping_profile |
|
119
|
|
|
120
|
const product = |
|
121
|
productOverride ?? |
|
122
|
( |
|
123
|
await api.post( |
|
124
|
"/admin/products", |
|
125
|
{ |
|
126
|
title: `Test fixture ${shippingProfile.id}`, |
|
127
|
status: ProductStatus.PUBLISHED, |
|
128
|
shipping_profile_id: withoutShipping ? undefined : shippingProfile.id, |
|
129
|
options: [ |
|
130
|
{ title: "size", values: ["large", "small"] }, |
|
131
|
{ title: "color", values: ["green"] }, |
|
132
|
], |
|
133
|
variants: [ |
|
134
|
{ |
|
135
|
title: "Test variant", |
|
136
|
sku: "test-variant", |
|
137
|
inventory_items: [ |
|
138
|
{ |
|
139
|
inventory_item_id: inventoryItem.id, |
|
140
|
required_quantity: 1, |
|
141
|
}, |
|
142
|
], |
|
143
|
prices: [ |
|
144
|
{ |
|
145
|
currency_code: "usd", |
|
146
|
amount: 100, |
|
147
|
}, |
|
148
|
], |
|
149
|
options: { |
|
150
|
size: "large", |
|
151
|
color: "green", |
|
152
|
}, |
|
153
|
}, |
|
154
|
], |
|
155
|
}, |
|
156
|
adminHeaders |
|
157
|
) |
|
158
|
).data.product |
|
159
|
|
|
160
|
const fulfillmentSets = ( |
|
161
|
await api.post( |
|
162
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
|
163
|
{ |
|
164
|
name: `Test-${shippingProfile.id}`, |
|
165
|
type: "test-type", |
|
166
|
}, |
|
167
|
adminHeaders |
|
168
|
) |
|
169
|
).data.stock_location.fulfillment_sets |
|
170
|
|
|
171
|
const fulfillmentSet = ( |
|
172
|
await api.post( |
|
173
|
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
|
174
|
{ |
|
175
|
name: `Test-${shippingProfile.id}`, |
|
176
|
geo_zones: [{ type: "country", country_code: "us" }], |
|
177
|
}, |
|
178
|
adminHeaders |
|
179
|
) |
|
180
|
).data.fulfillment_set |
|
181
|
|
|
182
|
await api.post( |
|
183
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
|
184
|
{ add: ["manual_test-provider"] }, |
|
185
|
adminHeaders |
|
186
|
) |
|
187
|
|
|
188
|
const remoteLink = container.resolve(ContainerRegistrationKeys.LINK) |
|
189
|
await remoteLink.create([ |
|
190
|
{ |
|
191
|
[Modules.SALES_CHANNEL]: { |
|
192
|
sales_channel_id: salesChannel.id, |
|
193
|
}, |
|
194
|
[Modules.STOCK_LOCATION]: { |
|
195
|
stock_location_id: stockLocation.id, |
|
196
|
}, |
|
197
|
}, |
|
198
|
{ |
|
199
|
[Modules.PRODUCT]: { |
|
200
|
variant_id: product.variants[0].id, |
|
201
|
}, |
|
202
|
[Modules.INVENTORY]: { |
|
203
|
inventory_item_id: inventoryItem.id, |
|
204
|
}, |
|
205
|
}, |
|
206
|
]) |
|
207
|
|
|
208
|
/** |
|
209
|
* Create shipping options for each shipping profile provided |
|
210
|
*/ |
|
211
|
const shippingOptions = await Promise.all( |
|
212
|
(shippingProfileOverrideArray || [shippingProfile]).map(async (sp) => { |
|
213
|
return ( |
|
214
|
await api.post( |
|
215
|
`/admin/shipping-options`, |
|
216
|
{ |
|
217
|
name: `Test shipping option ${fulfillmentSet.id}`, |
|
218
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
219
|
shipping_profile_id: sp.id, |
|
220
|
provider_id: "manual_test-provider", |
|
221
|
price_type: "flat", |
|
222
|
type: { |
|
223
|
label: "Test type", |
|
224
|
description: "Test description", |
|
225
|
code: "test-code", |
|
226
|
}, |
|
227
|
prices: [ |
|
228
|
{ currency_code: "usd", amount: 1000 }, |
|
229
|
{ region_id: region.id, amount: 1100 }, |
|
230
|
], |
|
231
|
rules: [], |
|
232
|
}, |
|
233
|
adminHeaders |
|
234
|
) |
|
235
|
).data.shipping_option |
|
236
|
}) |
|
237
|
) |
|
238
|
|
|
239
|
const shippingOption = shippingOptions[0] |
|
240
|
|
|
241
|
const cart = ( |
|
242
|
await api.post( |
|
243
|
`/store/carts`, |
|
244
|
{ |
|
245
|
currency_code: "usd", |
|
246
|
email: "tony@stark-industries.com", |
|
247
|
region_id: region.id, |
|
248
|
shipping_address: { |
|
249
|
address_1: "test address 1", |
|
250
|
address_2: "test address 2", |
|
251
|
city: "ny", |
|
252
|
country_code: "us", |
|
253
|
province: "ny", |
|
254
|
postal_code: "94016", |
|
255
|
}, |
|
256
|
billing_address: { |
|
257
|
address_1: "test billing address 1", |
|
258
|
address_2: "test billing address 2", |
|
259
|
city: "ny", |
|
260
|
country_code: "us", |
|
261
|
province: "ny", |
|
262
|
postal_code: "94016", |
|
263
|
}, |
|
264
|
sales_channel_id: salesChannel.id, |
|
265
|
items: [ |
|
266
|
{ quantity: 1, variant_id: product.variants[0].id }, |
|
267
|
...(additionalProducts || []), |
|
268
|
], |
|
269
|
}, |
|
270
|
storeHeaders |
|
271
|
) |
|
272
|
).data.cart |
|
273
|
|
|
274
|
if (!withoutShipping) { |
|
275
|
// Create shipping methods for each shipping option so shipping profiles of products in the cart are supported |
|
276
|
await addShippingMethodToCartWorkflow(container).run({ |
|
277
|
input: { |
|
278
|
cart_id: cart.id, |
|
279
|
options: shippingOptions.map((so) => ({ id: so.id })), |
|
280
|
}, |
|
281
|
}) |
|
282
|
} |
|
283
|
|
|
284
|
const paymentCollection = ( |
|
285
|
await api.post( |
|
286
|
`/store/payment-collections`, |
|
287
|
{ |
|
288
|
cart_id: cart.id, |
|
289
|
}, |
|
290
|
storeHeaders |
|
291
|
) |
|
292
|
).data.payment_collection |
|
293
|
|
|
294
|
await api.post( |
|
295
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
296
|
{ provider_id: "pp_system_default" }, |
|
297
|
storeHeaders |
|
298
|
) |
|
299
|
|
|
300
|
let order = ( |
|
301
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
302
|
).data.order |
|
303
|
|
|
304
|
order = (await api.get(`/admin/orders/${order.id}`, adminHeaders)).data.order |
|
305
|
|
|
306
|
return { |
|
307
|
order, |
|
308
|
region, |
|
309
|
salesChannel, |
|
310
|
stockLocation, |
|
311
|
inventoryItem, |
|
312
|
shippingProfile, |
|
313
|
product, |
|
314
|
fulfillmentSets, |
|
315
|
fulfillmentSet, |
|
316
|
shippingOption, |
|
317
|
cart, |
|
318
|
paymentCollection, |
|
319
|
} |
|
320
|
} |