Skip to content

Commit

Permalink
add monk in favor of mongojs
Browse files Browse the repository at this point in the history
  • Loading branch information
mjlescano committed Jun 20, 2017
1 parent 3ea08a0 commit f5783c9
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 113 deletions.
2 changes: 2 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-process-exit */

const config = require('./lib/config')
const notifier = require('.')

Expand Down
15 changes: 3 additions & 12 deletions lib/db/connection.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
const MongoClient = require('mongodb').MongoClient
const Graceful = require('node-graceful')
const config = require('../config')
const db = require('.')

module.exports = new Promise((resolve, reject) => {
const url = config.get('mongoUrl')

MongoClient.connect(url, (err, conn) => {
if (err) return reject(err)
resolve(conn)

Graceful.on('exit', () => conn.close())
})
module.exports = db.then(() => {
return db._db
})
20 changes: 5 additions & 15 deletions lib/db/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
const mongojs = require('mongojs')
const connection = require('./connection')
const monk = require('monk')
const Graceful = require('node-graceful')
const config = require('../config')

module.exports = connection.then((conn) => {
return new Promise((resolve, reject) => {
const db = mongojs(conn, [
'comments',
'forums',
'tags',
'topics',
'users'
])
const db = module.exports = monk(config.get('mongoUrl'))

db.on('error', reject)
db.on('connect', () => resolve(db))
})
}).catch((err) => { throw err })
Graceful.on('exit', () => db.close())
21 changes: 7 additions & 14 deletions lib/jobs/lib/forgot-password.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@ const jobName = 'forgot-password'
const template = templates[jobName]

module.exports = Promise.all([
require('../../db'),
require('../../db').get('users'),
require('../../agenda')
]).then((results) => {
const db = results[0]
const agenda = results[1]

]).then(([users, agenda]) => {
agenda.define(jobName, (job, done) => {
const data = job.attrs.data

db.users.findOne({ email: data.to }, (err, user) => {
if (err) return done(err)

if (!user) {
return done(new Error(`User not found for email "${data.email}"`))
}
users.findOne({ email: data.to }).then((user) => {
if (!user) throw new Error(`User not found for email "${data.email}"`)

mailer.send({
return mailer.send({
to: utils.emailAddress(user),
html: template({
USER_NAME: user.firstName,
Expand All @@ -32,7 +25,7 @@ module.exports = Promise.all([
lang: user.locale
}),
subject: t(`templates.${jobName}.subject`)
}).then(done).catch(done)
})
})
}).then(done).catch(done)
})
}).catch((err) => { throw err })
34 changes: 14 additions & 20 deletions lib/jobs/lib/topic-published.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@ const jobName = 'topic-published'
const jobNameForSingleUser = 'topic-published-single-recipient'

module.exports = Promise.all([
require('../../db'),
require('../../db').get('users'),
require('../../agenda')
]).then(([db, agenda]) => {
]).then(([users, agenda]) => {
agenda.define(jobName, (job, done) => {
const data = job.attrs.data

db.users.find({ 'notifications.new-topic': true }, (err, users) => {
if (err) return done(err)

const workers = users.map((user) => {
return new Promise((resolve, reject) => {
agenda.now(jobNameForSingleUser, {
topic: data.topic,
url: data.url,
to: utils.emailAddress(user)
}, (err) => {
if (err) return reject(err)
resolve()
})
users.find({ 'notifications.new-topic': true }).each((user) => {
return new Promise((resolve, reject) => {
agenda.now(jobNameForSingleUser, {
topic: data.topic,
url: data.url,
to: utils.emailAddress(user)
}, (err) => {
if (err) return reject(err)
resolve()
})
})

Promise.all(workers)
.then(() => { done() })
.catch(done)
})
}).then(() => {
done()
}).catch(done)
})
}).catch((err) => { throw err })
32 changes: 15 additions & 17 deletions lib/jobs/lib/welcome-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@ const jobName = 'welcome-email'
const template = templates[jobName]

module.exports = Promise.all([
require('../../db'),
require('../../db').get('users'),
require('../../agenda')
]).then(([db, agenda]) => {
]).then(([users, agenda]) => {
agenda.define(jobName, { priority: 'high' }, welcomeEmailJob)
agenda.define('signup', { priority: 'high' }, welcomeEmailJob)
agenda.define('resend-validation', { priority: 'high' }, welcomeEmailJob)

function welcomeEmailJob (job, done) {
const data = job.attrs.data

db.users.findOne({ email: data.to }, (err, user) => {
if (err) return done(err)
users.findOne({ email: data.to }).then((user) => {
if (!user) throw new Error(`User not found for email "${data.email}"`)

if (!user) {
return done(new Error(`User not found for email "${data.email}"`))
}
const html = template({
USER_NAME: user.firstName,
VALIDATE_MAIL_URL: data.validateUrl
}, {
lang: user.locale
})

mailer.send({
return mailer.send({
to: utils.emailAddress(user),
html: template({
USER_NAME: user.firstName,
VALIDATE_MAIL_URL: data.validateUrl
}, {
lang: user.locale
}),
subject: t(`templates.${jobName}.subject`)
}).then(done).catch(done)
})
subject: t(`templates.${jobName}.subject`),
html
})
}).catch(done)
}
}).catch((err) => { throw err })
75 changes: 41 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"defaults-deep": "0.2.3",
"es6-string-html-template": "~1.0.2",
"mongodb": "~2.2.28",
"mongojs": "2.4.0",
"monk": "~6.0.0",
"node-graceful": "~0.2.3",
"nodemailer": "~2.7.2",
"require-all": "~2.1.0",
Expand Down

0 comments on commit f5783c9

Please sign in to comment.