|
1
|
import { |
|
2
|
AdminShippingProfile, |
|
3
|
AdminStockLocation, |
|
4
|
AdminSalesChannel, |
|
5
|
MedusaContainer, |
|
6
|
} from "@medusajs/types" |
|
7
|
import { adminHeaders } from "../../../helpers/create-admin-user" |
|
8
|
|
|
9
|
export async function createShippingOptionSeeder({ |
|
10
|
api, |
|
11
|
container, |
|
12
|
salesChannelOverride, |
|
13
|
stockLocationOverride, |
|
14
|
shippingProfileOverride, |
|
15
|
countries = ["us"], |
|
16
|
}: { |
|
17
|
api: any |
|
18
|
container: MedusaContainer |
|
19
|
salesChannelOverride?: AdminSalesChannel |
|
20
|
stockLocationOverride?: AdminStockLocation |
|
21
|
shippingProfileOverride?: AdminShippingProfile |
|
22
|
countries?: string[] |
|
23
|
}) { |
|
24
|
const salesChannel = |
|
25
|
salesChannelOverride ?? |
|
26
|
( |
|
27
|
await api.post( |
|
28
|
"/admin/sales-channels", |
|
29
|
{ name: "first channel", description: "channel" }, |
|
30
|
adminHeaders |
|
31
|
) |
|
32
|
).data.sales_channel |
|
33
|
|
|
34
|
const stockLocation = |
|
35
|
stockLocationOverride ?? |
|
36
|
( |
|
37
|
await api.post( |
|
38
|
`/admin/stock-locations`, |
|
39
|
{ name: "test location" }, |
|
40
|
adminHeaders |
|
41
|
) |
|
42
|
).data.stock_location |
|
43
|
|
|
44
|
await api.post( |
|
45
|
`/admin/stock-locations/${stockLocation.id}/sales-channels`, |
|
46
|
{ add: [salesChannel.id] }, |
|
47
|
adminHeaders |
|
48
|
) |
|
49
|
|
|
50
|
const shippingProfile = |
|
51
|
shippingProfileOverride ?? |
|
52
|
( |
|
53
|
await api.post( |
|
54
|
`/admin/shipping-profiles`, |
|
55
|
{ name: `test-${stockLocation.id}`, type: "default" }, |
|
56
|
adminHeaders |
|
57
|
) |
|
58
|
).data.shipping_profile |
|
59
|
|
|
60
|
const fulfillmentSets = ( |
|
61
|
await api.post( |
|
62
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
|
63
|
{ |
|
64
|
name: `Test-${shippingProfile.id}`, |
|
65
|
type: "test-type", |
|
66
|
}, |
|
67
|
adminHeaders |
|
68
|
) |
|
69
|
).data.stock_location.fulfillment_sets |
|
70
|
|
|
71
|
const fulfillmentSet = ( |
|
72
|
await api.post( |
|
73
|
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
|
74
|
{ |
|
75
|
name: `Test-${shippingProfile.id}`, |
|
76
|
geo_zones: countries.map((country) => ({ |
|
77
|
type: "country", |
|
78
|
country_code: country, |
|
79
|
})), |
|
80
|
}, |
|
81
|
adminHeaders |
|
82
|
) |
|
83
|
).data.fulfillment_set |
|
84
|
|
|
85
|
await api.post( |
|
86
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
|
87
|
{ add: ["manual_test-provider"] }, |
|
88
|
adminHeaders |
|
89
|
) |
|
90
|
|
|
91
|
const shippingOption = ( |
|
92
|
await api.post( |
|
93
|
`/admin/shipping-options?fields=+service_zone.fulfillment_set.*,service_zone.geo_zones.*,service_zone.fulfillment_set.location*`, |
|
94
|
{ |
|
95
|
name: `Test shipping option ${fulfillmentSet.id}`, |
|
96
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
97
|
shipping_profile_id: shippingProfile.id, |
|
98
|
provider_id: "manual_test-provider", |
|
99
|
price_type: "flat", |
|
100
|
type: { |
|
101
|
label: "Test type", |
|
102
|
description: "Test description", |
|
103
|
code: "test-code", |
|
104
|
}, |
|
105
|
prices: [{ currency_code: "usd", amount: 1000 }], |
|
106
|
rules: [], |
|
107
|
}, |
|
108
|
adminHeaders |
|
109
|
) |
|
110
|
).data.shipping_option |
|
111
|
|
|
112
|
return { |
|
113
|
salesChannel, |
|
114
|
stockLocation, |
|
115
|
shippingProfile, |
|
116
|
fulfillmentSet, |
|
117
|
shippingOption, |
|
118
|
} |
|
119
|
} |