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
/
default_responses.js
115 lines (108 loc) · 3.61 KB
/
default_responses.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
var _ = require('underscore'),
moment = require('moment'),
libphonenumber = require('libphonenumber'),
config = require('../config'),
utils = require('../lib/utils'),
logger = require('../lib/logger'),
messages = require('../lib/messages');
module.exports = {
filter: function(doc) {
var self = module.exports;
return Boolean(
doc &&
doc.from &&
doc.type === 'data_record' &&
!doc.kujua_message &&
self._isReportedAfterStartDate(doc) &&
!self._hasRun(doc) &&
!self._isMessageFromGateway(doc)
);
},
_hasRun: function(doc) {
return Boolean(
doc &&
doc.transitions &&
doc.transitions.default_responses
);
},
/*
* Avoid infinite loops of auto-reply messages between gateway and itself.
*/
_isMessageFromGateway: function(doc) {
var self = module.exports,
gw = self._getConfig('gateway_number'),
from = doc.sms_message && doc.sms_message.from;
if (typeof gw === 'string' && typeof from === 'string') {
return libphonenumber.phoneUtil.isNumberMatch(gw, from) >= 3;
}
return false;
},
_isReportedAfterStartDate: function(doc) {
var self = module.exports,
config = self._getConfig('default_responses'),
start_date;
function isEmpty() {
return !Boolean(
config &&
config.start_date &&
config.start_date.trim()
);
}
if (!isEmpty()) {
start_date = moment(config.start_date, 'YYYY-MM-DD');
if (start_date.isValid() && doc.reported_date) {
return moment(doc.reported_date).isAfter(start_date);
} else {
logger.error('Invalid default_responses start date: ' + start_date);
}
}
return false;
},
_isMessageEmpty: function(doc) {
return Boolean(_.find(doc.errors, function(err) {
return err.code === 'sys.empty';
}));
},
_isFormNotFound: function(doc) {
return Boolean(_.find(doc.errors, function(err) {
return err.code === 'sys.form_not_found';
}));
},
_isValidUnstructuredMessage: function(doc) {
return Boolean(typeof doc.form !== 'string');
},
_isConfigFormsOnlyMode: function() {
return module.exports._getConfig('forms_only_mode');
},
_getConfig: function(key) {
return config.get(key);
},
_getLocale: function(doc) {
return utils.getLocale(doc);
},
_translate: function(key, locale) {
return utils.translate(key, locale);
},
_addMessage: function(doc, msg) {
messages.addMessage({
doc: doc,
phone: doc.from,
message: msg
});
},
onMatch: function(change, db, audit, callback) {
var self = module.exports,
doc = change.doc,
locale = self._getLocale(doc);
if (self._isMessageEmpty(doc)) {
self._addMessage(doc, self._translate('empty', locale));
} else if (self._isConfigFormsOnlyMode() && self._isFormNotFound(doc)) {
self._addMessage(doc, self._translate('form_not_found', locale));
} else if (self._isFormNotFound(doc)) {
self._addMessage(doc, self._translate('sms_received', locale));
} else if (self._isValidUnstructuredMessage(doc)) {
self._addMessage(doc, self._translate('sms_received', locale));
}
callback(null, true);
}
};