Skip to content

Commit

Permalink
implement signup submit
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbarnum4 committed Oct 4, 2024
1 parent a99af99 commit dc718d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
35 changes: 18 additions & 17 deletions pages/api/signups/info/facebook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ReqHandler } from '@this/types/request';

import { runCloudFunction } from '@this/src/api/googleFunctions';
import {
fetchLead,
formatFacebookPayload,
Expand All @@ -9,6 +10,7 @@ import {
WebhookBody,
WebhookQuery,
} from '@this/src/api/helpers/facebookWebhook';
import { ISessionSignup } from '@this/types/signups';

/**
* Disabled body parser to allow for raw body parsing
Expand Down Expand Up @@ -61,23 +63,22 @@ const webhookHandler: WebhookHandler = async (req, res) => {
return;
}

// TODO: Handle multiple payload submissions
// const { GREENLIGHT_API_TOKEN, SIGNUP_API_ENDPOINT } = process.env;
// await Promise.all(
// payloads.map(async (payload) => {
// try {
// return await runCloudFunction<ISessionSignup, { url: string }>({
// url: SIGNUP_API_ENDPOINT,
// body: payload,
// headers: {
// 'X-Greenlight-Signup-Api-Key': GREENLIGHT_API_TOKEN,
// },
// });
// } catch (err) {
// console.error('Could not POST to signup service', err);
// }
// }),
// );
const { GREENLIGHT_API_TOKEN, SIGNUP_API_ENDPOINT } = process.env;
await Promise.all(
payloads.map(async (payload) => {
try {
return await runCloudFunction<ISessionSignup, { url: string }>({
url: SIGNUP_API_ENDPOINT,
body: payload,
headers: {
'X-Greenlight-Signup-Api-Key': GREENLIGHT_API_TOKEN,
},
});
} catch (err) {
console.error('Could not POST to signup service', err);
}
}),
);

res.status(200).json({ message: 'Success' });
};
Expand Down
9 changes: 7 additions & 2 deletions src/api/helpers/facebookWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ import { formatSignupPayload } from '../formatSignupPayload';

const { FB_ACCESS_TOKEN } = process.env;

const REQUIRED_SIGNUP_FIELDS = ['nameFirst', 'nameLast', 'email', 'cell', 'zipCode'];

export type FacebookPayloadFieldKey = 'day' | 'time' | 'name' | 'email' | 'phone' | 'zip';
const REQUIRED_SIGNUP_FIELDS: (keyof ISessionSignup)[] = [
'nameFirst',
'nameLast',
'email',
'cell',
'zipCode',
];

export type FacebookPayloadField = {
name: FacebookPayloadFieldKey;
Expand Down
4 changes: 4 additions & 0 deletions tests/api/facebook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
} from '../support/facebookWebhook';

describe('Facebook Info Session Signup Webhook', () => {
afterAll(() => {
nock.cleanAll();
nock.restore();
});
describe('Initial Webhook Subscription | GET /api/signups/info/facebook', () => {
test('should accept incoming webhook and respond with correct challenge code and status of 200', async () => {
process.env.FB_WEBHOOK_TOKEN = 'FAKE_TESTING_WEBHOOK_TOKEN';
Expand Down

0 comments on commit dc718d1

Please sign in to comment.