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
7 changes: 4 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ services:
postgres:
image: postgres:14-alpine
ports:
- "127.0.0.1:8000:8000"
- "5432:5432"
environment:
- POSTGRES_PASSWORD=pass
- POSTGRES_USER=user
- POSTGRES_DB=brain
networks:
- my-network
# extra_hosts:
# - "host.docker.internal:host-gateway"
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- db:/var/lib/postgres

app:
build:
context: .
Expand Down
89 changes: 89 additions & 0 deletions src/controllers/DashboardController2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Request, Response } from "express"

// eslint-disable-next-line @typescript-eslint/no-var-requires
const Farmer = require("../models/Farmer")
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Address = require("../models/Address")
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Crop = require("../models/Crop")
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Farm = require("../models/Farm")

/**
* Generate dashboard results based on farmer's farms.
*
* @param {Request} request - the request object
* @param {Response} response - the response object
* @return {Promise<object>} the dashboard results object
*/
const DashboardController = {
async list(request: Request, response: Response): Promise<object> {
const { farmer_id } = request.params
const farmer = await Farmer.findByPk(farmer_id, {
include: [
{
model: Farm,
as: `farms`,
include: [{
model: Address,
as: "address"
},
{
model: Crop,
as: "crops",
through: 'farms_crops'
}]
}
]
});

if(!farmer) response.status(404).send()
if(!farmer.farms) response.send({
total: 0,
hectares: 0,
byState: {},
byCrop: {},
byVegetationAndArabelArea: {},
})

const hectares = farmer.farms.reduce((total: number, farm: { totalArea: number}) => total + farm.totalArea, 0);
const byState = farmer.farms.reduce((totalByState: {[state: string]: number }, farm: {address: {state: string}, totalArea: number}) => {
if (!totalByState[farm.address.state]) {
totalByState[farm.address.state] = 0;
}
totalByState[farm.address.state] += farm.totalArea;
return totalByState;
}, {});
const byCrop = farmer.farms.reduce((totalByCrop: {[state: string]: number }, farm: {crops: {name: string}[], totalArea: number}) => {
for (const crop in farm.crops) {
const name = farm.crops[crop].name
if (!totalByCrop[name]) {
totalByCrop[name] = 0;
}
totalByCrop[name] += farm.totalArea;
}
return totalByCrop;

}, {});

const byVegetationAndArabelArea = farmer.farms.reduce((totalByVegetationAndArabelArea: {[name: string]: {vegetationArea: number, arabelArea:number }}, farm: {arableArea: number, vegetationArea: number, name: string}) => {
totalByVegetationAndArabelArea[farm.name] = {
vegetationArea: farm.vegetationArea,
arabelArea: farm.arableArea,
}
return totalByVegetationAndArabelArea;
}, {})

const dashboardResults = {
total: farmer.farms.length,
hectares,
byState,
byCrop,
byVegetationAndArabelArea,
}

return response.send(dashboardResults)
},
}

export { DashboardController }
2 changes: 1 addition & 1 deletion src/database/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const dbConfig = {
dialect: "postgres",
host: "postgres",
host: "localhost",
port: 5432,
username: "user",
password: "pass",
Expand Down