From 70a11a6ab974e708d701ab427aff57c1de7dbba5 Mon Sep 17 00:00:00 2001 From: filou Date: Sat, 9 Dec 2023 17:40:54 +0100 Subject: [PATCH] Fix: calls with undefined user not going through --- front/lib/api/assistant/configuration.ts | 41 +++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/front/lib/api/assistant/configuration.ts b/front/lib/api/assistant/configuration.ts index 34ee4d6f6f34..b460360e771a 100644 --- a/front/lib/api/assistant/configuration.ts +++ b/front/lib/api/assistant/configuration.ts @@ -55,7 +55,7 @@ export async function getAgentConfiguration( agentConfiguration?: AgentConfiguration ): Promise { const owner = auth.workspace(); - if (!owner) { + if (!owner || !auth.isUser()) { throw new Error("Unexpected `auth` without `workspace`."); } const plan = auth.plan(); @@ -66,7 +66,7 @@ export async function getAgentConfiguration( if (isGlobalAgentId(agentId)) { return await getGlobalAgent(auth, agentId, null); } - + const user = auth.user(); const agent = agentConfiguration ?? (await AgentConfiguration.findOne({ @@ -88,14 +88,16 @@ export async function getAgentConfiguration( model: AgentDustAppRunConfiguration, as: "dustAppRunConfiguration", }, - { - model: AgentUserRelation, - where: { - userId: auth.user()?.id, - }, - attributes: ["relation"], - required: false, - }, + ...(user + ? [ + { + model: AgentUserRelation, + where: { userId: user.id }, + attributes: ["relation"], + required: false, + }, + ] + : []), ], limit: 1, })); @@ -247,6 +249,7 @@ export async function getAgentConfigurations( throw new Error("Unexpected `auth` from outside workspace."); } + const user = auth.user(); const baseAgentsSequelizeQuery = { where: { workspaceId: owner.id, @@ -266,14 +269,16 @@ export async function getAgentConfigurations( model: AgentDustAppRunConfiguration, as: "dustAppRunConfiguration", }, - { - model: AgentUserRelation, - where: { - userId: auth.user()?.id, - }, - attributes: ["relation"], - required: false, - }, + ...(user + ? [ + { + model: AgentUserRelation, + where: { userId: user.id }, + attributes: ["relation"], + required: false, + }, + ] + : []), ], };