From 137d6522f220a1eed1d655d3122fd01035023354 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Mon, 15 Apr 2024 18:19:52 +0100 Subject: [PATCH] basic setup of overview page --- src/app/db/schema/relations.ts | 13 ++++++++-- src/app/portfolio/[slug]/utils.ts | 40 ++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/app/db/schema/relations.ts b/src/app/db/schema/relations.ts index dfbd1e13..7ff93285 100644 --- a/src/app/db/schema/relations.ts +++ b/src/app/db/schema/relations.ts @@ -104,10 +104,19 @@ export const portfolioToUsersRelations = relations(portfolio, ({ many }) => ({ })); // Define relation from portfolioUsers to portfolios (we can have many users to a portfolio) +// export const portfolioUsersToPortfolioRelations = relations( +// portfolioUsers, +// ({ many }) => ({ +// portfolio: many(portfolio), +// }) +// ); export const portfolioUsersToPortfolioRelations = relations( portfolioUsers, - ({ many }) => ({ - portfolio: many(portfolio), + ({ one }) => ({ + portfolio: one(portfolio, { + fields: [portfolioUsers.portfolioId], // Ensure the correct foreign key is specified + references: [portfolio.id], + }), }) ); diff --git a/src/app/portfolio/[slug]/utils.ts b/src/app/portfolio/[slug]/utils.ts index c268c7fa..e1225cde 100644 --- a/src/app/portfolio/[slug]/utils.ts +++ b/src/app/portfolio/[slug]/utils.ts @@ -6,7 +6,7 @@ import { } from "./../../db/schema/recommendations"; import { and, eq, inArray } from "drizzle-orm"; import { db } from "@/app/db/db"; -import { portfolio } from "@/app/db/schema/portfolio"; +import { portfolio, portfolioUsers } from "@/app/db/schema/portfolio"; import { property } from "@/app/db/schema/property"; import type { Portfolio } from "@/app/db/schema/portfolio"; import type { PropertyWithRelations } from "@/app/db/schema/property"; @@ -29,6 +29,44 @@ export async function getPortfolio(portfolioId: string): Promise { return data[0]; } +export async function getUserPortfolios( + portfolioId: string +): Promise<{ id: bigint; name: string }[]> { + // This function will retrieve all of the portfolio ids attributed to a user + // This can be done by querying the portfolioUsers table + + const portfolioUser = await db.query.portfolioUsers.findFirst({ + where: eq(portfolioUsers.portfolioId, BigInt(portfolioId)), + }); + + if (!portfolioUser) { + throw new Error("Portfolio not found"); + } + + const userId = portfolioUser?.userId; + + if (!userId) { + throw new Error("User not found"); + } + + // We now filter portfolioUsers by the userId + const data = await db.query.portfolioUsers.findMany({ + where: eq(portfolioUsers.userId, userId), + with: { + portfolio: { + // Make sure this matches the relation name defined in relations setup + columns: { + id: true, + name: true, + }, + }, + }, + }); + + // Extract the portfolio data from the nested object + return data.map((item) => item.portfolio); +} + export async function getProperties( portfolioId: string, limit: number = 1000,