Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: body payload of MEETING_STARTED & MEETING_ENDED webhook #15036

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

hussamkhatib
Copy link
Contributor

@hussamkhatib hussamkhatib commented May 14, 2024

What does this PR do?

The PR now provides the proper body in the webhook payload for scheduled Triggers (MEETING_STARTED & MEETING_ENDED)

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected)
  • I have added a Docs issue here if this PR makes changes that would require a documentation change
  • I have added or modified automated tests that prove my fix is effective or that my feature works (PRs might be rejected if logical changes are not properly tested)

How should this be tested?

  • Follow steps to setup webhooks in your applciation from the docs

  • Adding logs to print (req.body.payload, req.body)

  • Trigger the MEETING_START or MEETING_ENDED event

  • You should find req.body.payload will be undefined and req.body having the contents of payload which isn't the format mentioned in the docs, neither do other events have.

  • What are the minimal test data to have?
    Booking a event creates the necessary records in the database, for ease of testing set buffer time to book to 0 and booking interval to 1 min, so that MEETING_STARTED & MEETING_ENDED will be trigerred faster.

Checklist

  • I haven't checked if my changes generate no new warnings

Copy link

vercel bot commented May 14, 2024

@hussamkhatib is attempting to deploy a commit to the cal Team on Vercel.

A member of the Team first needs to authorize it.

@graphite-app graphite-app bot added the community Created by Linear-GitHub Sync label May 14, 2024
@graphite-app graphite-app bot requested a review from a team May 14, 2024 21:49
Copy link
Contributor

github-actions bot commented May 14, 2024

Thank you for following the naming conventions! 🙏 Feel free to join our discord and post your PR link.

@github-actions github-actions bot added docs area: docs, documentation, cal.com/docs webhooks area: webhooks, callback, webhook payload labels May 14, 2024
@dosubot dosubot bot added the 🐛 bug Something isn't working label May 14, 2024
Copy link

graphite-app bot commented May 14, 2024

Graphite Automations

"Add community label" took an action on this PR • (05/14/24)

1 label was added to this PR based on Keith Williams's automation.

"Add consumer team as reviewer" took an action on this PR • (05/14/24)

1 reviewer was added to this PR based on Keith Williams's automation.

@hussamkhatib hussamkhatib changed the title fix body payload of MEETING_STARTED & MEETING_ENDED webhook fix: body payload of MEETING_STARTED & MEETING_ENDED webhook May 14, 2024
Copy link
Contributor

github-actions bot commented May 14, 2024

📦 Next.js Bundle Analysis for @calcom/web

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@Udit-takkar Udit-takkar added this to the v4.1 milestone May 15, 2024
@Udit-takkar
Copy link
Contributor

The unit tests are failling

@@ -295,8 +295,6 @@ export function expectWebhookToHaveBeenCalledWith(
}
if (data.payload.responses !== undefined)
expect(parsedBody.payload.responses).toEqual(expect.objectContaining(data.payload.responses));
const { responses: _1, metadata: _2, ...remainingPayload } = data.payload;
expect(parsedBody.payload).toEqual(expect.objectContaining(remainingPayload));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare the 2 snippets at the end of the Issue for why this is not needed.

Copy link
Member

@CarinaWolli CarinaWolli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve added some comments. I’m also concerned that these changes will break existing workflows for users, as fields won't be accessible anymore as they used to

@@ -295,8 +295,6 @@ export function expectWebhookToHaveBeenCalledWith(
}
if (data.payload.responses !== undefined)
expect(parsedBody.payload.responses).toEqual(expect.objectContaining(data.payload.responses));
const { responses: _1, metadata: _2, ...remainingPayload } = data.payload;
expect(parsedBody.payload).toEqual(expect.objectContaining(remainingPayload));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's fix the test instead of deleting it, this function is also used for other tests were it passes

};

const parsedPayload = jsonParse(job.payload);

const body = JSON.stringify({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to use sendPayload that would handle everything including headers, body, secret and the fetch request

@CarinaWolli
Copy link
Member

I’m also concerned that these changes will break existing workflows for users, as fields won't be accessible anymore as they used to

To make sure we don't break existing workflows for users, we decided to keep all fields as they are right now but additionally add the missing fields payload and createdAt

@hussamkhatib
Copy link
Contributor Author

I’m also concerned that these changes will break existing workflows for users, as fields won't be accessible anymore as they used to

To make sure we don't break existing workflows for users, we decided to keep all fields as they are right now but additionally add the missing fields payload and createdAt

Sounds good @CarinaWolli, So could I go ahead without using sendPayload as using that won't allow to handle existing workflow for users

@CarinaWolli
Copy link
Member

Sounds good @CarinaWolli, So could I go ahead without using sendPayload as using that won't allow to handle existing workflow for users

Yes we can do that for now 👍

@github-actions github-actions bot added High priority Created by Linear-GitHub Sync Urgent Created by Linear-GitHub Sync labels May 17, 2024
@hussamkhatib
Copy link
Contributor Author

Can anyone help with the unit tests ?

@CarinaWolli
Copy link
Member

Can anyone help with the unit tests ?

Wasn't able to make changes to your branch. This should fix the test:

test(`should trigger if current date is after startAfter`, async () => {
  const now = dayjs();
  const payload = { triggerEvent: "MEETING_ENDED" };
  await prismock.webhookScheduledTriggers.createMany({
    data: [
      {
        id: 1,
        subscriberUrl: "https://example.com",
        startAfter: now.add(5, "minute").toDate(),
        payload: JSON.stringify(payload),
      },
      {
        id: 2,
        subscriberUrl: "https://example.com/test",
        startAfter: now.subtract(5, "minute").toDate(),
        payload: JSON.stringify(payload),
      },
    ],
  });
  await handleWebhookScheduledTriggers(prismock);

  expectWebhookToHaveBeenCalledWith("https://example.com/test", { triggerEvent: "MEETING_ENDED", payload });
  expect(() =>
    expectWebhookToHaveBeenCalledWith("https://example.com", { triggerEvent: "MEETING_ENDED", payload })
  ).toThrow("Webhook not sent to https://example.com for MEETING_ENDED. All webhooks: []");
});

@hussamkhatib
Copy link
Contributor Author

hussamkhatib commented May 21, 2024

Great Thanks.

const body = JSON.stringify({
...(job.payload && parsedPayload),
triggerEvent: parsedPayload.triggerEvent,
payload: parsedPayload,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

payload still has different fields than for the Booking created webhook and as it's described in the docs

We need to make sure to already save the correct payload on creation of WebhookScheduledTriggers. We are sending different data than for the other triggers

@CarinaWolli
Copy link
Member

Hey @hussamkhatib, are you still trying to work on that? Let us know if you need any help 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working community Created by Linear-GitHub Sync docs area: docs, documentation, cal.com/docs High priority Created by Linear-GitHub Sync Urgent Created by Linear-GitHub Sync webhooks area: webhooks, callback, webhook payload
Projects
None yet
4 participants