* remove barrel export for client * fix more imports * remove server barrel * remove more barrel exporst and fix tssconfig * fix types * fix imports for removed barrels * fig ssg createserverside helpers * restore lock * new yarn lock * remove barel exports * remove barrel * replace client with react in tsconfig * fix yarn lock * Update exports for trpc to not be types * add ENDPOINTS export to the correct location * other exports fixes lost due to barrel * fix client error export * more imports --------- Co-authored-by: hbjORbj <[email protected]> Co-authored-by: Keith Williams <[email protected]>
90 lines
2.3 KiB
TypeScript
90 lines
2.3 KiB
TypeScript
import prisma from "@calcom/prisma";
|
|
import { SMSLockState } from "@calcom/prisma/enums";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { RateLimitHelper } from "./rateLimit";
|
|
import { rateLimiter } from "./rateLimit";
|
|
|
|
export async function checkRateLimitAndThrowError({
|
|
rateLimitingType = "core",
|
|
identifier,
|
|
onRateLimiterResponse,
|
|
opts,
|
|
}: RateLimitHelper) {
|
|
const response = await rateLimiter()({ rateLimitingType, identifier, opts });
|
|
const { success, reset } = response;
|
|
|
|
if (onRateLimiterResponse) onRateLimiterResponse(response);
|
|
|
|
if (!success) {
|
|
const convertToSeconds = (ms: number) => Math.floor(ms / 1000);
|
|
const secondsToWait = convertToSeconds(reset - Date.now());
|
|
throw new TRPCError({
|
|
code: "TOO_MANY_REQUESTS",
|
|
message: `Rate limit exceeded. Try again in ${secondsToWait} seconds.`,
|
|
});
|
|
}
|
|
return response;
|
|
}
|
|
|
|
export async function checkSMSRateLimit({
|
|
rateLimitingType = "sms",
|
|
identifier,
|
|
onRateLimiterResponse,
|
|
opts,
|
|
}: RateLimitHelper) {
|
|
const response = await rateLimiter()({ rateLimitingType, identifier, opts });
|
|
const { success } = response;
|
|
|
|
if (onRateLimiterResponse) onRateLimiterResponse(response);
|
|
|
|
if (!success) {
|
|
await changeSMSLockState(
|
|
identifier,
|
|
rateLimitingType === "sms" ? SMSLockState.LOCKED : SMSLockState.REVIEW_NEEDED
|
|
);
|
|
}
|
|
}
|
|
|
|
async function changeSMSLockState(identifier: string, status: SMSLockState) {
|
|
let userId, teamId;
|
|
|
|
if (identifier.startsWith("sms:user:")) {
|
|
userId = Number(identifier.slice(9));
|
|
} else if (identifier.startsWith("sms:team:")) {
|
|
teamId = Number(identifier.slice(9));
|
|
}
|
|
|
|
if (userId) {
|
|
const user = await prisma.user.findUnique({ where: { id: userId, profiles: { none: {} } } });
|
|
if (user?.smsLockReviewedByAdmin) return;
|
|
|
|
await prisma.user.update({
|
|
where: {
|
|
id: userId,
|
|
profiles: { none: {} },
|
|
},
|
|
data: {
|
|
smsLockState: status,
|
|
},
|
|
});
|
|
} else {
|
|
const team = await prisma.team.findUnique({
|
|
where: { id: teamId, parentId: null, isOrganization: false },
|
|
});
|
|
if (team?.smsLockReviewedByAdmin) return;
|
|
|
|
await prisma.team.update({
|
|
where: {
|
|
id: teamId,
|
|
parentId: null,
|
|
isOrganization: false,
|
|
},
|
|
data: {
|
|
smsLockState: status,
|
|
},
|
|
});
|
|
}
|
|
}
|