mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-30 12:55:02 +00:00
basic setup of overview page
This commit is contained in:
parent
ad8b8bed63
commit
137d6522f2
2 changed files with 50 additions and 3 deletions
|
|
@ -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],
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue