Skip to content

Commit

Permalink
feat: add additional attributes to intercom (#14794)
Browse files Browse the repository at this point in the history
* feat: add additional attributes to intercom

* remove console.log

* added is_premium
  • Loading branch information
SomayChauhan committed Apr 29, 2024
1 parent 22425bb commit 46ffeae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/features/ee/support/lib/intercom/useIntercom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { z } from "zod";

import dayjs from "@calcom/dayjs";
import { WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants";
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
import { useHasTeamPlan, useHasPaidPlan } from "@calcom/lib/hooks/useHasPaidPlan";
import { trpc } from "@calcom/trpc/react";

Expand Down Expand Up @@ -56,6 +57,13 @@ export const useIntercom = () => {
metadata: data?.metadata,
completed_onboarding: data.completedOnboarding,
is_logged_in: !!data,
sum_of_bookings: data.sumOfBookings,
sum_of_calendars: data.sumOfCalendars,
sum_of_teams: data.sumOfTeams,
has_org: !data.organizationId,
sum_of_event_types: data.sumOfEventTypes,
sum_of_team_event_types: data.sumOfTeamEventTypes,
is_premium: hasKeyInMetadata(data, "isPremium") ? !!data.metadata.isPremium : false,
},
});
};
Expand Down
36 changes: 36 additions & 0 deletions packages/trpc/server/routers/loggedInViewer/me.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ export const meHandler = async ({ ctx, input }: MeOptions) => {
identityProviderEmail = account?.providerEmail || "";
}

const additionalUserInfo = await prisma.user.findFirst({
where: {
id: user.id,
},
select: {
bookings: {
select: { id: true },
},
selectedCalendars: true,
teams: {
select: {
team: {
select: {
id: true,
eventTypes: true,
},
},
},
},
eventTypes: {
select: { id: true },
},
},
});
let sumOfTeamEventTypes = 0;
for (const team of additionalUserInfo?.teams || []) {
for (const _eventType of team.team.eventTypes) {
sumOfTeamEventTypes++;
}
}

// Destructuring here only makes it more illegible
// pick only the part we want to expose in the API
return {
Expand Down Expand Up @@ -113,6 +144,11 @@ export const meHandler = async ({ ctx, input }: MeOptions) => {
profile: user.profile ?? null,
profiles: allUserEnrichedProfiles,
secondaryEmails,
sumOfBookings: additionalUserInfo?.bookings.length,
sumOfCalendars: additionalUserInfo?.selectedCalendars.length,
sumOfTeams: additionalUserInfo?.teams.length,
sumOfEventTypes: additionalUserInfo?.eventTypes.length,
sumOfTeamEventTypes,
...(passwordAdded ? { passwordAdded } : {}),
};
};

0 comments on commit 46ffeae

Please sign in to comment.