1 import {
2 CreateFulfillmentDTO,
3 CreateShippingOptionDTO,
4 IFulfillmentModuleService,
5 } from "@medusajs/types"
6
7 export function generateCreateFulfillmentData(
8 data: Partial<CreateFulfillmentDTO> & {
9 provider_id: string
10 shipping_option_id: string
11 order_id: string
12 location_id: string
13 }
14 ) {
15 const randomString = Math.random().toString(36).substring(7)
16
17 return {
18 location_id: data.location_id,
19 packed_at: null,
20 shipped_at: null,
21 delivered_at: null,
22 canceled_at: null,
23 data: null,
24 provider_id: data.provider_id,
25 shipping_option_id: data.shipping_option_id,
26 metadata: data.metadata ?? null,
27 delivery_address: data.delivery_address ?? {
28 address_1: "test-address_" + randomString,
29 address_2: "test-address_" + randomString,
30 city: "test-city_" + randomString,
31 postal_code: "test-postal-code_" + randomString,
32 country_code: "test-country-code_" + randomString,
33 province: "test-province_" + randomString,
34 phone: "test-phone_" + randomString,
35 first_name: "test-first-name_" + randomString,
36 last_name: "test-last-name_" + randomString,
37 },
38 items: data.items ?? [
39 {
40 title: "test-title_" + randomString,
41 sku: "test-sku_" + randomString,
42 quantity: 1,
43 barcode: "test-barcode_" + randomString,
44 },
45 ],
46 labels: data.labels ?? [
47 {
48 tracking_number: "test-tracking-number_" + randomString,
49 tracking_url: "https://example.com/tracking/" + randomString,
50 label_url: "https://example.com/label/" + randomString,
51 },
52 ],
53 order_id: data.order_id,
54 }
55 }
56
57 export function generateCreateShippingOptionsData({
58 name,
59 service_zone_id,
60 shipping_profile_id,
61 provider_id,
62 price_type,
63 rules,
64 type,
65 data,
66 }: Omit<CreateShippingOptionDTO, "name" | "price_type" | "type"> & {
67 price_type?: CreateShippingOptionDTO["price_type"]
68 name?: string
69 type?: CreateShippingOptionDTO["type"]
70 }): Required<CreateShippingOptionDTO> {
71 const randomString = Math.random().toString(36).substring(7)
72
73 return {
74 service_zone_id: service_zone_id,
75 shipping_profile_id: shipping_profile_id,
76 provider_id: provider_id,
77 type: type ?? {
78 code: "test-type_" + randomString,
79 description: "test-description_" + randomString,
80 label: "test-label_" + randomString,
81 },
82 data: data ?? {
83 amount: 1000,
84 },
85 name: name ?? Math.random().toString(36).substring(7),
86 price_type: price_type ?? "flat",
87 rules: rules ?? [
88 {
89 attribute: "weight",
90 operator: "eq",
91 value: "test",
92 },
93 ],
94 }
95 }
96
97 export async function setupFullDataFulfillmentStructure(
98 service: IFulfillmentModuleService,
99 {
100 providerId,
101 locationId,
102 }: {
103 providerId: string
104 locationId: string
105 }
106 ) {
107 const randomString = Math.random().toString(36).substring(7)
108
109 const shippingProfile = await service.createShippingProfiles({
110 // generate random string
111 name: "test_" + randomString,
112 type: "default",
113 })
114 const fulfillmentSet = await service.createFulfillmentSets({
115 name: "test_" + randomString,
116 type: "test-type",
117 })
118 const serviceZone = await service.createServiceZones({
119 name: "test_" + randomString,
120 fulfillment_set_id: fulfillmentSet.id,
121 geo_zones: [
122 {
123 type: "country",
124 country_code: "US_" + randomString,
125 },
126 ],
127 })
128
129 const shippingOption = await service.createShippingOptions(
130 generateCreateShippingOptionsData({
131 provider_id: providerId,
132 service_zone_id: serviceZone.id,
133 shipping_profile_id: shippingProfile.id,
134 })
135 )
136
137 await service.createFulfillment(
138 generateCreateFulfillmentData({
139 order_id: "fake-order",
140 location_id: locationId,
141 provider_id: providerId,
142 shipping_option_id: shippingOption.id,
143 })
144 )
145 }