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: organization creation handler #14911

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

Conversation

Ryukemeister
Copy link
Contributor

@Ryukemeister Ryukemeister commented May 7, 2024

What does this PR do?

This PR refactors the create handler that we use for organizations. We use the same handler in order to create a platform user as well, thats why now we have two different handlers, one that handles the creation of a platform user i.e. createPlatformUser and the other one that handles the creation of an org user i.e. createOrgUser. I've tried to split everything else in smaller and more meaningful functions for better readability and understanding of what piece does what.

Copy link
Contributor

github-actions bot commented May 7, 2024

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

@keithwillcode keithwillcode added core area: core, team members only platform Anything related to our platform plan labels May 7, 2024
@graphite-app graphite-app bot requested a review from a team May 7, 2024 08:07
@Ryukemeister Ryukemeister marked this pull request as draft May 7, 2024 08:07
@dosubot dosubot bot added organizations area: organizations, orgs 💻 refactor labels May 7, 2024
Copy link

vercel bot commented May 7, 2024

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

3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
ai ⬜️ Ignored (Inspect) Visit Preview May 17, 2024 8:07am
cal ⬜️ Ignored (Inspect) Visit Preview May 17, 2024 8:07am
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview May 17, 2024 8:07am

Copy link

graphite-app bot commented May 7, 2024

Graphite Automations

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

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

Copy link
Contributor

github-actions bot commented May 7, 2024

📦 Next.js Bundle Analysis for @calcom/web

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

Three Pages Changed Size

The following pages changed size from the code in this PR compared to its base branch:

Page Size (compressed) First Load % of Budget (350 KB)
/settings/platform 261.21 KB 489.84 KB 139.95% (🟢 -0.18%)
/settings/platform/oauth-clients/[clientId]/edit 259.82 KB 488.45 KB 139.56% (🟢 -0.18%)
/settings/platform/oauth-clients/create 259.1 KB 487.73 KB 139.35% (🟢 -0.18%)
Details

Only the gzipped size is provided here based on an expert tip.

First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If next/link is used, subsequent page loads would only need to download that page's bundle (the number in the "Size" column), since the global bundle has already been downloaded.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

The "Budget %" column shows what percentage of your performance budget the First Load total takes up. For example, if your budget was 100kb, and a given page's first load size was 10kb, it would be 10% of your budget. You can also see how much this has increased or decreased compared to the base branch of your PR. If this percentage has increased by 20% or more, there will be a red status indicator applied, indicating that special attention should be given to this. If you see "+/- <0.01%" it means that there was a change in bundle size, but it is a trivial enough amount that it can be ignored.

Copy link

deploysentinel bot commented May 7, 2024

Current Playwright Test Results Summary

✅ 310 Passing - ❌ 5 Failing - ⚠️ 12 Flaky

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

(Last updated on 05/17/2024 08:23:57am UTC)

Run Details

Running Workflow PR Update on Github Actions

Commit: bee7ff3

Started: 05/17/2024 08:20:29am UTC

❌ Failures

📄   apps/web/playwright/profile.e2e.ts • 2 Failures

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Update Profile Can update a users email (verification enabled)
Retry 2Retry 1Initial Attempt
Error: page.waitForURL: Timeout 30000ms exceeded....
page.waitForURL: Timeout 30000ms exceeded.
=========================== logs ===========================
waiting for navigation to "/event-types" until "load"
  navigated to "http://localhost:3000/auth/login"
============================================================
39.83% (96) 96 / 241 runs
failed over last 7 days
31.95% (77) 77 / 241 runs
flaked over last 7 days
Update Profile Can resend verification link if the secondary email is unverified
Retry 2Retry 1Initial Attempt
Error: expect(received).toEqual(expected) // deep equality...
expect(received).toEqual(expected) // deep equality

Expected: true
Received: false
12.76% (31) 31 / 243 runs
failed over last 7 days
30.04% (73) 73 / 243 runs
flaked over last 7 days

