Skip to content

Commit

Permalink
Merge pull request #15 from Kasulejoseph/amount-endpoint-170301522
Browse files Browse the repository at this point in the history
#170301522 Add analysis endpoint
  • Loading branch information
Kasulejoseph committed Dec 15, 2019
2 parents 0959bdc + 45a48e6 commit bdd255e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import User from "../model/user";
import updateHelper from "../helper/updateHelper";
import queryHelper from "../helper/queryHelper";
import handle500 from "../helper/handle500";
import analysisHelper from "../helper/analysisHelper";

class ExpenseController {
static async getExpenses(req, res) {
Expand Down Expand Up @@ -53,6 +54,16 @@ class ExpenseController {
res.status(500).send(response);
}
}
static async analystsData(req, res){
try {
const response = await analysisHelper()
res.send(response)
} catch (error) {
const response = await handle500();
res.status(response.statusCode).send(error);
}

}
}

export default ExpenseController;
18 changes: 18 additions & 0 deletions src/helper/analysisHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Expense from "../model/expense";

export default async () => {
let data = [];
const amount = [];
const expenseData = await Expense.find({});
expenseData.forEach(item => {
const dataObj = {
amount: item.amount,
date: item.created_at,
employee: item.employee
};
data.push(dataObj);
amount.push(item.amount);
});
const sum = amount.reduce((a, b) => a + b, 0);
return { sum, data };
};
3 changes: 2 additions & 1 deletion src/routers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express from "express";
import ExpenseController from "../controller";
const router = express.Router();
const { getExpenses, userExpenses, updateExpenses } = ExpenseController;
const { getExpenses, userExpenses, updateExpenses, analystsData } = ExpenseController;

router.get("/", getExpenses);
router.get("/users/:id", userExpenses);
router.patch("/:id", updateExpenses);
router.get("/analysis", analystsData);
router.all('*', (req, res) => {
res.status(405).send({
status: 405,
Expand Down
11 changes: 11 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ describe("/users:id GET user expenses", () => {
done();
});
});
it("should fetch analysis", done => {
chai
.request(server)
.get("/analysis")
.end((err, res) => {
res.should.have.status(200);
res.body.data.should.be.a("array");
res.text.includes('"status":200');
done();
});
});
});

// describe('/:id PUT update expenses', () => {
Expand Down

0 comments on commit bdd255e

Please sign in to comment.