This repository has been archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.js
81 lines (76 loc) · 2.19 KB
/
config.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
const _ = require('underscore'),
follow = require('follow'),
db = require('./db'),
logger = require('./lib/logger'),
defaults = require('./defaults'),
translations = {},
SETTINGS_PATH = '_design/medic/_rewrite/app_settings/medic';
let config = require('./defaults');
const loadTranslations = () => {
return new Promise(resolve => {
const options = {
startkey: [ 'translations', false ],
endkey: [ 'translations', true ],
include_docs: true
};
db.medic.view('medic-client', 'doc_by_type', options, (err, result) => {
if (err) {
logger.error('Error loading translations - starting up anyway', err);
} else {
result.rows.forEach(row => translations[row.doc.code] = row.doc.values);
}
resolve();
});
});
};
const initFeed = () => {
// Use since=now on ddoc listener so we don't replay an old change.
const feed = new follow.Feed({ db: process.env.COUCH_URL, since: 'now' });
feed.on('change', change => {
if (change.id === '_design/medic') {
logger.info('Reloading configuration');
initConfig();
} else if (change.id.startsWith('messages-')) {
logger.info('Detected translations change - reloading');
loadTranslations();
}
});
feed.follow();
};
const initConfig = () => {
return new Promise((resolve, reject) => {
db.medic.get(SETTINGS_PATH, (err, data) => {
if (err) {
console.error(err);
return reject(new Error('Error loading configuration'));
}
_.defaults(data.settings, defaults);
config = data.settings;
logger.debug(
'Reminder messages allowed between %s:%s and %s:%s',
config.schedule_morning_hours,
config.schedule_morning_minutes,
config.schedule_evening_hours,
config.schedule_evening_minutes
);
return resolve();
});
}).then(() => {
return require('./transitions').loadTransitions();
});
};
module.exports = {
_initConfig: initConfig,
_initFeed: initFeed,
get: key => {
return config[key];
},
getTranslations: () => {
return translations;
},
init: () => {
initFeed();
return loadTranslations()
.then(initConfig);
}
};