Skip to content

Commit

Permalink
fix: replace return with throw error
Browse files Browse the repository at this point in the history
  • Loading branch information
LeleDallas committed May 13, 2023
1 parent 0529f12 commit 4c01fc0
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions db/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const hasCountedDay = function (allDay: any, date: any) {

export const addData = asyncHandler(async (req, res) => {
if (!req.params.id) {
res.status(400)
return
throw Error('Error')
}
const exist = await collections?.bills?.findOne({ buildingId: new ObjectId(req.params.id) })
if (!exist) {
Expand All @@ -34,14 +33,14 @@ export const addData = asyncHandler(async (req, res) => {
res.status(200).json(bills)
}
} else {
res.status(400)
throw Error('Error')
}
})

export const updateData = asyncHandler(async (req, res) => {
if (!req.params.id) {
res.status(400)
return

throw Error('Error')
}
const exist = await collections?.bills?.findOne({ buildingId: new ObjectId(req.params.id) })
if (exist) {
Expand All @@ -65,68 +64,61 @@ export const updateData = asyncHandler(async (req, res) => {
})

export const getBills = asyncHandler(async (req, res) => {
const bills = await collections?.bills?.find({}).toArray();
const bills = await collections.bills?.find({}).toArray();
if (!bills) throw Error('Error')
res.status(200).json(bills)
})

export const getBuildingBills = asyncHandler(async (req, res) => {
if (!req.params.id) {
res.status(400)
return
throw Error('Error')
}
const goal = await collections?.bills?.findOne({ buildingId: new ObjectId(req.params.id) })
if (!goal) {
res.status(404)
} else {
res.status(200).json(goal);
}
if (!goal)
throw Error('Error')
res.status(200).json(goal);
})

export const getBillsByOrganizationId = asyncHandler(async (req, res) => {
if (!req.params.id) {
res.status(400)
return

throw Error('Error')
}
const bills = await collections?.bills?.find({ organizationId: new ObjectId(req.params.id) }).toArray();
if (bills?.length === 0) {
res.status(401)

throw Error('Error')
} else {
res.status(200).json(bills);
}
})

export const getBillsRenewableOnly = asyncHandler(async (req, res) => {
if (!req.params.id) {
res.status(400);
return;
throw Error('Error')
}

const bills = await collections?.bills?.findOne({ buildingId: new ObjectId(req.params.id) });
if (!bills) {
res.status(401).json({ renewable: [], totalSolar: 0, totalWind: 0, totalGeo: 0, totalHydro: 0 });
return;
throw Error('Error');
}

let totalSolar = 0, totalWind = 0, totalGeo = 0, totalHydro = 0;
const renewable = Object.values(bills.bills)
.map((el: any) => {
if (el.resources.length === 0) {
return null;
}

el.resources.forEach((ele: any) => {
const value = parseFloat(Object.values(ele).toString());
if (Object.keys(ele).includes("Solar")) {
totalSolar += value;
} else if (Object.keys(ele).includes("Wind")) {
totalWind += value;
} else if (Object.keys(ele).includes("Geo")) {
totalGeo += value;
} else if (Object.keys(ele).includes("Hydro")) {
totalHydro += value;
}
});

if (!el.resources || el.resources.length === 0) { }
else
el.resources.forEach((ele: any) => {
const value = parseFloat(Object.values(ele).toString());
if (Object.keys(ele).includes("Solar")) {
totalSolar += value;
} else if (Object.keys(ele).includes("Wind")) {
totalWind += value;
} else if (Object.keys(ele).includes("Geo")) {
totalGeo += value;
} else if (Object.keys(ele).includes("Hydro")) {
totalHydro += value;
}
});
return { date: el.date, resources: el.resources };
})
.filter((el) => el !== null);
Expand All @@ -144,7 +136,7 @@ export const getBillsRenewableOnly = asyncHandler(async (req, res) => {
export const getBillsAggregatedFiltered = asyncHandler(async (req, result) => {
if (!req.params.id) {
result.status(400)
return
throw Error('Error')
}

const bills = await collections?.bills?.find({}).toArray()
Expand Down Expand Up @@ -236,20 +228,20 @@ export const getBillsAggregatedFiltered = asyncHandler(async (req, result) => {
export const getBillsByOrganizationIdAggregated = asyncHandler(async (req, res) => {
const id = req.params.id;
if (!id) {
res.status(400)
return;

throw Error('Error');
}

const bills = await collections?.bills?.find({ organizationId: new ObjectId(id) }).toArray();
if (!bills || bills.length === 0) {
res.status(400)
return;

throw Error('Error');
}

const organization = await fetchOrganization(id);
if (!organization) {
res.status(400)
return;

throw Error('Error');
}

const { electric, gas, water } = computeTotals(bills, organization);
Expand All @@ -269,9 +261,8 @@ async function fetchOrganization(id: string) {
try {
const response = await fetch(`http://localhost:3000/api/organization/${id}`);
return await response.json();
} catch (e) {
console.error(e);
return null;
} catch {
throw Error('Error')
}
}

Expand Down

0 comments on commit 4c01fc0

Please sign in to comment.