Skip to content

Commit 762b995

Browse files
Shaik-SirajuddinShaik-Sirajuddinsean-brydon
authored
fix: event type uses calvideo as location instead of user default (#15442)
* fix_event_type_created_with_calvideo_instead_of_user_default_conferencing_app * test: add tests for getDefaultLocation --------- Co-authored-by: Shaik-Sirajuddin <sirajudddinshaik30@gmail.com> Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com>
1 parent d2d7453 commit 762b995

File tree

4 files changed

+132
-25
lines changed

4 files changed

+132
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import prismaMock from "../../../tests/libs/__mocks__/prisma";
2+
3+
import { getGoogleMeetCredential, TestData } from "@calcom/web/test/utils/bookingScenario/bookingScenario";
4+
5+
import { describe, expect, it } from "vitest";
6+
7+
import { DailyLocationType, MeetLocationType } from "@calcom/app-store/locations";
8+
9+
import { getDefaultLocations } from "./getDefaultLocations";
10+
11+
type User = {
12+
id: number;
13+
email?: string;
14+
name?: string;
15+
metadata: {
16+
defaultConferencingApp?: {
17+
appSlug: string;
18+
appLink: string;
19+
};
20+
};
21+
credentials?: [
22+
{
23+
key: {
24+
expiry_date?: number;
25+
token_type?: string;
26+
access_token?: string;
27+
refresh_token?: string;
28+
scope: string;
29+
};
30+
}
31+
];
32+
};
33+
describe("getDefaultLocation ", async () => {
34+
it("should return location based on user default conferencing app", async () => {
35+
const user: User = {
36+
id: 101,
37+
metadata: {
38+
defaultConferencingApp: {
39+
appSlug: "google-meet",
40+
appLink: "https://example.com",
41+
},
42+
},
43+
credentials: [getGoogleMeetCredential()],
44+
};
45+
await mockUser(user);
46+
await addAppsToDb([TestData.apps["google-meet"]]);
47+
const res = await getDefaultLocations(user);
48+
expect(res[0]).toEqual({
49+
link: "https://example.com",
50+
type: MeetLocationType,
51+
});
52+
});
53+
it("should return calvideo when default conferencing app is not set", async () => {
54+
const user: User = {
55+
id: 101,
56+
metadata: {},
57+
};
58+
await mockUser(user);
59+
await addAppsToDb([TestData.apps["daily-video"]]);
60+
await prismaMock.app.create({
61+
data: {
62+
...TestData.apps["daily-video"],
63+
enabled: true,
64+
},
65+
});
66+
const res = await getDefaultLocations(user);
67+
expect(res[0]).toContain({
68+
type: DailyLocationType,
69+
});
70+
});
71+
});
72+
73+
async function mockUser(user: User) {
74+
const userToCreate: any = {
75+
...TestData.users.example,
76+
...user,
77+
};
78+
if (user.credentials) {
79+
userToCreate.credentials = {
80+
createMany: {
81+
data: user.credentials,
82+
},
83+
};
84+
}
85+
return await prismaMock.user.create({
86+
data: userToCreate,
87+
});
88+
}
89+
async function addAppsToDb(apps: any[]) {
90+
await prismaMock.app.createMany({
91+
data: apps.map((app) => {
92+
return { ...app, enabled: true };
93+
}),
94+
});
95+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
2+
import { DailyLocationType } from "@calcom/app-store/locations";
3+
import getApps from "@calcom/app-store/utils";
4+
import { getUsersCredentials } from "@calcom/lib/server/getUsersCredentials";
5+
import { userMetadata as userMetadataSchema } from "@calcom/prisma/zod-utils";
6+
import type { EventTypeLocation } from "@calcom/prisma/zod/custom/eventtype";
7+
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
8+
9+
type SessionUser = NonNullable<TrpcSessionUser>;
10+
type User = {
11+
id: SessionUser["id"];
12+
metadata: SessionUser["metadata"];
13+
};
14+
15+
export async function getDefaultLocations(user: User): Promise<EventTypeLocation[]> {
16+
const defaultConferencingData = userMetadataSchema.parse(user.metadata)?.defaultConferencingApp;
17+
18+
if (defaultConferencingData && defaultConferencingData.appSlug !== "daily-video") {
19+
const credentials = await getUsersCredentials(user);
20+
21+
const foundApp = getApps(credentials, true).filter(
22+
(app) => app.slug === defaultConferencingData.appSlug
23+
)[0]; // There is only one possible install here so index [0] is the one we are looking for ;
24+
const locationType = foundApp?.locationOption?.value ?? DailyLocationType; // Default to Daily if no location type is found
25+
return [{ type: locationType, link: defaultConferencingData.appLink }];
26+
}
27+
28+
const appKeys = await getAppKeysFromSlug("daily-video");
29+
30+
if (typeof appKeys.api_key === "string") {
31+
return [{ type: DailyLocationType }];
32+
}
33+
34+
return [];
35+
}

packages/lib/server/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export { defaultResponder } from "./defaultResponder";
66
export { getLuckyUser } from "./getLuckyUser";
77
export { getServerErrorFromUnknown } from "./getServerErrorFromUnknown";
88
export { getTranslation } from "./i18n";
9+
export { getDefaultLocations } from "./getDefaultLocations";
910
export { default as perfObserver } from "./perfObserver";

packages/trpc/server/routers/viewer/eventTypes/create.handler.ts

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import type { Prisma } from "@prisma/client";
22
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
33

4-
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
5-
import { DailyLocationType } from "@calcom/app-store/locations";
6-
import getApps from "@calcom/app-store/utils";
7-
import { getUsersCredentials } from "@calcom/lib/server/getUsersCredentials";
4+
import { getDefaultLocations } from "@calcom/lib/server";
85
import { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
96
import type { PrismaClient } from "@calcom/prisma";
107
import { SchedulingType } from "@calcom/prisma/enums";
11-
import { userMetadata as userMetadataSchema } from "@calcom/prisma/zod-utils";
128
import type { EventTypeLocation } from "@calcom/prisma/zod/custom/eventtype";
139

1410
import { TRPCError } from "@trpc/server";
@@ -116,23 +112,3 @@ export const createHandler = async ({ ctx, input }: CreateOptions) => {
116112
throw new TRPCError({ code: "BAD_REQUEST" });
117113
}
118114
};
119-
120-
async function getDefaultLocations(user: User): Promise<EventTypeLocation[]> {
121-
const defaultConferencingData = userMetadataSchema.parse(user.metadata)?.defaultConferencingApp;
122-
const appKeys = await getAppKeysFromSlug("daily-video");
123-
124-
if (typeof appKeys.api_key === "string") {
125-
return [{ type: DailyLocationType }];
126-
}
127-
128-
if (defaultConferencingData && defaultConferencingData.appSlug !== "daily-video") {
129-
const credentials = await getUsersCredentials(user);
130-
const foundApp = getApps(credentials, true).filter(
131-
(app) => app.slug === defaultConferencingData.appSlug
132-
)[0]; // There is only one possible install here so index [0] is the one we are looking for ;
133-
const locationType = foundApp?.locationOption?.value ?? DailyLocationType; // Default to Daily if no location type is found
134-
return [{ type: locationType, link: defaultConferencingData.appLink }];
135-
}
136-
137-
return [];
138-
}

0 commit comments

Comments
 (0)