forked from medic/cht-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway-api.utils.js
139 lines (118 loc) · 2.9 KB
/
gateway-api.utils.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
const testUtils = require('./utils');
const uuid = require('uuid/v4');
const CHW_CONTACT_NUMBER = '+32049832049';
module.exports = {
api: {
get: get,
post: post,
postMessage: postMessage,
postMessages: postMessages,
postStatus: postStatus,
postStatuses: postStatuses,
},
db: {
getMessageContents: getMessageContents,
getMessageStates: getMessageStates,
},
setup: {
saveWoMessage: saveWoMessage,
saveWoMessages: saveWoMessages,
},
cleanUp: () => {
// delete WO and WT messages
return allMessageDocs()
.then(docs => docs.map(doc => {
doc._deleted = true;
return doc;
}))
.then(docs => testUtils.db.bulkDocs(docs));
},
};
function get() {
return testUtils.request({
path: '/api/sms',
method: 'GET',
headers: { 'Content-Type':'application/json' },
});
}
function post(body) {
return testUtils.request({
path: '/api/sms',
method: 'POST',
headers: { 'Content-Type':'application/json' },
body: body,
});
}
function postMessage(...messages) {
return postMessages(...messages);
}
function postMessages(...messages) {
return post({ messages });
}
function postStatus(messageId, newStatus) {
return postStatuses({ id:messageId, status:newStatus });
}
function postStatuses(...updates) {
return post({ updates });
}
function saveWoMessage(id, content) {
return saveWoMessages({ id, content });
}
function saveWoMessages(...details) {
return testUtils.db.bulkDocs(details.map(d => createWoMessage(d.id, d.content)));
}
function createWoMessage(id, content) {
const task = {
messages: [
{
from: '+123',
sent_by: 'some name',
to: CHW_CONTACT_NUMBER,
contact: {},
message: content,
uuid: id,
}
]
};
const messageDoc = {
_id: uuid(),
errors: [],
form: null,
from: '+123',
reported_date: 1520416423761,
tasks: [ task ],
kujua_message: true,
type: 'data_record',
sent_by: 'some name',
};
return messageDoc;
}
function getMessageStates() {
return allMessageDocs()
.then(docs => docs.reduce((acc, doc) => {
doc.tasks.forEach(task => task.messages.forEach(m => {
const states = task.state_history &&
task.state_history.map(h => h.state);
acc.push({ id:m.uuid, states });
}));
return acc;
}, []));
}
function getMessageContents() {
return allMessageDocs()
.then(docs => docs.reduce((acc, doc) => {
if(doc.kujua_message) {
doc.tasks.forEach(task =>
task.messages.forEach(m => acc.push(m.message)));
}
if(doc.sms_message) {
acc.push(doc.sms_message.message);
}
return acc;
}, []));
}
function allMessageDocs() {
return testUtils.db.query('medic-client/messages_by_contact_date',
{ reduce:false, include_docs:true })
.then(res => res.rows.map(row => row.doc));
}