* fix: dont check OrganizationSettings.orgAutoAcceptEmail uniqueness for platform teams * feat: display organizationId in OAuthClientCard * feat: ApiAuthStrategy handle oauth credentials * refactor: clean up ApiAuthStrategy test * refactor: use ApiAuthGuard in OauthClientsUsersController and OAuthFlowController * fix: copying org id * refactor: more specific error message when api auth * refactor: auto accept team creator membership * feat: useTeamEventType and useTeamEventTypes hooks * refactor: make team event-types public & searchable by eventSlug * feat: include host name in team event-types hosts response * fix: isFixed=true by default for COLLECTIVE event hosts * fix: useTeamEventType eventSlug access * feat: BookerPlatformWrapper enable team events by exposting orgId and teamId props * refactor: provide orgId in atoms context * refactor: use orgId from context in team event-types hooks * refactor: return teams in useMe * chore: examples app teams setup * Revert "refactor: return teams in useMe" This reverts commit de992ddc9af6ee9a2111938069f5b9c34cc2d8ea. * Revert "chore: examples app teams setup" This reverts commit 0766aa21acc25efa2361d38c3f87ddba773a0245. * feat: useTeams hook * chore: setup examples app with team event-type * fix: small fixes * swagger * Revert "refactor: provide orgId in atoms context" This reverts commit f053a498ee6f8fa8ece5ec8d8630c59eda8873e3. * feat: orgId in atoms context * feat: PlatformBilling guard * chore: delete test of the deleted oauth-client-credentials.guard * refactor: org event-types collective events isFixed always true and priority medium * refactor: org event-types COLLECTIVE response ignore isFixed and priority as they are same * fix: organizations event-types e2e * fix: billing guard * refactor: tests cleanup * fix: platform plan guard spec * refactor: e2e test cleanup * seed error if not team * refactor: rename authenticateApiKey to authenticateBearerToken * refactor: transforming response hosts * refactor: rename findUniqueByMatchingAutoAcceptEmail to findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail * refactor: rename findUniqueByMatchingAutoAcceptEmail to findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail --------- Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
196 lines
5.4 KiB
TypeScript
196 lines
5.4 KiB
TypeScript
import { getOrgUsernameFromEmail } from "@calcom/features/auth/signup/utils/getOrgUsernameFromEmail";
|
|
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
|
import logger from "@calcom/lib/logger";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import { prisma } from "@calcom/prisma";
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
|
|
|
import { createAProfileForAnExistingUser } from "../../createAProfileForAnExistingUser";
|
|
import { getParsedTeam } from "./teamUtils";
|
|
import { UserRepository } from "./user";
|
|
|
|
const orgSelect = {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
logoUrl: true,
|
|
};
|
|
|
|
export class OrganizationRepository {
|
|
static async createWithExistingUserAsOwner({
|
|
orgData,
|
|
owner,
|
|
}: {
|
|
orgData: {
|
|
name: string;
|
|
slug: string;
|
|
isOrganizationConfigured: boolean;
|
|
isOrganizationAdminReviewed: boolean;
|
|
autoAcceptEmail: string;
|
|
seats: number | null;
|
|
pricePerSeat: number | null;
|
|
isPlatform: boolean;
|
|
billingPeriod?: "MONTHLY" | "ANNUALLY";
|
|
};
|
|
owner: {
|
|
id: number;
|
|
email: string;
|
|
nonOrgUsername: string;
|
|
};
|
|
}) {
|
|
logger.debug("createWithExistingUserAsOwner", safeStringify({ orgData, owner }));
|
|
const organization = await this.create(orgData);
|
|
const ownerProfile = await createAProfileForAnExistingUser({
|
|
user: {
|
|
id: owner.id,
|
|
email: owner.email,
|
|
currentUsername: owner.nonOrgUsername,
|
|
},
|
|
organizationId: organization.id,
|
|
});
|
|
|
|
await prisma.membership.create({
|
|
data: {
|
|
userId: owner.id,
|
|
role: MembershipRole.OWNER,
|
|
accepted: true,
|
|
teamId: organization.id,
|
|
},
|
|
});
|
|
return { organization, ownerProfile };
|
|
}
|
|
|
|
static async createWithNonExistentOwner({
|
|
orgData,
|
|
owner,
|
|
}: {
|
|
orgData: {
|
|
name: string;
|
|
slug: string;
|
|
isOrganizationConfigured: boolean;
|
|
isOrganizationAdminReviewed: boolean;
|
|
autoAcceptEmail: string;
|
|
seats: number | null;
|
|
billingPeriod?: "MONTHLY" | "ANNUALLY";
|
|
pricePerSeat: number | null;
|
|
isPlatform: boolean;
|
|
};
|
|
owner: {
|
|
email: string;
|
|
};
|
|
}) {
|
|
logger.debug("createWithNonExistentOwner", safeStringify({ orgData, owner }));
|
|
const organization = await this.create(orgData);
|
|
const ownerUsernameInOrg = getOrgUsernameFromEmail(owner.email, orgData.autoAcceptEmail);
|
|
const ownerInDb = await UserRepository.create({
|
|
email: owner.email,
|
|
username: ownerUsernameInOrg,
|
|
organizationId: organization.id,
|
|
});
|
|
|
|
await prisma.membership.create({
|
|
data: {
|
|
userId: ownerInDb.id,
|
|
role: MembershipRole.OWNER,
|
|
accepted: true,
|
|
teamId: organization.id,
|
|
},
|
|
});
|
|
|
|
return {
|
|
orgOwner: ownerInDb,
|
|
organization,
|
|
ownerProfile: {
|
|
username: ownerUsernameInOrg,
|
|
},
|
|
};
|
|
}
|
|
|
|
static async create(orgData: {
|
|
name: string;
|
|
slug: string;
|
|
isOrganizationConfigured: boolean;
|
|
isOrganizationAdminReviewed: boolean;
|
|
autoAcceptEmail: string;
|
|
seats: number | null;
|
|
billingPeriod?: "MONTHLY" | "ANNUALLY";
|
|
pricePerSeat: number | null;
|
|
isPlatform: boolean;
|
|
}) {
|
|
return await prisma.team.create({
|
|
data: {
|
|
name: orgData.name,
|
|
isOrganization: true,
|
|
...(!IS_TEAM_BILLING_ENABLED ? { slug: orgData.slug } : {}),
|
|
organizationSettings: {
|
|
create: {
|
|
isAdminReviewed: orgData.isOrganizationAdminReviewed,
|
|
isOrganizationVerified: true,
|
|
isOrganizationConfigured: orgData.isOrganizationConfigured,
|
|
orgAutoAcceptEmail: orgData.autoAcceptEmail,
|
|
},
|
|
},
|
|
metadata: {
|
|
...(IS_TEAM_BILLING_ENABLED ? { requestedSlug: orgData.slug } : {}),
|
|
orgSeats: orgData.seats,
|
|
orgPricePerSeat: orgData.pricePerSeat,
|
|
isPlatform: orgData.isPlatform,
|
|
billingPeriod: orgData.billingPeriod,
|
|
},
|
|
isPlatform: orgData.isPlatform,
|
|
},
|
|
});
|
|
}
|
|
|
|
static async findById({ id }: { id: number }) {
|
|
return prisma.team.findUnique({
|
|
where: {
|
|
id,
|
|
isOrganization: true,
|
|
},
|
|
select: orgSelect,
|
|
});
|
|
}
|
|
|
|
static async findByIdIncludeOrganizationSettings({ id }: { id: number }) {
|
|
return prisma.team.findUnique({
|
|
where: {
|
|
id,
|
|
isOrganization: true,
|
|
},
|
|
select: {
|
|
...orgSelect,
|
|
organizationSettings: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
static async findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email }: { email: string }) {
|
|
const emailDomain = email.split("@").at(-1);
|
|
const orgs = await prisma.team.findMany({
|
|
where: {
|
|
isOrganization: true,
|
|
isPlatform: false,
|
|
organizationSettings: {
|
|
orgAutoAcceptEmail: emailDomain,
|
|
isOrganizationVerified: true,
|
|
isAdminReviewed: true,
|
|
},
|
|
},
|
|
});
|
|
if (orgs.length > 1) {
|
|
logger.error(
|
|
"Multiple organizations found with the same auto accept email domain",
|
|
safeStringify({ orgs, emailDomain })
|
|
);
|
|
// Detect and fail just in case this situation arises. We should really identify the problem in this case and fix the data.
|
|
throw new Error("Multiple organizations found with the same auto accept email domain");
|
|
}
|
|
const org = orgs[0];
|
|
if (!org) {
|
|
return null;
|
|
}
|
|
return getParsedTeam(org);
|
|
}
|
|
}
|