11import { websitesApi } from "@databuddy/auth" ;
22import {
33 and ,
4- db ,
54 desc ,
65 eq ,
76 flags ,
@@ -10,19 +9,17 @@ import {
109 isNull ,
1110 targetGroups ,
1211} from "@databuddy/db" ;
13- import {
14- createDrizzleCache ,
15- invalidateCacheableWithArgs ,
16- redis ,
17- } from "@databuddy/redis" ;
12+ import { createDrizzleCache , redis } from "@databuddy/redis" ;
1813import {
1914 flagFormSchema ,
2015 userRuleSchema ,
2116 variantSchema ,
2217} from "@databuddy/shared/flags" ;
2318import {
19+ getScope ,
2420 getScopeCondition ,
2521 handleFlagUpdateDependencyCascading ,
22+ invalidateFlagCache ,
2623} from "@databuddy/shared/flags/utils" ;
2724import { ORPCError } from "@orpc/server" ;
2825import { randomUUIDv7 } from "bun" ;
@@ -35,9 +32,6 @@ import { getCacheAuthContext } from "../utils/cache-keys";
3532const flagsCache = createDrizzleCache ( { redis, namespace : "flags" } ) ;
3633const CACHE_DURATION = 60 ;
3734
38- const getScope = ( websiteId ?: string , organizationId ?: string ) =>
39- websiteId ? `website:${ websiteId } ` : `org:${ organizationId } ` ;
40-
4135const authorizeScope = async (
4236 context : Context ,
4337 websiteId ?: string ,
@@ -64,41 +58,6 @@ const authorizeScope = async (
6458 }
6559} ;
6660
67- const invalidateFlagCache = async (
68- id : string ,
69- websiteId ?: string | null ,
70- organizationId ?: string | null
71- ) => {
72- const scope = getScope ( websiteId ?? undefined , organizationId ?? undefined ) ;
73-
74- // Get the flag details to invalidate public API cache
75- const flag = await db
76- . select ( { key : flags . key } )
77- . from ( flags )
78- . where ( eq ( flags . id , id ) )
79- . limit ( 1 ) ;
80-
81- const invalidations : Promise < unknown > [ ] = [
82- flagsCache . invalidateByTables ( [ "flags" ] ) ,
83- flagsCache . invalidateByKey ( `byId:${ id } :${ scope } ` ) ,
84- ] ;
85-
86- if ( flag [ 0 ] ?. key ) {
87- const clientId = websiteId || organizationId ;
88- if ( clientId ) {
89- invalidations . push (
90- invalidateCacheableWithArgs ( "flag" , [ flag [ 0 ] . key , clientId ] )
91- ) ;
92-
93- invalidations . push (
94- invalidateCacheableWithArgs ( "flags-client" , [ clientId ] )
95- ) ;
96- }
97- }
98-
99- await Promise . all ( invalidations ) ;
100- } ;
101-
10261const listFlagsSchema = z
10362 . object ( {
10463 websiteId : z . string ( ) . optional ( ) ,
@@ -264,7 +223,7 @@ function sanitizeFlagForDemo<T extends FlagWithTargetGroups>(flag: T): T {
264223 ...flag ,
265224 rules : Array . isArray ( flag . rules ) && flag . rules . length > 0 ? [ ] : flag . rules ,
266225 targetGroups : flag . targetGroups ?. map (
267- ( group : { rules ?: unknown ; [ key : string ] : unknown } ) => ( {
226+ ( group : { rules ?: unknown ; [ key : string ] : unknown } ) => ( {
268227 ...group ,
269228 rules :
270229 Array . isArray ( group . rules ) && group . rules . length > 0
0 commit comments