@porulle/adapter-shopify 0.73.1 → 0.73.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@porulle/adapter-shopify",
3
- "version": "0.73.1",
3
+ "version": "0.73.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "zod": "^4.1.11",
19
- "@porulle/core": "0.73.1"
19
+ "@porulle/core": "0.73.2"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/node": "^24.5.2",
@@ -26,8 +26,8 @@
26
26
  "miniflare": "4.20260722.1",
27
27
  "typescript": "5.9.2",
28
28
  "vitest": "^3.2.4",
29
- "@porulle/eslint-config": "0.1.0",
30
- "@porulle/typescript-config": "0.1.0"
29
+ "@porulle/typescript-config": "0.1.0",
30
+ "@porulle/eslint-config": "0.1.0"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"
package/src/index.ts CHANGED
@@ -72,6 +72,22 @@ export const ORDER_FULFILLMENT_LINES_QUERY = `query PorulleOrderFulfillmentLines
72
72
  order(id: $id) { fulfillments(first: 20) { fulfillmentLineItems(first: 50) { nodes { id quantity lineItem { variant { legacyResourceId } } } } } }
73
73
  }`;
74
74
 
75
+ /**
76
+ * Shopify's reason library, by handle. Live Shopify refuses a return line with no reason ("Return reason
77
+ * can't be blank") although the schema marks both reason fields optional.
78
+ */
79
+ export const RETURN_REASON_QUERY = `query PorulleReturnReasons($handles: [String!]) {
80
+ returnReasonDefinitions(first: 2, handles: $handles) { nodes { id handle } }
81
+ }`;
82
+
83
+ /** Shopify's catch-all, for a reason its library has no handle for. */
84
+ const OTHER_RETURN_REASON = "other-reason";
85
+
86
+ /** "Too small" → "too-small": Shopify's handles are its reason names, lower-cased and hyphenated. */
87
+ function returnReasonHandle(reason: string): string {
88
+ return reason.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
89
+ }
90
+
75
91
  export const RETURN_REQUEST_MUTATION = `mutation PorulleReturnRequest($input: ReturnRequestInput!) {
76
92
  returnRequest(input: $input) { return { id } userErrors { code message } }
77
93
  }`;
@@ -116,6 +132,10 @@ const fulfillmentLinesSchema = z.object({
116
132
  })),
117
133
  }).nullable(),
118
134
  });
135
+ const returnReasonSchema = z.object({
136
+ returnReasonDefinitions: z.object({ nodes: z.array(z.object({ id: z.string(), handle: z.string() })) }),
137
+ });
138
+
119
139
  const returnRequestSchema = z.object({
120
140
  returnRequest: z.object({ return: z.object({ id: z.string() }).nullable(), userErrors: z.array(z.object({ code: z.string().nullable(), message: z.string() })) }),
121
141
  });
@@ -345,14 +365,20 @@ export function shopifyConnector(options: ShopifyConnectorOptions): ChannelConne
345
365
  const shipped = await shopifyGraphql(shop, ORDER_FULFILLMENT_LINES_QUERY, { id: `gid://shopify/Order/${remoteOrderId}` }, fulfillmentLinesSchema);
346
366
  if (!shipped.ok) return shipped;
347
367
  const fulfilmentLines = (shipped.value.order?.fulfillments ?? []).flatMap((fulfillment) => fulfillment.fulfillmentLineItems.nodes);
348
- const returnLineItems: Array<{ fulfillmentLineItemId: string; quantity: number; customerNote: string }> = [];
368
+ const handle = returnReasonHandle(input.reason);
369
+ const reasons = await shopifyGraphql(shop, RETURN_REASON_QUERY, { handles: [handle, OTHER_RETURN_REASON] }, returnReasonSchema);
370
+ if (!reasons.ok) return reasons;
371
+ const definitions = reasons.value.returnReasonDefinitions.nodes;
372
+ const reason = definitions.find((definition) => definition.handle === handle) ?? definitions.find((definition) => definition.handle === OTHER_RETURN_REASON);
373
+ if (!reason) return Err({ code: "SHOPIFY_RETURN_REASON_MISSING", message: `Shopify has no return reason "${handle}" and no "${OTHER_RETURN_REASON}".`, retriable: false });
374
+ const returnLineItems: Array<{ fulfillmentLineItemId: string; quantity: number; customerNote: string; returnReasonDefinitionId: string }> = [];
349
375
  const note = [input.reason, input.note].filter((part) => part !== undefined && part !== "").join(" — ").slice(0, 300);
350
376
  for (const wanted of input.lines) {
351
377
  let remaining = wanted.quantity;
352
378
  for (const line of fulfilmentLines.filter((candidate) => candidate.lineItem.variant?.legacyResourceId === wanted.externalVariantId)) {
353
379
  if (remaining === 0) break;
354
380
  const take = Math.min(remaining, line.quantity);
355
- returnLineItems.push({ fulfillmentLineItemId: line.id, quantity: take, customerNote: note });
381
+ returnLineItems.push({ fulfillmentLineItemId: line.id, quantity: take, customerNote: note, returnReasonDefinitionId: reason.id });
356
382
  remaining -= take;
357
383
  }
358
384
  if (remaining > 0) return Err({ code: "SHOPIFY_RETURN_NOT_SHIPPED", message: `Shopify has not shipped ${remaining} of variant ${wanted.externalVariantId}, so it cannot take them back.`, retriable: false });