1import { Inject, Injectable, type OnModuleInit } from "@nestjs/common";
2import { z } from "zod";
3import { CacheService } from "./cache.js";
4import { setPermissionCache } from "../lib/authorize.js";
5
6const LEVEL = z.enum(["NONE", "VIEW", "COMMENT", "EDIT", "FULL_ACCESS"]);
7
8
17@Injectable()
18export class PermissionCacheBridge implements OnModuleInit {
19 constructor(@Inject(CacheService) private readonly cache: CacheService) {}
20
21 onModuleInit(): void {
22 if (this.cache.isOn()) {
23 setPermissionCache((workspaceId, userId, scope, loader) =>
24 this.cache.getPermission(workspaceId, userId, scope, loader, LEVEL)
25 );
26 } else {
27 setPermissionCache(null);
28 }
29 }
30}
31