2023-08-02 03:54:28 +00:00
|
|
|
import type { TFunction } from "next-i18next";
|
|
|
|
|
|
|
|
import type { CredentialDataWithTeamName } from "@calcom/app-store/utils";
|
2023-08-14 15:02:29 +00:00
|
|
|
import { defaultVideoAppCategories } from "@calcom/app-store/utils";
|
2023-08-15 00:18:26 +00:00
|
|
|
import getEnabledAppsFromCredentials from "@calcom/lib/apps/getEnabledAppsFromCredentials";
|
2023-08-02 03:54:28 +00:00
|
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
import { AppCategories } from "@calcom/prisma/enums";
|
2023-09-14 16:53:58 +00:00
|
|
|
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
2023-08-02 03:54:28 +00:00
|
|
|
|
|
|
|
import { defaultLocations } from "./locations";
|
|
|
|
|
|
|
|
export async function getLocationGroupedOptions(
|
|
|
|
userOrTeamId: { userId: number } | { teamId: number },
|
|
|
|
t: TFunction
|
|
|
|
) {
|
|
|
|
const apps: Record<
|
|
|
|
string,
|
|
|
|
{
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
icon?: string;
|
|
|
|
slug?: string;
|
|
|
|
credential?: CredentialDataWithTeamName;
|
|
|
|
}[]
|
|
|
|
> = {};
|
|
|
|
|
|
|
|
let idToSearchObject = {};
|
|
|
|
|
|
|
|
if ("teamId" in userOrTeamId) {
|
|
|
|
const teamId = userOrTeamId.teamId;
|
|
|
|
// See if the team event belongs to an org
|
|
|
|
const org = await prisma.team.findFirst({
|
|
|
|
where: {
|
|
|
|
children: {
|
|
|
|
some: {
|
|
|
|
id: teamId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (org) {
|
|
|
|
idToSearchObject = {
|
|
|
|
teamId: {
|
|
|
|
in: [teamId, org.id],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
idToSearchObject = { teamId };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
idToSearchObject = { userId: userOrTeamId.userId };
|
|
|
|
}
|
|
|
|
|
|
|
|
const credentials = await prisma.credential.findMany({
|
|
|
|
where: {
|
|
|
|
...idToSearchObject,
|
|
|
|
app: {
|
|
|
|
categories: {
|
2023-08-14 15:02:29 +00:00
|
|
|
hasSome: defaultVideoAppCategories,
|
2023-08-02 03:54:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
2023-09-14 16:53:58 +00:00
|
|
|
...credentialForCalendarServiceSelect,
|
2023-08-02 03:54:28 +00:00
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-08-15 00:18:26 +00:00
|
|
|
const integrations = await getEnabledAppsFromCredentials(credentials, { filterOnCredentials: true });
|
2023-08-02 03:54:28 +00:00
|
|
|
|
|
|
|
integrations.forEach((app) => {
|
2023-08-14 15:02:29 +00:00
|
|
|
// All apps that are labeled as a locationOption are video apps.
|
2023-08-02 03:54:28 +00:00
|
|
|
if (app.locationOption) {
|
|
|
|
// All apps that are labeled as a locationOption are video apps. Extract the secondary category if available
|
2023-08-14 15:02:29 +00:00
|
|
|
let groupByCategory =
|
2023-08-02 03:54:28 +00:00
|
|
|
app.categories.length >= 2
|
2023-08-14 15:02:29 +00:00
|
|
|
? app.categories.find((groupByCategory) => !defaultVideoAppCategories.includes(groupByCategory))
|
|
|
|
: app.categories[0] || app.category;
|
|
|
|
if (!groupByCategory) groupByCategory = AppCategories.conferencing;
|
2023-08-02 03:54:28 +00:00
|
|
|
|
|
|
|
for (const credential of app.credentials) {
|
|
|
|
const label = `${app.locationOption.label} ${
|
|
|
|
credential.team?.name ? `(${credential.team.name})` : ""
|
|
|
|
}`;
|
|
|
|
const option = { ...app.locationOption, label, icon: app.logo, slug: app.slug, credential };
|
2023-08-14 15:02:29 +00:00
|
|
|
if (apps[groupByCategory]) {
|
|
|
|
apps[groupByCategory] = [...apps[groupByCategory], option];
|
2023-08-02 03:54:28 +00:00
|
|
|
} else {
|
2023-08-14 15:02:29 +00:00
|
|
|
apps[groupByCategory] = [option];
|
2023-08-02 03:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
defaultLocations.forEach((l) => {
|
|
|
|
const category = l.category;
|
|
|
|
if (apps[category]) {
|
|
|
|
apps[category] = [
|
|
|
|
...apps[category],
|
|
|
|
{
|
|
|
|
label: l.label,
|
|
|
|
value: l.type,
|
|
|
|
icon: l.iconUrl,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
apps[category] = [
|
|
|
|
{
|
|
|
|
label: l.label,
|
|
|
|
value: l.type,
|
|
|
|
icon: l.iconUrl,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const locations = [];
|
|
|
|
|
|
|
|
// Translating labels and pushing into array
|
|
|
|
for (const category in apps) {
|
|
|
|
const tmp = {
|
|
|
|
label: t(category),
|
|
|
|
options: apps[category].map((l) => ({
|
|
|
|
...l,
|
|
|
|
label: t(l.label),
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
|
|
|
|
locations.push(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return locations;
|
|
|
|
}
|