Skip to content

Commit 50c15f0

Browse files
fix: dub installation on teams (calcom#21654)
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1 parent 86d5c43 commit 50c15f0

8 files changed

Lines changed: 40 additions & 1 deletion

File tree

packages/app-store/apps.keys-schemas.generated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { appKeysSchema as alby_zod_ts } from "./alby/zod";
66
import { appKeysSchema as basecamp3_zod_ts } from "./basecamp3/zod";
77
import { appKeysSchema as closecom_zod_ts } from "./closecom/zod";
88
import { appKeysSchema as dailyvideo_zod_ts } from "./dailyvideo/zod";
9+
import { appKeysSchema as dub_zod_ts } from "./dub/zod";
910
import { appKeysSchema as fathom_zod_ts } from "./fathom/zod";
1011
import { appKeysSchema as feishucalendar_zod_ts } from "./feishucalendar/zod";
1112
import { appKeysSchema as ga4_zod_ts } from "./ga4/zod";
@@ -55,6 +56,7 @@ export const appKeysSchemas = {
5556
basecamp3: basecamp3_zod_ts,
5657
closecom: closecom_zod_ts,
5758
dailyvideo: dailyvideo_zod_ts,
59+
dub: dub_zod_ts,
5860
fathom: fathom_zod_ts,
5961
feishucalendar: feishucalendar_zod_ts,
6062
ga4: ga4_zod_ts,

packages/app-store/apps.schemas.generated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { appDataSchema as alby_zod_ts } from "./alby/zod";
66
import { appDataSchema as basecamp3_zod_ts } from "./basecamp3/zod";
77
import { appDataSchema as closecom_zod_ts } from "./closecom/zod";
88
import { appDataSchema as dailyvideo_zod_ts } from "./dailyvideo/zod";
9+
import { appDataSchema as dub_zod_ts } from "./dub/zod";
910
import { appDataSchema as fathom_zod_ts } from "./fathom/zod";
1011
import { appDataSchema as feishucalendar_zod_ts } from "./feishucalendar/zod";
1112
import { appDataSchema as ga4_zod_ts } from "./ga4/zod";
@@ -55,6 +56,7 @@ export const appDataSchemas = {
5556
basecamp3: basecamp3_zod_ts,
5657
closecom: closecom_zod_ts,
5758
dailyvideo: dailyvideo_zod_ts,
59+
dub: dub_zod_ts,
5860
fathom: fathom_zod_ts,
5961
feishucalendar: feishucalendar_zod_ts,
6062
ga4: ga4_zod_ts,

packages/app-store/dub/api/add.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
2020
throw new HttpError({ statusCode: 400, message: "Session user must have an email" });
2121
}
2222

23+
const { teamId } = req.query;
2324
const { client_id, redirect_uris } = await getParsedAppKeysFromSlug("dub", dubAppKeysSchema);
2425

2526
const url = new URL("https://app.dub.co/oauth/authorize");
2627
url.searchParams.append("client_id", client_id);
2728
url.searchParams.append("redirect_uri", redirect_uris);
2829
url.searchParams.append("response_type", "code");
2930
url.searchParams.append("scope", scopeString);
31+
if (typeof teamId === "string" && !Number.isNaN(Number(teamId))) {
32+
url.searchParams.append("state", JSON.stringify({ teamId: Number(teamId) }));
33+
}
3034
const oauthUrl = url.toString();
3135

3236
return res.status(200).json({ url: oauthUrl });

packages/app-store/dub/lib/AnalyticsService.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ export default class DubService implements AnalyticsService {
7373
"Content-Type": "application/x-www-form-urlencoded",
7474
},
7575
});
76+
77+
if (!response.ok) {
78+
const res = await response.json();
79+
if (response.status === 401) {
80+
await CredentialRepository.updateCredentialById({
81+
id: this.credential.id,
82+
data: {
83+
invalid: true,
84+
},
85+
});
86+
}
87+
throw new Error(`Error refreshing dub token: ${res?.error?.message ?? response.statusText}`);
88+
}
7689
return await response.json();
7790
},
7891
"dub",
@@ -89,6 +102,7 @@ export default class DubService implements AnalyticsService {
89102
return newToken;
90103
} catch (err) {
91104
this.log.error(err);
105+
throw err;
92106
}
93107
}
94108

packages/app-store/dub/zod.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from "zod";
2+
3+
export const appKeysSchema = z.object({
4+
redirect_uris: z.string(),
5+
client_id: z.string(),
6+
client_secret: z.string(),
7+
});
8+
9+
export const appDataSchema = z.object({});

packages/features/bookings/lib/handleNewBooking.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,7 @@ async function handler(
21952195
email: bookerEmail,
21962196
eventName: "Cal.com lead",
21972197
},
2198+
isTeamEventType,
21982199
});
21992200
}
22002201

packages/lib/analyticsManager/handleAnalyticsEvents.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ interface HandleAnalyticsEventsProps {
99
name: string;
1010
eventName: string;
1111
};
12+
isTeamEventType: boolean;
1213
}
1314

1415
export const handleAnalyticsEvents = async ({
1516
credentials,
1617
rawBookingData,
1718
bookingInfo,
19+
isTeamEventType,
1820
}: HandleAnalyticsEventsProps) => {
1921
const { dub_id } = await rawBookingData;
2022

2123
if (!dub_id || typeof dub_id !== "string") return;
2224

23-
const dubCredential = credentials.find((cred) => cred.appId === "dub");
25+
const dubCredential = credentials.find((cred) => {
26+
if (cred.appId !== "dub") return false;
27+
if (isTeamEventType && !cred.teamId) return false;
28+
return true;
29+
});
2430

2531
if (!dubCredential) return;
2632

packages/lib/server/repository/credential.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type CredentialUpdateInput = {
2323
userId?: number;
2424
appId?: string;
2525
delegationCredentialId?: string | null;
26+
invalid?: boolean;
2627
};
2728

2829
export class CredentialRepository {

0 commit comments

Comments
 (0)