Skip to content
Open
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
2 changes: 1 addition & 1 deletion backend/src/controller/Cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const getCellValuesbyYearandCtype = async (

while (hasMoreData) {
const { data, error } = await supabase
.rpc("categorize_cells_by_year", {
.rpc("categorize_cells_by_year_flat", {
selected_year: selected_year,
category_type: classificationType,
gte: gte,
Expand Down
32 changes: 13 additions & 19 deletions backend/src/controller/County.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ export const getCounties = async (
if (supabase) {
try {
const { data, error } = await supabase
.from("Counties")
.select("county_id,province_id,county_data");
// error handling in case the collection doesn't work
.from("County_Data")
.select("county_id,province_id,sid,soum_name,province_name");
if (error) {
res.status(500).json({
log: "Error while collecting the data",
Expand All @@ -37,9 +36,8 @@ export const getCountyGeometry = async (
if (supabase) {
try {
const { data, error } = await supabase
.from("Counties")
.from("County_Data")
.select("county_id,province_id,county_geometry");
// error handling in case the insertion doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
Expand All @@ -64,10 +62,9 @@ export const getCountyByID = async (
try {
const { county_id } = req.params;
const { data, error } = await supabase
.from("Counties")
.select("county_id,province_id,county_data")
.from("County_Data")
.select("county_id,province_id,sid,soum_name,province_name")
.eq("county_id", county_id);
// error handling in case the insertion doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
Expand All @@ -92,10 +89,9 @@ export const getCountyGeometryByID = async (
try {
const { county_id } = req.params;
const { data, error } = await supabase
.from("Counties")
.from("County_Data")
.select("county_id,province_id,county_geometry")
.eq("county_id", county_id);
// error handling in case the insertion doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
Expand All @@ -120,11 +116,10 @@ export const getCountyLivestockByID = async (
try {
const { county_id, year } = req.params;
const { data, error } = await supabase
.from("county_livestock")
.select("yearly_agg")
.from("counties_livestock")
.select("goat,camel,horse,sheep,cattle,total")
.eq("asid", county_id)
.eq("year", year);
// error handling in case the insertion doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
Expand All @@ -150,19 +145,18 @@ export const getCountyCellSummary = async (
const county_id = parseInt(req.params.county_id as string, 10);
const category_type = req.params.category_type as string;

// Validate category_type
if (!["carrying_capacity", "z_score"].includes(category_type)) {
res.status(400).json({
log: "Invalid category type. Use 'carrying_capacity' or 'z_score'.",
});
return;
}
// Call the stored procedure using Supabase RPC
const { data, error } = await supabase.rpc("categorize_cells_by_county", {

const { data, error } = await supabase.rpc("categorize_cells_by_new_county", {
c_id: county_id,
category_type: category_type,
});
// Error handling in case the query fails

if (error) {
res.status(500).json({
log: "Error while collecting the data",
Expand All @@ -181,6 +175,7 @@ export const getCountyCellSummary = async (
}
}
};

export const getCountiesGeomInProvince = async (
req: Request,
res: Response
Expand All @@ -189,12 +184,11 @@ export const getCountiesGeomInProvince = async (
try {
const province_id = parseInt(req.params.province_id as string, 10);
const { data, error } = await supabase.rpc(
"retrieve_counties_geom_in_province",
"retrieve_new_counties_geom_in_province",
{
p_id: province_id,
}
);
// Error handling in case the query fails
if (error) {
res.status(500).json({
log: "Error while collecting the data",
Expand Down
69 changes: 20 additions & 49 deletions backend/src/controller/Province.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ export const getProvinces = async (
if (supabase) {
try {
const { data, error } = await supabase
.from("Provinces")
.select("province_id,province_data");
// error handling in case the collection doesn't work
.from("Province_Data")
.select("province_id,province_name,province_herders,province_land_area");
if (error) {
res.status(500).json({
log: "Error while collecting the data",
error: error.message,
});
return;
}
res
.status(201)
.json({ log: "Data was successfully Collected", data: data });
res.status(201).json({ log: "Data was successfully Collected", data: data });
} catch (error: any) {
res.status(400).json({ message: error.message });
}
Expand All @@ -35,19 +32,16 @@ export const getProvinceGeometry = async (
if (supabase) {
try {
const { data, error } = await supabase
.from("Provinces")
.select("province_id, province_data->province_name, province_geometry");
// error handling in case the insertion doesn't work
.from("Province_Data")
.select("province_id,province_name,province_geometry");
if (error) {
res.status(500).json({
log: "Error while collecting the data",
error: error.message,
});
return;
}
res
.status(201)
.json({ log: "Data was successfully Collected", data: data });
res.status(201).json({ log: "Data was successfully Collected", data: data });
} catch (error: any) {
res.status(400).json({ message: error.message });
}
Expand All @@ -62,20 +56,17 @@ export const getProvinceByID = async (
try {
const { province_id } = req.params;
const { data, error } = await supabase
.from("Provinces")
.select("province_id,province_data")
.from("Province_Data")
.select("province_id,province_name,province_herders,province_land_area")
.eq("province_id", province_id);
// error handling in case the insertion doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
error: error.message,
});
return;
}
res
.status(201)
.json({ log: "Data was successfully Collected", data: data });
res.status(201).json({ log: "Data was successfully Collected", data: data });
} catch (error: any) {
res.status(400).json({ message: error.message });
}
Expand All @@ -90,20 +81,17 @@ export const getProvinceGeometryByID = async (
try {
const { province_id } = req.params;
const { data, error } = await supabase
.from("Provinces")
.from("Province_Data")
.select("province_id,province_geometry")
.eq("province_id", province_id);
// error handling in case the insertion doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
error: error.message,
});
return;
}
res
.status(201)
.json({ log: "Data was successfully Collected", data: data });
res.status(201).json({ log: "Data was successfully Collected", data: data });
} catch (error: any) {
res.status(400).json({ message: error.message });
}
Expand All @@ -118,21 +106,18 @@ export const getProvinceLivestockByID = async (
try {
const { province_id, year } = req.params;
const { data, error } = await supabase
.from("province_livestock")
.select("yearly_agg")
.from("provinces_livestock")
.select("goat,camel,horse,sheep,cattle,total")
.eq("asid_prefix", province_id)
.eq("year", year);
// error handling in case the insertion doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
error: error.message,
});
return;
}
res
.status(201)
.json({ log: "Data was successfully Collected", data: data });
res.status(201).json({ log: "Data was successfully Collected", data: data });
} catch (error: any) {
res.status(400).json({ message: error.message });
}
Expand All @@ -146,20 +131,17 @@ export const getProvinceLivestockByClass = async (
if (supabase) {
try {
const { type } = req.params;
const { data, error } = await supabase.rpc("fetch_livestock_by_class", {
const { data, error } = await supabase.rpc("fetch_new_livestock_by_class", {
livestock_type: type,
});
// error handling in case the collection doesn't work
if (error) {
res.status(500).json({
log: "Error while collecting the data",
error: error.message,
});
return;
}
res
.status(201)
.json({ log: "Data was successfully Collected", data: data });
res.status(201).json({ log: "Data was successfully Collected", data: data });
} catch (error: any) {
res.status(400).json({ message: error.message });
}
Expand All @@ -175,25 +157,22 @@ export const getProvinceCellSummary = async (
const province_id = parseInt(req.params.province_id as string, 10);
const category_type = req.params.category_type as string;

// Validate province_id
if (province_id < 11 || province_id > 85) {
res.status(400).json({
log: "Invalid province_id. It must be a valid number.",
});
return;
}

// Validate category_type
if (!["carrying_capacity", "z_score"].includes(category_type)) {
res.status(400).json({
log: "Invalid category type. Use 'carrying_capacity' or 'z_score'.",
});
return;
}

// Fetch data
const { data, error } = await supabase.rpc(
"categorize_cells_by_province",
"categorize_cells_by_new_province",
{
p_id: province_id,
category_type: category_type,
Expand All @@ -208,13 +187,9 @@ export const getProvinceCellSummary = async (
return;
}

res
.status(200)
.json({ log: "Data was successfully collected", data: data });
res.status(200).json({ log: "Data was successfully collected", data: data });
} catch (error: any) {
res
.status(500)
.json({ log: "Internal server error", error: error.message });
res.status(500).json({ log: "Internal server error", error: error.message });
}
}
};
Expand All @@ -237,7 +212,6 @@ export const getProvinceGR = async (
const parsedProvinceId = parseInt(province_id, 10);
console.log(`Parsed province_id: ${parsedProvinceId}`);

// Validate province_id
if (
isNaN(parsedProvinceId) ||
parsedProvinceId < 11 ||
Expand All @@ -250,18 +224,15 @@ export const getProvinceGR = async (
return;
}

// Log before calling the RPC
console.log(`Calling Supabase RPC with p_id: ${parsedProvinceId}`);

// Call Supabase function
const { data, error } = await supabase.rpc(
"find_grazing_range_percentage",
"find_new_grazing_range_percentage",
{
p_id: parsedProvinceId,
}
);

// Log response from Supabase
console.log("Supabase RPC Response:", { data, error });

if (error) {
Expand Down
Loading