diff --git a/src/app/api/plan/trigger/route.ts b/src/app/api/plan/trigger/route.ts index 5c21a116..bf6be78f 100644 --- a/src/app/api/plan/trigger/route.ts +++ b/src/app/api/plan/trigger/route.ts @@ -1,30 +1,36 @@ import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; import { MeasureKeyEnum } from "@/app/db/schema/recommendations"; +import { PortfolioGoal } from "@/app/db/schema/portfolio"; -const PresignedUrlBodySchema = z.object({ - portfolio_id: z.string(), - housing_type: z.enum(["Social", "Private"]), - goal: z.enum(["Increasing EPC", "Reduce energy consumption"]), - goal_value: z.string(), - trigger_file_path: z.string(), - valuation_file_path: z.string(), - multi_plan: z.boolean().optional(), - budget: z.number().optional().nullable(), - scenario_name: z.string().optional(), - sheet_count: z.number().optional(), // Number of rows in the selected sheet - event_type: z.enum(["remote_assessment"]).optional(), - ashp_cop: z.number().optional(), - // inclusions is a list of measures, where the values are in measuresList - inclusions: z.array(MeasureKeyEnum).optional(), - exclusions: z.array(MeasureKeyEnum).optional(), - already_installed_file_path: z.string().optional(), - // optional scenario_id to link the plan to an existing scenario - scenario_id: z.string().optional().nullable(), - file_type: z.enum(["csv", "xlsx"]).optional(), // Specify the file type - file_format: z.enum(["domna_asset_list"]).optional().nullable(), // Specify the file format - sheet_name: z.string().optional().nullable(), // Specify the sheet name if applicable -}); +const PresignedUrlBodySchema = z + .object({ + portfolio_id: z.string(), + housing_type: z.enum(["Social", "Private"]), + goal: z.enum(PortfolioGoal), + goal_value: z.string().nullable(), // Nullable to handle cases where goal_value is not applicable + trigger_file_path: z.string(), + valuation_file_path: z.string(), + multi_plan: z.boolean().optional(), + budget: z.number().optional().nullable(), + scenario_name: z.string().optional(), + sheet_count: z.number().optional(), // Number of rows in the selected sheet + event_type: z.enum(["remote_assessment"]).optional(), + ashp_cop: z.number().optional(), + // inclusions is a list of measures, where the values are in measuresList + inclusions: z.array(MeasureKeyEnum).optional(), + exclusions: z.array(MeasureKeyEnum).optional(), + already_installed_file_path: z.string().optional(), + // optional scenario_id to link the plan to an existing scenario + scenario_id: z.string().optional().nullable(), + file_type: z.enum(["csv", "xlsx"]).optional(), // Specify the file type + file_format: z.enum(["domna_asset_list"]).optional().nullable(), // Specify the file format + sheet_name: z.string().optional().nullable(), // Specify the sheet name if applicable + }) + .refine((data) => data.goal !== "Increasing EPC" || !!data.goal_value, { + path: ["goal_value"], + message: "Target EPC Rating is required when goal is Increasing EPC", + }); export async function POST(request: NextRequest) { // For the moment, this api specifically handles uploads of csvs diff --git a/src/app/portfolio/[slug]/components/FormSchema.tsx b/src/app/portfolio/[slug]/components/FormSchema.tsx index 9e281fb8..1a2c36f1 100644 --- a/src/app/portfolio/[slug]/components/FormSchema.tsx +++ b/src/app/portfolio/[slug]/components/FormSchema.tsx @@ -1,6 +1,6 @@ // formSchemas.ts import * as z from "zod"; -import { MeasureKeyEnum } from "@/app/db/schema/recommendations"; +import { MeasureKeyEnum, HousingType } from "@/app/db/schema/recommendations"; export const baseFormSchema = z.object({ measures: z.array(MeasureKeyEnum).min(1, "At least one measure is required"), @@ -15,7 +15,7 @@ export const RemoteAssessmentFormSchema = baseFormSchema // goalValue: z.string().min(1), goalValue: z.string().optional(), budget: z.number().optional(), - housingType: z.string().min(1), + housingType: z.enum(HousingType), addressLineOne: z.string().min(1), postcode: z.string().min(1), uprn: z.number().min(1, "UPRN must be a valid number"), diff --git a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx index 85daa287..54752650 100644 --- a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx +++ b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx @@ -119,12 +119,12 @@ const selectGoalOptions = [ disabled: false, }, { - label: "Reduce energy consumption", + label: "Energy Savings", value: "Energy Savings", disabled: false, }, { - label: "Reduce CO2 emissions", + label: "Reducing CO2 emissions", value: "Reducing CO2 emissions", disabled: false, }, @@ -373,7 +373,7 @@ function useCreateRemoteAssessment({ housing_type: data.housingType, goal: data.goal, // We only send goal_value if the goal is "Increasing EPC" - goal_value: data.goal, + goal_value: data.goalValue || null, trigger_file_path: assetListFileKey, already_installed_file_path: "", patches_file_path: "",