-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprune.js
More file actions
65 lines (57 loc) · 1.74 KB
/
prune.js
File metadata and controls
65 lines (57 loc) · 1.74 KB
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
const mongoose = require('mongoose')
const async = require('async')
const die = require('./lib/die')
const config = require('./config.json')
const Order = require('./models/order')
const Shopify = require('./lib/shopify')
const MONGODB_URI = config.mongodbURI
config.encryptionKey = config.encryptionKey || ""
if (!MONGODB_URI) {
die('MONGODB_URI not set in config')
}
if (!config.encryptionKey || config.encryptionKey.length != 32) {
die('encryptionKey in config must be 32 characters long. Current value: "' + config.encryptionKey + '" len:' + config.encryptionKey.length)
}
mongoose.Promise = global.Promise;
mongoose.connect(MONGODB_URI);
mongoose.connection.on('error', (err) => {
console.error(err);
die('MongoDB connection error. Please make sure MongoDB is running at ' + MONGODB_URI)
});
const seconds = config.cancelOrderAfter || (60 * 5)
const cutoff = new Date(new Date().getTime() - (1000 * seconds))
let agg = [{
$match: {
status: 'pending',
createdAt: {
$lt: cutoff
}
}
}, {
$lookup: {
from: 'shops',
localField: 'shopId',
foreignField: '_id',
as: 'shop'
}
}]
Order.aggregate(agg, (err, orders) => {
async.each(orders, (order, next) => {
console.log('order',order);
let shop = order.shop[0]
if (!shop) {
console.log('ERROR: No shop found for this order', order);
return next()
}
let shopify = new Shopify(shop.endpoint, shop.username, shop.password)
shopify.cancelOrder(order.orderId, (err) => {
if (err) {
return next(err)
}
console.log('Pruned order ' + order.orderId + ' for ' + shop.endpoint)
Order.update({orderId: order.orderId}, {$set: {status: 'cancelled'}}, next)
})
}, () => {
process.exit()
})
})