-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
258 lines (204 loc) · 6.66 KB
/
server.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const express = require('express');
const bodyParser = require('body-parser');
const yaml = require('js-yaml');
const Fetch = require('./fetch.js');
const DevFetch = require('./devFetch.js');
const {getInvoice, getInvoices, getContactData} = require('./moneybird');
const {parse} = require('url');
const {ENV} = process.env;
const {keyBy} = require('lodash');
const {getPaymentData, createPaymentData, getPaymentForInvoice} = require('./payment');
const server = express();
server.use(bodyParser.urlencoded({extended: true}));
server.use(bodyParser.json());
var fetch;
switch (ENV) {
case 'prod':
case 'test':
fetch = new Fetch();
break;
case 'dev':
default:
fetch = new DevFetch();
break;
}
const loadSubscriptionFile = async (req) =>
yaml.load(await fetch.fetchFromFile(req, 'subscription.yml'))
server.get('/api/config', function (req, res) {
let result = {};
function onFulfilledSubscription(data) {
let obj = yaml.load(data);
result.users = obj.users;
}
function onFulfilledHosts(data) {
let obj = yaml.load(data);
let workers = [];
for (worker in obj.all.children.workers.hosts) {
workers.push({name: worker});
}
result.workers = workers;
result.workers_memory = workers.length * 16
let build_slaves = [];
for (build_slave in obj.all.children.build_slaves.hosts) {
build_slaves.push({name: build_slave});
}
result.build_slaves = build_slaves;
let lvm_volumes = [];
result.diskspace = 0;
obj.all.children.nfs.hosts.nfs.lvm_volumes.forEach((value, index) => {
lvm_volumes.push({name: value.lv_name});
let memory = 100;
if (index === 0) {
memory = 200;
}
result.diskspace = result.diskspace + memory
});
result.lvm_volumes = lvm_volumes;
res.status(200).send(result);
}
fetch.fetchFromFile(req, 'subscription.yml')
.then(data => onFulfilledSubscription(data)).then(() => {
fetch.fetchFromFile(req, 'hosts')
.then(data => onFulfilledHosts(data))
.catch(error => {
onRejected(error)
});
})
.catch(error => {
onRejected(error)
});
function onRejected(err) {
res.status(500).send({
message: 'Settings fetch error - ' + err
});
}
});
server.get('/api/users', function (req, res) {
function onFulfilled(data) {
let obj = yaml.load(data);
res.status(200).send(obj);
}
function onRejected(err) {
res.status(500).send({
message: 'Settings fetch error - ' + err
});
}
fetch.fetchFromFile(req, 'hosts')
.then(data => onFulfilled(data))
.catch(error => {
onRejected(error)
});
});
server.get('/api/workers', function (req, res) {
function onFulfilled(data) {
let obj = yaml.load(data);
let workers = {workers: []};
for (worker in obj.all.children.workers.hosts) {
workers.workers.push({name: worker});
}
res.status(200).send(workers);
}
function onRejected(err) {
res.status(500).send({
message: 'Settings fetch error - ' + err
});
}
fetch.fetchFromFile(req, 'hosts')
.then(data => onFulfilled(data))
.catch(error => {
onRejected(error)
});
});
server.get('/api/can_view_invoices', async function (req, res) {
try {
const {moneybird} = await loadSubscriptionFile(req)
if (moneybird.contact_id && moneybird.reference) {
res.status(200).send({message: true});
}
} catch (err) {
res.status(200).send({message: false});
}
});
server.get('/api/invoice_by_id', async function (req, res) {
try {
const {query} = parse(req.url, true);
const invoice = await getInvoice(req, query.id);
res.status(200).send(invoice);
} catch (err) {
res.status(500).send({message: "Error fetching invoice, is the configuration available and the id valid?"});
}
});
server.get('/api/invoice_payment', async function (req, res) {
try {
const {query} = parse(req.url, true);
console.log("getting payment for invoice ", query.invoice_id)
const invoice = await getInvoice(req, query.invoice_id);
const payment = await getPaymentForInvoice(invoice).catch(null);
res.status(200).send({invoice, payment_status: payment.status, payment_url: payment.getPaymentUrl()});
} catch (err) {
console.log(err)
res.status(500).send({message: "Error fetching payment, is the configuration available and the id valid?"});
}
});
server.get('/api/invoices', async function (req, res) {
try {
const invoices = await getInvoices(req);
res.status(200).send(invoices);
} catch (err) {
console.error(err)
res.status(500).send({message: "Error fetching invoices, is the configuration available?"});
}
});
server.get('/api/invoice_contact', async function (req, res) {
try {
const contact_data = await getContactData(req);
res.status(200).send(contact_data);
} catch (err) {
console.error(err)
res.status(500).send({message: "Error fetching invoice contact, is the configuration available?"});
}
});
server.post('/api/workers', function (req, res) {
function onFulfilled() {
res.status(200).send();
}
function onRejected(err) {
res.status(500).send({
message: 'Settings fetch error - ' + err.message
});
}
});
server.get('/api/build-slaves', function (req, res) {
function onFulfilled(data) {
let obj = yaml.load(data);
let build_slaves = {build_slaves: []};
for (build_slave in obj.all.children.build_slaves.hosts) {
build_slaves.build_slaves.push({name: build_slave});
}
res.status(200).send(build_slaves);
}
function onRejected(err) {
res.status(500).send({
message: 'Settings fetch error - ' + err
});
}
fetch.fetchFromFile(req, 'hosts')
.then(data => onFulfilled(data))
.catch(error => {
onRejected(error)
});
});
server.post('/api/build_slaves', function (req, res) {
function onFulfilled() {
res.status(200).send();
}
function onRejected(err) {
res.status(500).send({
message: 'Settings fetch error - ' + err.message
});
}
});
server.listen(3100, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3100')
});