📄   apps/web/playwright/webhook.e2e.ts • 1 Failure

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
MEETING_ENDED, MEETING_STARTED should create/remove scheduledWebhookTriggers for existing bookings
Retry 1Initial Attempt
Error: expect(received).toBe(expected) // Object.is equality...
expect(received).toBe(expected) // Object.is equality

Expected: 0
Received: 2
4.22% (7) 7 / 166 runs
failed over last 7 days
34.94% (58) 58 / 166 runs
flaked over last 7 days

📄   apps/web/playwright/team/team-invitation.e2e.ts • 1 Failure

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Team Invitation (non verified)
Retry 2Retry 1Initial Attempt
Error: expect(received).toBe(expected) // Object.is equality...
expect(received).toBe(expected) // Object.is equality

Expected: 1
Received: 0
6.99% (16) 16 / 229 runs
failed over last 7 days
4.80% (11) 11 / 229 runs
flaked over last 7 days

📄   apps/web/playwright/organization/organization-creation.e2e.ts • 1 Failure

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Organization Admin should be able to create an org where an existing user is made an owner
Retry 2Retry 1Initial Attempt
Error: Email with subject Your organization has been created not found
Email with subject Your organization has been created not found
100% (6) 6 / 6 runs
failed over last 7 days
0% (0) 0 / 6 runs
flaked over last 7 days

⚠️ Flakes

📄   apps/web/playwright/integrations-stripe.e2e.ts • 1 Flake

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Stripe integration Paid booking should be able to be rescheduled
Retry 1Initial Attempt
3.03% (6) 6 / 198 runs
failed over last 7 days
14.14% (28) 28 / 198 runs
flaked over last 7 days

📄   apps/web/playwright/event-types.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
Event Types tests -- legacy user Different Locations Tests Can remove location from multiple locations that are saved
Retry 2Retry 1Initial Attempt
3.72% (8) 8 / 215 runs
failed over last 7 days
19.53% (42) 42 / 215 runs
flaked over last 7 days
Event Types tests -- legacy user Different Locations Tests can add single organizer address location without display location public option
Retry 1Initial Attempt
0% (0) 0 / 216 runs
failed over last 7 days
5.09% (11) 11 / 216 runs
flaked over last 7 days

📄   apps/web/playwright/teams.e2e.ts • 1 Flake

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Teams - NonOrg -- legacy Can create a booking for Round Robin EventType
Retry 1Initial Attempt
7.11% (16) 16 / 225 runs
failed over last 7 days
28.89% (65) 65 / 225 runs
flaked over last 7 days

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

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Team Invitation (verified)
Retry 2Retry 1Initial Attempt
3.49% (8) 8 / 229 runs
failed over last 7 days
6.99% (16) 16 / 229 runs
flaked over last 7 days

📄   packages/embeds/embed-core/playwright/tests/namespacing.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
Namespacing Inline Embed Add inline embed using a namespace without reload
Retry 1Initial Attempt
0.47% (1) 1 / 213 run
failed over last 7 days
60.56% (129) 129 / 213 runs
flaked over last 7 days
Namespacing Inline Embed Double install Embed Snippet with inline embed using a namespace
Retry 1Initial Attempt
0.47% (1) 1 / 213 run
failed over last 7 days
59.62% (127) 127 / 213 runs
flaked over last 7 days
Namespacing Inline Embed Double install Embed Snippet with inline embed without a namespace(i.e. default namespace)
Retry 1Initial Attempt
0% (0) 0 / 213 runs
failed over last 7 days
63.38% (135) 135 / 213 runs
flaked over last 7 days
Namespacing Different namespaces can have different init configs
Retry 1Initial Attempt
0% (0) 0 / 211 runs
failed over last 7 days
60.19% (127) 127 / 211 runs
flaked over last 7 days

📄   apps/web/playwright/profile.e2e.ts • 1 Flake

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Update Profile Newly added secondary email should show as Unverified
Retry 1Initial Attempt
2.06% (5) 5 / 243 runs
failed over last 7 days
26.75% (65) 65 / 243 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 1Initial Attempt
0.47% (1) 1 / 215 run
failed over last 7 days
15.35% (33) 33 / 215 runs
flaked over last 7 days

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

