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

refactor: removed isNotAnApiCall #13568

Merged
merged 5 commits into from
Feb 7, 2024
Merged

Conversation

zomars
Copy link
Member

@zomars zomars commented Feb 6, 2024

What does this PR do?

  • Since the API handler is the odd one out. It should only pass the desired schema instead of doing isNotAnApiCall: true in all other calls.
  • Removes the need for isNotAnApiCall in code it makes the logic unnecessarily harder to read.
  • Allows handleNewBooking to receive an optional schema for the request body. Fallbacks to the default one if none is passed.
  • Split api specific logic to it's own file

Type of change

  • Chore (refactoring code, technical debt, workflow improvements)

How should this be tested?

  • yarn build
  • yarn e2e

Mandatory Tasks

  • Make sure you have self-reviewed the code. A decent size PR without self-review might be rejected.

Copy link

vercel bot commented Feb 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
api 🔄 Building (Inspect) Visit Preview Feb 7, 2024 0:07am
6 Ignored Deployments
Name Status Preview Comments Updated (UTC)
ai ⬜️ Ignored (Inspect) Visit Preview Feb 7, 2024 0:07am
cal ⬜️ Ignored (Inspect) Visit Preview Feb 7, 2024 0:07am
cal-demo ⬜️ Ignored (Inspect) Feb 7, 2024 0:07am
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview Feb 7, 2024 0:07am
qa ⬜️ Ignored (Inspect) Visit Preview Feb 7, 2024 0:07am
ui ⬜️ Ignored (Inspect) Visit Preview Feb 7, 2024 0:07am

Copy link
Contributor

github-actions bot commented Feb 6, 2024

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

@keithwillcode keithwillcode added api area: API, enterprise API, access token, OAuth core area: core, team members only foundation labels Feb 6, 2024
Copy link
Contributor

github-actions bot commented Feb 6, 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! 🙌

Copy link

deploysentinel bot commented Feb 6, 2024

Current Playwright Test Results Summary

✅ 440 Passing - ⚠️ 15 Flaky

Run may still be in progress, this comment will be updated as current testing workflow or job completes...

(Last updated on 02/06/2024 11:27:31pm UTC)

Run Details

Running Workflow PR Update on Github Actions

Commit: 074e7bb

Started: 02/06/2024 11:18:42pm UTC

⚠️ Flakes

📄   apps/web/playwright/booking-pages.e2e.ts • 2 Flakes

Top 1 Common Error Messages

null

2 Test Cases Affected

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
pro user -- future Time slots should be reserved when selected
Retry 1Initial Attempt
0.76% (2) 2 / 264 runs
failed over last 7 days
5.68% (15) 15 / 264 runs
flaked over last 7 days
pro user -- legacy Time slots should be reserved when selected
Retry 1Initial Attempt
0% (0) 0 / 258 runs
failed over last 7 days
4.26% (11) 11 / 258 runs
flaked over last 7 days

📄   apps/web/playwright/booking/longTextQuestion.e2e.ts • 4 Flakes

Top 1 Common Error Messages

null

4 Test Cases Affected

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Booking With Long Text Question and Each Other Question Booking With Long Text Question and multiselect Question Long Text required and multiselect text not required
Retry 1Initial Attempt
0% (0) 0 / 300 runs
failed over last 7 days
3% (9) 9 / 300 runs
flaked over last 7 days
Booking With Long Text Question and Each Other Question Booking With Long Text Question and Number Question Long Text and Number required
Retry 1Initial Attempt
0.33% (1) 1 / 300 run
failed over last 7 days
4.33% (13) 13 / 300 runs
flaked over last 7 days
Booking With Long Text Question and Each Other Question Long Text required and Number not required
Retry 1Initial Attempt
0% (0) 0 / 300 runs
failed over last 7 days
5% (15) 15 / 300 runs
flaked over last 7 days
Booking With Long Text Question and Each Other Question Booking With Long Text Question and Short text question Long Text required and Short text not required
Retry 1Initial Attempt
0% (0) 0 / 294 runs
failed over last 7 days
4.42% (13) 13 / 294 runs
flaked over last 7 days

