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: team billing on batch operations #15570

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { updateQuantitySubscriptionFromStripe } from "@calcom/features/ee/teams/lib/payments";
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import { prisma } from "@calcom/prisma";
Expand Down Expand Up @@ -62,7 +63,7 @@ export async function bulkDeleteUsersHandler({ ctx, input }: BulkDeleteUsersHand
// We do this in a transaction to make sure that all memberships are removed before we remove the organization relation from the user
// We also do this to make sure that if one of the queries fail, the whole transaction fails
await prisma.$transaction([removeProfiles, deleteMany, removeOrgrelation]);

await updateQuantitySubscriptionFromStripe(currentUser.organizationId);
Copy link
Member Author

Choose a reason for hiding this comment

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

Batch deleting didn't update Stripe subscription before.

return {
success: true,
usersDeleted: input.userIds.length,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ import type { TInviteMemberInputSchema } from "./inviteMember.schema";
import type { TeamWithParent } from "./types";
import {
checkPermissions,
getTeamOrThrow,
getUniqueUsernameOrEmailsOrThrow,
createMemberships,
createNewUsersConnectToOrgIfExists,
getExistingUsersToInvite,
getOrgConnectionInfo,
getOrgState,
sendSignupToOrganizationEmail,
getExistingUsersToInvite,
createNewUsersConnectToOrgIfExists,
createMemberships,
getTeamOrThrow,
getUniqueUsernameOrEmailsOrThrow,
groupUsersByJoinability,
sendExistingUserTeamInviteEmails,
sendEmails,
INVITE_STATUS,
sendEmails,
sendExistingUserTeamInviteEmails,
sendSignupToOrganizationEmail,
} from "./utils";

const log = logger.getSubLogger({ prefix: ["inviteMember.handler"] });
Expand Down Expand Up @@ -151,7 +151,7 @@ export const inviteMemberHandler = async ({ ctx, input }: InviteMemberOptions) =
isOrg: input.isOrg,
});
});
sendEmails(sendVerifEmailsPromises);
await sendEmails(sendVerifEmailsPromises);
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this was causing a race condition that caused to report a wrong membership count

}

const organization = ctx.user.profile.organization;
Expand All @@ -166,20 +166,20 @@ export const inviteMemberHandler = async ({ ctx, input }: InviteMemberOptions) =
orgSlug,
});

if (IS_TEAM_BILLING_ENABLED) {
if (team.parentId) {
await updateQuantitySubscriptionFromStripe(team.parentId);
} else {
await updateQuantitySubscriptionFromStripe(input.teamId);
}
}
await handleSubscriptionUpdates(team.parentId || input.teamId);
Copy link
Member Author

Choose a reason for hiding this comment

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

Refactored for denesting


return {
...input,
numUsersInvited:
existingUsersWithMembershipsThatNeedToBeInvited.length + newUsersEmailsOrUsernames.length,
};
};

async function handleSubscriptionUpdates(teamId: number) {
if (!IS_TEAM_BILLING_ENABLED) return;
await updateQuantitySubscriptionFromStripe(teamId);
}

export default inviteMemberHandler;

async function handleExistingUsersInvites({
Expand Down
Loading