This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sale-scheduler.js
88 lines (67 loc) · 2.68 KB
/
sale-scheduler.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
84
85
86
87
88
const _ = require("lodash");
const keystone = require("keystone");
const moment = require("moment");
const schedule = require("node-schedule");
const jobs = {};
let io;
let Product;
async function markAsSold(oldProduct) {
if (!io) io = keystone.get("io");
const product = await Product.model.findOne({
_id: oldProduct._id
});
if (product) {
product.sold = true;
product.save();
io.sockets.emit("sold", _.omit(product, ["createdBy", "updatedBy"]));
if (product.currentBid) {
await product.populate("currentBid").execPopulate();
if (product.currentBid.amount >= (product.reserve || 0)) {
keystone.get("log")(`:white_check_mark: Sold for **${product.currentBid.amount.toLocaleString()} KST** to **${product.currentBid.username.substring(0, 16)}**!`, "green", product, product.currentBid.address);
} else {
keystone.get("log")(`:x: Auction ended but did not meet the reserve price.`, "red", product);
}
} else {
keystone.get("log")(`:x: Auction ended with no bids.`, "red", product);
}
console.log(`Marked product ${product.name} as sold`);
} else {
console.log(`Product ${oldProduct._id} (${oldProduct.name}) no longer exists`);
}
}
async function scheduleProduct(product) {
if (!product.endsAt || !product.visible) return;
console.log(`Scheduling product ${product.name} for ${product.endsAt} (pre)`);
if (moment().isAfter(moment(product.endsAt))) {
return await markAsSold(product);
}
console.log(`Scheduling product ${product.name} for ${product.endsAt}`);
jobs[product._id] = {
job: schedule.scheduleJob(product.endsAt, () => markAsSold(product)),
product: product
};
}
module.exports.updateProduct = async function(product) {
if (jobs[product._id] && jobs[product._id].product.endsAt !== product.endsAt) {
console.log(`Product ${product.name} updated`);
jobs[product._id].job.cancel();
} else {
console.log(`Product ${product.name} added`);
}
await scheduleProduct(product);
};
module.exports.announceProduct = async function(product) {
await product.populate("createdBy");
const fullName = product.createdBy && product.createdBy.name ? `${product.createdBy.name.first} ${product.createdBy.name.last}` : "Unknown Seller";
const seller = product.seller || fullName;
keystone.get("log")(`:new: For sale ${product.compulsory ? "on behalf of" : "by"}: **${seller}**!`, "green", product);
};
module.exports.start = async function() {
Product = keystone.list("Product");
const products = await Product.model.find({
saleType: "auction",
sold: false,
visible: true
});
if (products) products.forEach(scheduleProduct);
};