Skip to content

Commit

Permalink
Merge pull request #1 from frontinbh/add-staff
Browse files Browse the repository at this point in the history
Add staff
  • Loading branch information
keppelen authored Sep 26, 2017
2 parents 56100e8 + 812f7df commit d975e3c
Show file tree
Hide file tree
Showing 17 changed files with 407 additions and 37 deletions.
56 changes: 40 additions & 16 deletions api/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ const findExpenses = (db, query, callback) => {
})
}

const findCashbacks = (db, query, callback) => {
const collection = db.collection('staff')
collection.find({}).toArray((err, result) => {
callback(err, result)
})
}

module.exports = (req, res) => {

MongoClient.connect(url, (err, db) => {
Expand All @@ -57,23 +64,40 @@ module.exports = (req, res) => {
}
})

db.close()
return res.send({
results: [
{
name: 'Receitas',
tp: revenues.map(x => x.tp).reduce((a,b) => a + b, 0),
tr: revenues.map(x => x.tr).reduce((a,b) => a + b, 0)
},
{
name: 'Despesas',
tp: expenses.map(x => x.tp).reduce((a,b) => a + b, 0),
tr: expenses.map(x => x.tr).reduce((a,b) => a + b, 0)
},
],
revenueForecasts: revenues,
expensesForecast: expenses
findCashbacks(db, {}, (err, cashbacks) => {
db.close()
const cashbacksCount = cashbacks.map(cash => {
return {
name: cash.staff,
tr: cash.pricing,
tp: cash.pricing,
}
})

return res.send({
results: [
{
name: 'Receitas',
tp: revenues.map(x => x.tp).reduce((a,b) => a + b, 0),
tr: revenues.map(x => x.tr).reduce((a,b) => a + b, 0)
},
{
name: 'Despesas',
tp: expenses.map(x => x.tp).reduce((a,b) => a + b, 0),
tr: expenses.map(x => x.tr).reduce((a,b) => a + b, 0)
},
{
name: 'Reembolsos',
tp: cashbacks.map(x => x.pricing).reduce((a,b) => a + b, 0),
tr: cashbacks.map(x => x.pricing).reduce((a,b) => a + b, 0)
},
],
revenueForecasts: revenues,
expensesForecast: expenses,
cashbacks: cashbacksCount
})
})

})

})
Expand Down
4 changes: 0 additions & 4 deletions api/forecast/edit-expenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ const assert = require('assert')
const url = process.env.MONGO_DB;

