From c45b5a863e3ba400db9889ee3e67a486efbedb71 Mon Sep 17 00:00:00 2001 From: chronark Date: Thu, 17 Oct 2024 00:23:39 +0200 Subject: [PATCH] fix: ensure workspace is loaded --- apps/api/src/pkg/keys/service.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/apps/api/src/pkg/keys/service.ts b/apps/api/src/pkg/keys/service.ts index 07301d5df1..67df0b53d6 100644 --- a/apps/api/src/pkg/keys/service.ts +++ b/apps/api/src/pkg/keys/service.ts @@ -347,8 +347,26 @@ export class KeyService { this.logger.info("data from cache or db", { data, }); + // Quick fix + if (!data.workspace) { + this.logger.warn("workspace not found, trying again", { + workspace: data.key.workspaceId, + }); + await this.cache.keyByHash.remove(keyHash); + const ws = await this.db.primary.query.workspaces.findFirst({ + where: (table, { eq }) => eq(table.id, data.key.workspaceId), + }); + if (!ws) { + this.logger.error("fallback workspace not found either", { + workspaceId: data.key.workspaceId, + }); + return Err(new DisabledWorkspaceError(data.key.workspaceId)); + } + data.workspace = ws; + } + if ((data.forWorkspace && !data.forWorkspace.enabled) || !data.workspace?.enabled) { - return Err(new DisabledWorkspaceError(data.workspace.id)); + return Err(new DisabledWorkspaceError(data.workspace?.id ?? "N/A")); } /**