Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var router = express.Router();
- var User = require('../model/user.js');
- var bodyParser = require('body-parser');
- var app = express();
- var nodemailer = require('nodemailer');
- var transporter = nodemailer.createTransport({
- host: 'smtp.mail.ru',
- port: 587,
- secure: false, //
- auth: {
- user: "iorbox@mail.ru", // generated ethereal user
- pass: "usercreation2007" // generated ethereal password
- }
- });
- function makePassword() {
- var text = "";
- var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- for (var i = 0; i < 5; i++)
- text += possible.charAt(Math.floor(Math.random() * possible.length));
- return text;
- }
- app.use(bodyParser.json());
- app.post('/users/auth/', function (req, res, next) {
- User.findOne({ "mail": req.body.mail, "password": req.body.password }, function (err, user) {
- if (err) throw err;
- if (user == undefined) {
- res.send({ "result": "error" })
- res.sendStatus(200)
- } else {
- res.send({ "result": "ok", "_id": user._id, "type": user.type })
- }
- });
- })
- app.post('/users/create', function (req, res) {
- var password = makePassword()
- var status = new User({ name: req.body.name, mail: req.body.mail, type: req.body.type, phone: req.body.phone, password: password })
- status.save(function (err) {
- if (err) throw err;
- })
- var mailOptions = {
- from: '"IOR Support 👻" <iorbox@mail.ru>',
- to: req.body.mail, // list of receivers
- subject: 'Вы были зарегистрированы в системе IOR', // Subject line
- html: '<b>Добрый день</b><p>Ваш пароль</p><p>' + password + '</p>' // html body
- };
- transporter.sendMail(mailOptions, (error, info) => {
- if (error) {
- return console.log(error);
- }
- console.log('Message sent: %s', info.messageId);
- console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
- });
- res.send('CREATED!')
- })
- app.post('/users/', function (req, res) {
- User.find().exec(function (err, users) {
- if (err) throw err;
- res.send(users)
- });
- })
- app.post('/users/employee', function (req, res) {
- User.find({ type: "employee" }).exec(function (err, users) {
- if (err) throw err;
- res.send(users)
- });
- })
- app.post('/users/update', function (req, res) {
- 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) {
- if (err) throw err;
- res.send(user)
- });
- })
- app.post('/users/delete', function (req, res) {
- User.findOneAndRemove({ _id: req.body._id }, function (err) {
- if (err) throw err;
- res.send("DELETED!")
- });
- })
- app.post('/users/type', function (req, res) {
- User.findOne({ _id: req.body._id }, function (err, user) {
- if (err) throw err;
- res.send({ "type": user.type })
- });
- })
- app.post('/user/:id', function (req, res) {
- User.findOne({ _id: req.params.id }).exec(function (err, user) {
- if (err) throw err;
- res.send(user)
- });
- })
- app.post('/addfavorder/', function (req, res) {
- User.findOne({ _id: req.body._id }).exec(function (err, user) {
- user.favorites.push(req.body.order_id)
- User.findOneAndUpdate({ _id: req.body._id }, { $set: { favorites: user.favorites } }, function (err, user) {
- if (err) throw err;
- res.send(user)
- })
- });
- })
- app.post('/removefavorder/', function (req, res) {
- User.findOne({ _id: req.body._id }).exec(function (err, user) {
- var index;
- for (var i = 0; i < user.favorites.length; i++) {
- if (user.favorites[i] == req.body.order_id) {
- index = i
- }
- }
- user.favorites.splice(index, 1)
- User.findOneAndUpdate({ _id: req.body._id }, { $set: { favorites: user.favorites } }, function (err, user) {
- if (err) throw err;
- res.send(user)
- })
- });
- })
- module.exports = app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement