Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.post('/api/order', function (req, res) {
  2.         authorize(req).then(function (foundUser) {
  3.             var newOrder = {};
  4.             newOrder.startDate = req.body.startDate;
  5.             newOrder.stopDate = req.body.stopDate;
  6.             var parkPriceRate = 1;
  7.             newOrder.price = parkPriceRate;
  8.             var parkTotalMin = (req.body.stopDate - req.body.startDate) / 1000 / 60;
  9.             var parkRegPeriod = parkTotalMin / 30;
  10.             parkRegPeriod = Math.round(parkRegPeriod);
  11.             newOrder.totalPrice = parkRegPeriod * parkPriceRate;
  12.             newOrder.userId = foundUser.id;
  13.  
  14.             return  db.orders.create(newOrder);
  15.         })
  16.         .then(function (order) {
  17.             return db.orders.findAll({where: {userId: foundUser.id}});
  18.         })
  19.         .then(function (orders) {
  20.             var ordersList = [];
  21.            
  22.             orders.forEach(function (item, i, arr) {
  23.                 var order = {};
  24.                 order.id = item['id'];
  25.                 order.startDate = item['startDate'];
  26.                 order.stopDate = item['stopDate'];
  27.                 order.price = item['price'];
  28.                 order.totalPrice = item['totalPrice'];
  29.                 ordersList.push(order);
  30.             })
  31.  
  32.             res.json({message: 'order added', body: ordersList});
  33.  
  34.             return true;
  35.         });
  36.         .catch(err => {
  37.             console.log('tratata oshibko: ', err.message;)
  38.             res.status(200).send('User not found');
  39.         })
  40.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement