-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
103 lines (92 loc) · 3.49 KB
/
app.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
var sys = require('sys'),
fs = require('fs'),
http = require('http'),
express = require('express'),
multiparser = require('./multiparser'),
_ = require('underscore')._,
markdown = require('node-markdown').Markdown,
Story = require('./models/story'),
Attachment = require('./models/attachment'),
Notification = require('./models/notification');
var app = express.createServer(express.logger());
app.use(multiparser());
app.use(express.bodyParser());
var README = markdown(fs.readFileSync('./README.md', 'utf8'), true);
app.get('/', function(request, response) {
response.send('<html><head><title>Book Order</title></head><body>' + README + '</body></html>');
});
app.post('/:gateway/projects/:project/stories/new/:token', function(request, response) {
try {
var attachments = [];
switch(request.params.gateway){
case 'sendgrid':
for(var i=1; i<=parseInt(request.body.attachments); i++)
attachments.push( request.body['attachment' + i]);
break;
case 'mailgun':
for(var i=1; i<=parseInt(request.body['attachment-count']); i++)
attachments.push(request.body['attachment-' + i]);
break;
}
var story = new Story({
projectId: request.params.project,
token: request.params.token,
from: request.body.from,
cc: (request.body.cc || request.body.Cc),
subject: request.body.subject,
body: ( request.body['stripped-text'] || request.body['text'] ),
attachments: attachments
});
story.bind('error', function(err) {
console.log(err);
if(process.env.BUG_NOTIFICATION_TO) {
var notificationBody;
try {
notificationBody = "Sorry, there was an error processing mail.\n\n" +
"from: " + request.body.from + "\n" +
"subject: " + request.body.subject + "\n" +
"body:\n" + ( request.body['stripped-text'] || request.body['text'] ) + "\n\n" +
new String(err);
} catch(e) {
notificationBody = "Sorry, there was an error processing mail.\n\n" + new String(e) + "\n\n" + new String(err);
}
var notification = new Notification({
body: notificationBody,
attachments: attachments
});
notification.send();
}
});
if(process.env.STORY_NOTIFICATION_FROM) {
story.bind('done', function(url) {
var notification = new Notification({
subject: 'PT Story Created',
body: "Your story has been created in Pivotal Tracker. You can see it here:\n\n" + url,
from: process.env.STORY_NOTIFICATION_FROM,
to: story.get('from')
});
notification.send();
});
if(process.env.ERROR_NOTIFICATION_TO_SENDER) {
story.bind('uncreated', function(message) {
var notification = new Notification({
subject: 'PT Story Not Created',
body: message,
from: process.env.STORY_NOTIFICATION_FROM,
to: story.get('from')
});
notification.send();
});
}
}
story.save();
}
catch (exception) {
console.log(sys.inspect(exception));
}
response.send(200);
});
var port = process.env.PORT || 3000;
app.listen(port, function(){
console.log('Listening on ' + port);
});