order-management 0.0.17 → 0.0.19

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.
Files changed (48) hide show
  1. package/.medusa/server/src/admin/index.js +644 -11
  2. package/.medusa/server/src/admin/index.mjs +644 -11
  3. package/.medusa/server/src/api/admin/swaps/[id]/approve/route.js +74 -0
  4. package/.medusa/server/src/api/admin/swaps/[id]/process-payment/route.js +152 -0
  5. package/.medusa/server/src/api/admin/swaps/[id]/reject/route.js +43 -0
  6. package/.medusa/server/src/api/admin/swaps/[id]/route.js +70 -0
  7. package/.medusa/server/src/api/admin/swaps/[id]/status/route.js +45 -0
  8. package/.medusa/server/src/api/admin/swaps/[id]/sync/route.js +148 -0
  9. package/.medusa/server/src/api/admin/swaps/route.js +51 -0
  10. package/.medusa/server/src/api/admin/swaps/validators.js +22 -0
  11. package/.medusa/server/src/api/store/guest-orders/[id]/invoice/route.js +17 -5
  12. package/.medusa/server/src/api/store/guest-orders/[id]/returns/route.js +17 -5
  13. package/.medusa/server/src/api/store/guest-orders/[id]/route.js +17 -5
  14. package/.medusa/server/src/api/store/guest-orders/route.js +17 -5
  15. package/.medusa/server/src/api/store/orders/[order_id]/swaps/route.js +107 -0
  16. package/.medusa/server/src/api/store/otp/request/route.js +12 -5
  17. package/.medusa/server/src/api/store/swaps/[id]/cancel/route.js +64 -0
  18. package/.medusa/server/src/api/store/swaps/[id]/route.js +112 -0
  19. package/.medusa/server/src/api/store/swaps/route.js +117 -0
  20. package/.medusa/server/src/helpers/index.js +18 -0
  21. package/.medusa/server/src/helpers/swaps.js +88 -0
  22. package/.medusa/server/src/modules/swap/index.js +13 -0
  23. package/.medusa/server/src/modules/swap/migrations/Migration20260121164326.js +49 -0
  24. package/.medusa/server/src/modules/swap/models/swap.js +21 -0
  25. package/.medusa/server/src/modules/swap/service.js +224 -0
  26. package/.medusa/server/src/services/otp-service.js +16 -5
  27. package/.medusa/server/src/subscribers/send-order-email.js +27 -8
  28. package/.medusa/server/src/workflows/index.js +8 -2
  29. package/.medusa/server/src/workflows/steps/swap/calculate-difference-step.js +56 -0
  30. package/.medusa/server/src/workflows/steps/swap/create-medusa-exchange-step.js +71 -0
  31. package/.medusa/server/src/workflows/steps/swap/create-medusa-return-step.js +79 -0
  32. package/.medusa/server/src/workflows/steps/swap/create-swap-step.js +29 -0
  33. package/.medusa/server/src/workflows/steps/swap/handle-payment-difference-step.js +102 -0
  34. package/.medusa/server/src/workflows/steps/swap/index.js +26 -0
  35. package/.medusa/server/src/workflows/steps/swap/retrieve-swap-step.js +26 -0
  36. package/.medusa/server/src/workflows/steps/swap/sync-medusa-status-step.js +132 -0
  37. package/.medusa/server/src/workflows/steps/swap/update-swap-status-step.js +25 -0
  38. package/.medusa/server/src/workflows/steps/swap/validate-eligibility-step.js +25 -0
  39. package/.medusa/server/src/workflows/steps/swap/validate-order-step.js +69 -0
  40. package/.medusa/server/src/workflows/steps/swap/validate-swap-items-step.js +41 -0
  41. package/.medusa/server/src/workflows/swaps/approve-swap-workflow.js +22 -0
  42. package/.medusa/server/src/workflows/swaps/create-swap-workflow.js +52 -0
  43. package/.medusa/server/src/workflows/swaps/execute-swap-workflow.js +36 -0
  44. package/.medusa/server/src/workflows/swaps/types.js +3 -0
  45. package/.medusa/server/src/workflows/swaps/update-swap-status-workflow.js +23 -0
  46. package/README.md +208 -0
  47. package/package.json +1 -1
  48. package/.medusa/server/src/utils/resolve-options.js +0 -101
@@ -4,7 +4,7 @@ import { defineRouteConfig } from "@medusajs/admin-sdk";
4
4
  import { Container, Heading, Text, Button, Input, Badge } from "@medusajs/ui";
5
5
  import { ArrowPath, ArrowLeft } from "@medusajs/icons";
6
6
  import { useNavigate, useParams } from "react-router-dom";
