fix: Reschedule URL for an org migrated/moved user's booking. (#15736)

* Fix reschedule URl

* Add e2e for org

* Add team booking reschedule test

* Add unit tests

* Remove unnecessary select

---------

Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
Hariom BalharaandUdit Takkar authored and GitHub committed 2024-07-31 14:25:46 +02:00
1 parent 63c7e4b55a
commit 5b7f2a6ffc
9 files changed
+442 -31

No files matched your search

+6
View File
@@ -5,6 +5,7 @@ import type * as constants from "@calcom/lib/constants";
const mockedConstants = {
IS_PRODUCTION: false,
IS_TEAM_BILLING_ENABLED: false,
WEBSITE_URL: "",
} as typeof constants;
vi.mock("@calcom/lib/constants", () => {
@@ -25,4 +26,9 @@ export const constantsScenarios = {
// @ts-ignore
mockedConstants.IS_TEAM_BILLING_ENABLED = true;
},
setWebsiteUrl: (url: string) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
mockedConstants.WEBSITE_URL = url;
},
};
@@ -0,0 +1,168 @@
import { constantsScenarios } from "@calcom/lib/__mocks__/constants";
import { describe, it, vi, expect, beforeEach } from "vitest";
import { getBrand } from "@calcom/lib/server/getBrand";
import { buildEventUrlFromBooking } from "./buildEventUrlFromBooking";
vi.mock("@calcom/lib/server/getBrand", () => ({
getBrand: vi.fn(),
}));
const WEBSITE_URL = "https://buildEventTest.example";
beforeEach(() => {
constantsScenarios.setWebsiteUrl(WEBSITE_URL);
});
describe("buildEventUrlFromBooking", () => {
describe("Non Organization", () => {
it("should correctly build the event URL for a team event booking", async () => {
const booking = {
eventType: {
slug: "30min",
team: {
slug: "engineering",
parentId: 123,
},
},
profileEnrichedBookingUser: {
profile: {
organizationId: null,
username: "john",
},
},
dynamicGroupSlugRef: null,
};
const expectedUrl = `${WEBSITE_URL}/team/engineering/30min`;
const result = await buildEventUrlFromBooking(booking);
expect(result).toBe(expectedUrl);
});
it("should correctly build the event URL for a dynamic group booking", async () => {
const booking = {
eventType: {
slug: "30min",
team: null,
},
profileEnrichedBookingUser: {
profile: {
organizationId: null,
username: "john",
},
},
dynamicGroupSlugRef: "john+jane",
};
const expectedUrl = `${WEBSITE_URL}/john+jane/30min`;
const result = await buildEventUrlFromBooking(booking);
expect(result).toBe(expectedUrl);
});
it("should correctly build the event URL for a personal booking", async () => {
const booking = {
eventType: {
slug: "30min",
team: null,
},
profileEnrichedBookingUser: {
profile: {
organizationId: null,
username: "john",
},
},
dynamicGroupSlugRef: null,
};
const expectedUrl = `${WEBSITE_URL}/john/30min`;
const result = await buildEventUrlFromBooking(booking);
expect(result).toBe(expectedUrl);
});
});
describe("Organization", () => {
const organizationId = 123;
const orgOrigin = "https://acme.cal.local";
beforeEach(() => {
getBrand.mockResolvedValue({
fullDomain: orgOrigin,
});
});
it("should correctly build the event URL for a team event booking", async () => {
const booking = {
eventType: {
slug: "30min",
team: {
slug: "engineering",
parentId: 123,
},
},
profileEnrichedBookingUser: {
profile: {
organizationId,
username: "john",
},
},
dynamicGroupSlugRef: null,
};
const expectedUrl = `${orgOrigin}/team/engineering/30min`;
const result = await buildEventUrlFromBooking(booking);
expect(result).toBe(expectedUrl);
});
it("should correctly build the event URL for a dynamic group booking", async () => {
const booking = {
eventType: {
slug: "30min",
team: null,
},
profileEnrichedBookingUser: {
profile: {
organizationId,
username: "john",
},
},
dynamicGroupSlugRef: "john+jane",
};
const expectedUrl = `${orgOrigin}/john+jane/30min`;
const result = await buildEventUrlFromBooking(booking);
expect(result).toBe(expectedUrl);
});
it("should correctly build the event URL for a personal booking", async () => {
const booking = {
eventType: {
slug: "30min",
team: null,
},
profileEnrichedBookingUser: {
profile: {
organizationId,
username: "john",
},
},
dynamicGroupSlugRef: null,
};
const expectedUrl = `${orgOrigin}/john/30min`;
const result = await buildEventUrlFromBooking(booking);
expect(result).toBe(expectedUrl);
});
});
it("should throw if the username isn't set", async () => {
const booking = {
eventType: {
slug: "30min",
team: null,
},
profileEnrichedBookingUser: {
profile: {
organizationId: null,
username: null,
},
},
dynamicGroupSlugRef: null,
};
await expect(() => buildEventUrlFromBooking(booking)).rejects.toThrow(
"No username found for booking user."
);
});
});
@@ -0,0 +1,56 @@
import { getBookerBaseUrl } from "@calcom/lib/getBookerUrl/server";
import { safeStringify } from "@calcom/lib/safeStringify";
import logger from "../logger";
type BookingEventType = {
slug: string;
team: {
slug: string | null;
parentId: number | null;
} | null;
};
/**
* It has its profile always set and if organizationId is null, then username would be regular(non-org) username.
*/
type ProfileEnrichedBookingUser = {
profile: { organizationId: number | null; username: string | null };
} | null;
export function getOrganizationIdOfBooking(booking: {
eventType: BookingEventType;
profileEnrichedBookingUser: ProfileEnrichedBookingUser;
}) {
const { eventType, profileEnrichedBookingUser } = booking;
return eventType.team
? eventType.team.parentId
: profileEnrichedBookingUser?.profile.organizationId ?? null;
}
export async function buildEventUrlFromBooking(booking: {
eventType: BookingEventType;
profileEnrichedBookingUser: ProfileEnrichedBookingUser;
dynamicGroupSlugRef: string | null;
}) {
const { eventType, dynamicGroupSlugRef, profileEnrichedBookingUser } = booking;
const eventSlug = eventType.slug;
const eventTeam = eventType.team;
const bookingOrganizationId = getOrganizationIdOfBooking({ eventType, profileEnrichedBookingUser });
const bookerUrl = await getBookerBaseUrl(bookingOrganizationId);
if (dynamicGroupSlugRef) {
return `${bookerUrl}/${dynamicGroupSlugRef}/${eventSlug}`;
}
if (eventTeam?.slug) {
return `${bookerUrl}/team/${eventTeam.slug}/${eventSlug}`;
}
const username = profileEnrichedBookingUser?.profile?.username;
if (!username) {
logger.error("No username found for booking user.", safeStringify({ profileEnrichedBookingUser }));
throw new Error("No username found for booking user.");
}
return `${bookerUrl}/${username}/${eventSlug}`;
}
+3 -2
View File
@@ -311,10 +311,11 @@ export class UserRepository {
}
/**
* Use this method if you don't directly has the profileId.
* It can happen in two cases:
* Use this method instead of `enrichUserWithTheProfile` if you don't directly have the profileId.
* It can happen in following cases:
* 1. While dealing with a User that hasn't been added to any organization yet and thus have no Profile entries.
* 2. While dealing with a User that has been moved to a Profile i.e. he was invited to an organization when he was an existing user.
* 3. We haven't added profileId to all the entities, so they aren't aware of which profile they belong to. So, we still mostly use this function to enrich the user with its profile.
*/
static async enrichUserWithItsProfile<T extends { id: number; username: string | null }>({
user,