feat: Populate gCal calendar cache via webhooks (#11928)
* WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * Update CalendarService.ts * Type fixes * WIP * fix: improve cache hits * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Feedback * Update CalendarService.ts * Update CalendarService.ts * Update _router.ts * feedback * WIP * WIP * Update schema.prisma * feedback * typefixes * Update Calendar.d.ts * fix: watches when adding a calendar * Discard changes to packages/app-store/googlecalendar/api/add.ts * WIP Signed-off-by: Omar López <[email protected]> * WIP Signed-off-by: Omar López <[email protected]> * WP Signed-off-by: Omar López <[email protected]> * Update calendar.ts * Update calendar.ts * Update callback.ts * Update callback.ts * Conflicts * WIP * WIP * Update CalendarService.ts * Cleanup * Discard changes to packages/features/settings/layouts/SettingsLayout.tsx * Update calendar-cache.repository.ts * WIP * Update getSelectedCalendarsToWatch.sql * WIP * Update CalendarService.ts * Cleanup * Discard changes to packages/app-store/googlecalendar/lib/CalendarService.ts * Create CalendarService.wip.ts * WIP * Update CalendarService.ts * Update getSelectedCalendarsToWatch.sql * Delete CalendarService.wip.ts * test updates * cleanup Signed-off-by: Omar López <[email protected]> * Update CalendarService.test.ts * Update CalendarService.ts * type fixes * Update OAuthManager.ts * Update CalendarService.ts * Almost there * Update CalendarService.test.ts * Update calendar.ts * Update callback.ts * Update toggleFeatureFlag.handler.ts * fix: feedback * Update getSelectedCalendarsToWatch.sql * Fix unit tests --------- Signed-off-by: Omar López <[email protected]> Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
5012a16794
commit
d294a74aad
40 files changed
+963
-240
No files matched your search
@@ -1,4 +1,7 @@
|
||||
import type { AppFlags } from "./config";
|
||||
|
||||
export interface IFeaturesRepository {
|
||||
checkIfFeatureIsEnabledGlobally(slug: keyof AppFlags): Promise<boolean>;
|
||||
checkIfUserHasFeature(userId: number, slug: string): Promise<boolean>;
|
||||
checkIfTeamHasFeature(teamId: number, slug: string): Promise<boolean>;
|
||||
checkIfTeamOrUserHasFeature(args: { teamId?: number; userId?: number }, slug: string): Promise<boolean>;
|
||||
|
||||
@@ -2,9 +2,19 @@ import { captureException } from "@sentry/nextjs";
|
||||
|
||||
import db from "@calcom/prisma";
|
||||
|
||||
import type { AppFlags } from "./config";
|
||||
import type { IFeaturesRepository } from "./features.repository.interface";
|
||||
import { getFeatureFlag } from "./server/utils";
|
||||
|
||||
export class FeaturesRepository implements IFeaturesRepository {
|
||||
async checkIfFeatureIsEnabledGlobally(slug: keyof AppFlags) {
|
||||
try {
|
||||
return await getFeatureFlag(db, slug);
|
||||
} catch (err) {
|
||||
captureException(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
async checkIfTeamHasFeature(teamId: number, slug: string) {
|
||||
try {
|
||||
const teamFeature = await db.teamFeatures.findUnique({
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
import type { AppFlags } from "@calcom/features/flags/config";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
const initialData: Partial<AppFlags> = process.env.NEXT_PUBLIC_IS_E2E
|
||||
? { organizations: true, teams: true }
|
||||
: {};
|
||||
const initialData: AppFlags = {
|
||||
organizations: false,
|
||||
teams: false,
|
||||
"calendar-cache": false,
|
||||
emails: false,
|
||||
insights: false,
|
||||
webhooks: false,
|
||||
workflows: false,
|
||||
"email-verification": false,
|
||||
"google-workspace-directory": false,
|
||||
"disable-signup": false,
|
||||
attributes: false,
|
||||
"organizer-request-email-v2": false,
|
||||
};
|
||||
|
||||
if (process.env.NEXT_PUBLIC_IS_E2E) {
|
||||
initialData.organizations = true;
|
||||
initialData.teams = true;
|
||||
}
|
||||
|
||||
export function useFlags(): Partial<AppFlags> {
|
||||
const query = trpc.viewer.features.map.useQuery(undefined, {
|
||||
initialData,
|
||||
|
||||
@@ -2,15 +2,21 @@ import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
import type { AppFlags } from "../config";
|
||||
|
||||
// This is a temporary cache to avoid hitting the database on every lambda invocation
|
||||
let TEMP_CACHE: AppFlags | null = null;
|
||||
|
||||
export async function getFeatureFlagMap(prisma: PrismaClient) {
|
||||
// If we've already fetched the flags, return them
|
||||
if (TEMP_CACHE) return TEMP_CACHE;
|
||||
const flags = await prisma.feature.findMany({
|
||||
orderBy: { slug: "asc" },
|
||||
cacheStrategy: { swr: 300, ttl: 300 },
|
||||
});
|
||||
return flags.reduce((acc, flag) => {
|
||||
TEMP_CACHE = flags.reduce((acc, flag) => {
|
||||
acc[flag.slug as keyof AppFlags] = flag.enabled;
|
||||
return acc;
|
||||
}, {} as Partial<AppFlags>);
|
||||
}, {} as AppFlags);
|
||||
return TEMP_CACHE;
|
||||
}
|
||||
|
||||
interface CacheEntry {
|
||||
|
||||
Reference in new issue
Block a user