7
- const useDebounce = (value, delay) => {
7
+ const useDebounce$1 = (value, delay) => {
8
8
  const [debouncedValue, setDebouncedValue] = useState(value);
9
9
  useEffect(() => {
10
10
  const handler = setTimeout(() => setDebouncedValue(value), delay);
@@ -12,7 +12,7 @@ const useDebounce = (value, delay) => {
12
12
  }, [value, delay]);
13
13
  return debouncedValue;
14
14
  };
15
- const getStatusBadgeClass$1 = (status) => {
15
+ const getStatusBadgeClass$3 = (status) => {
16
16
  const statusLower = status.toLowerCase();
17
17
  if (statusLower === "requested") {
18
18
  return "bg-ui-tag-orange-bg text-ui-tag-orange-text";
@@ -36,7 +36,7 @@ const ReturnsPage = () => {
36
36
  const [items, setItems] = useState([]);
37
37
  const [statusFilter, setStatusFilter] = useState("all");
38
38
  const [searchQuery, setSearchQuery] = useState("");
39
- const debouncedSearchQuery = useDebounce(searchQuery, 300);
39
+ const debouncedSearchQuery = useDebounce$1(searchQuery, 300);
40
40
  const [isLoading, setIsLoading] = useState(true);
41
41
  const [isFetchingMore, setIsFetchingMore] = useState(false);
42
42
  const [error, setError] = useState(null);
@@ -164,7 +164,7 @@ const ReturnsPage = () => {
164
164
  Badge,
165
165
  {
166
166
  size: "2xsmall",
167
- className: `uppercase ${getStatusBadgeClass$1(returnOrder.status)}`,
167
+ className: `uppercase ${getStatusBadgeClass$3(returnOrder.status)}`,
168
168
  children: returnOrder.status.replace(/_/g, " ")
169
169
  }
170
170
  ) }),
@@ -213,11 +213,222 @@ const ReturnsPage = () => {
213
213
  ) }) : null
214
214
  ] }) });
215
215
  };
