@openneuro/server 4.4.0-alpha.2 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/config.js +3 -4
- package/src/libs/email/index.ts +21 -19
- package/src/libs/notifications.js +7 -63
- package/src/models/notification.ts +2 -0
- package/src/server.js +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openneuro/server",
|
|
3
|
-
"version": "4.4.0
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"description": "Core service for the OpenNeuro platform.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "src/server.js",
|
|
@@ -105,5 +105,5 @@
|
|
|
105
105
|
"publishConfig": {
|
|
106
106
|
"access": "public"
|
|
107
107
|
},
|
|
108
|
-
"gitHead": "
|
|
108
|
+
"gitHead": "03e40831fd46590613b9a2d392f55225420dc7fe"
|
|
109
109
|
}
|
package/src/config.js
CHANGED
|
@@ -42,10 +42,9 @@ const config = {
|
|
|
42
42
|
},
|
|
43
43
|
notifications: {
|
|
44
44
|
email: {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
pass: process.env.CRN_SERVER_MAIL_PASS,
|
|
45
|
+
apiKey: process.env.CRN_SERVER_MAIL_API_KEY,
|
|
46
|
+
secret: process.env.CRN_SERVER_MAIL_API_SECRET,
|
|
47
|
+
from: process.env.CRN_SERVER_MAIL_FROM,
|
|
49
48
|
},
|
|
50
49
|
},
|
|
51
50
|
sentry: {
|
package/src/libs/email/index.ts
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
1
|
import nodemailer from 'nodemailer'
|
|
2
|
+
import mailjetTransport from 'nodemailer-mailjet-transport'
|
|
2
3
|
import config from '../../config'
|
|
3
4
|
|
|
4
5
|
// setup email transporter
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
6
|
+
let transporter
|
|
7
|
+
try {
|
|
8
|
+
transporter = nodemailer.createTransport(
|
|
9
|
+
mailjetTransport({
|
|
10
|
+
auth: {
|
|
11
|
+
apiKey: config.notifications.email.apiKey,
|
|
12
|
+
secret: config.notifications.email.secret,
|
|
13
|
+
},
|
|
14
|
+
}),
|
|
15
|
+
)
|
|
16
|
+
} catch (err) {
|
|
17
|
+
// Mailjet is not configured, instead log emails
|
|
18
|
+
transporter = {
|
|
19
|
+
sendMail: (mail, cb) => {
|
|
20
|
+
console.dir(mail)
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
}
|
|
13
24
|
|
|
14
25
|
export const send = (
|
|
15
26
|
email: Record<string, string>,
|
|
16
27
|
callback: (err, response) => void,
|
|
17
28
|
): void => {
|
|
18
|
-
// inline styles
|
|
19
|
-
//html = juice(html)
|
|
20
|
-
|
|
21
|
-
// determine if the main is from a specific sender
|
|
22
|
-
// or the generic email address
|
|
23
|
-
const user: string =
|
|
24
|
-
email && email.from ? email.from : config.notifications.email.user
|
|
25
|
-
const from = user + '@' + config.notifications.email.url
|
|
26
|
-
|
|
27
29
|
// configure mail options
|
|
28
30
|
const mailOptions = {
|
|
29
|
-
from:
|
|
30
|
-
replyTo: from,
|
|
31
|
+
from: `"OpenNeuro" <${config.notifications.email.from}>`,
|
|
32
|
+
replyTo: config.notifications.email.from,
|
|
31
33
|
to: email.to,
|
|
32
34
|
subject: email.subject,
|
|
33
35
|
html: email.html,
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
/*eslint no-console: ["error", { allow: ["log"] }] */
|
|
2
|
-
import toDate from 'date-fns/toDate'
|
|
3
|
-
import subHours from 'date-fns/subHours'
|
|
4
2
|
import config from '../config'
|
|
5
3
|
import { send as emailSend } from './email'
|
|
6
4
|
import request from 'superagent'
|
|
7
|
-
import Notification from '../models/notification'
|
|
8
5
|
import User from '../models/user'
|
|
9
6
|
import Subscription from '../models/subscription'
|
|
10
|
-
import MailgunIdentifier from '../models/mailgunIdentifier'
|
|
11
7
|
import moment from 'moment'
|
|
12
8
|
import url from 'url'
|
|
13
9
|
import bidsId from './bidsId'
|
|
@@ -29,20 +25,6 @@ function noop() {
|
|
|
29
25
|
// public api ---------------------------------------------
|
|
30
26
|
|
|
31
27
|
const notifications = {
|
|
32
|
-
/**
|
|
33
|
-
* Add
|
|
34
|
-
*
|
|
35
|
-
* Takes a notification object and
|
|
36
|
-
* adds it to the database to be processed by
|
|
37
|
-
* the cron.
|
|
38
|
-
*/
|
|
39
|
-
add(notification, callback) {
|
|
40
|
-
Notification.updateOne({ _id: notification._id }, notification, {
|
|
41
|
-
upsert: true,
|
|
42
|
-
new: true,
|
|
43
|
-
}).then(callback)
|
|
44
|
-
},
|
|
45
|
-
|
|
46
28
|
/**
|
|
47
29
|
* Send
|
|
48
30
|
*/
|
|
@@ -106,8 +88,8 @@ const notifications = {
|
|
|
106
88
|
}),
|
|
107
89
|
},
|
|
108
90
|
}
|
|
109
|
-
// send the email
|
|
110
|
-
notifications.
|
|
91
|
+
// send the email
|
|
92
|
+
notifications.send(emailContent, noop)
|
|
111
93
|
}
|
|
112
94
|
})
|
|
113
95
|
},
|
|
@@ -179,7 +161,7 @@ const notifications = {
|
|
|
179
161
|
},
|
|
180
162
|
}
|
|
181
163
|
// send each email to the notification database for distribution
|
|
182
|
-
notifications.
|
|
164
|
+
notifications.send(emailContent, noop)
|
|
183
165
|
}
|
|
184
166
|
})
|
|
185
167
|
})
|
|
@@ -224,8 +206,7 @@ const notifications = {
|
|
|
224
206
|
}),
|
|
225
207
|
},
|
|
226
208
|
}
|
|
227
|
-
|
|
228
|
-
notifications.add(emailContent, noop)
|
|
209
|
+
notifications.send(emailContent, noop)
|
|
229
210
|
}
|
|
230
211
|
})
|
|
231
212
|
})
|
|
@@ -274,8 +255,7 @@ const notifications = {
|
|
|
274
255
|
}),
|
|
275
256
|
},
|
|
276
257
|
}
|
|
277
|
-
|
|
278
|
-
notifications.add(emailContent, noop)
|
|
258
|
+
notifications.send(emailContent, noop)
|
|
279
259
|
}
|
|
280
260
|
})
|
|
281
261
|
})
|
|
@@ -327,7 +307,7 @@ const notifications = {
|
|
|
327
307
|
},
|
|
328
308
|
}
|
|
329
309
|
// send each email to the notification database for distribution
|
|
330
|
-
notifications.
|
|
310
|
+
notifications.send(emailContent, noop)
|
|
331
311
|
}
|
|
332
312
|
})
|
|
333
313
|
})
|
|
@@ -376,43 +356,7 @@ const notifications = {
|
|
|
376
356
|
},
|
|
377
357
|
}
|
|
378
358
|
// send the email to the notifications database for distribution
|
|
379
|
-
notifications.
|
|
380
|
-
},
|
|
381
|
-
|
|
382
|
-
initCron() {
|
|
383
|
-
setInterval(() => {
|
|
384
|
-
// After one hour, retry a notification even if we have a lock
|
|
385
|
-
Notification.findOneAndUpdate(
|
|
386
|
-
{ notificationLock: { $lte: toDate(subHours(Date.now(), 1)) } },
|
|
387
|
-
{ $set: { notificationLock: new Date(Date.now()) } },
|
|
388
|
-
).exec((err, notification) => {
|
|
389
|
-
if (err) {
|
|
390
|
-
console.log(
|
|
391
|
-
'NOTIFICATION ERROR - Could not find notifications collection',
|
|
392
|
-
)
|
|
393
|
-
} else {
|
|
394
|
-
if (notification) {
|
|
395
|
-
notifications.send(notification, (err, response) => {
|
|
396
|
-
if (!err) {
|
|
397
|
-
notification.remove()
|
|
398
|
-
if (response && response.messageId) {
|
|
399
|
-
new MailgunIdentifier({
|
|
400
|
-
messageId: response.messageId,
|
|
401
|
-
}).save(err => {
|
|
402
|
-
if (err) {
|
|
403
|
-
throw `failed to save mailgunIdentifier ${response.messageId}`
|
|
404
|
-
}
|
|
405
|
-
})
|
|
406
|
-
}
|
|
407
|
-
} else {
|
|
408
|
-
console.log('NOTIFICATION ERROR ----------')
|
|
409
|
-
console.log(err)
|
|
410
|
-
}
|
|
411
|
-
})
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
})
|
|
415
|
-
}, 3600000)
|
|
359
|
+
notifications.send(emailContent, noop)
|
|
416
360
|
},
|
|
417
361
|
}
|
|
418
362
|
|
|
@@ -9,6 +9,7 @@ export interface NotificationDocument extends Document {
|
|
|
9
9
|
from: string
|
|
10
10
|
subject: string
|
|
11
11
|
template: string
|
|
12
|
+
html: string
|
|
12
13
|
data: {
|
|
13
14
|
name: string
|
|
14
15
|
appName: string
|
|
@@ -43,6 +44,7 @@ const notificationSchema = new Schema({
|
|
|
43
44
|
from: String,
|
|
44
45
|
subject: String,
|
|
45
46
|
template: String,
|
|
47
|
+
html: String,
|
|
46
48
|
data: {
|
|
47
49
|
name: String,
|
|
48
50
|
appName: String,
|
package/src/server.js
CHANGED
|
@@ -8,7 +8,6 @@ import { createServer } from 'http'
|
|
|
8
8
|
import mongoose from 'mongoose'
|
|
9
9
|
import subscriptionServerFactory from './libs/subscription-server.js'
|
|
10
10
|
import { connect as redisConnect } from './libs/redis'
|
|
11
|
-
import notifications from './libs/notifications'
|
|
12
11
|
import config from './config'
|
|
13
12
|
import createApp from './app'
|
|
14
13
|
import { version } from './lerna.json'
|
|
@@ -16,8 +15,6 @@ import { version } from './lerna.json'
|
|
|
16
15
|
const redisConnectionSetup = async () => {
|
|
17
16
|
try {
|
|
18
17
|
await redisConnect(config.redis)
|
|
19
|
-
// start background tasks
|
|
20
|
-
notifications.initCron()
|
|
21
18
|
} catch (err) {
|
|
22
19
|
// eslint-disable-next-line no-console
|
|
23
20
|
console.error(err)
|