import { Inject, Injectable, type OnModuleInit } from "@nestjs/common"; import { z } from "zod"; import { CacheService } from "./cache.js"; import { setPermissionCache } from "../lib/authorize.js"; const LEVEL = z.enum(["NONE", "VIEW", "COMMENT", "EDIT", "FULL_ACCESS"]); /** * TASK-0135: boot-time bridge that hands the live `CacheService` into the * plain `authorize` lib (which has no Nest DI). When the cache is enabled * (`PERF_CACHE` on), every `effectiveLevel` decision is memoized under a * (workspace, user, resource-scope) key with a 60 s TTL and busted on * `permission.changed`. When the cache is off, `setPermissionCache(null)` * keeps decisions going straight to Postgres. This is a pure accelerator — * the resolution logic (the loader) is unchanged. */ @Injectable() export class PermissionCacheBridge implements OnModuleInit { constructor(@Inject(CacheService) private readonly cache: CacheService) {} onModuleInit(): void { if (this.cache.isOn()) { setPermissionCache((workspaceId, userId, scope, loader) => this.cache.getPermission(workspaceId, userId, scope, loader, LEVEL) ); } else { setPermissionCache(null); } } }