Skip to content

Commit

Permalink
Add support for uploading SMS from CSV files
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed May 7, 2018
1 parent f57ef04 commit 06f78d6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 2 deletions.
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-conf",
"version": "1.13.0",
"version": "1.14.0",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -41,6 +41,7 @@
},
"devDependencies": {
"chai": "^4.0.0",
"chai-exclude": "^1.0.8",
"express-pouchdb": "^2.3.7",
"jshint": "^2.9.5",
"lodash": "^4.17.4",
Expand Down
1 change: 1 addition & 0 deletions src/cli/supported-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = [
'convert-contact-forms',
'create-users',
'csv-to-docs',
'csv-to-sms',
'delete-forms',
'delete-all-forms',
'fetch-forms-from-google-drive',
Expand Down
32 changes: 32 additions & 0 deletions src/fn/csv-to-sms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('../lib/sync-fs');
const request = require('request-promise-native');
const trace = require('../lib/log').trace;
const uuid = require('uuid/v4');

module.exports = (projectDir, couchUrl, extras) => {
const instanceUrl = couchUrl.replace(/\/medic$/, '');
const csvFiles = extras || ['sms.csv'];

trace('csv-to-sms', 'csv files:', csvFiles);

return csvFiles.map(fileName => `${projectDir}/${fileName}`)
.reduce((promiseChain, csvFile) => {
trace(`Processing csv file ${csvFile}…`);
const raw = fs.readCsv(csvFile);

const messages = raw.rows.map(row => ({
id: uuid(),
from: row[raw.cols.indexOf('from')],
content: row[raw.cols.indexOf('message')],
sms_sent: Date.now(),
sms_received: Date.now(),
}));

return request({
uri: `${instanceUrl}/api/sms`,
method: 'POST',
json: true,
body: { messages },
});
}, Promise.resolve());
};
7 changes: 7 additions & 0 deletions test/api-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const opts = {
};
const app = express();
app.use(bodyParser.json());
app.post('/api/sms', (req, res) => {
module.exports.gatewayRequests.push(req.body);
res.write('{}');
res.end();
});
app.all('/api/*', mockMiddleware.requestHandler);
app.use('/', stripAuth, expressPouch(memPouch, opts));

Expand All @@ -29,6 +34,8 @@ module.exports = {

const port = server.address().port;
module.exports.couchUrl = `http://admin:pass@localhost:${port}/medic`;

module.exports.gatewayRequests = [];
},
stop: () => {
server.close();
Expand Down
14 changes: 14 additions & 0 deletions test/data/csv-to-sms/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"messages": [
{
"from": "+1234567890",
"content": "hello there"
},
{
"from": "+987654321",
"content": "goodnight, dear prince"
}
]
}
]
3 changes: 3 additions & 0 deletions test/data/csv-to-sms/sms.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from,message
+1234567890,hello there
+987654321,"goodnight, dear prince"
31 changes: 31 additions & 0 deletions test/fn/csv-to-sms.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const api = require('../api-stub');
const fs = require('../../src/lib/sync-fs');
const csvToSms = require('../../src/fn/csv-to-sms');

const chai = require('chai');
chai.use(require('chai-exclude'));
const assert = chai.assert;

describe('csv-to-sms', function() {
beforeEach(api.start);
afterEach(api.stop);

it('should upload SMS supplied in CSV format to medic-api', function() {

// given
const testDir = 'data/csv-to-sms';

// when
return csvToSms(testDir, api.couchUrl)
.then(() => {

// then
const expected = fs.readJson(`${testDir}/expected.json`);
assert.deepEqualExcludingEvery(
api.gatewayRequests, expected,
['id', 'sms_sent', 'sms_received']);

});

});
});

0 comments on commit 06f78d6

Please sign in to comment.