fix locationId: use shop primary location instead of fulfillment service location (Shopify never returns it)
This commit is contained in:
parent
cceb714a73
commit
01935dc92e
@ -65,85 +65,26 @@ async function createFulfillmentService(shop, accessToken) {
|
||||
log(shop, `✅ [FulfillmentService] Created: ${fulfillmentService.id}`);
|
||||
}
|
||||
|
||||
// The fulfillment service has its own Shopify-managed location — use it as fallback
|
||||
// Shopify sometimes returns location: null from the create mutation — re-query if so
|
||||
let serviceFallbackLocationId = fulfillmentService?.location?.id || null;
|
||||
if (!serviceFallbackLocationId) {
|
||||
try {
|
||||
log(shop, `🔍 [FulfillmentService] location null from mutation — re-querying fulfillment services...`);
|
||||
const reQueryResp = await client.post('', {
|
||||
query: `query { fulfillmentServices(type: THIRD_PARTY) { id handle location { id name } } }`,
|
||||
});
|
||||
const svcs = reQueryResp.data?.data?.fulfillmentServices || [];
|
||||
const matched = svcs.find(s => s.handle === 'data4autos-distribution') || svcs[0] || null;
|
||||
serviceFallbackLocationId = matched?.location?.id || null;
|
||||
log(shop, `📍 [FulfillmentService] Re-query location: ${serviceFallbackLocationId}`);
|
||||
} catch (reQErr) {
|
||||
log(shop, `⚠️ [FulfillmentService] Re-query failed: ${reQErr.message}`);
|
||||
}
|
||||
}
|
||||
log(shop, `📍 [FulfillmentService] Service location (fallback): ${serviceFallbackLocationId}`);
|
||||
|
||||
// ── Step 2: Try to create a custom named location ────────────────────────
|
||||
// First get the store's primary location address (search broadly, not by exact name)
|
||||
let storeAddress = null;
|
||||
// ── Step 2: Get the shop's primary location ID ───────────────────────────
|
||||
// Shopify never returns a location from fulfillmentServiceCreate — use the
|
||||
// shop's first active location (the physical/online store location) instead.
|
||||
let locationId = null;
|
||||
try {
|
||||
const locResp = await client.post('', {
|
||||
query: `query { locations(first: 1) { nodes { id name address { address1 address2 city province provinceCode country countryCode zip phone } } } }`,
|
||||
query: `query { locations(first: 1, includeLegacy: true) { nodes { id name isActive } } }`,
|
||||
});
|
||||
const locNode = locResp.data?.data?.locations?.nodes?.[0];
|
||||
if (locNode) {
|
||||
storeAddress = locNode.address;
|
||||
log(shop, `📍 [FulfillmentService] Store address from "${locNode.name}": ${JSON.stringify(storeAddress)}`);
|
||||
}
|
||||
} catch (addrErr) {
|
||||
log(shop, `⚠️ [FulfillmentService] Could not fetch store address: ${addrErr.message}`);
|
||||
}
|
||||
|
||||
let customLocationId = null;
|
||||
if (storeAddress) {
|
||||
try {
|
||||
const provinceCode = storeAddress.provinceCode || storeAddress.province || '';
|
||||
const countryCode = storeAddress.countryCode || 'US';
|
||||
|
||||
const locAddResp = await client.post('', {
|
||||
query: `
|
||||
mutation {
|
||||
locationAdd(input: {
|
||||
name: "(App) Data4Autos Distribution API",
|
||||
address: {
|
||||
address1: ${JSON.stringify(storeAddress.address1 || '')},
|
||||
address2: ${JSON.stringify(storeAddress.address2 || '')},
|
||||
city: ${JSON.stringify(storeAddress.city || '')},
|
||||
provinceCode: ${JSON.stringify(provinceCode)},
|
||||
countryCode: ${countryCode},
|
||||
zip: ${JSON.stringify(storeAddress.zip || '')},
|
||||
phone: ${JSON.stringify(storeAddress.phone || '')}
|
||||
},
|
||||
fulfillsOnlineOrders: true
|
||||
}) {
|
||||
location { id name }
|
||||
userErrors { code field message }
|
||||
}
|
||||
}
|
||||
`,
|
||||
});
|
||||
|
||||
const locAddData = locAddResp.data?.data?.locationAdd;
|
||||
if (locAddData?.userErrors?.length > 0) {
|
||||
log(shop, `⚠️ [FulfillmentService] locationAdd errors: ${JSON.stringify(locAddData.userErrors)}`);
|
||||
} else if (locAddData?.location?.id) {
|
||||
customLocationId = locAddData.location.id;
|
||||
log(shop, `✅ [FulfillmentService] Custom location created: ${customLocationId}`);
|
||||
if (locNode?.id) {
|
||||
locationId = locNode.id;
|
||||
log(shop, `📍 [FulfillmentService] Primary shop location: "${locNode.name}" → ${locationId}`);
|
||||
} else {
|
||||
log(shop, `⚠️ [FulfillmentService] No locations returned from Shopify`);
|
||||
}
|
||||
} catch (locErr) {
|
||||
log(shop, `⚠️ [FulfillmentService] locationAdd threw: ${locErr.message}`);
|
||||
}
|
||||
log(shop, `⚠️ [FulfillmentService] Could not fetch shop location: ${locErr.message}`);
|
||||
}
|
||||
|
||||
// Use custom location if created, otherwise fall back to fulfillment service location
|
||||
const locationId = customLocationId || serviceFallbackLocationId;
|
||||
log(shop, `📍 [FulfillmentService] Final locationId: ${locationId} (${customLocationId ? 'custom' : 'service fallback'})`);
|
||||
log(shop, `📍 [FulfillmentService] Final locationId: ${locationId}`);
|
||||
|
||||
return { fulfillmentService, locationId };
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user