prettier: convert app/js decaffeinated files to Prettier format

This commit is contained in:
Tim Alby 2020-01-13 20:00:49 +01:00
parent 9835481cbf
commit dc218cd503
3 changed files with 283 additions and 203 deletions

View file

@ -11,79 +11,112 @@
* DS207: Consider shorter variations of null checks * DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { ObjectId } = require("mongojs"); const { ObjectId } = require('mongojs')
const request = require("request"); const request = require('request')
const async = require("async"); const async = require('async')
const _ = require("underscore"); const _ = require('underscore')
const settings = require("settings-sharelatex"); const settings = require('settings-sharelatex')
const { port } = settings.internal.notifications; const { port } = settings.internal.notifications
const logger = require("logger-sharelatex"); const logger = require('logger-sharelatex')
const mongojs = require('mongojs'); const mongojs = require('mongojs')
const Settings = require('settings-sharelatex'); const Settings = require('settings-sharelatex')
const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, ['notifications']); const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, [
'notifications'
])
module.exports = { module.exports = {
check(callback){ check(callback) {
const user_id = ObjectId(); const user_id = ObjectId()
const cleanupNotifications = callback=> db.notifications.remove({user_id}, callback); const cleanupNotifications = callback =>
db.notifications.remove({ user_id }, callback)
let notification_key = `smoke-test-notification-${ObjectId()}`; let notification_key = `smoke-test-notification-${ObjectId()}`
const getOpts = endPath=> ({url:`http://localhost:${port}/user/${user_id}${endPath}`, timeout:5000}); const getOpts = endPath => ({
logger.log({user_id, opts:getOpts(), key:notification_key, user_id}, "Health Check: running"); url: `http://localhost:${port}/user/${user_id}${endPath}`,
timeout: 5000
})
logger.log(
{ user_id, opts: getOpts(), key: notification_key, user_id },
'Health Check: running'
)
const jobs = [ const jobs = [
function(cb){ function(cb) {
const opts = getOpts("/"); const opts = getOpts('/')
opts.json = {key: notification_key, messageOpts:'', templateKey:'f4g5', user_id}; opts.json = {
return request.post(opts, cb); key: notification_key,
messageOpts: '',
templateKey: 'f4g5',
user_id
}
return request.post(opts, cb)
}, },
function(cb){ function(cb) {
const opts = getOpts("/"); const opts = getOpts('/')
opts.json = true; opts.json = true
return request.get(opts, function(err, res, body){ return request.get(opts, function(err, res, body) {
if (err != null) { if (err != null) {
logger.err({err}, "Health Check: error getting notification"); logger.err({ err }, 'Health Check: error getting notification')
return callback(err); return callback(err)
} else if (res.statusCode !== 200) { } else if (res.statusCode !== 200) {
const e = `status code not 200 ${res.statusCode}`; const e = `status code not 200 ${res.statusCode}`
logger.err({err}, e); logger.err({ err }, e)
return cb(e); return cb(e)
} }
const hasNotification = _.some(body, notification=> (notification.key === notification_key) && (notification.user_id === user_id.toString())); const hasNotification = _.some(
body,
notification =>
notification.key === notification_key &&
notification.user_id === user_id.toString()
)
if (hasNotification) { if (hasNotification) {
return cb(null, body); return cb(null, body)
} else { } else {
logger.err({body, notification_key}, "Health Check: notification not in response"); logger.err(
return cb("notification not found in response"); { body, notification_key },
'Health Check: notification not in response'
)
return cb('notification not found in response')
} }
}); })
} }
]; ]
return async.series(jobs, function(err, body){ return async.series(jobs, function(err, body) {
if (err != null) { if (err != null) {
logger.err({err}, "Health Check: error running health check"); logger.err({ err }, 'Health Check: error running health check')
return cleanupNotifications(() => callback(err)); return cleanupNotifications(() => callback(err))
} else { } else {
const notification_id = body[1][0]._id; const notification_id = body[1][0]._id
notification_key = body[1][0].key; notification_key = body[1][0].key
let opts = getOpts(`/notification/${notification_id}`); let opts = getOpts(`/notification/${notification_id}`)
logger.log({notification_id, notification_key}, "Health Check: doing cleanup"); logger.log(
return request.del(opts, function(err, res, body){ { notification_id, notification_key },
'Health Check: doing cleanup'
)
return request.del(opts, function(err, res, body) {
if (err != null) { if (err != null) {
logger.err(err, opts, "Health Check: error cleaning up notification"); logger.err(
return callback(err); err,
opts,
'Health Check: error cleaning up notification'
)
return callback(err)
} }
opts = getOpts(""); opts = getOpts('')
opts.json = {key: notification_key}; opts.json = { key: notification_key }
return request.del(opts, function(err, res, body){ return request.del(opts, function(err, res, body) {
if (err != null) { if (err != null) {
logger.err(err, opts, "Health Check: error cleaning up notification"); logger.err(
return callback(err); err,
opts,
'Health Check: error cleaning up notification'
)
return callback(err)
} }
return cleanupNotifications(callback); return cleanupNotifications(callback)
}); })
}); })
} }
}); })
} }
}; }

View file

@ -11,104 +11,120 @@
* DS207: Consider shorter variations of null checks * DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
let Notifications; let Notifications
const Settings = require('settings-sharelatex'); const Settings = require('settings-sharelatex')
const logger = require('logger-sharelatex'); const logger = require('logger-sharelatex')
const mongojs = require('mongojs'); const mongojs = require('mongojs')
const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, ['notifications']); const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, [
const { ObjectId } = require("mongojs"); 'notifications'
const metrics = require('metrics-sharelatex'); ])
const { ObjectId } = require('mongojs')
const metrics = require('metrics-sharelatex')
module.exports = (Notifications = { module.exports = Notifications = {
getUserNotifications(user_id, callback) {
getUserNotifications(user_id, callback){ if (callback == null) {
if (callback == null) { callback = function(err, notifications){}; } callback = function(err, notifications) {}
}
const query = { const query = {
user_id: ObjectId(user_id), user_id: ObjectId(user_id),
templateKey: {"$exists":true} templateKey: { $exists: true }
}; }
return db.notifications.find(query, (err, notifications)=> callback(err, notifications)); return db.notifications.find(query, (err, notifications) =>
callback(err, notifications)
)
}, },
_countExistingNotifications(user_id, notification, callback) {
_countExistingNotifications(user_id, notification, callback){ if (callback == null) {
if (callback == null) { callback = function(err, count){}; } callback = function(err, count) {}
}
const query = { const query = {
user_id: ObjectId(user_id), user_id: ObjectId(user_id),
key: notification.key key: notification.key
}; }
return db.notifications.count(query, function(err, count){ return db.notifications.count(query, function(err, count) {
if (err != null) { return callback(err); } if (err != null) {
return callback(null, count); return callback(err)
}); }
return callback(null, count)
})
}, },
addNotification(user_id, notification, callback){ addNotification(user_id, notification, callback) {
return this._countExistingNotifications(user_id, notification, function(err, count){ return this._countExistingNotifications(user_id, notification, function(
if (err != null) { return callback(err); } err,
if ((count !== 0) && !notification.forceCreate) { return callback(); } count
) {
if (err != null) {
return callback(err)
}
if (count !== 0 && !notification.forceCreate) {
return callback()
}
const doc = { const doc = {
user_id: ObjectId(user_id), user_id: ObjectId(user_id),
key: notification.key, key: notification.key,
messageOpts: notification.messageOpts, messageOpts: notification.messageOpts,
templateKey: notification.templateKey templateKey: notification.templateKey
}; }
// TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to // TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
// a datetime in the future when the document should be automatically removed. // a datetime in the future when the document should be automatically removed.
// in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing // in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
// see `README.md` for instruction on creating TTL index // see `README.md` for instruction on creating TTL index
if (notification.expires != null) { if (notification.expires != null) {
try { try {
doc.expires = new Date(notification.expires); doc.expires = new Date(notification.expires)
const _testValue = doc.expires.toISOString(); const _testValue = doc.expires.toISOString()
} catch (error) { } catch (error) {
err = error; err = error
logger.error({user_id, expires: notification.expires}, "error converting `expires` field to Date"); logger.error(
return callback(err); { user_id, expires: notification.expires },
'error converting `expires` field to Date'
)
return callback(err)
} }
} }
return db.notifications.update({user_id: doc.user_id, key: notification.key}, doc, {upsert: true}, callback); return db.notifications.update(
}); { user_id: doc.user_id, key: notification.key },
doc,
{ upsert: true },
callback
)
})
}, },
removeNotificationId(user_id, notification_id, callback){ removeNotificationId(user_id, notification_id, callback) {
const searchOps = { const searchOps = {
user_id:ObjectId(user_id), user_id: ObjectId(user_id),
_id:ObjectId(notification_id) _id: ObjectId(notification_id)
}; }
const updateOperation = const updateOperation = { $unset: { templateKey: true, messageOpts: true } }
{"$unset": {templateKey:true, messageOpts: true}}; return db.notifications.update(searchOps, updateOperation, callback)
return db.notifications.update(searchOps, updateOperation, callback);
}, },
removeNotificationKey(user_id, notification_key, callback){ removeNotificationKey(user_id, notification_key, callback) {
const searchOps = { const searchOps = {
user_id:ObjectId(user_id), user_id: ObjectId(user_id),
key: notification_key key: notification_key
}; }
const updateOperation = const updateOperation = { $unset: { templateKey: true } }
{"$unset": {templateKey:true}}; return db.notifications.update(searchOps, updateOperation, callback)
return db.notifications.update(searchOps, updateOperation, callback);
}, },
removeNotificationByKeyOnly(notification_key, callback){ removeNotificationByKeyOnly(notification_key, callback) {
const searchOps = const searchOps = { key: notification_key }
{key: notification_key}; const updateOperation = { $unset: { templateKey: true } }
const updateOperation = return db.notifications.update(searchOps, updateOperation, callback)
{"$unset": {templateKey:true}};
return db.notifications.update(searchOps, updateOperation, callback);
}, },
// hard delete of doc, rather than removing the templateKey // hard delete of doc, rather than removing the templateKey
deleteNotificationByKeyOnly(notification_key, callback){ deleteNotificationByKeyOnly(notification_key, callback) {
const searchOps = const searchOps = { key: notification_key }
{key: notification_key}; return db.notifications.remove(searchOps, { justOne: true }, callback)
return db.notifications.remove(searchOps, {justOne: true}, callback);
} }
}); }
;['getUserNotifications', 'addNotification'].map(method =>
[ metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)
'getUserNotifications', )
'addNotification'
].map(method => metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger));

