From 341977b8b9e44bc710c22092b5c055a08b4685e9 Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Tue, 30 Apr 2024 16:21:38 -0400 Subject: [PATCH 1/4] Rough draft working --- .../index.js | 77 +++++++++++++++++++ .../zoom-meeting-webhook-handler/index.js | 21 ++--- netlify.toml | 5 ++ 3 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 functions/handle-participant-joined-background/index.js diff --git a/functions/handle-participant-joined-background/index.js b/functions/handle-participant-joined-background/index.js new file mode 100644 index 0000000..9c2bd63 --- /dev/null +++ b/functions/handle-participant-joined-background/index.js @@ -0,0 +1,77 @@ +require('dotenv').config(); + +const crypto = require('crypto'); + +const { updateMeetingStatus, updateMeetingAttendence } = require('../zoom-meeting-webhook-handler/slack.js'); + +const rooms = require('../../data/rooms.json'); + +const EVENT_MEETING_STARTED = 'meeting.started'; +const EVENT_MEETING_ENDED = 'meeting.ended'; +const EVENT_PARTICIPANT_JOINED = 'meeting.participant_joined'; +const EVENT_PARTICIPANT_LEFT = 'meeting.participant_left'; + +const ZOOM_SECRET = + process.env.TEST_ZOOM_WEBHOOK_SECRET_TOKEN || + process.env.ZOOM_WEBHOOK_SECRET_TOKEN; + +const ZOOM_AUTH = + process.env.TEST_ZOOM_WEBHOOK_AUTH || process.env.ZOOM_WEBHOOK_AUTH; + +const handler = async function (event, context) { + try { + console.log(event) + + const request = JSON.parse(event.body); + + console.log('PRINTING REQUEST FROM handle-participant-joined-background') + console.log(JSON.stringify(request, null, 2)) + + // check our meeting ID. The meeting ID never changes, but the uuid is different for each instance + + const room = rooms.find( + (room) => room.ZoomMeetingId === request.payload.object.id + ); + + if (room) { + const Airtable = require('airtable'); + const base = new Airtable().base(process.env.AIRTABLE_COWORKING_BASE); + + const { findRoomInstance } = require('../zoom-meeting-webhook-handler/airtable'); + + let roomInstance = await findRoomInstance( + room, + base, + request.payload.object.uuid + ); + + if (roomInstance) { + // create room event record + console.log(`found room instance ${roomInstance.getId()}`); + + const updatedMeeting = await updateMeetingAttendence( + room, + roomInstance.get('slack_thread_timestamp'), + request + ); + } + } else { + console.log('meeting ID is not co-working meeting'); + } + + return { + statusCode: 200, + body: '', + }; + } catch (error) { + // output to netlify function log + console.log(error); + return { + statusCode: 500, + // Could be a custom message or object i.e. JSON.stringify(err) + body: JSON.stringify({ msg: error.message }), + }; + } +}; + +module.exports = { handler }; diff --git a/functions/zoom-meeting-webhook-handler/index.js b/functions/zoom-meeting-webhook-handler/index.js index 76fa365..60ebc66 100644 --- a/functions/zoom-meeting-webhook-handler/index.js +++ b/functions/zoom-meeting-webhook-handler/index.js @@ -96,22 +96,15 @@ const handler = async function (event, context) { switch (request.event) { case EVENT_PARTICIPANT_JOINED: case EVENT_PARTICIPANT_LEFT: - let roomInstance = await findRoomInstance( - room, - base, - request.payload.object.uuid - ); + console.log('CALLING handle-participant-joined-background') - if (roomInstance) { - // create room event record - console.log(`found room instance ${roomInstance.getId()}`); + APP_HOST = 'https://4990a465--capable-gecko-a354d1.netlify.live' + response = await fetch(`${APP_HOST}/handle-participant-joined-background`, { + method: 'POST', + body: event.body, + }); - const updatedMeeting = await updateMeetingAttendence( - room, - roomInstance.get('slack_thread_timestamp'), - request - ); - } + console.log('EXITING IMMEDIATELY FROM zoom-meeting-webhook-handler') break; diff --git a/netlify.toml b/netlify.toml index 287f05a..530a506 100644 --- a/netlify.toml +++ b/netlify.toml @@ -26,3 +26,8 @@ from = "/event-reminders" to = "/.netlify/functions/event-reminders-background" status = 200 + +[[redirects]] + from = "/handle-participant-joined-background" + to = "/.netlify/functions/handle-participant-joined-background" + status = 200 \ No newline at end of file From 20c00d9a4a4e8daacafd7b4ccd36af07acf87371 Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Tue, 30 Apr 2024 16:50:08 -0400 Subject: [PATCH 2/4] Remove unused imports --- .../index.js | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/functions/handle-participant-joined-background/index.js b/functions/handle-participant-joined-background/index.js index 9c2bd63..01047da 100644 --- a/functions/handle-participant-joined-background/index.js +++ b/functions/handle-participant-joined-background/index.js @@ -1,27 +1,11 @@ require('dotenv').config(); -const crypto = require('crypto'); - -const { updateMeetingStatus, updateMeetingAttendence } = require('../zoom-meeting-webhook-handler/slack.js'); +const { updateMeetingAttendence } = require('../zoom-meeting-webhook-handler/slack.js'); const rooms = require('../../data/rooms.json'); -const EVENT_MEETING_STARTED = 'meeting.started'; -const EVENT_MEETING_ENDED = 'meeting.ended'; -const EVENT_PARTICIPANT_JOINED = 'meeting.participant_joined'; -const EVENT_PARTICIPANT_LEFT = 'meeting.participant_left'; - -const ZOOM_SECRET = - process.env.TEST_ZOOM_WEBHOOK_SECRET_TOKEN || - process.env.ZOOM_WEBHOOK_SECRET_TOKEN; - -const ZOOM_AUTH = - process.env.TEST_ZOOM_WEBHOOK_AUTH || process.env.ZOOM_WEBHOOK_AUTH; - const handler = async function (event, context) { try { - console.log(event) - const request = JSON.parse(event.body); console.log('PRINTING REQUEST FROM handle-participant-joined-background') From 67522c7fe7b06e8dc5c4464509e5b5825f4a7e74 Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Wed, 1 May 2024 14:30:15 -0400 Subject: [PATCH 3/4] Don't hardcode APP_HOST Use env value instead --- functions/zoom-meeting-webhook-handler/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/functions/zoom-meeting-webhook-handler/index.js b/functions/zoom-meeting-webhook-handler/index.js index 60ebc66..739f4c8 100644 --- a/functions/zoom-meeting-webhook-handler/index.js +++ b/functions/zoom-meeting-webhook-handler/index.js @@ -18,6 +18,8 @@ const ZOOM_SECRET = const ZOOM_AUTH = process.env.TEST_ZOOM_WEBHOOK_AUTH || process.env.ZOOM_WEBHOOK_AUTH; +const APP_HOST = process.env.TEST_APP_HOST || process.env.APP_HOST; + const handler = async function (event, context) { try { /** @@ -98,7 +100,6 @@ const handler = async function (event, context) { case EVENT_PARTICIPANT_LEFT: console.log('CALLING handle-participant-joined-background') - APP_HOST = 'https://4990a465--capable-gecko-a354d1.netlify.live' response = await fetch(`${APP_HOST}/handle-participant-joined-background`, { method: 'POST', body: event.body, From dd5d484324dba225e22b8d3e8511f867cee95108 Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Wed, 1 May 2024 14:40:36 -0400 Subject: [PATCH 4/4] Rename background function --- .../index.js | 2 +- functions/zoom-meeting-webhook-handler/index.js | 4 ++-- netlify.toml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename functions/{handle-participant-joined-background => handle-participant-joined-left-background}/index.js (98%) diff --git a/functions/handle-participant-joined-background/index.js b/functions/handle-participant-joined-left-background/index.js similarity index 98% rename from functions/handle-participant-joined-background/index.js rename to functions/handle-participant-joined-left-background/index.js index 01047da..d2d2c0f 100644 --- a/functions/handle-participant-joined-background/index.js +++ b/functions/handle-participant-joined-left-background/index.js @@ -8,7 +8,7 @@ const handler = async function (event, context) { try { const request = JSON.parse(event.body); - console.log('PRINTING REQUEST FROM handle-participant-joined-background') + console.log('PRINTING REQUEST FROM handle-participant-joined-left-background') console.log(JSON.stringify(request, null, 2)) // check our meeting ID. The meeting ID never changes, but the uuid is different for each instance diff --git a/functions/zoom-meeting-webhook-handler/index.js b/functions/zoom-meeting-webhook-handler/index.js index 739f4c8..44debad 100644 --- a/functions/zoom-meeting-webhook-handler/index.js +++ b/functions/zoom-meeting-webhook-handler/index.js @@ -98,9 +98,9 @@ const handler = async function (event, context) { switch (request.event) { case EVENT_PARTICIPANT_JOINED: case EVENT_PARTICIPANT_LEFT: - console.log('CALLING handle-participant-joined-background') + console.log('CALLING handle-participant-joined-left-background') - response = await fetch(`${APP_HOST}/handle-participant-joined-background`, { + response = await fetch(`${APP_HOST}/handle-participant-joined-left-background`, { method: 'POST', body: event.body, }); diff --git a/netlify.toml b/netlify.toml index 530a506..b18539d 100644 --- a/netlify.toml +++ b/netlify.toml @@ -28,6 +28,6 @@ status = 200 [[redirects]] - from = "/handle-participant-joined-background" - to = "/.netlify/functions/handle-participant-joined-background" + from = "/handle-participant-joined-left-background" + to = "/.netlify/functions/handle-participant-joined-left-background" status = 200 \ No newline at end of file