Skip to content

Commit

Permalink
Handle webhooks async
Browse files Browse the repository at this point in the history
  • Loading branch information
farski committed May 23, 2024
1 parent fe0b1f3 commit b78d38f
Show file tree
Hide file tree
Showing 3 changed files with 849 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"homepage": "https://github.com/PRX/eventbrite-toolkit#readme",
"dependencies": {
"@aws-sdk/client-eventbridge": "*"
"@aws-sdk/client-eventbridge": "*",
"@aws-sdk/client-lambda": "*"
},
"devDependencies": {
"@types/node": "*",
Expand Down
23 changes: 22 additions & 1 deletion src/ticket-notifications/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
EventBridgeClient,
PutEventsCommand,
} from "@aws-sdk/client-eventbridge";
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

const SLACK_CHANNEL = "#garage-registration";
const SLACK_USERNAME = "Eventbrite";
Expand All @@ -10,6 +11,7 @@ const SLACK_ICON = ":eventbrite:";
const TOKEN = `token=${process.env.EVENTBRITE_TOKEN}`;

const eventbridge = new EventBridgeClient({ apiVersion: "2015-10-07" });
const lambda = new LambdaClient({ apiVersion: "2015-03-31" });

async function getOrder(orderApiUrl) {
console.info(`Webhook triggered by object ${orderApiUrl}`);
Expand Down Expand Up @@ -65,6 +67,25 @@ function message(order, event) {
}

export const handler = async (event) => {
// When handling the webhook, call this function again with the same payload,
// plus a flag to indicate the payload has already been received. This allows
// for returning a response to the webhook request very quickly (to avoid
// timeouts, which seem to be common), and then do the slower part of the
// message creation async.
if (!event.prx_invoke) {
// eslint-disable-next-line no-param-reassign
event.prx_invoke = true;
await lambda.send(
new InvokeCommand({
FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
InvocationType: "Event",
Payload: JSON.stringify(event),
}),
);

return { statusCode: 200, headers: {}, body: "" };
}

try {
const body = event.isBase64Encoded
? Buffer.from(event.body, "base64").toString("utf-8")
Expand Down Expand Up @@ -94,7 +115,7 @@ export const handler = async (event) => {
}),
);

return { statusCode: 200, headers: {}, body: "" };
return true;
} catch (e) {
console.log(e);
throw e;
Expand Down
Loading

0 comments on commit b78d38f

Please sign in to comment.