View file

@ -10,46 +10,77 @@
* DS207: Consider shorter variations of null checks * DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const Notifications = require("./Notifications"); const Notifications = require('./Notifications')
const logger = require("logger-sharelatex"); const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex'); const metrics = require('metrics-sharelatex')
module.exports = { module.exports = {
getUserNotifications(req, res) {
getUserNotifications(req, res){ logger.log(
logger.log({user_id: req.params.user_id}, "getting user unread notifications"); { user_id: req.params.user_id },
metrics.inc("getUserNotifications"); 'getting user unread notifications'
return Notifications.getUserNotifications(req.params.user_id, (err, notifications)=> res.json(notifications)); )
metrics.inc('getUserNotifications')
return Notifications.getUserNotifications(
req.params.user_id,
(err, notifications) => res.json(notifications)
)
}, },
addNotification(req, res){ addNotification(req, res) {
logger.log({user_id: req.params.user_id, notification:req.body}, "adding notification"); logger.log(
metrics.inc("addNotification"); { user_id: req.params.user_id, notification: req.body },
return Notifications.addNotification(req.params.user_id, req.body, function(err, notifications){ 'adding notification'
)
metrics.inc('addNotification')
return Notifications.addNotification(req.params.user_id, req.body, function(
err,
notifications
) {
if (err != null) { if (err != null) {
return res.send(500); return res.send(500)
} else { } else {
return res.send(); return res.send()
} }
}); })
}, },
removeNotificationId(req, res){ removeNotificationId(req, res) {
logger.log({user_id: req.params.user_id, notification_id: req.params.notification_id}, "mark id notification as read"); logger.log(
metrics.inc("removeNotificationId"); {
return Notifications.removeNotificationId(req.params.user_id, req.params.notification_id, (err, notifications)=> res.send()); user_id: req.params.user_id,
notification_id: req.params.notification_id
},
'mark id notification as read'
)
metrics.inc('removeNotificationId')
return Notifications.removeNotificationId(
req.params.user_id,
req.params.notification_id,
(err, notifications) => res.send()
)
}, },
removeNotificationKey(req, res){ removeNotificationKey(req, res) {
logger.log({user_id: req.params.user_id, notification_key: req.body.key}, "mark key notification as read"); logger.log(
metrics.inc("removeNotificationKey"); { user_id: req.params.user_id, notification_key: req.body.key },
return Notifications.removeNotificationKey(req.params.user_id, req.body.key, (err, notifications)=> res.send()); 'mark key notification as read'
)
metrics.inc('removeNotificationKey')
return Notifications.removeNotificationKey(
req.params.user_id,
req.body.key,
(err, notifications) => res.send()
)
}, },
removeNotificationByKeyOnly(req, res){ removeNotificationByKeyOnly(req, res) {
const notification_key = req.params.key; const notification_key = req.params.key
logger.log({notification_key}, "mark notification as read by key only"); logger.log({ notification_key }, 'mark notification as read by key only')
metrics.inc("removeNotificationKey"); metrics.inc('removeNotificationKey')
return Notifications.removeNotificationByKeyOnly(notification_key, (err, notifications)=> res.send()); return Notifications.removeNotificationByKeyOnly(
notification_key,
(err, notifications) => res.send()
)
} }
}; }