Skip to content

Commit

Permalink
Fetch agents only available for the current user
Browse files Browse the repository at this point in the history
  • Loading branch information
satococoa committed Nov 1, 2024
1 parent 9813f8e commit d6621ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
14 changes: 1 addition & 13 deletions app/(main)/agents/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getTeamMembershipByAgentId } from "@/app/(auth)/lib/get-team-membership-by-agent-id";
import { getUser } from "@/lib/supabase";
import { createAgent, getAgents } from "@/services/agents";
import { CreateAgentButton } from "@/services/agents/components";
Expand All @@ -9,18 +8,7 @@ type AgentListProps = {
userId: string;
};
async function AgentList(props: AgentListProps) {
const allAgents = await getAgents({ userId: props.userId });
const agents = (
await Promise.all(
allAgents.map(async (agent) => {
const teamMembership = await getTeamMembershipByAgentId(
agent.id,
props.userId,
);
return teamMembership ? agent : null;
}),
)
).filter((v) => v != null);
const agents = await getAgents({ userId: props.userId });

return (
<div className="flex flex-col gap-2">
Expand Down
19 changes: 17 additions & 2 deletions services/agents/actions/get-agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use server";

import { agents, db } from "@/drizzle";
import { agents, db, supabaseUserMappings, teamMemberships } from "@/drizzle";
import { eq } from "drizzle-orm";
import { revalidateTag, unstable_cache } from "next/cache";
import type { AgentId } from "../types";
Expand All @@ -17,7 +17,22 @@ const getAgentsTag = (params: TagParams) => `${params.userId}.getAgents`;

export const getAgents = async (args: GetAgentsArgs) => {
const cachedAgents = unstable_cache(
() => db.select().from(agents),
async () => {
const result = await db
.select({ agents })
.from(agents)
.innerJoin(
teamMemberships,
eq(agents.teamDbId, teamMemberships.teamDbId),
)
.innerJoin(
supabaseUserMappings,
eq(teamMemberships.userDbId, supabaseUserMappings.userDbId),
)
.where(eq(supabaseUserMappings.supabaseUserId, args.userId));

return result.map((row) => row.agents);
},
[args.userId],
{ tags: [getAgentsTag(args)] },
);
Expand Down

0 comments on commit d6621ef

Please sign in to comment.