Skip to content

Commit 1a53765

Browse files
committed
initial implementation
0 parents  commit 1a53765

13 files changed

+768
-0
lines changed

.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
#Temporary data
6+
.tmp
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
13+
# Directory for instrumented libs generated by jscoverage/JSCover
14+
lib-cov
15+
16+
# Coverage directory used by tools like istanbul
17+
coverage
18+
19+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
20+
.grunt
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# Deployed apps should consider commenting this line out:
27+
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
28+
node_modules
29+
doc
30+
startup.sh
31+
spec/intergration.spec.js
32+
config

.jshintrc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"bitwise": true,
3+
"browser": true,
4+
"camelcase": true,
5+
"curly": true,
6+
"eqeqeq": true,
7+
"esnext": true,
8+
"immed": true,
9+
"latedef": true,
10+
"newcap": true,
11+
"noarg": true,
12+
"node": true,
13+
"proto": true,
14+
"mocha": true,
15+
"quotmark": "single",
16+
"strict": true,
17+
"undef": true,
18+
"unused": true,
19+
"expr": true,
20+
"ignore": true
21+
}

.npmignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
node_modules
2+
ssl
3+
.DS_STORE
4+
*~
5+
.idea
6+
nbproject
7+
test
8+
.git
9+
.gitignore
10+
.tmp
11+
*.swo
12+
*.swp
13+
*.swn
14+
*.swm
15+
*.log
16+
.jshintrc
17+
.editorconfig
18+
docs
19+
config
20+
views
21+
spec/intergration.spec.js
22+
doc.html
23+
.travis.yml
24+
Gruntfile.js

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
services: mongodb
3+
node_js:
4+
- "0.12"
5+
before_script:
6+
- npm install -g grunt-cli

Gruntfile.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
module.exports = function(grunt) {
4+
5+
// add grunt tasks.
6+
grunt.loadNpmTasks('grunt-mocha-test');
7+
grunt.loadNpmTasks('grunt-contrib-jshint');
8+
grunt.loadNpmTasks('grunt-contrib-watch');
9+
10+
grunt.initConfig({
11+
// Configure a mochaTest task
12+
mochaTest: {
13+
test: {
14+
options: {
15+
reporter: 'spec',
16+
timeout: 20000
17+
},
18+
src: [
19+
'spec/**/*.js'
20+
]
21+
}
22+
},
23+
jshint: {
24+
options: {
25+
reporter: require('jshint-stylish'),
26+
jshintrc: '.jshintrc'
27+
},
28+
all: [
29+
'Gruntfile.js',
30+
'index.js',
31+
'lib/**/*.js',
32+
'spec/**/*.js'
33+
]
34+
},
35+
watch: {
36+
all: {
37+
files: [
38+
'Gruntfile.js',
39+
'index.js',
40+
'lib/**/*.js',
41+
'spec/**/*.js'
42+
],
43+
tasks: ['default']
44+
}
45+
}
46+
});
47+
48+
//custom tasks
49+
grunt.registerTask('default', ['jshint', 'mochaTest', 'watch']);
50+
grunt.registerTask('test', ['jshint', 'mochaTest']);
51+
52+
};

