Skip to content

Commit

Permalink
Updating the webhook handler to push once to yext instead of for ever…
Browse files Browse the repository at this point in the history
…y entity in the payload
  • Loading branch information
csulham committed Mar 19, 2024
1 parent 2b6c015 commit aba01c3
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions pages/api/onPublishEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default async function onPublishEnd(req: NextApiRequest, res: NextApiResp
return res.status(400).json({ message: 'Bad Request. Check incoming data.' });
}

const items = [];

// Loop over all the entries in updates
for (const update of data.updates) {
// Check if the entity_definition is LayoutData
Expand Down Expand Up @@ -55,29 +57,32 @@ export default async function onPublishEnd(req: NextApiRequest, res: NextApiResp
console.log(`Item ${guid} is not in the right site`);
continue;
}

// Send the json data to the Yext Push API endpoint
const pushApiEndpoint = `${process.env.YEXT_PUSH_API_ENDPOINT}?v=${GetDate()}&api_key=${process.env.YEXT_PUSH_API_KEY}`;
console.log(`Pushing to ${pushApiEndpoint}\nData:\n${JSON.stringify(result)}`);

const yextResponse = await fetch(pushApiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(result),
});

if (!yextResponse.ok) {
console.log(`Failed to push data to Yext for item ${guid}: ${yextResponse.status} ${yextResponse.statusText}`);
}

// Add the item to the items array
items.push(result.item)

} catch (error) {
// If an error occurs while invoking the GraphQL query, return a 500 error
return res.status(500).json({ message: 'Internal Server Error: GraphQL query failed' })
}
}
}
// Send the json data to the Yext Push API endpoint
const pushApiEndpoint = `${process.env.YEXT_PUSH_API_ENDPOINT}?v=${GetDate()}&api_key=${process.env.YEXT_PUSH_API_KEY}`;
console.log(`Pushing to ${pushApiEndpoint}\nData:\n${JSON.stringify(items)}`);

// Send all the items to the Yext Push API endpoint
const yextResponse = await fetch(pushApiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(items),
});

if (!yextResponse.ok) {
console.log(`Failed to push data to Yext: ${yextResponse.status} ${yextResponse.statusText}`);
}

// Send a response
return res.status(200).json({ message: 'Webhook event received' })
Expand Down

0 comments on commit aba01c3

Please sign in to comment.