-
Notifications
You must be signed in to change notification settings - Fork 3
/
googleSpreadsheet.js
58 lines (52 loc) · 1.36 KB
/
googleSpreadsheet.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
const google = require('googleapis');
const keys = require('./config/keys');
const marked = require('marked');
function rowToObject(val, lab) {
const o = {};
for (let i = 0; i < lab.length; i += 1) {
o[lab[i]] = val[i];
}
return o;
}
function loadDatabase(callback) {
const sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
auth: keys.apiKey,
spreadsheetId: '1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY',
range: 'Agora inventory!A:Z',
}, (err, response) => {
if (err) {
console.log(`The API returned an error: ${err}`);
return;
}
return callback(response.values.map(row =>
rowToObject(row, response.values[0])).splice(1));
});
}
function searchDatabase(query, rows) {
let matches = rows;
Object.keys(query).map((key) => {
matches = matches.filter(item => item[key] === query[key]);
});
return matches;
}
function addSimilarItems(object, allObj) {
const obj = object;
obj.similarItems = searchDatabase({ fixture: obj.fixture }, allObj)
.filter(item => item.uuid !== obj.uuid)
.splice(0, 3);
return obj;
}
function addMarkdown(object) {
const obj = object;
obj.HOWTO = marked(obj.HOWTO);
obj.details = marked(obj.details);
obj.Troubleshooting = marked(obj.Troubleshooting);
return obj;
}
module.exports = {
loadDatabase,
searchDatabase,
addMarkdown,
addSimilarItems,
};