-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for uploading SMS from CSV files
- Loading branch information
Showing
8 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from,message | ||
+1234567890,hello there | ||
+987654321,"goodnight, dear prince" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
|
||
}); | ||
|
||
}); | ||
}); |