46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { CreditsRepository } from "@calcom/features/credits/repositories/CreditsRepository";
|
|
import { sendVerificationCode } from "@calcom/features/ee/workflows/lib/reminders/verifyPhoneNumber";
|
|
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
|
|
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import { hasTeamPlanHandler } from "../teams/hasTeamPlan.handler";
|
|
import type { TSendVerificationCodeInputSchema } from "./sendVerificationCode.schema";
|
|
|
|
type SendVerificationCodeOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TSendVerificationCodeInputSchema;
|
|
};
|
|
|
|
export const sendVerificationCodeHandler = async ({ ctx, input }: SendVerificationCodeOptions) => {
|
|
const { user } = ctx;
|
|
|
|
const isCurrentUsernamePremium =
|
|
user && hasKeyInMetadata(user, "isPremium") ? !!user.metadata.isPremium : false;
|
|
|
|
const creditBalance = await CreditsRepository.findCreditBalance({ userId: user.id });
|
|
const hasNoAdditionalCredits = !!creditBalance && creditBalance.additionalCredits <= 0;
|
|
|
|
let isTeamsPlan = false;
|
|
if (!isCurrentUsernamePremium) {
|
|
const { hasTeamPlan } = await hasTeamPlanHandler({ ctx });
|
|
isTeamsPlan = !!hasTeamPlan;
|
|
}
|
|
|
|
if (!isCurrentUsernamePremium && !isTeamsPlan && hasNoAdditionalCredits) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
|
|
await checkRateLimitAndThrowError({
|
|
identifier: `sendVerificationCode:${user.id}`,
|
|
rateLimitingType: "core",
|
|
});
|
|
|
|
const { phoneNumber } = input;
|
|
return sendVerificationCode(phoneNumber);
|
|
};
|