From b100c15052f888d55465348786309a6d7156b42f Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 1 May 2026 16:32:04 +0000 Subject: [PATCH] Fetching user data from users hubspot table --- .../(portfolio)/your-projects/live/page.tsx | 106 ++++++++++-------- 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/src/app/portfolio/[slug]/(portfolio)/your-projects/live/page.tsx b/src/app/portfolio/[slug]/(portfolio)/your-projects/live/page.tsx index 5dc9759e..aae8d8f8 100644 --- a/src/app/portfolio/[slug]/(portfolio)/your-projects/live/page.tsx +++ b/src/app/portfolio/[slug]/(portfolio)/your-projects/live/page.tsx @@ -1,11 +1,13 @@ import { getServerSession } from "next-auth"; import { AuthOptions } from "@/app/api/auth/[...nextauth]/authOptions"; import { redirect } from "next/navigation"; -import { eq, inArray, and, desc } from "drizzle-orm"; +import { eq, inArray, and, desc, sql } from "drizzle-orm"; import LiveTracker from "./LiveTracker"; import { computeLiveTrackerData } from "./transforms"; import { db } from "@/app/db/db"; import { hubspotDealData } from "@/app/db/schema/crm/hubspot_deal_table"; +import { alias } from "drizzle-orm/pg-core"; +import { hubspotUsers } from "@/app/db/schema/crm/hubspot_user_table"; import { uploadedFiles } from "@/app/db/schema/uploaded_files"; import { portfolioOrganisation } from "@/app/db/schema/portfolio_organisation"; import { organisation } from "@/app/db/schema/organisation"; @@ -20,53 +22,61 @@ import type { InferSelectModel } from "drizzle-orm"; import { Card, CardContent } from "@/app/shadcn_components/ui/card"; import { Building2 } from "lucide-react"; -type DbDeal = InferSelectModel; +const coordinatorUser = alias(hubspotUsers, "coordinator_user"); +const designerUser = alias(hubspotUsers, "designer_user"); -function mapDbRowToHubspotDeal(row: DbDeal): HubspotDeal { +type DealRow = { + deal: InferSelectModel; + coordinator: string | null; + designer: string | null; +}; + +function mapDbRowToHubspotDeal(row: DealRow): HubspotDeal { + const d = row.deal; return { - id: row.id, - dealId: row.dealId, - dealname: row.dealname, - dealstage: row.dealstage, - companyId: row.companyId, - projectCode: row.projectCode, - landlordPropertyId: row.landlordPropertyId, - uprn: row.uprn, - outcome: row.outcome, - outcomeNotes: row.outcomeNotes, - majorConditionIssueDescription: row.majorConditionIssueDescription, - majorConditionIssuePhotos: row.majorConditionIssuePhotos, - majorConditionIssuePhotosS3: row.majorConditionIssuePhotosS3, - coordinationStatus: row.coordinationStatus, - designStatus: row.designStatus, - pashubLink: row.pashubLink, - sharepointLink: row.sharepointLink, - dampMouldFlag: row.dampmouldGrowth, - dampMouldAndRepairComments: row.damnpMouldAndRepairComments, - preSapScore: row.preSap, + id: d.id, + dealId: d.dealId, + dealname: d.dealname, + dealstage: d.dealstage, + companyId: d.companyId, + projectCode: d.projectCode, + landlordPropertyId: d.landlordPropertyId, + uprn: d.uprn, + outcome: d.outcome, + outcomeNotes: d.outcomeNotes, + majorConditionIssueDescription: d.majorConditionIssueDescription, + majorConditionIssuePhotos: d.majorConditionIssuePhotos, + majorConditionIssuePhotosS3: d.majorConditionIssuePhotosS3, + coordinationStatus: d.coordinationStatus, + designStatus: d.designStatus, + pashubLink: d.pashubLink, + sharepointLink: d.sharepointLink, + dampMouldFlag: d.dampmouldGrowth, + dampMouldAndRepairComments: d.damnpMouldAndRepairComments, + preSapScore: d.preSap, coordinator: row.coordinator, - ioeV1Date: row.mtpCompletionDate, - ioeV2Date: row.mtpReModelCompletionDate, - ioeV3Date: row.ioeV3CompletionDate, - proposedMeasures: row.proposedMeasures, - approvedPackage: row.approvedPackage, + ioeV1Date: d.mtpCompletionDate, + ioeV2Date: d.mtpReModelCompletionDate, + ioeV3Date: d.ioeV3CompletionDate, + proposedMeasures: d.proposedMeasures, + approvedPackage: d.approvedPackage, designer: row.designer, - designDate: row.designCompletionDate, - actualMeasuresInstalled: row.actualMeasuresInstalled, - installer: row.installer, - installerHandover: row.installerHandover, - lodgementStatus: row.lodgementStatus, - measuresLodgementDate: row.measuresLodgementDate, - fullLodgementDate: row.lodgementDate, - confirmedSurveyDate: row.confirmedSurveyDate, - surveyedDate: row.surveyedDate, - designType: row.dealType, - eiScore: row.eiScore, - eiScorePotential: row.eiScorePotential, - epcSapScore: row.epcSapScore, - epcSapScorePotential: row.epcSapScorePotential, - createdAt: row.createdAt, - updatedAt: row.updatedAt, + designDate: d.designCompletionDate, + actualMeasuresInstalled: d.actualMeasuresInstalled, + installer: d.installer, + installerHandover: d.installerHandover, + lodgementStatus: d.lodgementStatus, + measuresLodgementDate: d.measuresLodgementDate, + fullLodgementDate: d.lodgementDate, + confirmedSurveyDate: d.confirmedSurveyDate, + surveyedDate: d.surveyedDate, + designType: d.dealType, + eiScore: d.eiScore, + eiScorePotential: d.eiScorePotential, + epcSapScore: d.epcSapScore, + epcSapScorePotential: d.epcSapScorePotential, + createdAt: d.createdAt, + updatedAt: d.updatedAt, }; } @@ -123,8 +133,14 @@ export default async function LiveReportingPage(props: { const companyId = link[0].hubspotCompanyId; const rawDeals = await db - .select() + .select({ + deal: hubspotDealData, + coordinator: sql`CASE WHEN ${hubspotDealData.coordinator} IS NULL THEN NULL ELSE COALESCE(${coordinatorUser.firstName} || ' ' || ${coordinatorUser.lastName}, 'Domna Coordinator') END`, + designer: sql`CASE WHEN ${hubspotDealData.designer} IS NULL THEN NULL ELSE COALESCE(${designerUser.firstName} || ' ' || ${designerUser.lastName}, 'Domna Designer') END`, + }) .from(hubspotDealData) + .leftJoin(coordinatorUser, eq(hubspotDealData.coordinator, coordinatorUser.hubspotOwnerId)) + .leftJoin(designerUser, eq(hubspotDealData.designer, designerUser.hubspotOwnerId)) .where(eq(hubspotDealData.companyId, companyId)); const deals = rawDeals.map(mapDbRowToHubspotDeal);