@porulle/adapter-shopify 0.73.1 → 0.73.3

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/dist/index.d.ts CHANGED
@@ -26,6 +26,11 @@ export declare const ORDER_CREATE_MUTATION = "mutation PorulleOrderCreate($order
26
26
  export declare const ORDER_CANCEL_MUTATION = "mutation PorulleOrderCancel($orderId: ID!, $reason: OrderCancelReason!, $staffNote: String) {\n orderCancel(orderId: $orderId, reason: $reason, restock: true, notifyCustomer: false, staffNote: $staffNote) { orderCancelUserErrors { code message } }\n}";
27
27
  /** The order's shipped lines: a return names what it takes back by fulfilment line, not order line. */
28
28
  export declare const ORDER_FULFILLMENT_LINES_QUERY = "query PorulleOrderFulfillmentLines($id: ID!) {\n order(id: $id) { fulfillments(first: 20) { fulfillmentLineItems(first: 50) { nodes { id quantity lineItem { variant { legacyResourceId } } } } } }\n}";
29
+ /**
30
+ * Shopify's reason library, by handle. Live Shopify refuses a return line with no reason ("Return reason
31
+ * can't be blank") although the schema marks both reason fields optional.
32
+ */
33
+ export declare const RETURN_REASON_QUERY = "query PorulleReturnReasons($handles: [String!]) {\n returnReasonDefinitions(first: 2, handles: $handles) { nodes { id handle } }\n}";
29
34
  export declare const RETURN_REQUEST_MUTATION = "mutation PorulleReturnRequest($input: ReturnRequestInput!) {\n returnRequest(input: $input) { return { id } userErrors { code message } }\n}";
30
35
  export declare const ORDER_BY_SOURCE_QUERY = "query PorulleOrderBySource($query: String!) {\n orders(first: 1, query: $query) { nodes { legacyResourceId } }\n}";
31
36
  export declare const ORDER_STATUS_QUERY = "query PorulleOrderStatus($id: ID!) {\n order(id: $id) { cancelledAt displayFinancialStatus displayFulfillmentStatus }\n}";
package/dist/index.js CHANGED
@@ -30,6 +30,19 @@ export const ORDER_CANCEL_MUTATION = `mutation PorulleOrderCancel($orderId: ID!,
30
30
  export const ORDER_FULFILLMENT_LINES_QUERY = `query PorulleOrderFulfillmentLines($id: ID!) {
31
31
  order(id: $id) { fulfillments(first: 20) { fulfillmentLineItems(first: 50) { nodes { id quantity lineItem { variant { legacyResourceId } } } } } }
32
32
  }`;
33
+ /**
34
+ * Shopify's reason library, by handle. Live Shopify refuses a return line with no reason ("Return reason
35
+ * can't be blank") although the schema marks both reason fields optional.
36
+ */
37
+ export const RETURN_REASON_QUERY = `query PorulleReturnReasons($handles: [String!]) {
38
+ returnReasonDefinitions(first: 2, handles: $handles) { nodes { id handle } }
39
+ }`;
40
+ /** Shopify's catch-all, for a reason its library has no handle for. */
41
+ const OTHER_RETURN_REASON = "other-reason";
42
+ /** "Too small" → "too-small": Shopify's handles are its reason names, lower-cased and hyphenated. */
43
+ function returnReasonHandle(reason) {
44
+ return reason.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
45
+ }
33
46
  export const RETURN_REQUEST_MUTATION = `mutation PorulleReturnRequest($input: ReturnRequestInput!) {
34
47
  returnRequest(input: $input) { return { id } userErrors { code message } }
35
48
  }`;
@@ -71,6 +84,9 @@ const fulfillmentLinesSchema = z.object({
71
84
  })),
72
85
  }).nullable(),
73
86
  });
87
+ const returnReasonSchema = z.object({
88
+ returnReasonDefinitions: z.object({ nodes: z.array(z.object({ id: z.string(), handle: z.string() })) }),
89
+ });
74
90
  const returnRequestSchema = z.object({
75
91
  returnRequest: z.object({ return: z.object({ id: z.string() }).nullable(), userErrors: z.array(z.object({ code: z.string().nullable(), message: z.string() })) }),
76
92
  });
@@ -272,7 +288,9 @@ export function shopifyConnector(options) {
272
288
  if (!created.ok)
273
289
  return created;
274
290
  const { order, userErrors } = created.value.orderCreate;
275
- if (userErrors.some((error) => error.code === "INVENTORY_CLAIM_FAILED")) {
291
+ // Live Shopify (2026-10, measured) refuses stock as `INVALID` on `["order","lineItems"]` with "Line items
292
+ // Unable to reserve inventory", not the documented `INVENTORY_CLAIM_FAILED`; both are read as out of stock.
293
+ if (userErrors.some((error) => error.code === "INVENTORY_CLAIM_FAILED" || /unable to reserve inventory/i.test(error.message))) {
276
294
  return Err({ code: CHANNEL_OUT_OF_STOCK, message: `The store does not have the stock: ${userErrors.map((error) => error.message).join("; ")}.`, retriable: false });
277
295
  }
278
296
  if (userErrors.length > 0 || !order) {
@@ -320,6 +338,14 @@ export function shopifyConnector(options) {
320
338
  if (!shipped.ok)
321
339
  return shipped;
322
340
  const fulfilmentLines = (shipped.value.order?.fulfillments ?? []).flatMap((fulfillment) => fulfillment.fulfillmentLineItems.nodes);
341
+ const handle = returnReasonHandle(input.reason);
342
+ const reasons = await shopifyGraphql(shop, RETURN_REASON_QUERY, { handles: [handle, OTHER_RETURN_REASON] }, returnReasonSchema);
343
+ if (!reasons.ok)
344
+ return reasons;
345
+ const definitions = reasons.value.returnReasonDefinitions.nodes;
346
+ const reason = definitions.find((definition) => definition.handle === handle) ?? definitions.find((definition) => definition.handle === OTHER_RETURN_REASON);
347
+ if (!reason)
348
+ return Err({ code: "SHOPIFY_RETURN_REASON_MISSING", message: `Shopify has no return reason "${handle}" and no "${OTHER_RETURN_REASON}".`, retriable: false });
323
349
  const returnLineItems = [];
324
350
  const note = [input.reason, input.note].filter((part) => part !== undefined && part !== "").join(" — ").slice(0, 300);
325
351
  for (const wanted of input.lines) {
@@ -328,7 +354,7 @@ export function shopifyConnector(options) {
328
354
  if (remaining === 0)
329
355
  break;
330
356
  const take = Math.min(remaining, line.quantity);
331
- returnLineItems.push({ fulfillmentLineItemId: line.id, quantity: take, customerNote: note });
357
+ returnLineItems.push({ fulfillmentLineItemId: line.id, quantity: take, customerNote: note, returnReasonDefinitionId: reason.id });
332
358
  remaining -= take;
333
359
  }
334
360
  if (remaining > 0)