const update = (db, body, id, callback) => {
console.log(
body,
id
)
const collection = db.collection('expenses')
collection.updateOne(
{ _id: ObjectId(id) },
Expand Down
4 changes: 0 additions & 4 deletions api/forecast/edit-revenue.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ const assert = require('assert')
const url = process.env.MONGO_DB;

const update = (db, body, id, callback) => {
console.log(
body,
id
)
const collection = db.collection('revenues')
collection.updateOne(
{ _id: ObjectId(id) },
Expand Down
28 changes: 28 additions & 0 deletions api/s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const aws = require('aws-sdk')
aws.config.region = 'us-east-1'

module.exports = (req, res) => {
const s3 = new aws.S3();
const fileName = `${new Date().getTime()}-${req.query['file-name']}`;
const fileType = req.query['file-type'];
const s3Params = {
Bucket: process.env.S3_BUCKET,
Key: fileName,
Expires: 60,
ContentType: fileType,
ACL: 'public-read'
};

s3.getSignedUrl('putObject', s3Params, (err, data) => {
if(err){
console.log(err);
return res.end();
}
const returnData = {
signedRequest: data,
url: `https://${process.env.S3_BUCKET}.s3.amazonaws.com/${fileName}`
};
res.write(JSON.stringify(returnData));
res.end();
});
}
24 changes: 24 additions & 0 deletions api/staff/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const MongoClient = require('mongodb').MongoClient
const ObjectId = require('mongodb').ObjectId
const assert = require('assert')
const _ = require('lodash')
const url = process.env.MONGO_DB;

const findAll = (db, callback) => {
const collection = db.collection('staff')
collection.find({}).toArray((err, result) => {
db.close()
callback(err, result)
})
}

module.exports = (req, res) => {
MongoClient.connect(url, function(err, db) {
findAll(db, (err, result) => {
if (err) {
return res.send({ status: false, message: result })
}
return res.send({ status: true, result: _.groupBy(result, 'staff') })
})
})
}
27 changes: 27 additions & 0 deletions api/staff/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const MongoClient = require('mongodb').MongoClient
const ObjectId = require('mongodb').ObjectId
const assert = require('assert')
const url = process.env.MONGO_DB;

const insert = (db, body, callback) => {
const collection = db.collection('staff')
collection.insertOne({ staff: body.staff, pricing: parseFloat(body.pricing), url: body.url }, (err, result) => {
db.close()
callback(err, result)
})
}

module.exports = (req, res) => {
MongoClient.connect(url, function(err, db) {
if (req.body.staff) {
insert(db, req.body, (err, result) => {
if (err) {
return res.send({ status: false, message: result })
}
return res.send({ status: true, message: result })
})
} else {
return res.send({ status: false, message: result })
}
})
}
2 changes: 1 addition & 1 deletion components/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Head = (props) => (
<title>{props.title || ''}</title>
<meta name="description" content={props.description || defaultDescription} />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" sizes="192x192" href="/static/touch-icon.png" />
<link rel="icon" sizes="192x192" href="//frontinbh.com.br/images/favicon.png" />
<link rel="apple-touch-icon" href="/static/touch-icon.png" />
<link rel="mask-icon" href="/static/favicon-mask.svg" color="#49B882" />
<link rel="icon" href="/static/favicon.ico" />
Expand Down
9 changes: 6 additions & 3 deletions components/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ const Nav = () => (
<a className="navbar-brand" href="#">FrontInBH Financial</a>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav">
<Link className="nav-item" href="/dashboard">
<Link prefetch className="nav-item" href="/dashboard">
<a className="nav-link">Dashboard</a>
</Link>
<Link className="nav-item" href="/revenue">
<Link prefetch className="nav-item" href="/revenue">
<a className="nav-link">Receita</a>
</Link>
<Link className="nav-item" href="/expense">
<Link prefetch className="nav-item" href="/expense">
<a className="nav-link">Despesas</a>
</Link>
<Link prefetch className="nav-item" href="/staff">
<a className="nav-link">Organizadores</a>
</Link>
</ul>
</div>
</nav>
Expand Down
61 changes: 61 additions & 0 deletions components/staff-split.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react'

export default class extends React.Component {
state = {
results: [
{
name: 'Receitas',
tr: 0,
tp: 0
},
{
name: 'Despesas',
tr: 0,
tp: 0
},
{
name: 'Reembolsos',
tr: 0,
tp: 0
}
],
countStaff: 4
}

componentDidMount() {
fetch('/api/dashboard').then(response => {
return response.json()
}).then(data => {
const { results } = data
this.setState({ results })
})
}

handleChange = (e) => {
const countStaff = e.target.value
this.setState({ countStaff })
}

render() {
const revenues = this.state.results.filter(r => r.name === 'Receitas')
const expenses = this.state.results.filter(r => r.name === 'Despesas')
const cashback = this.state.results.filter(r => r.name === 'Reembolsos')
const totalSplitReal = (revenues[0].tr - cashback[0].tr) - expenses[0].tr
const totalSplit = (revenues[0].tp - cashback[0].tp) - expenses[0].tp
return (
<div>
<input type="number" value={this.state.countStaff} onChange={this.handleChange} />
<p>
Real <br />
{(totalSplitReal / this.state.countStaff)
.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })}
</p>
<p>
Previsto <br />
{(totalSplit / this.state.countStaff)
.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })}
</p>
</div>
)
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"start": "node server.js"
},
"dependencies": {
"aws-sdk": "^2.94.0",
"body-parser": "^1.17.2",
"express": "^4.15.3",
"lodash": "^4.17.4",
"mongodb": "^2.2.30",
"next": "^3.0.1-beta.8",
"react": "^15.6.1",
Expand Down
9 changes: 7 additions & 2 deletions pages/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from 'next/link'
import Head from '../components/head'
import Nav from '../components/nav'
import Chart from '../components/dashboard/chart'
import StaffSplit from '../components/staff-split'

export default class Dashboard extends React.Component {
state = { results: [], revenueForecasts: [], expensesForecast: [] }
Expand All @@ -19,14 +20,18 @@ export default class Dashboard extends React.Component {
render() {
return (
<div>
<Head title="Dasboard" />
<Head title="Financial Dasboard" />
<Nav />

<div className="container">
<div className="row" style={{ marginTop: 30 }}>
<div className="col-12" style={{ marginBottom: 40 }}>
<div className="col-9" style={{ marginBottom: 40 }}>
<Chart name="Resultados" data={this.state.results} />
</div>
<div className="col-3" style={{ marginBottom: 40 }}>
<h2>Lucro staff</h2>
<StaffSplit />
</div>
<div className="col-6">
<Chart name="Receitas" data={this.state.revenueForecasts} />
</div>
Expand Down
2 changes: 1 addition & 1 deletion pages/expense.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Expenses extends React.Component {
render() {
return (
<div>
<Head title="Dasboard" />
<Head title="Financial Expenses" />
<Nav />
<Form iten='expenses' title='Despesas' />
</div>
Expand Down
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Nav from '../components/nav'

export default () => (
<div>
<Head title="Home" />
<Head title="Financial Front in BH" />

<div className="hero">
<h1 className="title">Front in BH Panel</h1>
Expand Down
2 changes: 1 addition & 1 deletion pages/revenue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Revenues extends React.Component {
render() {
return (
<div>
<Head title="Dasboard" />
<Head title="Financial Revenues" />
<Nav />
<Form iten='revenue' title='Receitas' />
</div>
Expand Down
Loading

0 comments on commit d975e3c

Please sign in to comment.