fix: Feature flag user cache (#10359)

Co-authored-by: zomars <[email protected]>
This commit is contained in:
sean-brydonandzomars authored and GitHub committed 2023-07-25 22:00:21 +00:00
1 parent 533371da20
commit ad3bdbbda7
5 files changed
+20 -18

No files matched your search

+6 -3
View File
@@ -8,7 +8,7 @@ import type { AnyRouter } from "@trpc/server";
/**
* Creates an API handler executed by Next.js.
*/
export function createNextApiHandler(router: AnyRouter, isPublic = false) {
export function createNextApiHandler(router: AnyRouter, isPublic = false, namespace = "") {
return trpcNext.createNextApiHandler({
router,
/**
@@ -64,10 +64,13 @@ export function createNextApiHandler(router: AnyRouter, isPublic = false) {
// Revalidation time here should be 1 second, per https://github.com/calcom/cal.com/pull/6823#issuecomment-1423215321
"slots.getSchedule": `no-cache`, // FIXME
cityTimezones: `max-age=${ONE_DAY_IN_SECONDS}, stale-while-revalidate`,
"features.map": `max-age=${ONE_DAY_IN_SECONDS}, stale-while-revalidate`, // "map" - Feature Flag Map
} as const;
const matchedPath = paths.find((v) => v in cacheRules) as keyof typeof cacheRules;
if (matchedPath) defaultHeaders.headers["cache-control"] = cacheRules[matchedPath];
const prependNamespace = (key: string) =>
(namespace ? `${namespace}.${key}` : key) as keyof typeof cacheRules;
const matchedPath = paths.find((v) => prependNamespace(v) in cacheRules);
if (matchedPath) defaultHeaders.headers["cache-control"] = cacheRules[prependNamespace(matchedPath)];
}
return defaultHeaders;
@@ -1,3 +1,5 @@
import { z } from "zod";
import { authedAdminProcedure } from "../../../procedures/authedProcedure";
import { router } from "../../../trpc";
import { ZListMembersSchema } from "./listPaginated.schema";
@@ -47,4 +49,14 @@ export const adminRouter = router({
input,
});
}),
toggleFeatureFlag: authedAdminProcedure
.input(z.object({ slug: z.string(), enabled: z.boolean() }))
.mutation(({ ctx, input }) => {
const { prisma, user } = ctx;
const { slug, enabled } = input;
return prisma.feature.update({
where: { slug },
data: { enabled, updatedBy: user.id },
});
}),
});