fix: getTeamOrThrow ran on every request (#12337)

* fix: getTeamOrThrow ran on every request

This creates unnecessary strain on the DB when someone is hitting the endpoint a lot.

* Add rate limit

* Update packages/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler.ts

---------

Co-authored-by: Sean Brydon <sean@cal.com>
Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com>
This commit is contained in:
Alex van Andel
2023-11-13 13:45:46 +00:00
committed by GitHub
co-authored by Sean Brydon sean-brydon
parent 199f41fb1a
commit 63f2f6edba
@@ -2,6 +2,7 @@ import { randomBytes } from "crypto";
import { sendTeamInviteEmail } from "@calcom/emails";
import { updateQuantitySubscriptionFromStripe } from "@calcom/features/ee/teams/lib/payments";
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
import { getTranslation } from "@calcom/lib/server/i18n";
import { prisma } from "@calcom/prisma";
@@ -32,8 +33,9 @@ type InviteMemberOptions = {
};
export const inviteMemberHandler = async ({ ctx, input }: InviteMemberOptions) => {
const team = await getTeamOrThrow(input.teamId, input.isOrg);
const { autoAcceptEmailDomain, orgVerified } = getIsOrgVerified(input.isOrg, team);
await checkRateLimitAndThrowError({
identifier: `invitedBy:${ctx.user.id}`,
});
await checkPermissions({
userId: ctx.user.id,
@@ -42,6 +44,9 @@ export const inviteMemberHandler = async ({ ctx, input }: InviteMemberOptions) =
isOrg: input.isOrg,
});
const team = await getTeamOrThrow(input.teamId, input.isOrg);
const { autoAcceptEmailDomain, orgVerified } = getIsOrgVerified(input.isOrg, team);
const translation = await getTranslation(input.language ?? "en", "common");
const emailsToInvite = await getEmailsToInvite(input.usernameOrEmail);