216
- const config$1 = defineRouteConfig({
216
+ const config$3 = defineRouteConfig({
217
217
  label: "Return Orders",
218
218
  icon: ArrowPath
219
219
  });
220
- const getStatusBadgeClass = (status) => {
220
+ const useDebounce = (value, delay) => {
221
+ const [debouncedValue, setDebouncedValue] = useState(value);
222
+ useEffect(() => {
223
+ const handler = setTimeout(() => setDebouncedValue(value), delay);
224
+ return () => clearTimeout(handler);
225
+ }, [value, delay]);
226
+ return debouncedValue;
227
+ };
228
+ const getStatusBadgeClass$2 = (status) => {
229
+ const statusLower = status.toLowerCase();
230
+ if (statusLower === "requested") {
231
+ return "bg-ui-tag-orange-bg text-ui-tag-orange-text";
232
+ }
233
+ if (statusLower === "approved") {
234
+ return "bg-ui-tag-blue-bg text-ui-tag-blue-text";
235
+ }
236
+ if (statusLower === "rejected") {
237
+ return "bg-ui-tag-red-bg text-ui-tag-red-text";
238
+ }
239
+ if (statusLower === "completed") {
240
+ return "bg-ui-tag-green-bg text-ui-tag-green-text";
241
+ }
242
+ if (statusLower === "cancelled") {
243
+ return "bg-ui-tag-grey-bg text-ui-tag-grey-text";
244
+ }
245
+ return "bg-ui-tag-purple-bg text-ui-tag-purple-text";
246
+ };
247
+ const SwapsPage = () => {
248
+ const navigate = useNavigate();
249
+ const [items, setItems] = useState([]);
250
+ const [statusFilter, setStatusFilter] = useState("all");
251
+ const [searchQuery, setSearchQuery] = useState("");
252
+ const debouncedSearchQuery = useDebounce(searchQuery, 300);
253
+ const [isLoading, setIsLoading] = useState(true);
254
+ const [isFetchingMore, setIsFetchingMore] = useState(false);
255
+ const [error, setError] = useState(null);
256
+ const [offset, setOffset] = useState(0);
257
+ const [count, setCount] = useState(0);
258
+ const limit = 50;
259
+ const loadSwaps = useCallback(
260
+ async (nextOffset, replace = false) => {
261
+ var _a;
262
+ try {
263
+ if (replace) {
264
+ setIsLoading(true);
265
+ } else {
266
+ setIsFetchingMore(true);
267
+ }
268
+ setError(null);
269
+ const params = new URLSearchParams();
270
+ params.set("limit", String(limit));
271
+ params.set("offset", String(nextOffset));
272
+ if (statusFilter !== "all") {
273
+ params.set("status", statusFilter);
274
+ }
275
+ if (debouncedSearchQuery.trim()) {
276
+ params.set("order_id", debouncedSearchQuery.trim());
277
+ }
278
+ const response = await fetch(
279
+ `/admin/swaps?${params.toString()}`,
280
+ { credentials: "include" }
281
+ );
282
+ if (!response.ok) {
283
+ const message = await response.text();
284
+ throw new Error(message || "Unable to load swaps");
285
+ }
286
+ const payload = await response.json();
287
+ setCount(payload.count ?? 0);
288
+ setOffset(nextOffset + (((_a = payload.swaps) == null ? void 0 : _a.length) ?? 0));
289
+ setItems(
290
+ (prev) => replace ? payload.swaps ?? [] : [...prev, ...payload.swaps ?? []]
291
+ );
292
+ } catch (loadError) {
293
+ const message = loadError instanceof Error ? loadError.message : "Unable to load swaps";
294
+ setError(message);
295
+ } finally {
296
+ setIsLoading(false);
297
+ setIsFetchingMore(false);
298
+ }
299
+ },
300
+ [statusFilter, debouncedSearchQuery]
301
+ );
302
+ useEffect(() => {
303
+ void loadSwaps(0, true);
304
+ }, [statusFilter, debouncedSearchQuery, loadSwaps]);
305
+ const hasMore = useMemo(() => offset < count, [offset, count]);
306
+ const availableStatuses = useMemo(() => {
307
+ const statuses = /* @__PURE__ */ new Set();
308
+ items.forEach((item) => statuses.add(item.status));
309
+ return Array.from(statuses).sort();
310
+ }, [items]);
311
+ return /* @__PURE__ */ jsx("div", { className: "w-full p-6", children: /* @__PURE__ */ jsxs(Container, { className: "mx-auto flex w-full max-w-7xl flex-col gap-6 p-6", children: [
312
+ /* @__PURE__ */ jsxs("header", { className: "flex flex-col gap-3 md:flex-row md:items-center md:justify-between", children: [
313
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
314
+ /* @__PURE__ */ jsx(Heading, { level: "h1", children: "Swaps" }),
315
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "View and manage all customer swap requests" })
316
+ ] }),
317
+ /* @__PURE__ */ jsx(Button, { variant: "primary", onClick: () => loadSwaps(0, true), children: "Refresh" })
318
+ ] }),
319
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3 md:flex-row md:items-center md:justify-between", children: [
320
+ /* @__PURE__ */ jsx(
321
+ Input,
322
+ {
323
+ placeholder: "Search by swap ID or order ID",
324
+ value: searchQuery,
325
+ onChange: (event) => setSearchQuery(event.target.value),
326
+ className: "md:max-w-sm"
327
+ }
328
+ ),
329
+ /* @__PURE__ */ jsx("div", { className: "flex gap-3", children: /* @__PURE__ */ jsxs(
330
+ "select",
331
+ {
332
+ value: statusFilter,
333
+ onChange: (event) => setStatusFilter(event.target.value),
334
+ className: "h-9 rounded-md border border-ui-border-base bg-transparent px-3 text-sm text-ui-fg-base outline-none transition focus:ring-2 focus:ring-ui-fg-interactive md:max-w-xs",
335
+ children: [
336
+ /* @__PURE__ */ jsx("option", { value: "all", children: "All Statuses" }),
337
+ availableStatuses.map((status) => /* @__PURE__ */ jsx("option", { value: status, children: status.replace(/_/g, " ").toUpperCase() }, status))
338
+ ]
339
+ }
340
+ ) })
341
+ ] }),
342
+ error ? /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-strong p-6 text-center", children: [
343
+ /* @__PURE__ */ jsx(Text, { weight: "plus", className: "text-ui-fg-error", children: error }),
344
+ /* @__PURE__ */ jsx("div", { className: "mt-4 flex justify-center", children: /* @__PURE__ */ jsx(
345
+ Button,
346
+ {
347
+ variant: "secondary",
348
+ onClick: () => loadSwaps(0, true),
349
+ children: "Try again"
350
+ }
351
+ ) })
352
+ ] }) : null,
353
+ isLoading ? /* @__PURE__ */ jsx("div", { className: "flex justify-center py-16", children: /* @__PURE__ */ jsx(Text, { children: "Loading swaps..." }) }) : items.length === 0 ? /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-dashed border-ui-border-strong p-10 text-center", children: [
354
+ /* @__PURE__ */ jsx(Heading, { level: "h3", className: "text-xl", children: "No swaps yet" }),
355
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "mt-2 text-ui-fg-subtle", children: "Swap requests created by customers will appear here." })
356
+ ] }) : /* @__PURE__ */ jsx("div", { className: "overflow-hidden rounded-xl border border-ui-border-base", children: /* @__PURE__ */ jsxs("table", { className: "min-w-full divide-y divide-ui-border-base", children: [
357
+ /* @__PURE__ */ jsx("thead", { className: "bg-ui-bg-subtle", children: /* @__PURE__ */ jsxs("tr", { children: [
358
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Swap ID" }),
359
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Order ID" }),
360
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Status" }),
361
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Difference Due" }),
362
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Created" }),
363
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Actions" })
364
+ ] }) }),
365
+ /* @__PURE__ */ jsx("tbody", { className: "divide-y divide-ui-border-subtle", children: items.map((swap) => /* @__PURE__ */ jsxs(
366
+ "tr",
367
+ {
368
+ className: "hover:bg-ui-bg-subtle/60 cursor-pointer",
369
+ onClick: () => navigate(`/swaps/${swap.id}`),
370
+ children: [
371
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 font-medium text-ui-fg-base", children: /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-0.5", children: /* @__PURE__ */ jsx("span", { children: swap.id }) }) }),
372
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 text-ui-fg-subtle", children: swap.order_id }),
373
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4", children: /* @__PURE__ */ jsx(
374
+ Badge,
375
+ {
376
+ size: "2xsmall",
377
+ className: `uppercase ${getStatusBadgeClass$2(swap.status)}`,
378
+ children: swap.status.replace(/_/g, " ")
379
+ }
380
+ ) }),
381
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 text-ui-fg-subtle", children: (() => {
382
+ const amount = swap.difference_due;
383
+ if (amount == null || amount === void 0) {
384
+ return "—";
385
+ }
386
+ const displayAmount = Number(amount) / 100;
387
+ const currency = swap.currency_code || "$";
388
+ const sign = displayAmount >= 0 ? "+" : "";
389
+ return `${sign}${currency}${displayAmount.toFixed(2)}`;
390
+ })() }),
391
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 text-ui-fg-subtle", children: new Date(swap.created_at).toLocaleDateString("en-US", {
392
+ year: "numeric",
393
+ month: "short",
394
+ day: "numeric",
395
+ hour: "numeric",
396
+ minute: "2-digit",
397
+ hour12: true
398
+ }) }),
399
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4", children: /* @__PURE__ */ jsx(
400
+ Button,
401
+ {
402
+ variant: "transparent",
403
+ size: "small",
404
+ onClick: (e) => {
405
+ e.stopPropagation();
406
+ navigate(`/swaps/${swap.id}`);
407
+ },
408
+ children: "View"
409
+ }
410
+ ) })
411
+ ]
412
+ },
413
+ swap.id
414
+ )) })
415
+ ] }) }),
416
+ hasMore ? /* @__PURE__ */ jsx("div", { className: "flex justify-center", children: /* @__PURE__ */ jsx(
417
+ Button,
418
+ {
419
+ variant: "secondary",
420
+ isLoading: isFetchingMore,
421
+ onClick: () => loadSwaps(offset, false),
422
+ children: "Load more"
423
+ }
424
+ ) }) : null
425
+ ] }) });
426
+ };
427
+ const config$2 = defineRouteConfig({
428
+ label: "Swaps",
429
+ icon: ArrowPath
430
+ });
431
+ const getStatusBadgeClass$1 = (status) => {
221
432
  const statusLower = status.toLowerCase();
222
433
  if (statusLower === "requested") {
223
434
  return "bg-ui-tag-orange-bg text-ui-tag-orange-text";
@@ -355,7 +566,7 @@ const ReturnDetailPage = () => {
355
566
  Badge,
356
567
  {
357
568
  size: "small",
358
- className: `uppercase ${getStatusBadgeClass(returnOrder.status)}`,
569
+ className: `uppercase ${getStatusBadgeClass$1(returnOrder.status)}`,
359
570
  children: returnOrder.status.replace("_", " ")
360
571
  }
361
572
  )
@@ -439,7 +650,7 @@ const ReturnDetailPage = () => {
439
650
  Badge,
440
651
  {
441
652
  size: "2xsmall",
442
- className: `uppercase ${getStatusBadgeClass(entry.to)}`,
653
+ className: `uppercase ${getStatusBadgeClass$1(entry.to)}`,
443
654
  children: entry.to.replace("_", " ")
444
655
  }
445
656
  ),
@@ -526,10 +737,412 @@ const ReturnDetailPage = () => {
526
737
  ] })
