From 132f6b606d3f1405a311c9e2a79dfa78f0352d81 Mon Sep 17 00:00:00 2001 From: MOHAN Date: Sat, 22 Aug 2026 00:15:56 +0530 Subject: [PATCH] fix(auth): auto-fetch primary locationId after OAuth when fulfillment service fails Falls back to querying locations(first:1) if no locationId was saved during auth, so inventory warnings are eliminated without manual config. Co-Authored-By: Claude Sonnet 4.6 --- auth.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/auth.js b/auth.js index 4818c64..19f4fce 100644 --- a/auth.js +++ b/auth.js @@ -57,6 +57,27 @@ router.get("/auth/callback", async (req, res) => { log(shop, `Fulfillment setup skipped/failed: ${JSON.stringify(fulfillment?.errors || fulfillment?.error || null)}`); } + // Fallback: always try to resolve a locationId from the store's primary location + const { getToken } = require("./tokenStore"); + const saved = getToken(shop); + if (!saved?.locationId) { + try { + const locResp = await axios.post( + `https://${shop}/admin/api/${process.env.SHOPIFY_API_VERSION || "2025-10"}/graphql.json`, + { query: "{ locations(first: 1) { nodes { id name } } }" }, + { headers: { "X-Shopify-Access-Token": access_token, "Content-Type": "application/json" } } + ); + const primaryLocId = locResp.data?.data?.locations?.nodes?.[0]?.id || null; + const primaryLocName = locResp.data?.data?.locations?.nodes?.[0]?.name || null; + if (primaryLocId) { + saveToken(shop, access_token, scope, saved?.fulfillmentService || null, primaryLocId); + log(shop, `Primary location saved: ${primaryLocName} (${primaryLocId})`); + } + } catch (locErr) { + log(shop, `Could not fetch primary location: ${locErr.message}`); + } + } + const redirectTarget = process.env.SHOPIFY_AFTER_AUTH_REDIRECT || "https://admin.shopify.com"; return res.redirect(redirectTarget); } catch (err) {