-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.js
153 lines (135 loc) · 5.42 KB
/
startup.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//Seed data for initial population of database
const initData = require('./initData');
//BTCPayServer client
const btcPayClient = require('./btcpay');
const startup = async models => {
async function originalInitialization() {
//Initialize the database on the first start up by inserting initData
await models.User.findOne({}, async (err, result) => {
if (!result) {
const newUser = await new models.User(initData.adminUser).save();
}
});
await models.BulletPoint.findOne({}, async (err, result) => {
if (!result) await models.BulletPoint.insertMany(initData.bulletPoint)
});
await models.Resource.findOne({}, async (err, result) => {
if (!result) await models.Resource.insertMany(initData.resource)
});
await models.Rhetoric.findOne({}, async (err, result) => {
if (!result) await models.Rhetoric.insertMany(initData.rhetoric)
});
}
function populateBulletPoints() {
models.BulletPoint.find({}, async (err, results) => {
for (const bulletPoint of results) {
models.Rhetoric.findOne({
slug: bulletPoint.slug,
metaSlug: bulletPoint.metaSlug
}, async (err, rhetoric) => {
let arrayToCompare = rhetoric.bulletPoints.map(function (v) {
return v.toString();
});
if (bulletPoint !== undefined) {
if (bulletPoint.slug === rhetoric.slug && bulletPoint.metaSlug === rhetoric.metaSlug) {
if (arrayToCompare.indexOf(bulletPoint._id.toString()) < 0) {
rhetoric.bulletPoints.push(bulletPoint._id);
rhetoric.save();
}
}
}
});
}
});
}
function populateResources() {
models.Resource.find({}, async (err, results) => {
for (const resource of results) {
models.Rhetoric.findOne({
slug: resource.slug,
metaSlug: resource.metaSlug
}, async (err, rhetoric) => {
const arrayToCompare = rhetoric.resources.map(function (v) {
return v.toString();
});
if (resource !== undefined) {
if (resource.slug === rhetoric.slug && resource.metaSlug === rhetoric.metaSlug) {
if (arrayToCompare.indexOf(resource._id.toString()) < 0) {
rhetoric.resources.push(resource._id);
rhetoric.save();
}
}
}
});
}
});
}
function populateResourceRhetoric() {
models.Resource.find({}, (err, results) => {
for (const resource of results) {
models.Rhetoric.findOne({
slug: "resources",
metaSlug: resource.metaSlug
}, (err, rhetoric) => {
const arrayToCompare = rhetoric.resources.map(function (v) {
return v.toString();
});
if (resource !== undefined) {
if (resource.metaSlug === rhetoric.metaSlug) {
if (arrayToCompare.indexOf(resource._id.toString()) < 0) {
rhetoric.resources.push(resource._id);
rhetoric.save();
}
}
}
});
}
})
}
await originalInitialization();
populateBulletPoints();
populateResources();
populateResourceRhetoric();
function updateBitcoinValue() {
btcPayClient.get_rates('BTC_USD', process.env.BTCPAY_STOREID)
.then(async function (rates) {
const cryptoDoc = await models.Crypto.findOne({
ticker: 'BTC'
});
if (cryptoDoc) {
cryptoDoc.valueUSD = Number(rates[0].rate);
cryptoDoc.save();
} else {
const id = require('mongodb').ObjectID();
const newCrypto = new models.Crypto({
_id: id,
ticker: 'BTC',
valueUSD: Number(rates[0].rate)
}).save();
}
})
.catch(err => console.log(err))
}
//Update the Bitcoin value on startup
updateBitcoinValue();
//Update the Bitcoin value every hour
setInterval(function () {
updateBitcoinValue();
}, 60 * 60000);
//Update user documents with new array which holds projects they've submitted
const users = await models.User.find({});
users.forEach(function(user) {
if(user.projects === undefined) {
console.log("adding projects array to", user.username);
user.projects = [];
user.save();
}
});
const opinions = await models.Opinion.find({});
opinions.forEach(function(opinion) {
opinion.set('censored', null);
opinion.active = true;
opinion.save();
});
}
module.exports = startup;