527
738
  ] }) });
528
739
  };
529
- const config = defineRouteConfig({
740
+ const config$1 = defineRouteConfig({
530
741
  label: "Return Order Details",
531
742
  icon: ArrowPath
532
743
  });
744
+ const getStatusBadgeClass = (status) => {
745
+ const statusLower = status.toLowerCase();
746
+ if (statusLower === "requested") {
747
+ return "bg-ui-tag-orange-bg text-ui-tag-orange-text";
748
+ }
749
+ if (statusLower === "approved") {
750
+ return "bg-ui-tag-blue-bg text-ui-tag-blue-text";
751
+ }
752
+ if (statusLower === "rejected") {
753
+ return "bg-ui-tag-red-bg text-ui-tag-red-text";
754
+ }
755
+ if (statusLower === "completed") {
756
+ return "bg-ui-tag-green-bg text-ui-tag-green-text";
757
+ }
758
+ if (statusLower === "cancelled") {
759
+ return "bg-ui-tag-grey-bg text-ui-tag-grey-text";
760
+ }
761
+ return "bg-ui-tag-purple-bg text-ui-tag-purple-text";
762
+ };
763
+ const SwapDetailPage = () => {
764
+ var _a, _b;
765
+ const navigate = useNavigate();
766
+ const { id } = useParams();
767
+ const [swap, setSwap] = useState(null);
768
+ const [order, setOrder] = useState(null);
769
+ const [selectedStatus, setSelectedStatus] = useState("");
770
+ const [isLoading, setIsLoading] = useState(true);
771
+ const [isUpdating, setIsUpdating] = useState(false);
772
+ const [isApproving, setIsApproving] = useState(false);
773
+ const [isRejecting, setIsRejecting] = useState(false);
774
+ const [error, setError] = useState(null);
775
+ const [updateError, setUpdateError] = useState(null);
776
+ const [updateSuccess, setUpdateSuccess] = useState(false);
777
+ const availableStatuses = [
778
+ "requested",
779
+ "approved",
780
+ "rejected",
781
+ "return_started",
782
+ "return_shipped",
783
+ "return_received",
784
+ "new_items_shipped",
785
+ "completed",
786
+ "cancelled"
787
+ ];
788
+ useEffect(() => {
789
+ if (!id) {
790
+ navigate("/swaps");
791
+ return;
792
+ }
793
+ const loadSwap = async () => {
794
+ try {
795
+ setIsLoading(true);
796
+ setError(null);
797
+ const response = await fetch(`/admin/swaps/${id}`, {
798
+ credentials: "include"
799
+ });
800
+ if (!response.ok) {
801
+ const message = await response.text();
802
+ throw new Error(message || "Unable to load swap");
803
+ }
804
+ const payload = await response.json();
805
+ setSwap(payload.swap);
806
+ setOrder(payload.order || null);
807
+ setSelectedStatus("");
808
+ } catch (loadError) {
809
+ const message = loadError instanceof Error ? loadError.message : "Unable to load swap";
810
+ setError(message);
811
+ } finally {
812
+ setIsLoading(false);
813
+ }
814
+ };
815
+ void loadSwap();
816
+ }, [id, navigate]);
817
+ const handleStatusUpdate = async () => {
818
+ if (!id || !selectedStatus) {
819
+ return;
820
+ }
821
+ try {
822
+ setIsUpdating(true);
823
+ setUpdateError(null);
824
+ setUpdateSuccess(false);
825
+ const response = await fetch(`/admin/swaps/${id}/status`, {
826
+ method: "POST",
827
+ headers: {
828
+ "Content-Type": "application/json"
829
+ },
830
+ credentials: "include",
831
+ body: JSON.stringify({ status: selectedStatus })
832
+ });
833
+ if (!response.ok) {
834
+ const message = await response.text();
835
+ throw new Error(message || "Unable to update status");
836
+ }
837
+ const payload = await response.json();
838
+ setSwap(payload.swap);
839
+ setSelectedStatus("");
840
+ setUpdateSuccess(true);
841
+ setTimeout(() => setUpdateSuccess(false), 3e3);
842
+ const detailResponse = await fetch(`/admin/swaps/${id}`, {
843
+ credentials: "include"
844
+ });
845
+ if (detailResponse.ok) {
846
+ const detailPayload = await detailResponse.json();
847
+ setSwap(detailPayload.swap);
848
+ setOrder(detailPayload.order || null);
849
+ }
850
+ } catch (updateErr) {
851
+ const message = updateErr instanceof Error ? updateErr.message : "Unable to update status";
852
+ setUpdateError(message);
853
+ } finally {
854
+ setIsUpdating(false);
855
+ }
856
+ };
857
+ const handleApprove = async () => {
858
+ if (!id) {
859
+ return;
860
+ }
861
+ try {
862
+ setIsApproving(true);
863
+ setUpdateError(null);
864
+ setUpdateSuccess(false);
865
+ const response = await fetch(`/admin/swaps/${id}/approve`, {
866
+ method: "POST",
867
+ headers: {
868
+ "Content-Type": "application/json"
869
+ },
870
+ credentials: "include"
871
+ });
872
+ if (!response.ok) {
873
+ const message = await response.text();
874
+ throw new Error(message || "Unable to approve swap");
875
+ }
876
+ const payload = await response.json();
877
+ setSwap(payload.swap);
878
+ setUpdateSuccess(true);
879
+ setTimeout(() => setUpdateSuccess(false), 3e3);
880
+ const detailResponse = await fetch(`/admin/swaps/${id}`, {
881
+ credentials: "include"
882
+ });
883
+ if (detailResponse.ok) {
884
+ const detailPayload = await response.json();
885
+ setSwap(detailPayload.swap);
886
+ setOrder(detailPayload.order || null);
887
+ }
888
+ } catch (updateErr) {
889
+ const message = updateErr instanceof Error ? updateErr.message : "Unable to approve swap";
890
+ setUpdateError(message);
891
+ } finally {
892
+ setIsApproving(false);
893
+ }
894
+ };
895
+ const handleReject = async () => {
896
+ if (!id) {
897
+ return;
898
+ }
899
+ try {
900
+ setIsRejecting(true);
901
+ setUpdateError(null);
902
+ setUpdateSuccess(false);
903
+ const response = await fetch(`/admin/swaps/${id}/reject`, {
904
+ method: "POST",
905
+ headers: {
906
+ "Content-Type": "application/json"
907
+ },
908
+ credentials: "include",
909
+ body: JSON.stringify({ reason: "Rejected by admin" })
910
+ });
911
+ if (!response.ok) {
912
+ const message = await response.text();
913
+ throw new Error(message || "Unable to reject swap");
914
+ }
915
+ const payload = await response.json();
916
+ setSwap(payload.swap);
917
+ setUpdateSuccess(true);
918
+ setTimeout(() => setUpdateSuccess(false), 3e3);
919
+ const detailResponse = await fetch(`/admin/swaps/${id}`, {
920
+ credentials: "include"
921
+ });
922
+ if (detailResponse.ok) {
923
+ const detailPayload = await response.json();
924
+ setSwap(detailPayload.swap);
925
+ setOrder(detailPayload.order || null);
926
+ }
927
+ } catch (updateErr) {
928
+ const message = updateErr instanceof Error ? updateErr.message : "Unable to reject swap";
929
+ setUpdateError(message);
930
+ } finally {
931
+ setIsRejecting(false);
932
+ }
933
+ };
934
+ if (isLoading) {
935
+ return /* @__PURE__ */ jsx("div", { className: "w-full p-6", children: /* @__PURE__ */ jsx(Container, { className: "mx-auto flex w-full max-w-5xl flex-col gap-6 p-6", children: /* @__PURE__ */ jsx("div", { className: "flex justify-center py-16", children: /* @__PURE__ */ jsx(Text, { children: "Loading swap..." }) }) }) });
936
+ }
937
+ if (error || !swap) {
938
+ return /* @__PURE__ */ jsx("div", { className: "w-full p-6", children: /* @__PURE__ */ jsx(Container, { className: "mx-auto flex w-full max-w-5xl flex-col gap-6 p-6", children: /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-strong p-6 text-center", children: [
939
+ /* @__PURE__ */ jsx(Text, { weight: "plus", className: "text-ui-fg-error", children: error || "Swap not found" }),
940
+ /* @__PURE__ */ jsx("div", { className: "mt-4 flex justify-center", children: /* @__PURE__ */ jsx(Button, { variant: "secondary", onClick: () => navigate("/swaps"), children: "Back to list" }) })
941
+ ] }) }) });
942
+ }
943
+ const statusHistory = ((_a = swap.metadata) == null ? void 0 : _a.status_history) || [];
944
+ return /* @__PURE__ */ jsx("div", { className: "w-full p-6", children: /* @__PURE__ */ jsxs(Container, { className: "mx-auto flex w-full max-w-5xl flex-col gap-6 p-6", children: [
945
+ /* @__PURE__ */ jsxs("header", { className: "flex flex-col gap-3", children: [
946
+ /* @__PURE__ */ jsxs(
947
+ Button,
948
+ {
949
+ variant: "transparent",
950
+ size: "small",
951
+ onClick: () => navigate("/swaps"),
952
+ className: "w-fit",
953
+ children: [
954
+ /* @__PURE__ */ jsx(ArrowLeft, { className: "mr-2" }),
955
+ "Back to list"
956
+ ]
957
+ }
958
+ ),
959
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1 md:flex-row md:items-center md:justify-between", children: [
960
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
961
+ /* @__PURE__ */ jsx(Heading, { level: "h1", children: "Swap Details" }),
962
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: swap.id })
963
+ ] }),
964
+ /* @__PURE__ */ jsx(
965
+ Badge,
966
+ {
967
+ size: "small",
968
+ className: `uppercase ${getStatusBadgeClass(swap.status)}`,
969
+ children: swap.status.replace(/_/g, " ")
970
+ }
971
+ )
972
+ ] })
973
+ ] }),
974
+ swap.status === "requested" && /* @__PURE__ */ jsxs("div", { className: "flex gap-3", children: [
975
+ /* @__PURE__ */ jsx(
976
+ Button,
977
+ {
978
+ variant: "primary",
979
+ onClick: handleApprove,
980
+ disabled: isApproving || isRejecting,
981
+ isLoading: isApproving,
982
+ children: "Approve Swap"
983
+ }
984
+ ),
985
+ /* @__PURE__ */ jsx(
986
+ Button,
987
+ {
988
+ variant: "secondary",
989
+ onClick: handleReject,
990
+ disabled: isApproving || isRejecting,
991
+ isLoading: isRejecting,
992
+ children: "Reject Swap"
993
+ }
994
+ )
995
+ ] }),
996
+ /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-base bg-ui-bg-base p-6", children: [
997
+ /* @__PURE__ */ jsx(Heading, { level: "h2", className: "mb-4 text-lg", children: "Update Status" }),
998
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3 md:flex-row md:items-end", children: [
999
+ /* @__PURE__ */ jsxs("div", { className: "flex-1", children: [
1000
+ /* @__PURE__ */ jsx("label", { className: "mb-2 block text-sm font-medium text-ui-fg-base", children: "New Status" }),
1001
+ /* @__PURE__ */ jsxs(
1002
+ "select",
1003
+ {
1004
+ value: selectedStatus,
1005
+ onChange: (event) => setSelectedStatus(event.target.value),
1006
+ className: "h-9 w-full rounded-md border border-ui-border-base bg-transparent px-3 text-sm text-ui-fg-base outline-none transition focus:ring-2 focus:ring-ui-fg-interactive",
1007
+ children: [
1008
+ /* @__PURE__ */ jsx("option", { value: "", children: "Select new status" }),
1009
+ availableStatuses.filter((status) => status !== swap.status).map((status) => /* @__PURE__ */ jsx("option", { value: status, children: status.replace(/_/g, " ").toUpperCase() }, status))
1010
+ ]
1011
+ }
1012
+ )
1013
+ ] }),
1014
+ /* @__PURE__ */ jsx(
1015
+ Button,
1016
+ {
1017
+ variant: "primary",
1018
+ onClick: handleStatusUpdate,
1019
+ disabled: !selectedStatus || isUpdating,
1020
+ isLoading: isUpdating,
1021
+ children: "Update Status"
1022
+ }
1023
+ )
1024
+ ] }),
1025
+ updateError && /* @__PURE__ */ jsx(Text, { size: "small", className: "mt-2 text-ui-fg-error", children: updateError }),
1026
+ updateSuccess && /* @__PURE__ */ jsx(Text, { size: "small", className: "mt-2 text-ui-fg-success", children: "Status updated successfully" })
1027
+ ] }),
1028
+ /* @__PURE__ */ jsxs("div", { className: "grid gap-6 md:grid-cols-2", children: [
1029
+ /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-base bg-ui-bg-base p-6", children: [
1030
+ /* @__PURE__ */ jsx(Heading, { level: "h2", className: "mb-4 text-lg", children: "Swap Information" }),
1031
+ /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
1032
+ /* @__PURE__ */ jsxs("div", { children: [
1033
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Swap ID" }),
1034
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: swap.id })
1035
+ ] }),
1036
+ /* @__PURE__ */ jsxs("div", { children: [
1037
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Status" }),
1038
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: swap.status })
1039
+ ] }),
1040
+ /* @__PURE__ */ jsxs("div", { children: [
1041
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Difference Due" }),
1042
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: swap.difference_due != null ? `${swap.currency_code || "$"}${(Number(swap.difference_due) / 100).toFixed(2)}` : "—" })
1043
+ ] }),
1044
+ swap.reason && /* @__PURE__ */ jsxs("div", { children: [
1045
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Reason" }),
1046
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: swap.reason })
1047
+ ] }),
1048
+ swap.note && /* @__PURE__ */ jsxs("div", { children: [
1049
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Note" }),
1050
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: swap.note })
1051
+ ] }),
1052
+ /* @__PURE__ */ jsxs("div", { children: [
1053
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Created" }),
1054
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: new Date(swap.created_at).toLocaleString() })
1055
+ ] }),
1056
+ /* @__PURE__ */ jsxs("div", { children: [
1057
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Last Updated" }),
1058
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: new Date(swap.updated_at).toLocaleString() })
1059
+ ] })
1060
+ ] })
1061
+ ] }),
1062
+ /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-base bg-ui-bg-base p-6", children: [
1063
+ /* @__PURE__ */ jsx(Heading, { level: "h2", className: "mb-4 text-lg", children: "Status History" }),
1064
+ statusHistory.length > 0 ? /* @__PURE__ */ jsx("div", { className: "space-y-2", children: statusHistory.map((entry, index) => /* @__PURE__ */ jsx(
1065
+ "div",
1066
+ {
1067
+ className: "flex items-center justify-between border-b border-ui-border-subtle pb-2 last:border-0",
1068
+ children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
1069
+ /* @__PURE__ */ jsx("div", { className: "flex items-center gap-2", children: /* @__PURE__ */ jsx(
1070
+ Badge,
1071
+ {
1072
+ size: "2xsmall",
1073
+ className: `uppercase ${getStatusBadgeClass(entry.status)}`,
1074
+ children: entry.status.replace(/_/g, " ")
1075
+ }
1076
+ ) }),
1077
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
1078
+ new Date(entry.timestamp).toLocaleString(),
1079
+ entry.admin_id && ` by ${entry.admin_id}`,
1080
+ entry.reason && ` - ${entry.reason}`
1081
+ ] })
1082
+ ] })
1083
+ },
1084
+ index
1085
+ )) }) : /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "No status history available" })
1086
+ ] })
1087
+ ] }),
1088
+ order && /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-base bg-ui-bg-base p-6", children: [
1089
+ /* @__PURE__ */ jsx(Heading, { level: "h2", className: "mb-4 text-lg", children: "Related Order Information" }),
1090
+ /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
1091
+ /* @__PURE__ */ jsxs("div", { children: [
1092
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Order ID" }),
1093
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: order.id })
1094
+ ] }),
1095
+ /* @__PURE__ */ jsxs("div", { children: [
1096
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Order Status" }),
1097
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: order.status || "—" })
1098
+ ] }),
1099
+ /* @__PURE__ */ jsxs("div", { children: [
1100
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Customer" }),
1101
+ /* @__PURE__ */ jsx(Text, { className: "font-medium", children: ((_b = order.customer) == null ? void 0 : _b.email) || order.email || "—" })
1102
+ ] }),
1103
+ order.total && /* @__PURE__ */ jsxs("div", { children: [
1104
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Order Total" }),
1105
+ /* @__PURE__ */ jsxs(Text, { className: "font-medium", children: [
1106
+ order.currency_code || "$",
1107
+ (Number(order.total) / 100).toFixed(2)
1108
+ ] })
1109
+ ] })
1110
+ ] })
1111
+ ] }),
1112
+ swap.return_items && swap.return_items.length > 0 && /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-base bg-ui-bg-base p-6", children: [
1113
+ /* @__PURE__ */ jsx(Heading, { level: "h2", className: "mb-4 text-lg", children: "Return Items" }),
1114
+ /* @__PURE__ */ jsx("div", { className: "overflow-hidden rounded-xl border border-ui-border-base", children: /* @__PURE__ */ jsxs("table", { className: "min-w-full divide-y divide-ui-border-base", children: [
1115
+ /* @__PURE__ */ jsx("thead", { className: "bg-ui-bg-subtle", children: /* @__PURE__ */ jsxs("tr", { children: [
1116
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Item ID" }),
1117
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Quantity" }),
1118
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Reason" })
1119
+ ] }) }),
1120
+ /* @__PURE__ */ jsx("tbody", { className: "divide-y divide-ui-border-subtle", children: swap.return_items.map((item, index) => /* @__PURE__ */ jsxs("tr", { children: [
1121
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 font-medium text-ui-fg-base", children: item.id }),
1122
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 text-ui-fg-subtle", children: item.quantity }),
1123
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 text-ui-fg-subtle", children: item.reason || "—" })
1124
+ ] }, item.id || index)) })
1125
+ ] }) })
1126
+ ] }),
1127
+ swap.new_items && swap.new_items.length > 0 && /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-ui-border-base bg-ui-bg-base p-6", children: [
1128
+ /* @__PURE__ */ jsx(Heading, { level: "h2", className: "mb-4 text-lg", children: "New Items" }),
1129
+ /* @__PURE__ */ jsx("div", { className: "overflow-hidden rounded-xl border border-ui-border-base", children: /* @__PURE__ */ jsxs("table", { className: "min-w-full divide-y divide-ui-border-base", children: [
1130
+ /* @__PURE__ */ jsx("thead", { className: "bg-ui-bg-subtle", children: /* @__PURE__ */ jsxs("tr", { children: [
1131
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Variant ID" }),
1132
+ /* @__PURE__ */ jsx("th", { className: "px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-ui-fg-muted", children: "Quantity" })
1133
+ ] }) }),
1134
+ /* @__PURE__ */ jsx("tbody", { className: "divide-y divide-ui-border-subtle", children: swap.new_items.map((item, index) => /* @__PURE__ */ jsxs("tr", { children: [
1135
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 font-medium text-ui-fg-base", children: item.variant_id }),
1136
+ /* @__PURE__ */ jsx("td", { className: "px-4 py-4 text-ui-fg-subtle", children: item.quantity })
1137
+ ] }, item.variant_id || index)) })
1138
+ ] }) })
1139
+ ] })
1140
+ ] }) });
1141
+ };
1142
+ const config = defineRouteConfig({
1143
+ label: "Swap Details",
1144
+ icon: ArrowPath
1145
+ });
533
1146
  const i18nTranslations0 = {};
534
1147
  const widgetModule = { widgets: [] };
535
1148
  const routeModule = {
@@ -538,24 +1151,44 @@ const routeModule = {
538
1151
  Component: ReturnsPage,
539
1152
  path: "/returns"
540
1153
  },
1154
+ {
1155
+ Component: SwapsPage,
1156
+ path: "/swaps"
1157
+ },
541
1158
  {
542
1159
  Component: ReturnDetailPage,
543
1160
  path: "/returns/:id"
1161
+ },
1162
+ {
1163
+ Component: SwapDetailPage,
1164
+ path: "/swaps/:id"
544
1165
  }
545
1166
  ]
546
1167
  };
547
1168
  const menuItemModule = {
548
1169
  menuItems: [
1170
+ {
1171
+ label: config$3.label,
1172
+ icon: config$3.icon,
1173
+ path: "/returns",
1174
+ nested: void 0
1175
+ },
1176
+ {
1177
+ label: config$2.label,
1178
+ icon: config$2.icon,
1179
+ path: "/swaps",
1180
+ nested: void 0
1181
+ },
549
1182
  {
550
1183
  label: config$1.label,
551
1184
  icon: config$1.icon,
552
- path: "/returns",
1185
+ path: "/returns/:id",
553
1186
  nested: void 0
554
1187
  },
555
1188
  {
556
1189
  label: config.label,
557
1190
  icon: config.icon,
558
- path: "/returns/:id",
1191
+ path: "/swaps/:id",
559
1192
  nested: void 0
560
1193
  }
561
1194
  ]