@@ -19,6 +19,7 @@ import (
1919 "github.com/theopenlane/core/internal/ent/eventqueue"
2020 entgen "github.com/theopenlane/core/internal/ent/generated"
2121 "github.com/theopenlane/core/internal/ent/generated/customdomain"
22+ "github.com/theopenlane/core/internal/ent/generated/dnsverification"
2223 notegen "github.com/theopenlane/core/internal/ent/generated/note"
2324 "github.com/theopenlane/core/internal/ent/generated/standard"
2425 "github.com/theopenlane/core/internal/ent/generated/subprocessor"
@@ -145,9 +146,7 @@ func handleTrustCenterDocMutationGala(ctx gala.HandlerContext, payload eventqueu
145146 return nil
146147 }
147148
148- if err := enqueueCacheRefresh (ctx .Context , client , trustCenterID ); err != nil {
149- logx .FromContext (ctx .Context ).Warn ().Err (err ).Str ("trust_center_id" , trustCenterID ).Msg ("failed to refresh trust center cache after doc mutation" )
150- }
149+ refreshTrustCenterCache (ctx .Context , client , trustCenterID , "doc mutation" )
151150
152151 return nil
153152}
@@ -183,9 +182,7 @@ func handleNoteMutationGala(ctx gala.HandlerContext, payload eventqueue.Mutation
183182 }
184183
185184 for _ , tcID := range tcIDs {
186- if err := enqueueCacheRefresh (ctx .Context , client , tcID ); err != nil {
187- logx .FromContext (ctx .Context ).Warn ().Err (err ).Str ("trust_center_id" , tcID ).Msg ("failed to trigger cache invalidation for note" )
188- }
185+ refreshTrustCenterCache (ctx .Context , client , tcID , "note mutation" )
189186 }
190187
191188 return nil
@@ -213,7 +210,9 @@ func handleTrustCenterEntityMutationGala(ctx gala.HandlerContext, payload eventq
213210 return nil
214211 }
215212
216- return enqueueCacheRefresh (ctx .Context , client , trustCenterID )
213+ refreshTrustCenterCache (ctx .Context , client , trustCenterID , "entity mutation" )
214+
215+ return nil
217216}
218217
219218// handleTrustCenterFAQMutationGala processes TrustCenterFAQ mutations and invalidates cache.
@@ -241,7 +240,9 @@ func handleTrustCenterFAQMutationGala(ctx gala.HandlerContext, payload eventqueu
241240 return nil
242241 }
243242
244- return enqueueCacheRefresh (ctx .Context , client , id )
243+ refreshTrustCenterCache (ctx .Context , client , id , "faq mutation" )
244+
245+ return nil
245246}
246247
247248// handleTrustCenterSubprocessorMutationGala processes TrustCenterSubprocessor mutations and invalidates cache.
@@ -269,7 +270,9 @@ func handleTrustCenterSubprocessorMutationGala(ctx gala.HandlerContext, payload
269270 return nil
270271 }
271272
272- return enqueueCacheRefresh (ctx .Context , client , trustCenterID )
273+ refreshTrustCenterCache (ctx .Context , client , trustCenterID , "trust center subprocessor mutation" )
274+
275+ return nil
273276}
274277
275278// handleTrustCenterComplianceMutationGala processes TrustCenterCompliance mutations and invalidates cache.
@@ -298,7 +301,9 @@ func handleTrustCenterComplianceMutationGala(ctx gala.HandlerContext, payload ev
298301 return nil
299302 }
300303
301- return enqueueCacheRefresh (ctx .Context , client , trustCenterID )
304+ refreshTrustCenterCache (ctx .Context , client , trustCenterID , "compliance mutation" )
305+
306+ return nil
302307}
303308
304309// handleSubprocessorMutationGala processes Subprocessor mutations and invalidates related trust center cache.
@@ -336,9 +341,7 @@ func handleSubprocessorMutationGala(ctx gala.HandlerContext, payload eventqueue.
336341 }))
337342
338343 for _ , tcID := range trustCenterIDs {
339- if err := enqueueCacheRefresh (ctx .Context , client , tcID ); err != nil {
340- logx .FromContext (ctx .Context ).Warn ().Err (err ).Str ("trust_center_id" , tcID ).Msg ("failed to trigger cache invalidation for subprocessor" )
341- }
344+ refreshTrustCenterCache (ctx .Context , client , tcID , "subprocessor mutation" )
342345 }
343346
344347 return nil
@@ -379,9 +382,7 @@ func handleStandardMutationGala(ctx gala.HandlerContext, payload eventqueue.Muta
379382 }))
380383
381384 for _ , tcID := range trustCenterIDs {
382- if err := enqueueCacheRefresh (ctx .Context , client , tcID ); err != nil {
383- logx .FromContext (ctx .Context ).Warn ().Err (err ).Str ("trust_center_id" , tcID ).Msg ("failed to trigger cache invalidation for standard" )
384- }
385+ refreshTrustCenterCache (ctx .Context , client , tcID , "standard mutation" )
385386 }
386387
387388 return nil
@@ -412,7 +413,9 @@ func handleTrustCenterSettingMutationGala(ctx gala.HandlerContext, payload event
412413 return nil
413414 }
414415
415- return enqueueCacheRefresh (ctx .Context , client , trustCenterID )
416+ refreshTrustCenterCache (ctx .Context , client , trustCenterID , "setting mutation" )
417+
418+ return nil
416419}
417420
418421// handleTrustCenterMutationGala processes TrustCenter mutations and refreshes cache.
@@ -427,7 +430,9 @@ func handleTrustCenterMutationGala(ctx gala.HandlerContext, payload eventqueue.M
427430 return nil
428431 }
429432
430- return enqueueCacheRefresh (ctx .Context , client , trustCenterID )
433+ refreshTrustCenterCache (ctx .Context , client , trustCenterID , "trust center mutation" )
434+
435+ return nil
431436}
432437
433438// shouldInvalidateCacheForSubprocessor determines if subprocessor changes require cache invalidation.
@@ -467,6 +472,13 @@ const (
467472 cacheRefreshMaxBackoff = 30 * time .Second
468473)
469474
475+ func refreshTrustCenterCache (ctx context.Context , client * entgen.Client , trustCenterID , source string ) {
476+ if err := enqueueCacheRefresh (ctx , client , trustCenterID ); err != nil {
477+ logx .FromContext (ctx ).Warn ().Err (err ).Str ("trust_center_id" , trustCenterID ).
478+ Str ("caller" , source ).Msg ("failed to refresh trust center cache" )
479+ }
480+ }
481+
470482// enqueueCacheRefresh triggers a cache refresh by hitting the trust center URL with ?fresh=1
471483func enqueueCacheRefresh (ctx context.Context , client * entgen.Client , trustCenterID string ) error {
472484 // In durable dispatch the context is reconstructed from a snapshot that does not include the
@@ -487,17 +499,10 @@ func enqueueCacheRefresh(ctx context.Context, client *entgen.Client, trustCenter
487499
488500 var customDomain string
489501 if tc .CustomDomainID != nil {
490- cd , err := client .CustomDomain .Query ().
491- Where (customdomain .ID (* tc .CustomDomainID )).
492- Select (customdomain .FieldCnameRecord ).
493- Only (ctx )
502+ customDomain , err = getVerifiedDomain (ctx , client , * tc .CustomDomainID , false )
494503 if err != nil {
495- logx .FromContext (ctx ).Error ().Err (err ).Str ("trust_center_id" , trustCenterID ).Str ("custom_domain_id" , * tc .CustomDomainID ).Msg ("failed to query custom domain for cache invalidation" )
496-
497504 return err
498505 }
499-
500- customDomain = cd .CnameRecord
501506 }
502507
503508 targetURL := buildTrustCenterURL (customDomain , tc .Slug )
@@ -511,24 +516,60 @@ func enqueueCacheRefresh(ctx context.Context, client *entgen.Client, trustCenter
511516 return nil
512517 }
513518
514- cd , err := client .CustomDomain .Query ().
515- Where (customdomain .ID (tc .PreviewDomainID )).
516- Select (customdomain .FieldCnameRecord ).
517- Only (ctx )
519+ previewDomain , err := getVerifiedDomain (ctx , client , tc .PreviewDomainID , true )
518520 if err != nil {
519- logx .FromContext (ctx ).Error ().Err (err ).Str ("trust_center_id" , trustCenterID ).Str ("preview_domain_id" , tc .PreviewDomainID ).Msg ("failed to query preview domain for cache invalidation" )
520-
521521 return err
522522 }
523+ if previewDomain == "" {
524+ return nil
525+ }
523526
524- previewURL := buildTrustCenterURL (cd . CnameRecord , "" )
527+ previewURL := buildTrustCenterURL (previewDomain , "" )
525528 if previewURL == "" {
526529 return nil
527530 }
528531
529532 return triggerCacheRefresh (ctx , previewURL )
530533}
531534
535+ func getVerifiedDomain (ctx context.Context , client * entgen.Client , domainID string , isPreviewDomain bool ) (string , error ) {
536+ logField := trustcenter .FieldCustomDomainID
537+ if isPreviewDomain {
538+ logField = trustcenter .FieldPreviewDomainID
539+ }
540+
541+ cd , err := client .CustomDomain .Query ().
542+ Where (customdomain .ID (domainID )).
543+ Select (customdomain .FieldCnameRecord , customdomain .FieldDNSVerificationID ).
544+ WithDNSVerification (func (q * entgen.DNSVerificationQuery ) {
545+ q .Select (dnsverification .FieldDNSVerificationStatus )
546+ }).
547+ Only (ctx )
548+ if err != nil {
549+ logx .FromContext (ctx ).Error ().Err (err ).Str (logField , domainID ).Msg ("failed to query custom domain for cache invalidation" )
550+
551+ return "" , err
552+ }
553+
554+ dnsVerification , err := cd .Edges .DNSVerificationOrErr ()
555+ if err != nil || dnsVerification == nil {
556+ logx .FromContext (ctx ).Warn ().Err (err ).Str (logField , domainID ).
557+ Msg ("dns verification not found for custom domain, skipping custom domain cache refresh url" )
558+
559+ return "" , nil
560+ }
561+
562+ if dnsVerification .DNSVerificationStatus != enums .DNSVerificationStatusActive {
563+ logx .FromContext (ctx ).Info ().Str (logField , domainID ).
564+ Str ("dns_verification_status" , dnsVerification .DNSVerificationStatus .String ()).
565+ Msg ("custom domain dns verification is not active, skipping custom domain cache refresh url" )
566+
567+ return "" , nil
568+ }
569+
570+ return cd .CnameRecord , nil
571+ }
572+
532573// buildTrustCenterURL constructs the trust center URL from custom domain or slug
533574func buildTrustCenterURL (customDomain , slug string ) string {
534575 scheme := trustCenterConfig .CacheRefreshScheme
0 commit comments