@blegaut/medusa-v2-fedex-fulfillment 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.js +336 -0
- package/.medusa/server/src/admin/index.mjs +335 -0
- package/.medusa/server/src/api/admin/fedex/route.js +65 -0
- package/.medusa/server/src/api/admin/fedex/validator.js +14 -0
- package/.medusa/server/src/fedex-api/auth.js +37 -0
- package/.medusa/server/src/fedex-api/create-fulfillment.js +156 -0
- package/.medusa/server/src/fedex-api/get-fulfullment-options.js +105 -0
- package/.medusa/server/src/fedex-api/get-shipping-rates.js +83 -0
- package/.medusa/server/src/fedex-api/types.js +68 -0
- package/.medusa/server/src/modules/setting/index.js +13 -0
- package/.medusa/server/src/modules/setting/migrations/Migration20250915204617.js +28 -0
- package/.medusa/server/src/modules/setting/models/setting.js +15 -0
- package/.medusa/server/src/modules/setting/service.js +42 -0
- package/.medusa/server/src/providers/fedex/index.js +11 -0
- package/.medusa/server/src/providers/fedex/service.js +209 -0
- package/.medusa/server/src/utils/medusa-money.js +48 -0
- package/.medusa/server/src/workflows/create-shipment.js +292 -0
- package/.medusa/server/src/workflows/get-credentials.js +29 -0
- package/.medusa/server/src/workflows/setup-credentials.js +28 -0
- package/README.md +211 -0
- package/package.json +109 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
3
|
+
const adminSdk = require("@medusajs/admin-sdk");
|
|
4
|
+
const ui = require("@medusajs/ui");
|
|
5
|
+
const reactQuery = require("@tanstack/react-query");
|
|
6
|
+
const react = require("react");
|
|
7
|
+
const Medusa = require("@medusajs/js-sdk");
|
|
8
|
+
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
|
9
|
+
const Medusa__default = /* @__PURE__ */ _interopDefault(Medusa);
|
|
10
|
+
const FedexWidget = ({
|
|
11
|
+
data
|
|
12
|
+
}) => {
|
|
13
|
+
if (!data.fulfillments || data.fulfillments.length === 0) {
|
|
14
|
+
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
|
|
15
|
+
}
|
|
16
|
+
const trackingInfo = data.fulfillments.flatMap(
|
|
17
|
+
(fulfillment) => (fulfillment.labels || []).filter((label) => label.label_url).map((label) => ({
|
|
18
|
+
trackingNumber: label.tracking_number,
|
|
19
|
+
trackingUrl: label.tracking_url,
|
|
20
|
+
labelUrl: label.label_url
|
|
21
|
+
}))
|
|
22
|
+
);
|
|
23
|
+
if (trackingInfo.length === 0) {
|
|
24
|
+
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
|
|
25
|
+
}
|
|
26
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "divide-y p-0", children: [
|
|
27
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: "FedEx Shipping Labels" }) }),
|
|
28
|
+
trackingInfo.map((info, idx) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
29
|
+
"div",
|
|
30
|
+
{
|
|
31
|
+
className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4",
|
|
32
|
+
children: [
|
|
33
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "font-medium font-sans txt-compact-small", children: "Tracking" }),
|
|
34
|
+
/* @__PURE__ */ jsxRuntime.jsx("ul", { children: /* @__PURE__ */ jsxRuntime.jsx("li", { children: /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "font-normal font-sans txt-compact-small", children: [
|
|
35
|
+
info.trackingNumber ? info.trackingUrl ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
36
|
+
"a",
|
|
37
|
+
{
|
|
38
|
+
href: info.trackingUrl,
|
|
39
|
+
target: "_blank",
|
|
40
|
+
rel: "noopener noreferrer",
|
|
41
|
+
className: "text-blue-600 hover:underline",
|
|
42
|
+
children: info.trackingNumber
|
|
43
|
+
}
|
|
44
|
+
) : info.trackingNumber : "N/A",
|
|
45
|
+
" ",
|
|
46
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
47
|
+
"a",
|
|
48
|
+
{
|
|
49
|
+
href: info.labelUrl,
|
|
50
|
+
target: "_blank",
|
|
51
|
+
rel: "noopener noreferrer",
|
|
52
|
+
className: "text-blue-600 hover:underline",
|
|
53
|
+
download: true,
|
|
54
|
+
children: "Download Label"
|
|
55
|
+
}
|
|
56
|
+
)
|
|
57
|
+
] }) }) })
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
idx
|
|
61
|
+
))
|
|
62
|
+
] });
|
|
63
|
+
};
|
|
64
|
+
adminSdk.defineWidgetConfig({
|
|
65
|
+
zone: "order.details.after"
|
|
66
|
+
});
|
|
67
|
+
const sdk = new Medusa__default.default({
|
|
68
|
+
baseUrl: "/",
|
|
69
|
+
debug: false,
|
|
70
|
+
auth: { type: "session" }
|
|
71
|
+
});
|
|
72
|
+
const initialState = {
|
|
73
|
+
is_enabled: true,
|
|
74
|
+
client_id: "",
|
|
75
|
+
client_secret: "",
|
|
76
|
+
account_number: "",
|
|
77
|
+
is_sandbox: false,
|
|
78
|
+
enable_logs: false,
|
|
79
|
+
weight_unit_of_measure: ""
|
|
80
|
+
};
|
|
81
|
+
const queryClient = new reactQuery.QueryClient();
|
|
82
|
+
const FedexSettingsPageInner = () => {
|
|
83
|
+
const { data, isLoading } = reactQuery.useQuery({
|
|
84
|
+
queryFn: () => sdk.client.fetch("/admin/fedex"),
|
|
85
|
+
queryKey: ["fedex-config"]
|
|
86
|
+
});
|
|
87
|
+
const [form, setForm] = react.useState(initialState);
|
|
88
|
+
const [setTouched] = react.useState({});
|
|
89
|
+
const [alert, setAlert] = react.useState(null);
|
|
90
|
+
react.useEffect(() => {
|
|
91
|
+
if (data) {
|
|
92
|
+
setForm((prev) => ({
|
|
93
|
+
...prev,
|
|
94
|
+
...data
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
}, [data]);
|
|
98
|
+
const handleChange = (e) => {
|
|
99
|
+
const { name, value, type, checked } = e.target;
|
|
100
|
+
setForm((prev) => ({
|
|
101
|
+
...prev,
|
|
102
|
+
[name]: type === "checkbox" ? checked : value
|
|
103
|
+
}));
|
|
104
|
+
setTouched((prev) => ({ ...prev, [name]: true }));
|
|
105
|
+
};
|
|
106
|
+
const handleSelect = (value) => {
|
|
107
|
+
setForm((prev) => ({
|
|
108
|
+
...prev,
|
|
109
|
+
weight_unit_of_measure: value
|
|
110
|
+
}));
|
|
111
|
+
setTouched((prev) => ({ ...prev, weight_unit_of_measure: true }));
|
|
112
|
+
};
|
|
113
|
+
const isValid = form.client_id.trim() && form.client_secret.trim() && form.account_number.trim() && form.weight_unit_of_measure;
|
|
114
|
+
if (isLoading) {
|
|
115
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "divide-y p-0", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-6 py-4", children: "Loading..." }) });
|
|
116
|
+
}
|
|
117
|
+
async function handleSave(event) {
|
|
118
|
+
var _a, _b;
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
try {
|
|
121
|
+
await sdk.client.fetch("/admin/fedex", {
|
|
122
|
+
method: "POST",
|
|
123
|
+
body: form,
|
|
124
|
+
headers: {
|
|
125
|
+
"Content-Type": "application/json"
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
setAlert({ type: "success", message: "FedEx settings saved successfully." });
|
|
129
|
+
(_a = document.querySelector(".alert")) == null ? void 0 : _a.scrollIntoView({ behavior: "smooth" });
|
|
130
|
+
} catch (error) {
|
|
131
|
+
setAlert({ type: "error", message: "Failed to save FedEx settings." });
|
|
132
|
+
(_b = document.querySelector(".alert")) == null ? void 0 : _b.scrollIntoView({ behavior: "smooth" });
|
|
133
|
+
console.error(error);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "divide-y p-0", children: [
|
|
137
|
+
alert && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-6 py-2", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Alert, { variant: alert.type === "success" ? "success" : "error", dismissible: true, children: alert.message }) }),
|
|
138
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
139
|
+
"svg",
|
|
140
|
+
{
|
|
141
|
+
height: "120",
|
|
142
|
+
width: "120",
|
|
143
|
+
viewBox: "0 0 512 512",
|
|
144
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
145
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("g", { id: "_x31_25-fedex", children: /* @__PURE__ */ jsxRuntime.jsxs("g", { children: [
|
|
146
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
147
|
+
"path",
|
|
148
|
+
{
|
|
149
|
+
d: "M251.777,183.644v61.553h-0.36c-6.899-7.906-15.452-10.637-25.443-10.637\n c-20.412,0-35.793,13.943-41.184,32.271c-12.937-42.694-70.006-41.4-87.615-10.063v-17.393H57.787v-18.831h43.125v-29.541H22.568\n V323.54h35.219v-55.703h35.147c-1.079,4.097-1.653,8.483-1.653,13.081c0,52.542,73.744,65.692,93.58,17.034h-30.187\n c-10.566,15.022-32.919,6.396-32.919-10.493h61.453c2.659,21.921,19.694,40.897,43.197,40.897\n c10.134,0,19.407-4.959,25.084-13.371h0.359v8.555h44.717v-19.896v-30v-30v-40l0.002-20H251.777z M122.688,268.123\n c4.386-18.902,29.973-18.398,33.423,0H122.688z M232.946,303.272c-24.869,0-24.438-45.139,0-45.139\n C256.377,258.134,257.742,303.272,232.946,303.272z",
|
|
150
|
+
fill: "#442E8C"
|
|
151
|
+
}
|
|
152
|
+
),
|
|
153
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
154
|
+
"polygon",
|
|
155
|
+
{
|
|
156
|
+
points: "375.345,323.332 296.156,323.332 296.156,183.661 375.848,183.661 375.848,208.995\n 328.787,208.995 328.787,237.432 375.345,237.432 375.345,261.004 328.283,261.004 328.283,298.503 375.345,298.503\n 375.345,323.332",
|
|
157
|
+
fill: "#F47624"
|
|
158
|
+
}
|
|
159
|
+
),
|
|
160
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
161
|
+
"polygon",
|
|
162
|
+
{
|
|
163
|
+
points: "433.393,298.585 411.496,323.333 379.537,323.333 417.789,280.382 379.537,237.429\n 412.84,237.429 435.154,262.01 456.631,237.429 488.928,237.429 450.844,280.215 489.432,323.333 455.456,323.333\n 433.393,298.585",
|
|
164
|
+
fill: "#F47624"
|
|
165
|
+
}
|
|
166
|
+
)
|
|
167
|
+
] }) })
|
|
168
|
+
}
|
|
169
|
+
) }),
|
|
170
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "block", children: [
|
|
171
|
+
"You can eventually configure the FedEx credentials on the medusa-config.ts file as described on ",
|
|
172
|
+
/* @__PURE__ */ jsxRuntime.jsx("a", { href: "https://github.com/Imagination-Media/medusa-fedex-fulfillment?tab=readme-ov-file#installation", target: "_blank", rel: "noopener noreferrer", className: "text-blue-600 hover:underline font-bold", children: "the documentation." }),
|
|
173
|
+
" If the credentials are set up here they will be used over the medusa-config.ts file."
|
|
174
|
+
] }) }),
|
|
175
|
+
/* @__PURE__ */ jsxRuntime.jsxs("form", { className: "flex flex-col gap-y-6 px-6 py-4", children: [
|
|
176
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
177
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
|
|
178
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "is_enabled", children: "Enabled" }),
|
|
179
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
180
|
+
ui.Switch,
|
|
181
|
+
{
|
|
182
|
+
id: "is_enabled",
|
|
183
|
+
name: "is_enabled",
|
|
184
|
+
checked: form.is_enabled,
|
|
185
|
+
onCheckedChange: (checked) => setForm((prev) => ({ ...prev, is_enabled: checked }))
|
|
186
|
+
}
|
|
187
|
+
)
|
|
188
|
+
] }),
|
|
189
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { className: "mb-2 mt-1", children: "Enable or disable the FedEx integration." })
|
|
190
|
+
] }),
|
|
191
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
192
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "client_id", children: "Client ID" }),
|
|
193
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { className: "mt-1 block pb-1", children: "Your FedEx API Client ID for authentication." }),
|
|
194
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
195
|
+
ui.Input,
|
|
196
|
+
{
|
|
197
|
+
id: "client_id",
|
|
198
|
+
name: "client_id",
|
|
199
|
+
className: "mt-1",
|
|
200
|
+
autoComplete: "off",
|
|
201
|
+
value: form.client_id,
|
|
202
|
+
onChange: handleChange,
|
|
203
|
+
required: true
|
|
204
|
+
}
|
|
205
|
+
)
|
|
206
|
+
] }),
|
|
207
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
208
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "client_secret", children: "Client Secret" }),
|
|
209
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { className: "mt-1 block pb-1", children: "Your FedEx API Client Secret for authentication." }),
|
|
210
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
211
|
+
ui.Input,
|
|
212
|
+
{
|
|
213
|
+
id: "client_secret",
|
|
214
|
+
name: "client_secret",
|
|
215
|
+
type: "password",
|
|
216
|
+
className: "mt-1",
|
|
217
|
+
autoComplete: "off",
|
|
218
|
+
value: form.client_secret,
|
|
219
|
+
onChange: handleChange,
|
|
220
|
+
required: true
|
|
221
|
+
}
|
|
222
|
+
)
|
|
223
|
+
] }),
|
|
224
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
225
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "account_number", children: "Account Number" }),
|
|
226
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { className: "mt-1 block pb-1", children: "Your FedEx account number used for shipments." }),
|
|
227
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
228
|
+
ui.Input,
|
|
229
|
+
{
|
|
230
|
+
id: "account_number",
|
|
231
|
+
name: "account_number",
|
|
232
|
+
className: "mt-1",
|
|
233
|
+
autoComplete: "off",
|
|
234
|
+
value: form.account_number,
|
|
235
|
+
onChange: handleChange,
|
|
236
|
+
required: true
|
|
237
|
+
}
|
|
238
|
+
)
|
|
239
|
+
] }),
|
|
240
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
241
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
|
|
242
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "is_sandbox", children: "Sandbox Mode" }),
|
|
243
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
244
|
+
ui.Switch,
|
|
245
|
+
{
|
|
246
|
+
id: "is_sandbox",
|
|
247
|
+
name: "is_sandbox",
|
|
248
|
+
checked: form.is_sandbox,
|
|
249
|
+
onCheckedChange: (checked) => setForm((prev) => ({ ...prev, is_sandbox: checked }))
|
|
250
|
+
}
|
|
251
|
+
)
|
|
252
|
+
] }),
|
|
253
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { className: "mb-2 mt-1", children: "Enable to use FedEx sandbox environment for testing." })
|
|
254
|
+
] }),
|
|
255
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
256
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
|
|
257
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "enable_logs", children: "Enable Logs" }),
|
|
258
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
259
|
+
ui.Switch,
|
|
260
|
+
{
|
|
261
|
+
id: "enable_logs",
|
|
262
|
+
name: "enable_logs",
|
|
263
|
+
checked: form.enable_logs,
|
|
264
|
+
onCheckedChange: (checked) => setForm((prev) => ({ ...prev, enable_logs: checked }))
|
|
265
|
+
}
|
|
266
|
+
)
|
|
267
|
+
] }),
|
|
268
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { className: "mb-2 mt-1", children: "Enable logging for debugging FedEx requests and responses." })
|
|
269
|
+
] }),
|
|
270
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
271
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "weight_unit_of_measure", children: "Weight Unit of Measure" }),
|
|
272
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { className: "mt-1 block pb-1", children: "Select the unit of measure for package weights (LB or KG)." }),
|
|
273
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
274
|
+
ui.Select,
|
|
275
|
+
{
|
|
276
|
+
value: form.weight_unit_of_measure,
|
|
277
|
+
onValueChange: handleSelect,
|
|
278
|
+
required: true,
|
|
279
|
+
children: [
|
|
280
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "Select a weight unit" }) }),
|
|
281
|
+
/* @__PURE__ */ jsxRuntime.jsxs(ui.Select.Content, { children: [
|
|
282
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "LB", children: "LB" }, "LB"),
|
|
283
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "KG", children: "KG" }, "KG")
|
|
284
|
+
] })
|
|
285
|
+
]
|
|
286
|
+
}
|
|
287
|
+
)
|
|
288
|
+
] }),
|
|
289
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "button", onClick: handleSave, disabled: !isValid, className: "mt-4 w-fit", children: "Save" })
|
|
290
|
+
] })
|
|
291
|
+
] });
|
|
292
|
+
};
|
|
293
|
+
const config = adminSdk.defineRouteConfig({
|
|
294
|
+
label: "FedEx"
|
|
295
|
+
});
|
|
296
|
+
const FedexSettingsPage = () => /* @__PURE__ */ jsxRuntime.jsx(reactQuery.QueryClientProvider, { client: queryClient, children: /* @__PURE__ */ jsxRuntime.jsx(FedexSettingsPageInner, {}) });
|
|
297
|
+
const widgetModule = { widgets: [
|
|
298
|
+
{
|
|
299
|
+
Component: FedexWidget,
|
|
300
|
+
zone: ["order.details.after"]
|
|
301
|
+
}
|
|
302
|
+
] };
|
|
303
|
+
const routeModule = {
|
|
304
|
+
routes: [
|
|
305
|
+
{
|
|
306
|
+
Component: FedexSettingsPage,
|
|
307
|
+
path: "/settings/fedex"
|
|
308
|
+
}
|
|
309
|
+
]
|
|
310
|
+
};
|
|
311
|
+
const menuItemModule = {
|
|
312
|
+
menuItems: [
|
|
313
|
+
{
|
|
314
|
+
label: config.label,
|
|
315
|
+
icon: void 0,
|
|
316
|
+
path: "/settings/fedex",
|
|
317
|
+
nested: void 0,
|
|
318
|
+
rank: void 0,
|
|
319
|
+
translationNs: void 0
|
|
320
|
+
}
|
|
321
|
+
]
|
|
322
|
+
};
|
|
323
|
+
const formModule = { customFields: {} };
|
|
324
|
+
const displayModule = {
|
|
325
|
+
displays: {}
|
|
326
|
+
};
|
|
327
|
+
const i18nModule = { resources: {} };
|
|
328
|
+
const plugin = {
|
|
329
|
+
widgetModule,
|
|
330
|
+
routeModule,
|
|
331
|
+
menuItemModule,
|
|
332
|
+
formModule,
|
|
333
|
+
displayModule,
|
|
334
|
+
i18nModule
|
|
335
|
+
};
|
|
336
|
+
module.exports = plugin;
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { jsx, Fragment, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { defineWidgetConfig, defineRouteConfig } from "@medusajs/admin-sdk";
|
|
3
|
+
import { Container, Heading, Alert, Label, Switch, Hint, Input, Select, Button } from "@medusajs/ui";
|
|
4
|
+
import { QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
|
|
5
|
+
import { useState, useEffect } from "react";
|
|
6
|
+
import Medusa from "@medusajs/js-sdk";
|
|
7
|
+
const FedexWidget = ({
|
|
8
|
+
data
|
|
9
|
+
}) => {
|
|
10
|
+
if (!data.fulfillments || data.fulfillments.length === 0) {
|
|
11
|
+
return /* @__PURE__ */ jsx(Fragment, {});
|
|
12
|
+
}
|
|
13
|
+
const trackingInfo = data.fulfillments.flatMap(
|
|
14
|
+
(fulfillment) => (fulfillment.labels || []).filter((label) => label.label_url).map((label) => ({
|
|
15
|
+
trackingNumber: label.tracking_number,
|
|
16
|
+
trackingUrl: label.tracking_url,
|
|
17
|
+
labelUrl: label.label_url
|
|
18
|
+
}))
|
|
19
|
+
);
|
|
20
|
+
if (trackingInfo.length === 0) {
|
|
21
|
+
return /* @__PURE__ */ jsx(Fragment, {});
|
|
22
|
+
}
|
|
23
|
+
return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", children: [
|
|
24
|
+
/* @__PURE__ */ jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx(Heading, { level: "h2", children: "FedEx Shipping Labels" }) }),
|
|
25
|
+
trackingInfo.map((info, idx) => /* @__PURE__ */ jsxs(
|
|
26
|
+
"div",
|
|
27
|
+
{
|
|
28
|
+
className: "text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4",
|
|
29
|
+
children: [
|
|
30
|
+
/* @__PURE__ */ jsx("p", { className: "font-medium font-sans txt-compact-small", children: "Tracking" }),
|
|
31
|
+
/* @__PURE__ */ jsx("ul", { children: /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsxs("p", { className: "font-normal font-sans txt-compact-small", children: [
|
|
32
|
+
info.trackingNumber ? info.trackingUrl ? /* @__PURE__ */ jsx(
|
|
33
|
+
"a",
|
|
34
|
+
{
|
|
35
|
+
href: info.trackingUrl,
|
|
36
|
+
target: "_blank",
|
|
37
|
+
rel: "noopener noreferrer",
|
|
38
|
+
className: "text-blue-600 hover:underline",
|
|
39
|
+
children: info.trackingNumber
|
|
40
|
+
}
|
|
41
|
+
) : info.trackingNumber : "N/A",
|
|
42
|
+
" ",
|
|
43
|
+
/* @__PURE__ */ jsx(
|
|
44
|
+
"a",
|
|
45
|
+
{
|
|
46
|
+
href: info.labelUrl,
|
|
47
|
+
target: "_blank",
|
|
48
|
+
rel: "noopener noreferrer",
|
|
49
|
+
className: "text-blue-600 hover:underline",
|
|
50
|
+
download: true,
|
|
51
|
+
children: "Download Label"
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
] }) }) })
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
idx
|
|
58
|
+
))
|
|
59
|
+
] });
|
|
60
|
+
};
|
|
61
|
+
defineWidgetConfig({
|
|
62
|
+
zone: "order.details.after"
|
|
63
|
+
});
|
|
64
|
+
const sdk = new Medusa({
|
|
65
|
+
baseUrl: "/",
|
|
66
|
+
debug: false,
|
|
67
|
+
auth: { type: "session" }
|
|
68
|
+
});
|
|
69
|
+
const initialState = {
|
|
70
|
+
is_enabled: true,
|
|
71
|
+
client_id: "",
|
|
72
|
+
client_secret: "",
|
|
73
|
+
account_number: "",
|
|
74
|
+
is_sandbox: false,
|
|
75
|
+
enable_logs: false,
|
|
76
|
+
weight_unit_of_measure: ""
|
|
77
|
+
};
|
|
78
|
+
const queryClient = new QueryClient();
|
|
79
|
+
const FedexSettingsPageInner = () => {
|
|
80
|
+
const { data, isLoading } = useQuery({
|
|
81
|
+
queryFn: () => sdk.client.fetch("/admin/fedex"),
|
|
82
|
+
queryKey: ["fedex-config"]
|
|
83
|
+
});
|
|
84
|
+
const [form, setForm] = useState(initialState);
|
|
85
|
+
const [setTouched] = useState({});
|
|
86
|
+
const [alert, setAlert] = useState(null);
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
if (data) {
|
|
89
|
+
setForm((prev) => ({
|
|
90
|
+
...prev,
|
|
91
|
+
...data
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
}, [data]);
|
|
95
|
+
const handleChange = (e) => {
|
|
96
|
+
const { name, value, type, checked } = e.target;
|
|
97
|
+
setForm((prev) => ({
|
|
98
|
+
...prev,
|
|
99
|
+
[name]: type === "checkbox" ? checked : value
|
|
100
|
+
}));
|
|
101
|
+
setTouched((prev) => ({ ...prev, [name]: true }));
|
|
102
|
+
};
|
|
103
|
+
const handleSelect = (value) => {
|
|
104
|
+
setForm((prev) => ({
|
|
105
|
+
...prev,
|
|
106
|
+
weight_unit_of_measure: value
|
|
107
|
+
}));
|
|
108
|
+
setTouched((prev) => ({ ...prev, weight_unit_of_measure: true }));
|
|
109
|
+
};
|
|
110
|
+
const isValid = form.client_id.trim() && form.client_secret.trim() && form.account_number.trim() && form.weight_unit_of_measure;
|
|
111
|
+
if (isLoading) {
|
|
112
|
+
return /* @__PURE__ */ jsx(Container, { className: "divide-y p-0", children: /* @__PURE__ */ jsx("div", { className: "px-6 py-4", children: "Loading..." }) });
|
|
113
|
+
}
|
|
114
|
+
async function handleSave(event) {
|
|
115
|
+
var _a, _b;
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
try {
|
|
118
|
+
await sdk.client.fetch("/admin/fedex", {
|
|
119
|
+
method: "POST",
|
|
120
|
+
body: form,
|
|
121
|
+
headers: {
|
|
122
|
+
"Content-Type": "application/json"
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
setAlert({ type: "success", message: "FedEx settings saved successfully." });
|
|
126
|
+
(_a = document.querySelector(".alert")) == null ? void 0 : _a.scrollIntoView({ behavior: "smooth" });
|
|
127
|
+
} catch (error) {
|
|
128
|
+
setAlert({ type: "error", message: "Failed to save FedEx settings." });
|
|
129
|
+
(_b = document.querySelector(".alert")) == null ? void 0 : _b.scrollIntoView({ behavior: "smooth" });
|
|
130
|
+
console.error(error);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", children: [
|
|
134
|
+
alert && /* @__PURE__ */ jsx("div", { className: "px-6 py-2", children: /* @__PURE__ */ jsx(Alert, { variant: alert.type === "success" ? "success" : "error", dismissible: true, children: alert.message }) }),
|
|
135
|
+
/* @__PURE__ */ jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx(
|
|
136
|
+
"svg",
|
|
137
|
+
{
|
|
138
|
+
height: "120",
|
|
139
|
+
width: "120",
|
|
140
|
+
viewBox: "0 0 512 512",
|
|
141
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
142
|
+
children: /* @__PURE__ */ jsx("g", { id: "_x31_25-fedex", children: /* @__PURE__ */ jsxs("g", { children: [
|
|
143
|
+
/* @__PURE__ */ jsx(
|
|
144
|
+
"path",
|
|
145
|
+
{
|
|
146
|
+
d: "M251.777,183.644v61.553h-0.36c-6.899-7.906-15.452-10.637-25.443-10.637\n c-20.412,0-35.793,13.943-41.184,32.271c-12.937-42.694-70.006-41.4-87.615-10.063v-17.393H57.787v-18.831h43.125v-29.541H22.568\n V323.54h35.219v-55.703h35.147c-1.079,4.097-1.653,8.483-1.653,13.081c0,52.542,73.744,65.692,93.58,17.034h-30.187\n c-10.566,15.022-32.919,6.396-32.919-10.493h61.453c2.659,21.921,19.694,40.897,43.197,40.897\n c10.134,0,19.407-4.959,25.084-13.371h0.359v8.555h44.717v-19.896v-30v-30v-40l0.002-20H251.777z M122.688,268.123\n c4.386-18.902,29.973-18.398,33.423,0H122.688z M232.946,303.272c-24.869,0-24.438-45.139,0-45.139\n C256.377,258.134,257.742,303.272,232.946,303.272z",
|
|
147
|
+
fill: "#442E8C"
|
|
148
|
+
}
|
|
149
|
+
),
|
|
150
|
+
/* @__PURE__ */ jsx(
|
|
151
|
+
"polygon",
|
|
152
|
+
{
|
|
153
|
+
points: "375.345,323.332 296.156,323.332 296.156,183.661 375.848,183.661 375.848,208.995\n 328.787,208.995 328.787,237.432 375.345,237.432 375.345,261.004 328.283,261.004 328.283,298.503 375.345,298.503\n 375.345,323.332",
|
|
154
|
+
fill: "#F47624"
|
|
155
|
+
}
|
|
156
|
+
),
|
|
157
|
+
/* @__PURE__ */ jsx(
|
|
158
|
+
"polygon",
|
|
159
|
+
{
|
|
160
|
+
points: "433.393,298.585 411.496,323.333 379.537,323.333 417.789,280.382 379.537,237.429\n 412.84,237.429 435.154,262.01 456.631,237.429 488.928,237.429 450.844,280.215 489.432,323.333 455.456,323.333\n 433.393,298.585",
|
|
161
|
+
fill: "#F47624"
|
|
162
|
+
}
|
|
163
|
+
)
|
|
164
|
+
] }) })
|
|
165
|
+
}
|
|
166
|
+
) }),
|
|
167
|
+
/* @__PURE__ */ jsx("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsxs("p", { className: "block", children: [
|
|
168
|
+
"You can eventually configure the FedEx credentials on the medusa-config.ts file as described on ",
|
|
169
|
+
/* @__PURE__ */ jsx("a", { href: "https://github.com/Imagination-Media/medusa-fedex-fulfillment?tab=readme-ov-file#installation", target: "_blank", rel: "noopener noreferrer", className: "text-blue-600 hover:underline font-bold", children: "the documentation." }),
|
|
170
|
+
" If the credentials are set up here they will be used over the medusa-config.ts file."
|
|
171
|
+
] }) }),
|
|
172
|
+
/* @__PURE__ */ jsxs("form", { className: "flex flex-col gap-y-6 px-6 py-4", children: [
|
|
173
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
174
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
175
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "is_enabled", children: "Enabled" }),
|
|
176
|
+
/* @__PURE__ */ jsx(
|
|
177
|
+
Switch,
|
|
178
|
+
{
|
|
179
|
+
id: "is_enabled",
|
|
180
|
+
name: "is_enabled",
|
|
181
|
+
checked: form.is_enabled,
|
|
182
|
+
onCheckedChange: (checked) => setForm((prev) => ({ ...prev, is_enabled: checked }))
|
|
183
|
+
}
|
|
184
|
+
)
|
|
185
|
+
] }),
|
|
186
|
+
/* @__PURE__ */ jsx(Hint, { className: "mb-2 mt-1", children: "Enable or disable the FedEx integration." })
|
|
187
|
+
] }),
|
|
188
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
189
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "client_id", children: "Client ID" }),
|
|
190
|
+
/* @__PURE__ */ jsx(Hint, { className: "mt-1 block pb-1", children: "Your FedEx API Client ID for authentication." }),
|
|
191
|
+
/* @__PURE__ */ jsx(
|
|
192
|
+
Input,
|
|
193
|
+
{
|
|
194
|
+
id: "client_id",
|
|
195
|
+
name: "client_id",
|
|
196
|
+
className: "mt-1",
|
|
197
|
+
autoComplete: "off",
|
|
198
|
+
value: form.client_id,
|
|
199
|
+
onChange: handleChange,
|
|
200
|
+
required: true
|
|
201
|
+
}
|
|
202
|
+
)
|
|
203
|
+
] }),
|
|
204
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
205
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "client_secret", children: "Client Secret" }),
|
|
206
|
+
/* @__PURE__ */ jsx(Hint, { className: "mt-1 block pb-1", children: "Your FedEx API Client Secret for authentication." }),
|
|
207
|
+
/* @__PURE__ */ jsx(
|
|
208
|
+
Input,
|
|
209
|
+
{
|
|
210
|
+
id: "client_secret",
|
|
211
|
+
name: "client_secret",
|
|
212
|
+
type: "password",
|
|
213
|
+
className: "mt-1",
|
|
214
|
+
autoComplete: "off",
|
|
215
|
+
value: form.client_secret,
|
|
216
|
+
onChange: handleChange,
|
|
217
|
+
required: true
|
|
218
|
+
}
|
|
219
|
+
)
|
|
220
|
+
] }),
|
|
221
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
222
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "account_number", children: "Account Number" }),
|
|
223
|
+
/* @__PURE__ */ jsx(Hint, { className: "mt-1 block pb-1", children: "Your FedEx account number used for shipments." }),
|
|
224
|
+
/* @__PURE__ */ jsx(
|
|
225
|
+
Input,
|
|
226
|
+
{
|
|
227
|
+
id: "account_number",
|
|
228
|
+
name: "account_number",
|
|
229
|
+
className: "mt-1",
|
|
230
|
+
autoComplete: "off",
|
|
231
|
+
value: form.account_number,
|
|
232
|
+
onChange: handleChange,
|
|
233
|
+
required: true
|
|
234
|
+
}
|
|
235
|
+
)
|
|
236
|
+
] }),
|
|
237
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
238
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
239
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "is_sandbox", children: "Sandbox Mode" }),
|
|
240
|
+
/* @__PURE__ */ jsx(
|
|
241
|
+
Switch,
|
|
242
|
+
{
|
|
243
|
+
id: "is_sandbox",
|
|
244
|
+
name: "is_sandbox",
|
|
245
|
+
checked: form.is_sandbox,
|
|
246
|
+
onCheckedChange: (checked) => setForm((prev) => ({ ...prev, is_sandbox: checked }))
|
|
247
|
+
}
|
|
248
|
+
)
|
|
249
|
+
] }),
|
|
250
|
+
/* @__PURE__ */ jsx(Hint, { className: "mb-2 mt-1", children: "Enable to use FedEx sandbox environment for testing." })
|
|
251
|
+
] }),
|
|
252
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
253
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
254
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "enable_logs", children: "Enable Logs" }),
|
|
255
|
+
/* @__PURE__ */ jsx(
|
|
256
|
+
Switch,
|
|
257
|
+
{
|
|
258
|
+
id: "enable_logs",
|
|
259
|
+
name: "enable_logs",
|
|
260
|
+
checked: form.enable_logs,
|
|
261
|
+
onCheckedChange: (checked) => setForm((prev) => ({ ...prev, enable_logs: checked }))
|
|
262
|
+
}
|
|
263
|
+
)
|
|
264
|
+
] }),
|
|
265
|
+
/* @__PURE__ */ jsx(Hint, { className: "mb-2 mt-1", children: "Enable logging for debugging FedEx requests and responses." })
|
|
266
|
+
] }),
|
|
267
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
268
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "weight_unit_of_measure", children: "Weight Unit of Measure" }),
|
|
269
|
+
/* @__PURE__ */ jsx(Hint, { className: "mt-1 block pb-1", children: "Select the unit of measure for package weights (LB or KG)." }),
|
|
270
|
+
/* @__PURE__ */ jsxs(
|
|
271
|
+
Select,
|
|
272
|
+
{
|
|
273
|
+
value: form.weight_unit_of_measure,
|
|
274
|
+
onValueChange: handleSelect,
|
|
275
|
+
required: true,
|
|
276
|
+
children: [
|
|
277
|
+
/* @__PURE__ */ jsx(Select.Trigger, { children: /* @__PURE__ */ jsx(Select.Value, { placeholder: "Select a weight unit" }) }),
|
|
278
|
+
/* @__PURE__ */ jsxs(Select.Content, { children: [
|
|
279
|
+
/* @__PURE__ */ jsx(Select.Item, { value: "LB", children: "LB" }, "LB"),
|
|
280
|
+
/* @__PURE__ */ jsx(Select.Item, { value: "KG", children: "KG" }, "KG")
|
|
281
|
+
] })
|
|
282
|
+
]
|
|
283
|
+
}
|
|
284
|
+
)
|
|
285
|
+
] }),
|
|
286
|
+
/* @__PURE__ */ jsx(Button, { type: "button", onClick: handleSave, disabled: !isValid, className: "mt-4 w-fit", children: "Save" })
|
|
287
|
+
] })
|
|
288
|
+
] });
|
|
289
|
+
};
|
|
290
|
+
const config = defineRouteConfig({
|
|
291
|
+
label: "FedEx"
|
|
292
|
+
});
|
|
293
|
+
const FedexSettingsPage = () => /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children: /* @__PURE__ */ jsx(FedexSettingsPageInner, {}) });
|
|
294
|
+
const widgetModule = { widgets: [
|
|
295
|
+
{
|
|
296
|
+
Component: FedexWidget,
|
|
297
|
+
zone: ["order.details.after"]
|
|
298
|
+
}
|
|
299
|
+
] };
|
|
300
|
+
const routeModule = {
|
|
301
|
+
routes: [
|
|
302
|
+
{
|
|
303
|
+
Component: FedexSettingsPage,
|
|
304
|
+
path: "/settings/fedex"
|
|
305
|
+
}
|
|
306
|
+
]
|
|
307
|
+
};
|
|
308
|
+
const menuItemModule = {
|
|
309
|
+
menuItems: [
|
|
310
|
+
{
|
|
311
|
+
label: config.label,
|
|
312
|
+
icon: void 0,
|
|
313
|
+
path: "/settings/fedex",
|
|
314
|
+
nested: void 0,
|
|
315
|
+
rank: void 0,
|
|
316
|
+
translationNs: void 0
|
|
317
|
+
}
|
|
318
|
+
]
|
|
319
|
+
};
|
|
320
|
+
const formModule = { customFields: {} };
|
|
321
|
+
const displayModule = {
|
|
322
|
+
displays: {}
|
|
323
|
+
};
|
|
324
|
+
const i18nModule = { resources: {} };
|
|
325
|
+
const plugin = {
|
|
326
|
+
widgetModule,
|
|
327
|
+
routeModule,
|
|
328
|
+
menuItemModule,
|
|
329
|
+
formModule,
|
|
330
|
+
displayModule,
|
|
331
|
+
i18nModule
|
|
332
|
+
};
|
|
333
|
+
export {
|
|
334
|
+
plugin as default
|
|
335
|
+
};
|