diff --git a/api/dashboard/index.js b/api/dashboard/index.js index de53ffd..750bbd5 100644 --- a/api/dashboard/index.js +++ b/api/dashboard/index.js @@ -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) => { @@ -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 + }) }) + }) }) diff --git a/api/forecast/edit-expenses.js b/api/forecast/edit-expenses.js index 7a9aa6d..df2ce3d 100644 --- a/api/forecast/edit-expenses.js +++ b/api/forecast/edit-expenses.js @@ -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) }, diff --git a/api/forecast/edit-revenue.js b/api/forecast/edit-revenue.js index 09d5da0..3e9f1c0 100644 --- a/api/forecast/edit-revenue.js +++ b/api/forecast/edit-revenue.js @@ -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) }, diff --git a/api/s3.js b/api/s3.js new file mode 100644 index 0000000..a929c7d --- /dev/null +++ b/api/s3.js @@ -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(); + }); +} diff --git a/api/staff/get.js b/api/staff/get.js new file mode 100644 index 0000000..6024a9a --- /dev/null +++ b/api/staff/get.js @@ -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') }) + }) + }) +} diff --git a/api/staff/index.js b/api/staff/index.js new file mode 100644 index 0000000..749f53a --- /dev/null +++ b/api/staff/index.js @@ -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 }) + } + }) +} diff --git a/components/head.js b/components/head.js index e437c37..5e3abd9 100644 --- a/components/head.js +++ b/components/head.js @@ -11,7 +11,7 @@ const Head = (props) => ( {props.title || ''} - + diff --git a/components/nav.js b/components/nav.js index 75c9052..0e3bd71 100644 --- a/components/nav.js +++ b/components/nav.js @@ -9,15 +9,18 @@ const Nav = () => ( FrontInBH Financial diff --git a/components/staff-split.js b/components/staff-split.js new file mode 100644 index 0000000..2f0691b --- /dev/null +++ b/components/staff-split.js @@ -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 ( +
+ +

+ Real
+ {(totalSplitReal / this.state.countStaff) + .toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })} +

+

+ Previsto
+ {(totalSplit / this.state.countStaff) + .toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })} +

+
+ ) + } +} diff --git a/package.json b/package.json index d40f277..96efcb8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pages/dashboard.js b/pages/dashboard.js index 3aebabf..0b2d94f 100644 --- a/pages/dashboard.js +++ b/pages/dashboard.js @@ -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: [] } @@ -19,14 +20,18 @@ export default class Dashboard extends React.Component { render() { return (
- +