README.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
byteskode-sms-callback
2+
=====================
3+
4+
[![Build Status](https://travis-ci.org/byteskode/byteskode-sms-callback.svg?branch=master)](https://travis-ci.org/byteskode/byteskode-sms-callback)
5+
byteskode infobip sms deliveries & track callbackbyteskode infobip sms with mongoose persistence support
6+
7+
*Note: All configuration are done using [config](https://github.com/lorenwest/node-config) using key `sms`*
8+
9+
## Requirements
10+
- [mongoose](https://github.com/Automattic/mongoose)
11+
- [byteskode-sms](https://github.com/byteskode/byteskode-sms)
12+
- [express](https://github.com/expressjs/express)
13+
14+
## Installation
15+
```sh
16+
$ npm install --save mongoose byteskode-sms-callback
17+
```
18+
19+
## Usage
20+
21+
```javascript
22+
var mongoose = require('mongoose');
23+
var smsCallback = require('byteskode-sms-callback');
24+
var methodOverride = require('method-override');
25+
var bodyParser = require('body-parser');
26+
var app = require('express')();
27+
28+
//add required middleware
29+
app.use(methodOverride('_method'));
30+
app.use(bodyParser.urlencoded({ extended: false }));
31+
app.use(bodyParser.json());
32+
33+
//register sms callback
34+
app.use(smsCallback);
35+
36+
```
37+
38+
## Configuration Options
39+
Base on your environment setup, ensure you have the following configurations in your `config` files.
40+
41+
```js
42+
sms: {
43+
username: <infobip_username>,
44+
password: <infobip_password>,
45+
callback: {
46+
baseUrl:'http://example.com', //No foward slash at the end
47+
deliveries: '/sms-deliveries', //ensure foward slush at the begin
48+
},
49+
intermediateReport: true,
50+
models: {
51+
sms: {
52+
name: 'SMS',
53+
// fields: {}
54+
},
55+
message: {
56+
name: 'SMSMessage',
57+
// fields:{}
58+
}
59+
}
60+
}
61+
```
62+
63+
## Testing
64+
* Clone this repository
65+
66+
* Install all development dependencies
67+
```sh
68+
$ npm install
69+
```
70+
71+
* Then run test
72+
```sh
73+
$ npm test
74+
```
75+
76+
## Contribute
77+
It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
78+
79+
80+
## Licence
81+
The MIT License (MIT)
82+
83+
Copyright (c) 2015 byteskode & Contributors
84+
85+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
86+
87+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
88+
89+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

index.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
'use strict';
2+
3+
/**
4+
* @name byteskode-sms-callback
5+
* @description byteskode infobip sms deliveries & track callback
6+
* @singleton
7+
*/
8+
9+
//dependencies
10+
var _ = require('lodash');
11+
var config = require('config');
12+
var mongoose = require('mongoose');
13+
require('byteskode-sms');
14+
var Utils = require('byteskode-sms/lib/utils');
15+
var express = require('express');
16+
var router = express.Router();
17+
18+
19+
//prepare configurations
20+
//obtain configuration from config
21+
var _config = config.has('sms') ? config.get('sms') : {};
22+
23+
24+
//merge default configurations
25+
_config = _.merge({}, {
26+
callback: {
27+
deliveries: '/sms-deliveries',
28+
},
29+
models: {
30+
sms: {
31+
name: 'SMS',
32+
},
33+
message: {
34+
name: 'SMSMessage',
35+
}
36+
}
37+
}, _config);
38+
39+
40+
//deduce sms deliveries & tracking callback url
41+
var deliveriesPath = _config.callback.deliveries;
42+
43+
//obtain SMS and Message model
44+
var Message = mongoose.model(_config.models.message.name);
45+
46+
47+
/**
48+
* @name handle
49+
* @description handle deliveries and tracking reports
50+
* @param {Request} request valid express http request
51+
* @param {Function} done a callback to invoke on success or error
52+
* @return {Object}
53+
*/
54+
function handle(request, done) {
55+
//grab sms _id
56+
var source = (request.query || {}).source;
57+
58+
//back of if source is tempered on not available
59+
if (_.isEmpty(source)) {
60+
return done(new Error('Invalid source'));
61+
}
62+
63+
64+
//ensure delivery / tracking report provided
65+
var hasNoDeliveryReport =
66+
request &&
67+
_.isEmpty(request.body) &&
68+
(_.isEmpty(request.body.results) || _.isEmpty(request.body.messages));
69+
70+
if (hasNoDeliveryReport) {
71+
return done(new Error('Missing delivery report'));
72+
}
73+
74+
//obtain message responses from the report
75+
var responses = Utils.normalize(request.body);
76+
77+
Message.updateStatuses(responses, function(error, updates) {
78+
if (error) {
79+
Message.emit('sms:deliveries:error', error);
80+
return done(new Error('Unprocessed delivery report'));
81+
} else {
82+
Message.emit('sms:deliveries', updates);
83+
return done(null, _.compact(updates));
84+
}
85+
});
86+
87+
}
88+
89+
90+
/**
91+
* Handle Http POST on sms callback path
92+
* @description process received sms deliveries & tracking reports
93+
* @param {HttpRequest} request a http request
94+
* @param {HttpResponse} response a http response
95+
*/
96+
router.post(deliveriesPath, function(request, response) {
97+
handle(request, function(error, result) {
98+
if (error) {
99+
response.status(error.status || 500);
100+
response.json({
101+
success: false,
102+
message: error.message
103+
});
104+
} else {
105+
response.json(result);
106+
}
107+
});
108+
});
109+
110+
111+
112+
/**
113+
* Handle Http PUT on sms callback path
114+
* @description process received sms deliveries & tracking reports
115+
* @param {HttpRequest} request a http request
116+
* @param {HttpResponse} response a http response
117+
*/
118+
router.put(deliveriesPath, function(request, response) {
119+
handle(request, function(error, result) {
120+
if (error) {
121+
response.status(error.status || 500);
122+
response.json({
123+
success: false,
124+
message: error.message
125+
});
126+
} else {
127+
response.json(result);
128+
}
129+
});
130+
});
131+
132+
module.exports = exports = router;

0 commit comments

Comments
 (0)