Advertisement
rhuntington

income.js

May 22nd, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const dotenv = require('dotenv');
  2. dotenv.config();
  3. const express = require('express');
  4. const incomeRoute = express.Router();
  5. const knex = require('../knex/knex');
  6. const income = knex('income');
  7. const passport = require('passport');
  8.  
  9. require('../middleware/passport')(passport);
  10.  
  11. incomeRoute.use(function timeLog (req, res, next) {
  12.     let timeStamp = new Date().toString();
  13.     console.log(`Request received at incomeRoute on: ${timeStamp}`);
  14.     next();
  15. });
  16.  
  17. // verify user is authenticated before accessing data
  18. let authenticate = (req, res, next) => {
  19.     if(req.isAuthenticated()) {
  20.         return next();
  21.     }
  22.     res.status(403).send('You are not logged in. You will be redirected to login.');
  23. }
  24.  
  25. incomeRoute.get('/', (req, res) => {
  26.     let userid = req.user[0]['userid'];
  27.     income.where({
  28.         userid: userid
  29.     }).orderBy('transactionid', 'desc').then((data, err) => {
  30.         return err? console.log(err) : res.send(data);
  31.     }).catch((err) => {
  32.         console.log(err);
  33.     })
  34. });
  35.  
  36. incomeRoute.post('/create', (req, res) => {
  37.     // let userid = req.user[0]['userid'];
  38.     let userid = req.body.userid;
  39.     let amount = req.body.amount;
  40.     let employer = req.body.employer;
  41.     let datereceived = req.body.datereceived;
  42.     let tax = amount * 0.15;
  43.    
  44.     income.where({
  45.         userid: userid
  46.     }).select('transactionid', 'taxytd')
  47.     .orderBy('transactionid', 'desc')
  48.     .then((data) => {
  49.         let taxytd = data[0]['taxytd']; // Set taxytd param to be used in existingData function
  50.         return data === undefined || data.length === 0 ? noExistingIncome() : existingData(taxytd);
  51.     }).catch((err) => {
  52.         console.log(err);
  53.     });
  54.    
  55.     let noExistingIncome = () => {
  56.         income.insert({
  57.             userid: userid,
  58.             amount: amount,
  59.             employer: employer,
  60.             datereceived: datereceived,
  61.             tax: tax,
  62.             taxytd: tax
  63.         }).then((data) => {
  64.             res.send('Data successfully entered');
  65.         }).catch((err) => {
  66.             console.log(err);
  67.             res.send('There was an issue while creating this data. Please try again');
  68.         });
  69.     };
  70.  
  71.     let existingData = (taxytd) => {
  72.         /*
  73.             While using income.insert({...}), it will fail on the second data submission w/ an error of NaN for taxytd.
  74.             It is unclear as to why this happens at this time however the below function works correctly when using it as knex.insert({...}).into('income')
  75.         */
  76.         let newTaxYtd = Number(taxytd) + Number(tax);
  77.         knex.insert({
  78.             userid: userid,
  79.             amount: amount,
  80.             employer: employer,
  81.             datereceived: datereceived,
  82.             tax: tax,
  83.             taxytd: newTaxYtd
  84.         }).into('income').then((data) => {
  85.             res.send('Data successfully entered');
  86.         }).catch((err) => {
  87.             console.log(err);
  88.             res.send('There was an issue while creating this data. Please try again');
  89.         });
  90.     }
  91. });
  92.  
  93. module.exports = incomeRoute;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement