mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-30 12:55:02 +00:00
Fetching user data from users hubspot table
This commit is contained in:
parent
0a6a01caaf
commit
b100c15052
1 changed files with 61 additions and 45 deletions
|
|
@ -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<typeof hubspotDealData>;
|
||||
const coordinatorUser = alias(hubspotUsers, "coordinator_user");
|
||||
const designerUser = alias(hubspotUsers, "designer_user");
|
||||
|
||||
function mapDbRowToHubspotDeal(row: DbDeal): HubspotDeal {
|
||||
type DealRow = {
|
||||
deal: InferSelectModel<typeof hubspotDealData>;
|
||||
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<string | null>`CASE WHEN ${hubspotDealData.coordinator} IS NULL THEN NULL ELSE COALESCE(${coordinatorUser.firstName} || ' ' || ${coordinatorUser.lastName}, 'Domna Coordinator') END`,
|
||||
designer: sql<string | null>`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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue