* move SystemField to features * migrate workflow service * merge two tests for team repository * update imports and migrate team repository * migrate delegation credential repository * migrate credential repository * migrate entityPermissionUtils * migrate hashedLink service and repository * migrate membership service * update imports * remove file * migrate buildEventUrlFromBooking * migrate getAllUserBookings to features * update imports * update organizationMock * migrate slots * migrate date-ranges to schedules dir * migrate getAggregatedAvailability * fix * refactor * migrate useCreateEventType hook to features * migrate assignValueToUser * migrate validateUsername to auth features * migrate system field back to lib * migrate getLabelValueMapFromResponses back to lib * update imports * use relative path * fix type checks * fix * fix * fix tests * update gh codeowners * fix * fix
66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import dayjs from "@calcom/dayjs";
|
|
import { validateAndGetCorrectedUsernameInTeam } from "@calcom/features/auth/signup/utils/validateUsername";
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
export async function findTokenByToken({ token }: { token: string }) {
|
|
const foundToken = await prisma.verificationToken.findUnique({
|
|
where: {
|
|
token,
|
|
},
|
|
select: {
|
|
id: true,
|
|
expires: true,
|
|
teamId: true,
|
|
},
|
|
});
|
|
|
|
if (!foundToken) {
|
|
throw new HttpError({
|
|
statusCode: 401,
|
|
message: "Invalid Token",
|
|
});
|
|
}
|
|
|
|
return foundToken;
|
|
}
|
|
|
|
export function throwIfTokenExpired(expires?: Date) {
|
|
if (!expires) return;
|
|
if (dayjs(expires).isBefore(dayjs())) {
|
|
throw new HttpError({
|
|
statusCode: 401,
|
|
message: "Token expired",
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function validateAndGetCorrectedUsernameForTeam({
|
|
username,
|
|
email,
|
|
teamId,
|
|
isSignup,
|
|
}: {
|
|
username: string;
|
|
email: string;
|
|
teamId: number | null;
|
|
isSignup: boolean;
|
|
}) {
|
|
if (!teamId) return username;
|
|
|
|
const teamUserValidation = await validateAndGetCorrectedUsernameInTeam(username, email, teamId, isSignup);
|
|
if (!teamUserValidation.isValid) {
|
|
throw new HttpError({
|
|
statusCode: 409,
|
|
message: "Username or email is already taken",
|
|
});
|
|
}
|
|
if (!teamUserValidation.username) {
|
|
throw new HttpError({
|
|
statusCode: 422,
|
|
message: "Invalid username",
|
|
});
|
|
}
|
|
return teamUserValidation.username;
|
|
}
|