📄   apps/web/playwright/booking/selectQuestion.e2e.ts • 2 Flakes

Top 1 Common Error Messages

null

2 Test Cases Affected

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Booking With Phone Question and Each Other Question Booking With Select Question and Multi email Question Select and Multi email not required
Retry 1Initial Attempt
0.35% (1) 1 / 283 run
failed over last 7 days
2.83% (8) 8 / 283 runs
flaked over last 7 days
Booking With Phone Question and Each Other Question Booking With Select Question and Number Question Select and Number not required
Retry 1Initial Attempt
0% (0) 0 / 282 runs
failed over last 7 days
3.90% (11) 11 / 282 runs
flaked over last 7 days

📄   apps/web/playwright/booking/responsiveBooking.e2e.ts • 1 Flake

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Booking page with no questions Booking page with 1920x1080 resolution
Retry 1Initial Attempt
0.35% (1) 1 / 289 run
failed over last 7 days
3.46% (10) 10 / 289 runs
flaked over last 7 days

📄   apps/web/playwright/organization/organization-invitation.e2e.ts • 1 Flake

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Organization Email matching orgAutoAcceptEmail and a Verified Organization Org Invitation
Retry 2Retry 1Initial Attempt
12.70% (40) 40 / 315 runs
failed over last 7 days
6.98% (22) 22 / 315 runs
flaked over last 7 days

📄   apps/web/playwright/booking/phoneQuestion.e2e.ts • 3 Flakes

Top 1 Common Error Messages

null

3 Test Cases Affected

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Booking With Phone Question and Each Other Question Booking With Phone Question and Address Question Booking With Phone Question and Number Question Phone and Number required
Retry 1Initial Attempt
0.65% (2) 2 / 310 runs
failed over last 7 days
6.13% (19) 19 / 310 runs
flaked over last 7 days
Booking With Phone Question and Each Other Question Booking With Phone Question and Address Question Booking With Phone Question and Number Question Phone required and Number not required
Retry 1Initial Attempt
0.32% (1) 1 / 310 run
failed over last 7 days
6.13% (19) 19 / 310 runs
flaked over last 7 days
Booking With Phone Question and Each Other Question Booking With Phone Question and Address Question Booking With Phone Question and Short text question Phone required and Short text not required
Retry 1Initial Attempt
0% (0) 0 / 308 runs
failed over last 7 days
6.82% (21) 21 / 308 runs
flaked over last 7 days

📄   apps/web/playwright/event-types.e2e.ts • 1 Flake

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Event Types tests -- future user can add multiple organizer address
Retry 1Initial Attempt
0.60% (2) 2 / 335 runs
failed over last 7 days
17.91% (60) 60 / 335 runs
flaked over last 7 days

📄   packages/app-store/routing-forms/playwright/tests/basic.e2e.ts • 1 Flake

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Routing Forms Seeded Routing Form Router URL should work
Retry 2Retry 1Initial Attempt
1.83% (6) 6 / 327 runs
failed over last 7 days
13.15% (43) 43 / 327 runs
flaked over last 7 days

View Detailed Build Results


Copy link
Member Author

@zomars zomars left a comment

Choose a reason for hiding this comment

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

Self review done

@@ -206,7 +207,7 @@ async function handler(req: NextApiRequest) {
const { userId, isAdmin } = req;
if (isAdmin) req.userId = req.body.userId || userId;

return await handleNewBooking(req);
return await handleNewBooking(req, getBookingDataSchemaForApi);
Copy link
Member Author

Choose a reason for hiding this comment

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

Since the API handler is the odd one out. It should only pass the desired schema instead of doing isNotAnApiCall: true in all other calls.

eventType: event?.data,
bookingFields: event.data.bookingFields,
Copy link
Member Author

Choose a reason for hiding this comment

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

We just need bookingFields in here so there's no need to pass the whole eventType

Copy link
Contributor

Choose a reason for hiding this comment

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

Great fix for Law of Demeter here 🙏🏼

Comment on lines -29 to -76
: bookingCreateBodySchemaForApi
.merge(
z.object({
responses: responsesSchema.optional(),
})
)
.superRefine((val, ctx) => {
if (val.responses && val.customInputs) {
ctx.addIssue({
code: "custom",
message:
"Don't use both customInputs and responses. `customInputs` is only there for legacy support.",
});
return;
}
const legacyProps = Object.keys(bookingCreateSchemaLegacyPropsForApi.shape);

if (val.responses) {
const unwantedProps: string[] = [];
legacyProps.forEach((legacyProp) => {
if (typeof val[legacyProp as keyof typeof val] !== "undefined") {
console.error(
`Deprecated: Unexpected falsy value for: ${unwantedProps.join(
","
)}. They can't be used with \`responses\`. This will become a 400 error in the future.`
);
}
if (val[legacyProp as keyof typeof val]) {
unwantedProps.push(legacyProp);
}
});
if (unwantedProps.length) {
ctx.addIssue({
code: "custom",
message: `Legacy Props: ${unwantedProps.join(",")}. They can't be used with \`responses\``,
});
return;
}
} else if (val.customInputs) {
const { success } = bookingCreateSchemaLegacyPropsForApi.safeParse(val);
if (!success) {
ctx.addIssue({
code: "custom",
message: `With \`customInputs\` you must specify legacy props ${legacyProps.join(",")}`,
});
}
}
});
Copy link
Member Author

Choose a reason for hiding this comment

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

Split API specific schema to packages/features/bookings/lib/getBookingDataSchemaForApi.ts

import type { getBookingFieldsWithSystemFields } from "./getBookingFields";
import getBookingResponsesSchema from "./getBookingResponsesSchema";

const getBookingDataSchemaForApi = ({
Copy link
Member Author

Choose a reason for hiding this comment

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

Self explanatory

@@ -990,25 +990,23 @@ describe("getBookingResponsesSchema", () => {
describe("getBookingResponsesPartialSchema - Prefill validation", () => {
test(`should be able to get fields prefilled even when name is empty string`, async ({}) => {
const schema = getBookingResponsesPartialSchema({
eventType: {
Copy link
Member Author

Choose a reason for hiding this comment

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

Denesting as well

};
} else {
Copy link
Member Author

Choose a reason for hiding this comment

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

Elses are unnecessary with early returns

Comment on lines -979 to -985
{
isNotAnApiCall = false,
}: {
isNotAnApiCall?: boolean;
} = {
isNotAnApiCall: false,
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for your service 🙏🏽

} = {
isNotAnApiCall: false,
}
bookingDataSchemaGetter: BookingDataSchemaGetter = getBookingDataSchema
Copy link
Member Author

Choose a reason for hiding this comment

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

If there no schema getter we fallback to the default one.

Comment on lines +1000 to +1003
const bookingDataSchema = bookingDataSchemaGetter({
view: req.body?.rescheduleUid ? "reschedule" : "booking",
bookingFields: eventType.bookingFields,
});
Copy link
Member Author

Choose a reason for hiding this comment

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

Here we retrieve the schema

const bookingData = await getBookingData({
req,
eventType,
schema: bookingDataSchema,
Copy link
Member Author

Choose a reason for hiding this comment

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

And pass it to getBookingData

@zomars zomars requested a review from a team February 6, 2024 22:34
@zomars zomars marked this pull request as ready for review February 6, 2024 22:35
@zomars zomars merged commit d2b4760 into main Feb 7, 2024
39 checks passed
@zomars zomars deleted the refactor/handle-new-booking-api-schemas branch February 7, 2024 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api area: API, enterprise API, access token, OAuth core area: core, team members only foundation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants