2020-01-13 14:00:40 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-dupe-keys,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-01-13 14:00:33 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-08-31 05:12:37 -04:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2020-01-13 14:00:49 -05:00
|
|
|
const request = require('request')
|
|
|
|
const async = require('async')
|
|
|
|
const _ = require('underscore')
|
2021-07-12 12:47:16 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-01-13 14:00:49 -05:00
|
|
|
const { port } = settings.internal.notifications
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2020-01-13 14:00:49 -05:00
|
|
|
module.exports = {
|
|
|
|
check(callback) {
|
|
|
|
const user_id = ObjectId()
|
2021-07-13 07:04:44 -04:00
|
|
|
const cleanupNotifications = callback =>
|
2020-01-13 14:00:49 -05:00
|
|
|
db.notifications.remove({ user_id }, callback)
|
2016-06-01 09:57:06 -04:00
|
|
|
|
2020-01-13 14:00:49 -05:00
|
|
|
let notification_key = `smoke-test-notification-${ObjectId()}`
|
2021-07-13 07:04:44 -04:00
|
|
|
const getOpts = endPath => ({
|
2020-01-13 14:00:49 -05:00
|
|
|
url: `http://localhost:${port}/user/${user_id}${endPath}`,
|
2021-07-13 07:04:44 -04:00
|
|
|
timeout: 5000,
|
2020-01-13 14:00:49 -05:00
|
|
|
})
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2020-01-13 14:00:49 -05:00
|
|
|
{ user_id, opts: getOpts(), key: notification_key, user_id },
|
|
|
|
'Health Check: running'
|
|
|
|
)
|
|
|
|
const jobs = [
|
2020-06-04 03:50:05 -04:00
|
|
|
function (cb) {
|
2020-01-13 14:00:49 -05:00
|
|
|
const opts = getOpts('/')
|
|
|
|
opts.json = {
|
|
|
|
key: notification_key,
|
|
|
|
messageOpts: '',
|
|
|
|
templateKey: 'f4g5',
|
2021-07-13 07:04:44 -04:00
|
|
|
user_id,
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
|
|
|
return request.post(opts, cb)
|
|
|
|
},
|
2020-06-04 03:50:05 -04:00
|
|
|
function (cb) {
|
2020-01-13 14:00:49 -05:00
|
|
|
const opts = getOpts('/')
|
|
|
|
opts.json = true
|
2020-06-04 03:50:05 -04:00
|
|
|
return request.get(opts, function (err, res, body) {
|
2020-01-13 14:00:49 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'Health Check: error getting notification')
|
|
|
|
return callback(err)
|
|
|
|
} else if (res.statusCode !== 200) {
|
|
|
|
const e = `status code not 200 ${res.statusCode}`
|
|
|
|
logger.err({ err }, e)
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
const hasNotification = _.some(
|
|
|
|
body,
|
2021-07-13 07:04:44 -04:00
|
|
|
notification =>
|
2020-01-13 14:00:49 -05:00
|
|
|
notification.key === notification_key &&
|
|
|
|
notification.user_id === user_id.toString()
|
|
|
|
)
|
|
|
|
if (hasNotification) {
|
|
|
|
return cb(null, body)
|
|
|
|
} else {
|
|
|
|
logger.err(
|
|
|
|
{ body, notification_key },
|
|
|
|
'Health Check: notification not in response'
|
|
|
|
)
|
2021-10-27 05:49:42 -04:00
|
|
|
return cb(new Error('notification not found in response'))
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:44 -04:00
|
|
|
},
|
2020-01-13 14:00:49 -05:00
|
|
|
]
|
2020-06-04 03:50:05 -04:00
|
|
|
return async.series(jobs, function (err, body) {
|
2020-01-13 14:00:49 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'Health Check: error running health check')
|
|
|
|
return cleanupNotifications(() => callback(err))
|
|
|
|
} else {
|
|
|
|
const notification_id = body[1][0]._id
|
|
|
|
notification_key = body[1][0].key
|
|
|
|
let opts = getOpts(`/notification/${notification_id}`)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2020-01-13 14:00:49 -05:00
|
|
|
{ notification_id, notification_key },
|
|
|
|
'Health Check: doing cleanup'
|
|
|
|
)
|
2020-06-04 03:50:05 -04:00
|
|
|
return request.del(opts, function (err, res, body) {
|
2020-01-13 14:00:49 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err(
|
|
|
|
err,
|
|
|
|
opts,
|
|
|
|
'Health Check: error cleaning up notification'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
opts = getOpts('')
|
|
|
|
opts.json = { key: notification_key }
|
2020-06-04 03:50:05 -04:00
|
|
|
return request.del(opts, function (err, res, body) {
|
2020-01-13 14:00:49 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err(
|
|
|
|
err,
|
|
|
|
opts,
|
|
|
|
'Health Check: error cleaning up notification'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return cleanupNotifications(callback)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:44 -04:00
|
|
|
},
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|