-
Notifications
You must be signed in to change notification settings - Fork 0
/
file61.js
83 lines (77 loc) · 2.6 KB
/
file61.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { Order, CartItem } = require('../models/order');
const { errorHandler } = require('../helpers/dbErrorHandler');
// sendgrid for email npm i @sendgrid/mail
//const sgMail = require('@sendgrid/mail');
//sgMail.setApiKey('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');
var api_key = '102c98b9df77378284a880cc6205d5d8-4d640632-2ea86ddc';
var domain = 'sandbox266e030958a34e17a7d863a58c9ad3dd.mailgun.org';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
exports.orderById = (req, res, next, id) => {
Order.findById(id)
.populate('products.product', 'name price')
.exec((err, order) => {
if (err || !order) {
return res.status(400).json({
error: errorHandler(err)
});
}
req.order = order;
next();
});
};
exports.create = (req, res) => {
console.log('CREATE ORDER: ', req.body);
req.body.order.user = req.profile;
const order = new Order(req.body.order);
order.save((error, data) => {
if (error) {
return res.status(400).json({
error: errorHandler(error)
});
}
// send email alert to admin
// order.address
// order.products.length
// order.amount
const emailData = {
from: 'Excited User <postmaster@sandbox266e030958a34e17a7d863a58c9ad3dd.mailgun.org>',
to: 'sajay7623@gmail.com',
subject: `A new order is received`,
html: `
<p>Customer name:</p>
<p>Total products: ${order.products.length}</p>
<p>Total cost: ${order.amount}</p>
<p>Login to dashboard to the order in detail.</p>
`
};
mailgun.messages().send(emailData);
res.json(data);
//console.log(data);
});
};
exports.listOrders = (req, res) => {
Order.find()
.populate('user', '_id name address')
.sort('-created')
.exec((err, orders) => {
if (err) {
return res.status(400).json({
error: errorHandler(error)
});
}
res.json(orders);
});
};
exports.getStatusValues = (req, res) => {
res.json(Order.schema.path('status').enumValues);
};
exports.updateOrderStatus = (req, res) => {
Order.update({ _id: req.body.orderId }, { $set: { status: req.body.status } }, (err, order) => {
if (err) {
return res.status(400).json({
error: errorHandler(err)
});
}
res.json(order);
});
};