-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-logs.js
98 lines (83 loc) · 2.91 KB
/
update-logs.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
#!/usr/bin/env node
const request = require('request');
const user = process.argv[2];
const pass = process.argv[3];
const parentCode = process.argv[4];
const domain = process.argv[5] || 'localhost:2200';
const protocol = process.argv[6] || 'http';
const couchAddress = protocol + '://' + user + ':' + pass + '@' + domain + '/';
const allDocsEndpoint = '/_all_docs?include_docs=true';
let code = '';
let resources = [];
let courses = [];
const getDocs = (db, callback) => {
request.get({ uri: couchAddress + db + allDocsEndpoint, json: true },
(err, response) =>
callback(response.body.rows.map(row => row.doc).filter(doc => doc._id.indexOf('_design') === -1)));
};
const updateDocs = (docs, db, callback) => {
request.post({
uri: couchAddress + db + '/_bulk_docs',
body: { docs },
json: true
}, callback);
};
const addParentToConfiguration = (configurations) => {
updateDocs(configurations.map(config => config.parentCode = parentCode), () => {
console.log('Configurations updated');
});
}
const appendPlanetCodes = (array, codeField) => {
return array.map(item => ({ ...item, parentCode, [codeField]: code }));
};
const addTitleToLog = (array) => {
return array.map(item => {
const relatedArray = item.type === 'course' ? courses : resources;
const titleField = item.type === 'course' ? 'courseTitle' : 'title';
return { ...item, title: titleFromId(relatedArray, item.resource || item.resourceId || item.item, titleField) };
});
};
const resourceActivitiesUpdate = (docs, codeField) => {
return ratingsUpdate(docs, codeField).map(rActivity => {
const { activity, ...newLog } = rActivity;
return { ...newLog, type: activity };
});
};
const ratingsUpdate = (docs, codeField) => {
return addTitleToLog(appendPlanetCodes(docs, codeField));
};
const titleFromId = (array, id, titleField) => {
const doc = array.find(item => item._id === id);
return doc ? doc[titleField] : undefined;
};
const getCourses = () => {
getDocs('courses', (docs) => {
courses = docs;
getResources();
});
};
const getResources = () => {
getDocs('resources', (docs) => {
resources = docs;
updateDatabases();
});
};
getDocs('configurations', (configurations) => {
code = configurations[0].code;
addParentToConfiguration(configurations);
getCourses();
});
const updateDatabase = (db, updateFunction, codeField = 'createdOn') => {
getDocs(db, (docs) => {
updateDocs(updateFunction(docs, codeField), db, () => console.log(db + ' updated'));
});
};
const updateDatabases = () => {
updateDatabase('login_activities', appendPlanetCodes);
updateDatabase('_users', appendPlanetCodes, 'planetCode');
updateDatabase('feedback', appendPlanetCodes, 'source');
updateDatabase('resource_activities', resourceActivitiesUpdate);
updateDatabase('ratings', ratingsUpdate);
updateDatabase('courses_progress', appendPlanetCodes);
updateDatabase('submissions', appendPlanetCodes, 'source');
};