fix(auth): fetch existing fulfillment service locationId when create fails
When the fulfillment service already exists the create mutation returns userErrors. Now queries existing fulfillmentServices to get the location ID instead of falling through with null. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ce6aad2466
commit
2ceb4b0d90
@ -101,14 +101,45 @@ async function createFulfillmentService(shop, accessToken) {
|
|||||||
const response = await client.post("", { query: mutation });
|
const response = await client.post("", { query: mutation });
|
||||||
const data = response.data?.data?.fulfillmentServiceCreate;
|
const data = response.data?.data?.fulfillmentServiceCreate;
|
||||||
|
|
||||||
|
let fulfillmentService = data?.fulfillmentService || null;
|
||||||
|
let locationId = fulfillmentService?.location?.id || null;
|
||||||
|
|
||||||
if (data?.userErrors?.length) {
|
if (data?.userErrors?.length) {
|
||||||
|
// Service already exists — query it to get its locationId
|
||||||
|
log(shop, `Fulfillment service already exists, querying existing...`);
|
||||||
|
try {
|
||||||
|
const existingResp = await client.post("", {
|
||||||
|
query: `{ fulfillmentServices { id serviceName location { id name } } }`,
|
||||||
|
});
|
||||||
|
const existing = existingResp.data?.data?.fulfillmentServices || [];
|
||||||
|
const ours = existing.find((s) => s.serviceName === "Race Nation Distribution") || existing[0];
|
||||||
|
if (ours?.location?.id) {
|
||||||
|
locationId = ours.location.id;
|
||||||
|
fulfillmentService = ours;
|
||||||
|
log(shop, `Found existing fulfillment service location: ${ours.location.name} (${locationId})`);
|
||||||
|
}
|
||||||
|
} catch (queryErr) {
|
||||||
|
log(shop, `Could not query existing fulfillment services: ${queryErr.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!locationId) {
|
||||||
|
// Last resort: try to get shop's primary location
|
||||||
|
try {
|
||||||
|
const locResp = await client.post("", {
|
||||||
|
query: `{ locations(first: 1) { nodes { id name } } }`,
|
||||||
|
});
|
||||||
|
locationId = locResp.data?.data?.locations?.nodes?.[0]?.id || null;
|
||||||
|
if (locationId) log(shop, `Using primary store location: ${locationId}`);
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!locationId && data?.userErrors?.length) {
|
||||||
return { success: false, errors: data.userErrors };
|
return { success: false, errors: data.userErrors };
|
||||||
}
|
}
|
||||||
|
|
||||||
const address = await getStoreAddress(client);
|
const address = await getStoreAddress(client).catch(() => null);
|
||||||
let locationId = data?.fulfillmentService?.location?.id || null;
|
if (address && !locationId) {
|
||||||
|
|
||||||
if (address) {
|
|
||||||
const customLocation = await createCustomLocation(address, client);
|
const customLocation = await createCustomLocation(address, client);
|
||||||
if (!customLocation?.userErrors?.length && customLocation?.location?.id) {
|
if (!customLocation?.userErrors?.length && customLocation?.location?.id) {
|
||||||
locationId = customLocation.location.id;
|
locationId = customLocation.location.id;
|
||||||
@ -117,7 +148,7 @@ async function createFulfillmentService(shop, accessToken) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
fulfillmentService: data?.fulfillmentService || null,
|
fulfillmentService,
|
||||||
locationId,
|
locationId,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user