Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. var express = require('express');
  2. var router = express.Router();
  3.  
  4. var User = require('../model/user.js');
  5. var bodyParser = require('body-parser');
  6.  
  7. var app = express();
  8.  
  9. var nodemailer = require('nodemailer');
  10.  
  11. var transporter = nodemailer.createTransport({
  12. host: 'smtp.mail.ru',
  13. port: 587,
  14. secure: false, //
  15. auth: {
  16. user: "iorbox@mail.ru", // generated ethereal user
  17. pass: "usercreation2007" // generated ethereal password
  18. }
  19. });
  20.  
  21. function makePassword() {
  22. var text = "";
  23. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  24.  
  25. for (var i = 0; i < 5; i++)
  26. text += possible.charAt(Math.floor(Math.random() * possible.length));
  27.  
  28. return text;
  29. }
  30.  
  31. app.use(bodyParser.json());
  32.  
  33. app.post('/users/auth/', function (req, res, next) {
  34. User.findOne({ "mail": req.body.mail, "password": req.body.password }, function (err, user) {
  35. if (err) throw err;
  36. if (user == undefined) {
  37. res.send({ "result": "error" })
  38. res.sendStatus(200)
  39. } else {
  40. res.send({ "result": "ok", "_id": user._id, "type": user.type })
  41. }
  42. });
  43. })
  44.  
  45. app.post('/users/create', function (req, res) {
  46. var password = makePassword()
  47. var status = new User({ name: req.body.name, mail: req.body.mail, type: req.body.type, phone: req.body.phone, password: password })
  48. status.save(function (err) {
  49. if (err) throw err;
  50. })
  51.  
  52. var mailOptions = {
  53. from: '"IOR Support 👻" <iorbox@mail.ru>',
  54. to: req.body.mail, // list of receivers
  55. subject: 'Вы были зарегистрированы в системе IOR', // Subject line
  56. html: '<b>Добрый день</b><p>Ваш пароль</p><p>' + password + '</p>' // html body
  57. };
  58.  
  59. transporter.sendMail(mailOptions, (error, info) => {
  60. if (error) {
  61. return console.log(error);
  62. }
  63. console.log('Message sent: %s', info.messageId);
  64. console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
  65. });
  66.  
  67. res.send('CREATED!')
  68. })
  69.  
  70. app.post('/users/', function (req, res) {
  71. User.find().exec(function (err, users) {
  72. if (err) throw err;
  73. res.send(users)
  74. });
  75. })
  76.  
  77. app.post('/users/employee', function (req, res) {
  78. User.find({ type: "employee" }).exec(function (err, users) {
  79. if (err) throw err;
  80. res.send(users)
  81. });
  82. })
  83.  
  84. app.post('/users/update', function (req, res) {
  85. User.findOneAndUpdate({ _id: req.body._id }, { $set: { name: req.body.name, mail: req.body.mail, phone: req.body.phone, new_orders_notification: req.body.new_orders_notification, new_status_notification: req.body.new_status_notification } }, function (err, user) {
  86. if (err) throw err;
  87.  
  88. res.send(user)
  89. });
  90. })
  91.  
  92. app.post('/users/delete', function (req, res) {
  93. User.findOneAndRemove({ _id: req.body._id }, function (err) {
  94. if (err) throw err;
  95.  
  96. res.send("DELETED!")
  97. });
  98. })
  99.  
  100. app.post('/users/type', function (req, res) {
  101. User.findOne({ _id: req.body._id }, function (err, user) {
  102. if (err) throw err;
  103.  
  104. res.send({ "type": user.type })
  105. });
  106. })
  107.  
  108. app.post('/user/:id', function (req, res) {
  109. User.findOne({ _id: req.params.id }).exec(function (err, user) {
  110. if (err) throw err;
  111. res.send(user)
  112. });
  113. })
  114.  
  115. app.post('/addfavorder/', function (req, res) {
  116. User.findOne({ _id: req.body._id }).exec(function (err, user) {
  117. user.favorites.push(req.body.order_id)
  118. User.findOneAndUpdate({ _id: req.body._id }, { $set: { favorites: user.favorites } }, function (err, user) {
  119. if (err) throw err;
  120. res.send(user)
  121. })
  122. });
  123. })
  124.  
  125. app.post('/removefavorder/', function (req, res) {
  126. User.findOne({ _id: req.body._id }).exec(function (err, user) {
  127.  
  128. var index;
  129.  
  130. for (var i = 0; i < user.favorites.length; i++) {
  131. if (user.favorites[i] == req.body.order_id) {
  132. index = i
  133. }
  134. }
  135.  
  136. user.favorites.splice(index, 1)
  137.  
  138. User.findOneAndUpdate({ _id: req.body._id }, { $set: { favorites: user.favorites } }, function (err, user) {
  139. if (err) throw err;
  140. res.send(user)
  141. })
  142. });
  143. })
  144.  
  145. module.exports = app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement