feat: Add a fresh signup to matching organization after email verification (#15705)

* Support automatically adding user to organization

* Use Repository

* Fix unit test

* Code Improvements

* Fix type error

---------

Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
Hariom BalharaandUdit Takkar authored and GitHub committed 2024-07-20 12:22:42 +05:30
1 parent a74522af3d
commit 5becc95fc4
14 files changed
+419 -84

No files matched your search

@@ -0,0 +1,36 @@
import { vi, beforeEach } from "vitest";
import { mockReset, mockDeep } from "vitest-mock-extended";
import type * as organization from "@calcom/lib/server/repository/organization";
vi.mock("@calcom/lib/server/repository/organization", () => organizationMock);
type OrganizationModule = typeof organization;
beforeEach(() => {
mockReset(organizationMock);
});
const organizationMock = mockDeep<OrganizationModule>();
const OrganizationRepository = organizationMock.OrganizationRepository;
export const organizationScenarios = {
OrganizationRepository: {
findUniqueByMatchingAutoAcceptEmail: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fakeReturnOrganization: (org: any, forInput: any) => {
OrganizationRepository.findUniqueByMatchingAutoAcceptEmail.mockImplementation((arg) => {
if (forInput.email === arg.email) {
return org;
}
const errorMsg = "Mock Error-fakeReturnOrganization: Unhandled input";
console.log(errorMsg, { arg, forInput });
throw new Error(errorMsg);
});
},
fakeNoMatch: () => {
OrganizationRepository.findUniqueByMatchingAutoAcceptEmail.mockResolvedValue(null);
},
},
} satisfies Partial<Record<keyof OrganizationModule["OrganizationRepository"], unknown>>,
} satisfies Partial<Record<keyof OrganizationModule, unknown>>;
export default organizationMock;
@@ -0,0 +1,105 @@
import prismock from "../../../../tests/libs/__mocks__/prisma";
import { describe, it, expect, beforeEach, vi } from "vitest";
import { OrganizationRepository } from "./organization";
vi.mock("./teamUtils", () => ({
getParsedTeam: (org: any) => org,
}));
describe("Organization.findUniqueByMatchingAutoAcceptEmail", () => {
beforeEach(async () => {
vi.resetAllMocks();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await prismock.reset();
});
it("should return null if no organization matches the email domain", async () => {
const result = await OrganizationRepository.findUniqueByMatchingAutoAcceptEmail({
email: "[email protected]",
});
expect(result).toBeNull();
});
it("should throw an error if multiple organizations match the email domain", async () => {
await createOrganization({ name: "Test Org 1", orgAutoAcceptEmail: "example.com" });
await createOrganization({ name: "Test Org 2", orgAutoAcceptEmail: "example.com" });
await expect(
OrganizationRepository.findUniqueByMatchingAutoAcceptEmail({ email: "[email protected]" })
).rejects.toThrow("Multiple organizations found with the same auto accept email domain");
});
it("should return the parsed organization if a single match is found", async () => {
const organization = await createOrganization({ name: "Test Org", orgAutoAcceptEmail: "example.com" });
const result = await OrganizationRepository.findUniqueByMatchingAutoAcceptEmail({
email: "[email protected]",
});
expect(result).toEqual(organization);
});
it("should not confuse a team with organization", async () => {
await createTeam({ name: "Test Team", orgAutoAcceptEmail: "example.com" });
const result = await OrganizationRepository.findUniqueByMatchingAutoAcceptEmail({
email: "[email protected]",
});
expect(result).toEqual(null);
});
it("should correctly match orgAutoAcceptEmail", async () => {
await createOrganization({ name: "Test Org", orgAutoAcceptEmail: "noexample.com" });
const result = await OrganizationRepository.findUniqueByMatchingAutoAcceptEmail({
email: "[email protected]",
});
expect(result).toEqual(null);
});
});
async function createOrganization({
name = "Test Org",
orgAutoAcceptEmail,
}: {
name: string;
orgAutoAcceptEmail: string;
}) {
return await prismock.team.create({
data: {
name,
isOrganization: true,
organizationSettings: {
create: {
orgAutoAcceptEmail,
},
},
},
});
}
async function createTeam({
name = "Test Team",
orgAutoAcceptEmail,
}: {
name: string;
orgAutoAcceptEmail: string;
}) {
return await prismock.team.create({
data: {
name,
isOrganization: false,
organizationSettings: {
create: {
orgAutoAcceptEmail,
},
},
},
});
}
@@ -6,6 +6,7 @@ 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 = {
@@ -163,4 +164,25 @@ export class OrganizationRepository {
},
});
}
static async findUniqueByMatchingAutoAcceptEmail({ email }: { email: string }) {
const emailDomain = email.split("@").at(-1);
const orgs = await prisma.team.findMany({
where: {
isOrganization: true,
organizationSettings: {
orgAutoAcceptEmail: emailDomain,
},
},
});
if (orgs.length > 1) {
// 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);
}
}