Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
support swift sms provider; fixes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Jun 26, 2019
1 parent 527ce20 commit cc7a2ea
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 14 deletions.
31 changes: 27 additions & 4 deletions common/mixins/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let axios = require('axios')
module.exports = function(Model, options) {
var ipRangeCheck = require('ip-range-check')
var _ = require('lodash')
Expand Down Expand Up @@ -72,12 +73,33 @@ module.exports = function(Model, options) {

let smsClient
const Twillio = require('twilio')
Model.sendSMS = function(to, textBody, cb) {
var smsServiceProvider = Model.app.get('smsServiceProvider')
Model.sendSMS = async function(to, textBody, cb) {
let smsServiceProvider = Model.app.get('smsServiceProvider')
let smsConfig = Model.app.get('sms')[smsServiceProvider]
switch (smsServiceProvider) {
case 'swift':
try {
let url = `${smsConfig['apiUrlPrefix']}${
smsConfig['accountKey']
}/${encodeURIComponent(to)}`
await axios.post(
url,
{
MessageBody: textBody
},
{
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}
)
} catch (ex) {
return cb && cb(ex)
}
cb && cb()
break
default:
// Twilio Credentials
var smsConfig = Model.app.get('sms')[smsServiceProvider]
var accountSid = smsConfig.accountSid
var authToken = smsConfig.authToken

Expand All @@ -91,7 +113,7 @@ module.exports = function(Model, options) {
body: textBody
},
function(err, message) {
cb(err, message)
cb && cb(err, message)
}
)
}
Expand Down Expand Up @@ -352,3 +374,4 @@ module.exports = function(Model, options) {
return res
}
}
module.exports.axios = axios
30 changes: 23 additions & 7 deletions docs/_docs/config-sms.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ permalink: /docs/config-sms/

*NotifyBC* depends on underlying SMS service providers to deliver SMS messages. The supported service providers are

* Twilio (default)
* [Twilio](https://twilio.com/) (default)
* [Swift](https://www.swiftsmsgateway.com)

Only one service provider can be chosen per installation. To change service provider, add following *smsServiceProvider* config object to file */server/config.local.js*

```js
module.exports = {
"smsServiceProvider": "twilio"
...
smsServiceProvider: 'swift'
}
```
The rest configs are service provider specific. You should have an account with the chosen service provider before proceeding.
Expand All @@ -22,14 +24,28 @@ Add *sms.twilio* config object to file */server/config.local.js*

```js
module.exports = {
"sms": {
"twilio": {
"accountSid": "<AccountSid>",
"authToken": "<AuthToken>",
"fromNumber": "<FromNumber>"
sms: {
twilio: {
accountSid: '<AccountSid>',
authToken: '<AuthToken>',
fromNumber: '<FromNumber>'
}
}
}
```
Obtain *\<AccountSid\>*, *\<AuthToken\>* and *\<FromNumber\>* from your Twilio account.

## Swift
Add *sms.swift* config object to file */server/config.local.js*

```js
module.exports = {
sms: {
swift: {
accountKey: '<accountKey>'
}
}
}
```
Obtain *\<accountKey\>* from your Swift account.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notification",
"version": "1.11.0",
"version": "1.12.0",
"dbSchemaVersion": "0.6.0",
"main": "server/server.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions server/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"name": "localhost"
},
"smsServiceProvider": "twilio",
"sms": {
"swift": {
"apiUrlPrefix": "https://secure.smsgateway.ca/services/message.svc/"
}
},
"subscription": {
"detectDuplicatedSubscription": false,
"duplicatedSubscriptionNotification": {
Expand Down
6 changes: 6 additions & 0 deletions server/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ module.exports = {
restApiRoot: '/api',
httpHost: '',
internalHttpHost: '',
smsServiceProvider: 'swift',
sms: {
swift: {
accountKey: '123'
},
},
subscription: {
detectDuplicatedSubscription: false
},
Expand Down
14 changes: 13 additions & 1 deletion spec/app/subscription.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,13 @@ describe('POST /subscriptions', function() {
expect(data[0].confirmationRequest.confirmationCode).toBe('12345')
})

it('should allow non-admin user create subscriptions', async function() {
it('should allow non-admin user create sms subscription using swift provider', async function() {
app.models.Subscription.sendSMS.and.stub().and.callThrough()
let common = require('../../common/mixins/common')
spyOn(common.axios, 'post').and.callFake(async () => {
return
})

let res = await request(app)
.post('/api/subscriptions')
.send({
Expand All @@ -249,6 +255,12 @@ describe('POST /subscriptions', function() {
.set('Accept', 'application/json')
expect(res.statusCode).toBe(200)
expect(app.models.Subscription.sendSMS).toHaveBeenCalledTimes(1)
expect(common.axios.post.calls.argsFor(0)[0]).toBe(
'https://secure.smsgateway.ca/services/message.svc/123/12345'
)
expect(common.axios.post.calls.argsFor(0)[1]['MessageBody']).toMatch(
/Enter \d{5} on screen/
)
let data = await app.models.Subscription.find({
where: {
serviceName: 'myService',
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4008,7 +4008,7 @@ loopback-component-explorer@f-w/loopback-component-explorer:
debug "^2.2.0"
depd "^1.1.0"
iframe-resizer "^3.5.15"
lodash "^3.10.0"
lodash "^4.17.11"
loopback-swagger "^5.0.0"
strong-globalize "^3.1.0"
swagger-ui "^2.2.5"
Expand Down

0 comments on commit cc7a2ea

Please sign in to comment.