Skip to content

Commit

Permalink
Using getNonNullableX in multiple places
Browse files Browse the repository at this point in the history
  • Loading branch information
overmode committed Dec 30, 2024
1 parent b266112 commit d3b6547
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 50 deletions.
33 changes: 3 additions & 30 deletions front/lib/api/assistant/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ export async function getConversationFeedbacksForUser(
auth: Authenticator,
conversation: ConversationType | ConversationWithoutContentType
): Promise<Result<AgentMessageFeedbackType[], ConversationError | Error>> {
const owner = auth.workspace();
if (!owner) {
return new Err(new Error("workspace_not_found"));
}
const user = auth.user();
if (!canAccessConversation(auth, conversation) || !user) {
if (!canAccessConversation(auth, conversation)) {
return new Err(new ConversationError("conversation_access_restricted"));
}

Expand Down Expand Up @@ -80,13 +75,6 @@ export async function upsertMessageFeedback(
isConversationShared?: boolean;
}
) {
const owner = auth.workspace();
if (!owner) {
return new Err({
type: "workspace_not_found",
message: "The workspace you're trying to access was not found.",
});
}
const feedbackWithConversationContext =
await AgentMessageFeedbackResource.getFeedbackWithConversationContext({
auth,
Expand All @@ -106,7 +94,7 @@ export async function upsertMessageFeedback(
await feedback.updateFields({
content,
thumbDirection,
isConversationShared: false,
isConversationShared,
});
return new Ok(undefined);
}
Expand Down Expand Up @@ -148,14 +136,6 @@ export async function deleteMessageFeedback(
user: UserType;
}
) {
const owner = auth.workspace();
if (!owner) {
return new Err({
type: "workspace_not_found",
message: "The workspace you're trying to access was not found.",
});
}

if (!canAccessConversation(auth, conversation)) {
return new Err({
type: "conversation_access_restricted",
Expand Down Expand Up @@ -210,14 +190,7 @@ export async function getAgentFeedbacks({
Error
>
> {
const owner = auth.workspace();
if (!owner || !auth.isUser()) {
return new Err(new Error("workspace_not_found"));
}
const plan = auth.plan();
if (!plan) {
return new Err(new Error("plan_not_found"));
}
const owner = auth.getNonNullableWorkspace();

// Make sure the user has access to the agent
const agentConfiguration = await getAgentConfiguration(
Expand Down
27 changes: 8 additions & 19 deletions front/lib/resources/agent_message_feedback_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,13 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
auth: Authenticator,
{ transaction }: { transaction?: Transaction } = {}
): Promise<Result<undefined, Error>> {
try {
await this.model.destroy({
where: {
id: this.id,
},
transaction,
});
return new Ok(undefined);
} catch (err) {
return new Err(err as Error);
}
await this.model.destroy({
where: {
id: this.id,
},
transaction,
});
return new Ok(undefined);
}

async updateFields(
Expand Down Expand Up @@ -217,14 +213,7 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
auth: Authenticator,
conversation: ConversationType | ConversationWithoutContentType
): Promise<Result<AgentMessageFeedbackType[], ConversationError | Error>> {
const owner = auth.workspace();
if (!owner) {
return new Err(new Error("workspace_not_found"));
}
const user = auth.user();
if (!user) {
return new Err(new Error("user_not_found"));
}
const user = auth.getNonNullableUser();

const feedbackForMessages = await Message.findAll({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function handler(
});
}

// Make sure the agent configuration is accessible by the user
// IMPORTANT: make sure the agent configuration is accessible by the user.
const agentConfiguration = await getAgentConfiguration(auth, aId);
if (!agentConfiguration) {
return apiError(req, res, {
Expand Down

0 comments on commit d3b6547

Please sign in to comment.