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 <noreply@anthropic.com>
This commit is contained in:
MOHAN 2026-08-22 00:15:56 +05:30
parent 3d98c873cc
commit 132f6b606d

21
auth.js
View File

@ -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) {