Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HouseHold calculated in the API #70

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/api/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { api, apiSchema } from "../schemas/apiSchema";
import { calculationSchema } from "../schemas/calculationSchema";
import * as calculationService from "../services/calculationService";
import calculateFairhold from "@/app/models/testClasses";

export async function POST(req: Request) {
try {
Expand All @@ -14,7 +15,8 @@ export async function POST(req: Request) {
input = data;
}
const householdData = await calculationService.getHouseholdData(input);
return NextResponse.json(householdData);
const processedData = calculateFairhold(householdData);
return NextResponse.json(processedData);
} catch (err) {
console.log("ERROR: API - ", (err as Error).message);
const response = { error: (err as Error).message };
Expand Down
4 changes: 1 addition & 3 deletions app/components/ui/CalculatorInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { useState } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";

import calculateFairhold from "@/app/models/testClasses";
import { Household } from "@/app/models/Household";
import Dashboard from "./Dashboard";
import {
Expand Down Expand Up @@ -49,8 +48,7 @@ const CalculatorInput = () => {
body: JSON.stringify(data), // pass the form data to the API
});

const jsonData = await response.json();
const processedData = calculateFairhold(jsonData);
const processedData = await response.json();

// saved processedData & switch to dashboard view
setData(processedData);
Expand Down
36 changes: 21 additions & 15 deletions app/data/rentRepo.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import prisma from "./db";

const getRentByITL3 = async (itl3: string): Promise<number | null> => {
try {
const result = await prisma.rent.aggregate({
where: {
itl3: { equals: itl3 },
},
_avg: {
monthlyMeanRent: true,
},
});
const getRentByITL3 = async (itl3: string): Promise<number> => {
try {
const result = await prisma.rent.aggregate({
where: {
itl3: { equals: itl3 },
},
_avg: {
monthlyMeanRent: true,
},
});

const monthlyMeanRent = result._avg.monthlyMeanRent;
const monthlyMeanRent = result._avg.monthlyMeanRent;

return monthlyMeanRent;
} catch (error) {
throw new Error(`Data error: Unable to find monthlyMeanRent for itl3 ${itl3}`);
if (monthlyMeanRent === null) {
throw new Error(`No monthlyMeanRent found for itl3 ${itl3}`);
}

return monthlyMeanRent;
} catch (error) {
throw new Error(
`Data error: Unable to find monthlyMeanRent for itl3 ${itl3}`
);
}
};

export const rentRepo = {
getRentByITL3,
getRentByITL3,
};
61 changes: 38 additions & 23 deletions app/data/socialRentAdjustmentsRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,51 @@ import prisma from "./db";

export type socialRentAdjustmentTypes = {
year: string;
inflation: string;
additional: string;
total: string;
inflation: number;
additional: number;
total: number;
}[];

const getSocialRentAdjustments = async (): Promise<socialRentAdjustmentTypes> => {
const getSocialRentAdjustments =
async (): Promise<socialRentAdjustmentTypes> => {
try {
const result = await prisma.socialRentAdjustments.findMany({
select: {
year: true,
inflation: true,
additional: true,
total: true,
}
});
const result = await prisma.socialRentAdjustments.findMany({
select: {
year: true,
inflation: true,
additional: true,
total: true,
},
});

const socialRentAdjustments: socialRentAdjustmentTypes = result.map(item => ({
year: item.year || '',
inflation: item.inflation?.toString() || '',
additional: item.additional?.toString() || '',
total: item.total?.toString() || '',
}));
const socialRentAdjustments: socialRentAdjustmentTypes = result.map(
(item) => {
if (
item.year === null ||
item.inflation === null ||
item.additional === null ||
item.total === null
) {
throw new Error(
`Data error: Found null values in socialRentAdjustments`
);
}
return {
year: item.year,
inflation: item.inflation,
additional: item.additional,
total: item.total,
};
}
);

return socialRentAdjustments;
// Return the array after validation
return socialRentAdjustments;
} catch (error) {
throw new Error(`Data error: Unable to find socialRentAdjustments`);
throw new Error(`Data error: unable to find socialRentAdjustments`);
}
};
};

export const socialRentAdjustmentsRepo = {
getSocialRentAdjustments,
getSocialRentAdjustments,
};
165 changes: 140 additions & 25 deletions app/models/Household.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,149 @@
import { DEFAULT_FORECAST_PARAMETERS } from "./ForecastParameters";
import { Household } from "./Household";
import { Property } from "./Property";
import { SocialRentAdjustments } from "./tenure/SocialRent";
import { socialRentAdjustmentTypes } from "../data/socialRentAdjustmentsRepo";

let property: Property;
let household: Household;
const socialRentAdjustments: SocialRentAdjustments = [
{ inflation: 3.3, total: 4.3, year: "2001-02" },
{ inflation: 1.7, total: 2.2, year: "2002-03" },
{ inflation: 1.7, total: 2.2, year: "2003-04" },
{ inflation: 2.8, total: 3.3, year: "2004-05" },
{ inflation: 3.1, total: 3.6, year: "2005-06" },
{ inflation: 2.7, total: 3.2, year: "2006-07" },
{ inflation: 3.6, total: 4.1, year: "2007-08" },
{ inflation: 3.9, total: 4.4, year: "2008-09" },
{ inflation: 5.0, total: 5.5, year: "2009-10" },
{ inflation: -1.4, total: -0.9, year: "2010-11" },
{ inflation: 4.6, total: 5.1, year: "2011-12" },
{ inflation: 5.6, total: 6.1, year: "2012-13" },
{ inflation: 2.6, total: 3.1, year: "2013-14" },
{ inflation: 3.2, total: 3.7, year: "2014-15" },
{ inflation: 1.2, total: 2.2, year: "2015-16" },
{ inflation: NaN, total: -1.0, year: "2016-17" },
{ inflation: NaN, total: -1.0, year: "2017-18" },
{ inflation: NaN, total: -1.0, year: "2018-19" },
{ inflation: NaN, total: -1.0, year: "2019-20" },
{ inflation: 1.7, total: 2.7, year: "2020-21" },
{ inflation: 0.5, total: 1.5, year: "2021-22" },
{ inflation: 3.1, total: 4.1, year: "2022-23" },
{ inflation: 10.1, total: 11.1, year: "2023-24" },
const socialRentAdjustments: socialRentAdjustmentTypes = [
{
inflation: 3.3,
total: 4.3,
year: "2001-02",
additional: 0,
},
{
inflation: 1.7,
total: 2.2,
year: "2002-03",
additional: 0,
},
{
inflation: 1.7,
total: 2.2,
year: "2003-04",
additional: 0,
},
{
inflation: 2.8,
total: 3.3,
year: "2004-05",
additional: 0,
},
{
inflation: 3.1,
total: 3.6,
year: "2005-06",
additional: 0,
},
{
inflation: 2.7,
total: 3.2,
year: "2006-07",
additional: 0,
},
{
inflation: 3.6,
total: 4.1,
year: "2007-08",
additional: 0,
},
{
inflation: 3.9,
total: 4.4,
year: "2008-09",
additional: 0,
},
{
inflation: 5.0,
total: 5.5,
year: "2009-10",
additional: 0,
},
{
inflation: -1.4,
total: -0.9,
year: "2010-11",
additional: 0,
},
{
inflation: 4.6,
total: 5.1,
year: "2011-12",
additional: 0,
},
{
inflation: 5.6,
total: 6.1,
year: "2012-13",
additional: 0,
},
{
inflation: 2.6,
total: 3.1,
year: "2013-14",
additional: 0,
},
{
inflation: 3.2,
total: 3.7,
year: "2014-15",
additional: 0,
},
{
inflation: 1.2,
total: 2.2,
year: "2015-16",
additional: 0,
},
{
inflation: NaN,
total: -1.0,
year: "2016-17",
additional: 0,
},
{
inflation: NaN,
total: -1.0,
year: "2017-18",
additional: 0,
},
{
inflation: NaN,
total: -1.0,
year: "2018-19",
additional: 0,
},
{
inflation: NaN,
total: -1.0,
year: "2019-20",
additional: 0,
},
{
inflation: 1.7,
total: 2.7,
year: "2020-21",
additional: 0,
},
{
inflation: 0.5,
total: 1.5,
year: "2021-22",
additional: 0,
},
{
inflation: 3.1,
total: 4.1,
year: "2022-23",
additional: 0,
},
{
inflation: 10.1,
total: 11.1,
year: "2023-24",
additional: 0,
},
];

beforeEach(() => {
Expand Down
5 changes: 3 additions & 2 deletions app/models/Household.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { FairholdLandPurchase } from "./tenure/FairholdLandPurchase";
import { FairholdLandRent } from "./tenure/FairholdLandRent";
import { Fairhold } from "./Fairhold";
import { Property } from "./Property";
import { SocialRent, SocialRentAdjustments } from "./tenure/SocialRent";
import { SocialRent } from "./tenure/SocialRent";
import { ForecastParameters } from "./ForecastParameters";
import { socialRentAdjustmentTypes } from "../data/socialRentAdjustmentsRepo";

const HOUSE_MULTIPLIER = 2.4;

Expand All @@ -15,7 +16,7 @@ type ConstructorParams = Pick<
> & {
averageRentYearly: number;
socialRentAverageEarning: number;
socialRentAdjustments: SocialRentAdjustments;
socialRentAdjustments: socialRentAdjustmentTypes;
housePriceIndex: number;
};

Expand Down
5 changes: 3 additions & 2 deletions app/models/tenure/SocialRent.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SocialRent, SocialRentAdjustments } from "./SocialRent";
import { SocialRent } from "./SocialRent";
import { socialRentAdjustmentTypes } from "../../data/socialRentAdjustmentsRepo";

let tenureSocialRent: SocialRent;

beforeEach(() => {
const socialRentAdjustments: SocialRentAdjustments = [
const socialRentAdjustments: socialRentAdjustmentTypes = [
{
inflation: 3.3,
total: 4.3,
Expand Down
11 changes: 2 additions & 9 deletions app/models/tenure/SocialRent.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { WEEKS_PER_MONTH } from "../constants";
import { BED_WEIGHTS_AND_CAPS, NATIONAL_AVERAGES } from "../constants";
import { socialRentAdjustmentTypes } from "../../data/socialRentAdjustmentsRepo";

interface SocialRentParams {
numberOfBedrooms: number;
socialRentAverageEarning: number;
socialRentAdjustments: SocialRentAdjustments;
socialRentAdjustments: socialRentAdjustmentTypes;
housePriceIndex: number;
landToTotalRatio: number;
}

export type SocialRentAdjustments = {
year: string;
inflation: number;
// TODO: Is this optional or required?
additional?: number;
total: number;
}[];

export class SocialRent {
socialRentAverageEarning: number;
/** adjustment factors that take into account the increase of living cost */
Expand Down
Loading
Loading