Test Case Results

Test Case Last 7 days Failures Last 7 days Flakes
Bookings Team Event Can create a booking for Round Robin EventType
Retry 1Initial Attempt
8.11% (18) 18 / 222 runs
failed over last 7 days
26.58% (59) 59 / 222 runs
flaked over last 7 days

View Detailed Build Results


Copy link
Member

@sean-brydon sean-brydon left a comment

Choose a reason for hiding this comment

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

I know this is in draft so feel free to tell me to shut up

few thoughts i had when i scanned through this

@@ -38,6 +47,93 @@ const getIPAddress = async (url: string): Promise<string> => {
});
};

const checkUserIsAdmin = (
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename this to something a bit more descriptive - In my eyes checkUserISAdmin would just return a true or false but this is doing a bit more than just checking if a user is admin as it throws also

Suggested change
const checkUserIsAdmin = (
const checkUserIsAdminOrThrow = (

Comment on lines 96 to 98
const publishedTeams = teams.filter((team) => !!team.team.slug);
if (!isAdmin && publishedTeams.length < ORG_MINIMUM_PUBLISHED_TEAMS_SELF_SERVE) {
throw new TRPCError({ code: "FORBIDDEN", message: "You need to have atleast two published teams." });
Copy link
Member

Choose a reason for hiding this comment

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

Lets make this a little easier to figure out whats going on

Suggested change
const publishedTeams = teams.filter((team) => !!team.team.slug);
if (!isAdmin && publishedTeams.length < ORG_MINIMUM_PUBLISHED_TEAMS_SELF_SERVE) {
throw new TRPCError({ code: "FORBIDDEN", message: "You need to have atleast two published teams." });
if(isAdmin) return;
const publishedTeams = teams.filter((team) => !!team.team.slug);
if (publishedTeams.length < ORG_MINIMUM_PUBLISHED_TEAMS_SELF_SERVE) {
throw new TRPCError({ code: "FORBIDDEN", message: "You need to have atleast two published teams." });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, this is actually much much better

Comment on lines 337 to 357
!orgData.isPlatform &&
(await sendEmailVerification({
email: orgOwnerEmail,
language: userLocale,
username: ownerProfile.username || "",
}));

const user = await sendEmailAndEnrichProfile({
org: {
owner: organizationOwner,
id: organization.id,
name: organization.name,
ownerEmail: orgOwnerEmail,
},
slug: orgData.slug,
userName: userName,
ownerProfileUsername: ownerProfile.username,
nonOrgUsernameForOwner: null,
inputLanguageTranslation: translation,
isPlatform: orgData.isPlatform,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

not a fan of using isPlatform here and also inside of sendEmailAndEnrichProfile passed as a parameter, we should try to extract those functions and call them directly in the createOrgUser/Platform functions

Comment on lines 418 to 441
return !orgOwner
? persistOrganizationWithNonExistentOwner({
orgData: orgData,
orgOwner: orgOwner,
orgOwnerEmail: orgOwnerEmail,
userLocale: userLocale,
userName: userName,
translation: inputLanguageTranslation,
})
: persistOrganizationWithExistingUserAsOwner({
orgData: orgData,
orgOwner: orgOwner,
orgOwnerEmail: orgOwnerEmail,
userName: userName,
existingOwnerDetails: {
ownerId: orgOwner.id,
orgId: orgId,
ownerUsername: orgOwner.username,
},
translation: inputLanguageTranslation,
loggedInUserId: loggedInUser.id,
availability: availability,
});
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return !orgOwner
? persistOrganizationWithNonExistentOwner({
orgData: orgData,
orgOwner: orgOwner,
orgOwnerEmail: orgOwnerEmail,
userLocale: userLocale,
userName: userName,
translation: inputLanguageTranslation,
})
: persistOrganizationWithExistingUserAsOwner({
orgData: orgData,
orgOwner: orgOwner,
orgOwnerEmail: orgOwnerEmail,
userName: userName,
existingOwnerDetails: {
ownerId: orgOwner.id,
orgId: orgId,
ownerUsername: orgOwner.username,
},
translation: inputLanguageTranslation,
loggedInUserId: loggedInUser.id,
availability: availability,
});
};
const persistParams = {
orgData: orgData,
orgOwner: orgOwner,
orgOwnerEmail: orgOwnerEmail,
userName: userName,
existingOwnerDetails: {
ownerId: orgOwner.id,
orgId: orgId,
ownerUsername: orgOwner.username,
},
translation: inputLanguageTranslation,
loggedInUserId: loggedInUser.id,
availability: availability,
};
return !orgOwner
? persistOrganizationWithNonExistentOwner(persistParams)
: persistOrganizationWithExistingUserAsOwner(persistParams);

Comment on lines 511 to 537
const user = isPlatform
? await createPlatformUser({
userId: ctx.user.id,
name: name,
slug: slug,
orgOwnerEmail: orgOwnerEmail,
seats: seats,
pricePerSeat: pricePerSeat,
isPlatform: isPlatform,
inputLanguageTranslation: inputLanguageTranslation,
availability: availability,
autoAcceptEmail: autoAcceptEmail,
ctx: { userLocale: ctx.user.locale, userName: ctx.user.name, orgId: ctx.user.profile.organizationId },
})
: await createOrgUser({
userId: ctx.user.id,
name: name,
slug: slug,
orgOwnerEmail: orgOwnerEmail,
seats: seats,
pricePerSeat: pricePerSeat,
isPlatform: isPlatform,
inputLanguageTranslation: inputLanguageTranslation,
availability: availability,
autoAcceptEmail: autoAcceptEmail,
ctx: { userLocale: ctx.user.locale, userName: ctx.user.name, orgId: ctx.user.profile.organizationId },
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const user = isPlatform
? await createPlatformUser({
userId: ctx.user.id,
name: name,
slug: slug,
orgOwnerEmail: orgOwnerEmail,
seats: seats,
pricePerSeat: pricePerSeat,
isPlatform: isPlatform,
inputLanguageTranslation: inputLanguageTranslation,
availability: availability,
autoAcceptEmail: autoAcceptEmail,
ctx: { userLocale: ctx.user.locale, userName: ctx.user.name, orgId: ctx.user.profile.organizationId },
})
: await createOrgUser({
userId: ctx.user.id,
name: name,
slug: slug,
orgOwnerEmail: orgOwnerEmail,
seats: seats,
pricePerSeat: pricePerSeat,
isPlatform: isPlatform,
inputLanguageTranslation: inputLanguageTranslation,
availability: availability,
autoAcceptEmail: autoAcceptEmail,
ctx: { userLocale: ctx.user.locale, userName: ctx.user.name, orgId: ctx.user.profile.organizationId },
});
const createUserParams = {
userId: ctx.user.id,
name: name,
slug: slug,
orgOwnerEmail: orgOwnerEmail,
seats: seats,
pricePerSeat: pricePerSeat,
isPlatform: isPlatform,
inputLanguageTranslation: inputLanguageTranslation,
availability: availability,
autoAcceptEmail: autoAcceptEmail,
ctx: { userLocale: ctx.user.locale, userName: ctx.user.name, orgId: ctx.user.profile.organizationId },
}
const user = isPlatform
? await createPlatformUser(createUserParams)
: await createOrgUser(createUserParams);

userName,
translation,
}: PersistOrganizationWithNonExistentOwnerProps) => {
let organizationOwner = orgOwner;
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure to understand why we need to do this, organizationOwner is fully replaced with data.orgOwner,

the orgOwner passed as a param of this function does not seem to be used at all really

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core area: core, team members only organizations area: organizations, orgs platform Anything related to our platform plan 💻 refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants