basic setup of overview page

This commit is contained in:
Khalim Conn-Kowlessar 2024-04-15 18:19:52 +01:00
parent ad8b8bed63
commit 137d6522f2
2 changed files with 50 additions and 3 deletions

View file

@ -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],
}),
})
);

View file

@ -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<Portfolio> {
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,