refactor: Remove circular deps between @calcom/lib and @calcom/features [2] (#24438)
* 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
This commit is contained in:
1 parent
79b319c1dd
commit
e91bf53d80
145 files changed
+412
-405
No files matched your search
@@ -32,7 +32,7 @@ import logger from "@calcom/lib/logger";
|
||||
import { randomString } from "@calcom/lib/random";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { hashEmail } from "@calcom/lib/server/PiiHasher";
|
||||
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
|
||||
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
|
||||
import { DeploymentRepository } from "@calcom/lib/server/repository/deployment";
|
||||
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
|
||||
import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository";
|
||||
|
||||
@@ -6,6 +6,7 @@ import { getLocaleFromRequest } from "@calcom/features/auth/lib/getLocaleFromReq
|
||||
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
||||
import { createOrUpdateMemberships } from "@calcom/features/auth/signup/utils/createOrUpdateMemberships";
|
||||
import { prefillAvatar } from "@calcom/features/auth/signup/utils/prefillAvatar";
|
||||
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/features/auth/signup/utils/validateUsername";
|
||||
import { StripeBillingService } from "@calcom/features/ee/billing/stripe-billing-service";
|
||||
import { sentrySpan } from "@calcom/features/watchlist/lib/telemetry";
|
||||
import { checkIfEmailIsBlockedInWatchlistController } from "@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller";
|
||||
@@ -15,7 +16,6 @@ import { HttpError } from "@calcom/lib/http-error";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import type { CustomNextApiHandler } from "@calcom/lib/server/username";
|
||||
import { usernameHandler } from "@calcom/lib/server/username";
|
||||
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/lib/validateUsername";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { CreationSource } from "@calcom/prisma/enums";
|
||||
import { IdentityProvider } from "@calcom/prisma/enums";
|
||||
|
||||
@@ -3,12 +3,12 @@ import { NextResponse } from "next/server";
|
||||
import { checkPremiumUsername } from "@calcom/ee/common/lib/checkPremiumUsername";
|
||||
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
||||
import { createOrUpdateMemberships } from "@calcom/features/auth/signup/utils/createOrUpdateMemberships";
|
||||
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/features/auth/signup/utils/validateUsername";
|
||||
import { hashPassword } from "@calcom/lib/auth/hashPassword";
|
||||
import { IS_PREMIUM_USERNAME_ENABLED } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { isUsernameReservedDueToMigration } from "@calcom/lib/server/username";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/lib/validateUsername";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { IdentityProvider } from "@calcom/prisma/enums";
|
||||
import { signupSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { validateAndGetCorrectedUsernameInTeam } from "@calcom/features/auth/signup/utils/validateUsername";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { validateAndGetCorrectedUsernameInTeam } from "@calcom/lib/validateUsername";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
export async function findTokenByToken({ token }: { token: string }) {
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import { getOrgUsernameFromEmail } from "@calcom/features/auth/signup/utils/getOrgUsernameFromEmail";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
export const getUsernameForOrgMember = async ({
|
||||
email,
|
||||
orgAutoAcceptEmail,
|
||||
isSignup,
|
||||
username,
|
||||
}: {
|
||||
username?: string;
|
||||
email: string;
|
||||
orgAutoAcceptEmail?: string;
|
||||
isSignup: boolean;
|
||||
}) => {
|
||||
if (isSignup) {
|
||||
// We ensure that the username is always derived from the email during signup.
|
||||
return getOrgUsernameFromEmail(email, orgAutoAcceptEmail || "");
|
||||
}
|
||||
if (!username) {
|
||||
throw new Error("Username is required");
|
||||
}
|
||||
// Right now it's not possible to change username in an org by the user but when that's allowed we would simply accept the provided username
|
||||
return username;
|
||||
};
|
||||
|
||||
export const validateAndGetCorrectedUsernameAndEmail = async ({
|
||||
username,
|
||||
email,
|
||||
organizationId,
|
||||
orgAutoAcceptEmail,
|
||||
isSignup,
|
||||
}: {
|
||||
username: string;
|
||||
email: string;
|
||||
organizationId?: number;
|
||||
orgAutoAcceptEmail?: string;
|
||||
isSignup: boolean;
|
||||
}) => {
|
||||
if (username.includes("+")) {
|
||||
return { isValid: false, username: undefined, email };
|
||||
}
|
||||
// There is an existingUser if, within an org context or not, the username matches
|
||||
// OR if the email matches AND either the email is verified
|
||||
// or both username and password are set
|
||||
const existingUser = await prisma.user.findFirst({
|
||||
where: {
|
||||
...(organizationId ? { organizationId } : {}),
|
||||
OR: [
|
||||
// When inviting to org, invited user gets created with username now, so in an org context we
|
||||
// can't check for username as it will exist on signup
|
||||
...(!organizationId ? [{ username }] : [{}]),
|
||||
{
|
||||
AND: [
|
||||
{ email },
|
||||
{
|
||||
OR: [
|
||||
{ emailVerified: { not: null } },
|
||||
{ AND: [{ password: { isNot: null } }, { username: { not: null } }] },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
});
|
||||
let validatedUsername = username;
|
||||
if (organizationId) {
|
||||
validatedUsername = await getUsernameForOrgMember({
|
||||
email,
|
||||
orgAutoAcceptEmail,
|
||||
isSignup,
|
||||
});
|
||||
}
|
||||
|
||||
return { isValid: !existingUser, username: validatedUsername, email: existingUser?.email };
|
||||
};
|
||||
|
||||
export const validateAndGetCorrectedUsernameInTeam = async (
|
||||
username: string,
|
||||
email: string,
|
||||
teamId: number,
|
||||
isSignup: boolean
|
||||
) => {
|
||||
try {
|
||||
const team = await prisma.team.findUnique({
|
||||
where: {
|
||||
id: teamId,
|
||||
},
|
||||
select: {
|
||||
metadata: true,
|
||||
isOrganization: true,
|
||||
parentId: true,
|
||||
organizationSettings: true,
|
||||
parent: {
|
||||
select: {
|
||||
organizationSettings: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log("validateAndGetCorrectedUsernameInTeam", {
|
||||
teamId,
|
||||
team,
|
||||
});
|
||||
const organization = team?.isOrganization ? team : team?.parent;
|
||||
if (organization) {
|
||||
// Organization context -> org-context username check
|
||||
const orgId = team?.parentId || teamId;
|
||||
return validateAndGetCorrectedUsernameAndEmail({
|
||||
username,
|
||||
email,
|
||||
organizationId: orgId,
|
||||
orgAutoAcceptEmail: organization?.organizationSettings?.orgAutoAcceptEmail || "",
|
||||
isSignup,
|
||||
});
|
||||
} else {
|
||||
// Regular team context -> regular username check
|
||||
return validateAndGetCorrectedUsernameAndEmail({ username, email, isSignup });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return { isValid: false, username: undefined, email: undefined };
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user