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) => (
+ Real
+ {(totalSplitReal / this.state.countStaff)
+ .toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })}
+
+ Previsto
+ {(totalSplit / this.state.countStaff)
+ .toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })}
+