Skip to content

Commit

Permalink
feat: Query new PricePaidSummary model
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Jan 8, 2025
1 parent 5cb8f6c commit cd9a5c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 82 deletions.
99 changes: 18 additions & 81 deletions app/data/pricesPaidRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,44 @@ import prisma from "./db";

const MINIMUM_NUMBER_OF_POSTCODES = 30;

interface pricesPaidParams {
interface PricesPaidParams {
averagePrice: number;
numberOfTransactions: number;
granularityPostcode: string;
}

const getPricesPaidByPostcodeAndHouseType = async (
postcodeDistrict: string,
postcodeArea: string,
postcodeSector: string,
houseType: string
): Promise<pricesPaidParams> => {
): Promise<PricesPaidParams> => {
try {
// create the progressive queries
let numberOfTransactions; // declare the variable for numbers of transactions retrieved
let granularityPostcode; // declare the granularity of the postcode
let averagePrice;

const pricesPaidSector = await prisma.pricesPaid.aggregate({
const summary = await prisma.pricesPaidSummary.findFirst({
where: {
propertyType: {
equals: houseType,
},
propertyType: houseType,
postcode: {
startsWith: postcodeSector,
in: [postcodeSector, postcodeDistrict, postcodeArea],
},
transactionCount: {
gte: MINIMUM_NUMBER_OF_POSTCODES,
},
},
_count: {
id: true,
},
_avg: {
price: true,
orderBy: {
// Coincidentally 'sector' comes before 'district' comes before 'area'
granularityLevel: "asc",
},
});

const numberPerSector = pricesPaidSector._count.id;
const isMinMetBySector = numberPerSector >= MINIMUM_NUMBER_OF_POSTCODES;

if (!isMinMetBySector) {
const pricesPaidDistrict = await prisma.pricesPaid.aggregate({
where: {
propertyType: {
equals: houseType,
},
postcode: {
startsWith: postcodeDistrict,
},
},
_count: {
id: true,
},
_avg: {
price: true,
},
});

const numberPerDistrict = pricesPaidDistrict._count.id;
const isMinMetByDistrict =
numberPerDistrict >= MINIMUM_NUMBER_OF_POSTCODES;

if (!isMinMetByDistrict) {
const pricesPaidArea = await prisma.pricesPaid.aggregate({
where: {
propertyType: {
equals: houseType,
},
postcode: {
startsWith: postcodeArea,
},
},
_count: {
id: true,
},
_avg: {
price: true,
},
});
const numberPerArea = pricesPaidArea._count.id;
numberOfTransactions = numberPerArea; // check the granularity
granularityPostcode = postcodeArea; // granularity of the postcode when performing the average price search
averagePrice = pricesPaidArea._avg.price;
} else {
numberOfTransactions = numberPerDistrict; // check the granularity
granularityPostcode = postcodeDistrict; // granularity of the postcode
averagePrice = pricesPaidDistrict._avg.price;
}
} else {
numberOfTransactions = numberPerSector; // check the granularity
granularityPostcode = postcodeSector; // granularity of the postcode
averagePrice = pricesPaidSector._avg.price;
}

if (averagePrice === null) {
throw new Error("Unable to calculate average price");
if (!summary) {
throw new Error("Unable to find sufficient transaction data");
}

// Cast to string as 'not: null' clause in Prisma query does not type narrow
return {
averagePrice,
numberOfTransactions,
granularityPostcode,
} as pricesPaidParams;
averagePrice: summary.averagePrice,
numberOfTransactions: summary.transactionCount,
granularityPostcode: summary.postcode,
};
} catch (error) {
throw new Error(
`Data error: Unable to get pricesPaid for postcode district ${postcodeDistrict} and houseType ${houseType}`
Expand Down
2 changes: 1 addition & 1 deletion app/models/testClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function calculateFairhold(responseData: ResponseData) {
property: property,
forecastParameters: createForecastParameters(responseData.maintenancePercentage),
});
console.log(household);

return household;
}

Expand Down

0 comments on commit cd9a5c7

Please sign in to comment.