cal.pub0.org/packages/trpc/server/routers/viewer/apps/toggle.handler.ts

173 lines
5.0 KiB
TypeScript
Raw Normal View History

import type { PrismaClient } from "@prisma/client";
import { getLocalAppMetadata } from "@calcom/app-store/utils";
import { sendDisabledAppEmail } from "@calcom/emails";
import { getTranslation } from "@calcom/lib/server";
chore: recategorize apps (#9306) * Remove unused code in InstalledAppsLayout * Add new app categories "crm", "conferencing" and "messaging" * Sort getAppCategories entries alphabetically * Fix 404s on new category pages (and remove hardcoded category lists) * Fix admin apps list not showing "no available apps" for new categories * Recategorise apps * Sync seed-app-store categories with config files * Replace unnecessary seed-app-store.config.json with appStoreMetadata * Copy video.svg to conferencing.svg * Add messaging.svg * Remove web3 from getAppCategories (used by installed apps, admin apps) * Fix app-store-cli categories - Add conferencing - Add CRM - Remove video - Remove web3 * Remove outdated web3 comment in seed-app-store * Update apps/web/public/static/locales/en/common.json * Add cron script to keep db apps in sync with app metadata * Add redirect for app category "video" to "conferencing" * Fix up "video" category overrides to apply to conferencing * Fix conferencing apps not showing as a location for non-team users * Restore "installed_app" string for conferencing apps * Make linter happier * Remove my "installed_app_conferencing_description" as this was fixed upstream * Quick tidy up * Add dry-run to syncAppMeta via CRON_ENABLE_APP_SYNC env * Replace console.log with logger in syncAppMeta --------- Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: alannnc <alannnc@gmail.com> Co-authored-by: Hariom Balhara <hariombalhara@gmail.com> Co-authored-by: Omar López <zomars@me.com>
2023-06-28 16:22:51 +00:00
import { AppCategories } from "@calcom/prisma/enums";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../trpc";
import type { TToggleInputSchema } from "./toggle.schema";
type ToggleOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
prisma: PrismaClient;
};
input: TToggleInputSchema;
};
export const toggleHandler = async ({ input, ctx }: ToggleOptions) => {
const { prisma } = ctx;
const { enabled, slug } = input;
// Get app name from metadata
const localApps = getLocalAppMetadata();
const appMetadata = localApps.find((localApp) => localApp.slug === slug);
if (!appMetadata) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: "App metadata could not be found" });
}
const app = await prisma.app.upsert({
where: {
slug,
},
update: {
enabled,
dirName: appMetadata?.dirName || appMetadata?.slug || "",
},
create: {
slug,
dirName: appMetadata?.dirName || appMetadata?.slug || "",
categories:
(appMetadata?.categories as AppCategories[]) ||
([appMetadata?.category] as AppCategories[]) ||
undefined,
keys: undefined,
enabled,
},
});
// If disabling an app then we need to alert users basesd on the app type
if (!enabled) {
const translations = new Map();
chore: recategorize apps (#9306) * Remove unused code in InstalledAppsLayout * Add new app categories "crm", "conferencing" and "messaging" * Sort getAppCategories entries alphabetically * Fix 404s on new category pages (and remove hardcoded category lists) * Fix admin apps list not showing "no available apps" for new categories * Recategorise apps * Sync seed-app-store categories with config files * Replace unnecessary seed-app-store.config.json with appStoreMetadata * Copy video.svg to conferencing.svg * Add messaging.svg * Remove web3 from getAppCategories (used by installed apps, admin apps) * Fix app-store-cli categories - Add conferencing - Add CRM - Remove video - Remove web3 * Remove outdated web3 comment in seed-app-store * Update apps/web/public/static/locales/en/common.json * Add cron script to keep db apps in sync with app metadata * Add redirect for app category "video" to "conferencing" * Fix up "video" category overrides to apply to conferencing * Fix conferencing apps not showing as a location for non-team users * Restore "installed_app" string for conferencing apps * Make linter happier * Remove my "installed_app_conferencing_description" as this was fixed upstream * Quick tidy up * Add dry-run to syncAppMeta via CRON_ENABLE_APP_SYNC env * Replace console.log with logger in syncAppMeta --------- Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: alannnc <alannnc@gmail.com> Co-authored-by: Hariom Balhara <hariombalhara@gmail.com> Co-authored-by: Omar López <zomars@me.com>
2023-06-28 16:22:51 +00:00
if (
app.categories.some((category) =>
(
[AppCategories.calendar, AppCategories.video, AppCategories.conferencing] as AppCategories[]
).includes(category)
)
) {
// Find all users with the app credentials
const appCredentials = await prisma.credential.findMany({
where: {
appId: app.slug,
},
select: {
user: {
select: {
email: true,
locale: true,
},
},
},
});
// TODO: This should be done async probably using a queue.
Promise.all(
appCredentials.map(async (credential) => {
// No need to continue if credential does not have a user
if (!credential.user || !credential.user.email) return;
const locale = credential.user.locale ?? "en";
let t = translations.get(locale);
if (!t) {
t = await getTranslation(locale, "common");
translations.set(locale, t);
}
await sendDisabledAppEmail({
email: credential.user.email,
appName: appMetadata?.name || app.slug,
appType: app.categories,
t,
});
})
);
} else {
const eventTypesWithApp = await prisma.eventType.findMany({
where: {
metadata: {
path: ["apps", app.slug as string, "enabled"],
equals: true,
},
},
select: {
id: true,
title: true,
users: {
select: {
email: true,
locale: true,
},
},
metadata: true,
},
});
// TODO: This should be done async probably using a queue.
Promise.all(
eventTypesWithApp.map(async (eventType) => {
// TODO: This update query can be removed by merging it with
// the previous `findMany` query, if that query returns certain values.
await prisma.eventType.update({
where: {
id: eventType.id,
},
data: {
metadata: {
...(eventType.metadata as object),
apps: {
// From this comment we can not type JSON fields in Prisma https://github.com/prisma/prisma/issues/3219#issuecomment-670202980
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
...eventType.metadata?.apps,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
[app.slug]: { ...eventType.metadata?.apps[app.slug], enabled: false },
},
},
},
});
return Promise.all(
eventType.users.map(async (user) => {
const locale = user.locale ?? "en";
let t = translations.get(locale);
if (!t) {
t = await getTranslation(locale, "common");
translations.set(locale, t);
}
await sendDisabledAppEmail({
email: user.email,
appName: appMetadata?.name || app.slug,
appType: app.categories,
t,
title: eventType.title,
eventTypeId: eventType.id,
});
})
);
})
);
}
}
return app.enabled;
};