Skip to content

Commit

Permalink
chore(core): code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed May 7, 2022
1 parent a4bc5da commit bf51da2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sonar-project.properties
public/uploads/assets/*.*
/src/backup/bin/
backups/
restores/

public/uploads/assets/upload/

Expand Down
16 changes: 8 additions & 8 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* Copyright (c) 2014-2019. All rights reserved.
*/

var nconf = require('nconf')
var mongoose = require('mongoose')
var winston = require('../logger')
const nconf = require('nconf')
const mongoose = require('mongoose')
const winston = require('../logger')

var db = {}
var mongoConnectionUri = {
const db = {}
const mongoConnectionUri = {
server: process.env.TD_MONGODB_SERVER || nconf.get('mongo:host'),
port: process.env.TD_MONGODB_PORT || nconf.get('mongo:port') || '27017',
username: process.env.TD_MONGODB_USERNAME || nconf.get('mongo:username'),
Expand All @@ -26,7 +26,7 @@ var mongoConnectionUri = {
shard: process.env.TD_MONGODB_SHARD || nconf.get('mongo:shard')
}

var CONNECTION_URI = ''
let CONNECTION_URI = ''
if (!mongoConnectionUri.username) {
CONNECTION_URI =
'mongodb://' + mongoConnectionUri.server + ':' + mongoConnectionUri.port + '/' + mongoConnectionUri.database
Expand Down Expand Up @@ -60,12 +60,12 @@ if (!mongoConnectionUri.username) {

if (process.env.TD_MONGODB_URI) CONNECTION_URI = process.env.TD_MONGODB_URI

var options = {
let options = {
keepAlive: 1,
connectTimeoutMS: 30000
}

module.exports.init = function (callback, connectionString, opts) {
module.exports.init = async function (callback, connectionString, opts) {
if (connectionString) CONNECTION_URI = connectionString
if (opts) options = opts
options.dbName = mongoConnectionUri.database
Expand Down
87 changes: 39 additions & 48 deletions src/emitter/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,19 @@
* Copyright (c) 2014-2019. All rights reserved.
*/

var _ = require('lodash')
var path = require('path')
var async = require('async')
var winston = require('../logger')
var emitter = require('../emitter')
var util = require('../helpers/utils')
var templateSchema = require('../models/template')
var ticketSchema = require('../models/ticket')
var userSchema = require('../models/user')
var departmentSchema = require('../models/department')
var NotificationSchema = require('../models/notification')
var settingsSchema = require('../models/setting')
var Email = require('email-templates')
var templateDir = path.resolve(__dirname, '..', 'mailer', 'templates')
var permissions = require('../permissions')

var socketUtils = require('../helpers/utils')
var sharedVars = require('../socketio/index').shared

var notifications = require('../notifications') // Load Push Events

var eventTicketCreated = require('./events/event_ticket_created')
const _ = require('lodash')
const path = require('path')
const async = require('async')
const winston = require('../logger')
const emitter = require('../emitter')
const NotificationSchema = require('../models/notification')
const settingsSchema = require('../models/setting')
const Email = require('email-templates')
const templateDir = path.resolve(__dirname, '..', 'mailer', 'templates')

const notifications = require('../notifications') // Load Push Events

const eventTicketCreated = require('./events/event_ticket_created')

;(function () {
notifications.init(emitter)
Expand All @@ -43,12 +34,12 @@ var eventTicketCreated = require('./events/event_ticket_created')
})

function sendPushNotification (tpsObj, data) {
var tpsEnabled = tpsObj.tpsEnabled
var tpsUsername = tpsObj.tpsUsername
var tpsApiKey = tpsObj.tpsApiKey
var hostname = tpsObj.hostname
var ticket = data.ticket
var message = data.message
const tpsEnabled = tpsObj.tpsEnabled
const tpsUsername = tpsObj.tpsUsername
const tpsApiKey = tpsObj.tpsApiKey
const hostname = tpsObj.hostname
let ticket = data.ticket
const message = data.message

if (!tpsEnabled || !tpsUsername || !tpsApiKey) {
winston.debug('Warn: TPS - Push Service Not Enabled')
Expand All @@ -66,9 +57,9 @@ var eventTicketCreated = require('./events/event_ticket_created')
// 3 - Ticket Note Added
// 4 - Ticket Assignee Set
// - Message
var title
var users = []
var content
let title
let users = []
let content, comment, assigneeId, ticketUid
switch (data.type) {
case 1:
title = 'Ticket #' + ticket.uid + ' Created'
Expand All @@ -80,7 +71,7 @@ var eventTicketCreated = require('./events/event_ticket_created')
case 2:
title = 'Ticket #' + ticket.uid + ' Updated'
content = _.last(ticket.history).description
var comment = _.last(ticket.comments)
comment = _.last(ticket.comments)
users = _.compact(
_.map(ticket.subscribers, function (o) {
if (comment.owner._id.toString() !== o._id.toString()) {
Expand All @@ -93,8 +84,8 @@ var eventTicketCreated = require('./events/event_ticket_created')
title = message.owner.fullname + ' sent you a message'
break
case 4:
var assigneeId = data.assigneeId
var ticketUid = data.ticketUid
assigneeId = data.assigneeId
ticketUid = data.ticketUid
ticket = {}
ticket._id = data.ticketId
ticket.uid = data.ticketUid
Expand All @@ -111,7 +102,7 @@ var eventTicketCreated = require('./events/event_ticket_created')
return
}

var n = {
const n = {
title: title,
data: {
ticketId: ticket._id,
Expand Down Expand Up @@ -151,10 +142,10 @@ var eventTicketCreated = require('./events/event_ticket_created')
) {
if (err) return false

var tpsEnabled = _.head(_.filter(tpsSettings, ['name', 'tps:enable']))
var tpsUsername = _.head(_.filter(tpsSettings, ['name', 'tps:username']))
var tpsApiKey = _.head(_.filter(tpsSettings), ['name', 'tps:apikey'])
var mailerEnabled = _.head(_.filter(tpsSettings), ['name', 'mailer:enable'])
let tpsEnabled = _.head(_.filter(tpsSettings, ['name', 'tps:enable']))
let tpsUsername = _.head(_.filter(tpsSettings, ['name', 'tps:username']))
let tpsApiKey = _.head(_.filter(tpsSettings), ['name', 'tps:apikey'])
let mailerEnabled = _.head(_.filter(tpsSettings), ['name', 'mailer:enable'])
mailerEnabled = !mailerEnabled ? false : mailerEnabled.value

if (!tpsEnabled || !tpsUsername || !tpsApiKey) {
Expand All @@ -172,7 +163,7 @@ var eventTicketCreated = require('./events/event_ticket_created')
if (!_.isUndefined(ticket.assignee) && ticket.assignee._id.toString() === comment.owner.toString())
return cb

var notification = new NotificationSchema({
const notification = new NotificationSchema({
owner: ticket.owner,
title: 'Comment Added to Ticket#' + ticket.uid,
message: ticket.subject,
Expand All @@ -190,7 +181,7 @@ var eventTicketCreated = require('./events/event_ticket_created')
if (ticket.assignee._id.toString() === comment.owner.toString()) return cb
if (ticket.owner._id.toString() === ticket.assignee._id.toString()) return cb()

var notification = new NotificationSchema({
const notification = new NotificationSchema({
owner: ticket.assignee,
title: 'Comment Added to Ticket#' + ticket.uid,
message: ticket.subject,
Expand Down Expand Up @@ -219,8 +210,8 @@ var eventTicketCreated = require('./events/event_ticket_created')
function (c) {
if (!mailerEnabled) return c()

var mailer = require('../mailer')
var emails = []
const mailer = require('../mailer')
let emails = []
async.each(
ticket.subscribers,
function (member, cb) {
Expand All @@ -241,7 +232,7 @@ var eventTicketCreated = require('./events/event_ticket_created')
return c()
}

var email = new Email({
const email = new Email({
views: {
root: templateDir,
options: {
Expand All @@ -262,7 +253,7 @@ var eventTicketCreated = require('./events/event_ticket_created')
comment: comment
})
.then(function (html) {
var mailOptions = {
const mailOptions = {
to: emails.join(),
subject: 'Updated: Ticket #' + ticket.uid + '-' + ticket.subject,
html: html,
Expand Down Expand Up @@ -297,9 +288,9 @@ var eventTicketCreated = require('./events/event_ticket_created')
settingsSchema.getSettingsByName(['tps:enable', 'tps:username', 'tps:apikey'], function (err, tpsSettings) {
if (err) return false

var tpsEnabled = _.head(_.filter(tpsSettings, ['name', 'tps:enable']))
var tpsUsername = _.head(_.filter(tpsSettings, ['name', 'tps:username']))
var tpsApiKey = _.head(_.filter(tpsSettings), ['name', 'tps:apikey'])
let tpsEnabled = _.head(_.filter(tpsSettings, ['name', 'tps:enable']))
let tpsUsername = _.head(_.filter(tpsSettings, ['name', 'tps:username']))
let tpsApiKey = _.head(_.filter(tpsSettings), ['name', 'tps:apikey'])

if (!tpsEnabled || !tpsUsername || !tpsApiKey) {
tpsEnabled = false
Expand Down
8 changes: 4 additions & 4 deletions src/emitter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

'use strict'

var eventEmitter = new (require('events')).EventEmitter()
const eventEmitter = new (require('events').EventEmitter)()

eventEmitter.all = function (events, callback) {
var eventList = events.slice(0)
const eventList = events.slice(0)

function onEvent (event) {
eventEmitter.on(events[event], function () {
Expand All @@ -29,8 +29,8 @@ eventEmitter.all = function (events, callback) {
})
}

for (var ev in events) {
if (events.hasOwnProperty(ev)) {
for (const ev in events) {
if (Object.hasOwn(events, ev)) {
onEvent(ev)
}
}
Expand Down
40 changes: 20 additions & 20 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
========================================================================
**/

var express = require('express')
var router = express.Router()
var controllers = require('../controllers')
var path = require('path')
var winston = require('winston')
var packagejson = require('../../package.json')
const express = require('express')
const router = express.Router()
const controllers = require('../controllers')
const path = require('path')
const winston = require('winston')
const packagejson = require('../../package.json')

function mainRoutes (router, middleware, controllers) {
router.get('/', middleware.redirectToDashboardIfLoggedIn, controllers.main.index)
Expand Down Expand Up @@ -44,8 +44,8 @@ function mainRoutes (router, middleware, controllers) {
router.get('/about', middleware.redirectToLogin, middleware.loadCommonData, controllers.main.about)

router.get('/captcha', function (req, res) {
var svgCaptcha = require('svg-captcha')
var captcha = svgCaptcha.create()
const svgCaptcha = require('svg-captcha')
const captcha = svgCaptcha.create()
req.session.captcha = captcha.text
res.set('Content-Type', 'image/svg+xml')
res.send(captcha.data)
Expand All @@ -57,8 +57,8 @@ function mainRoutes (router, middleware, controllers) {
router.get('/signup', controllers.accounts.signup)

router.get('/logoimage', function (req, res) {
var s = require('../models/setting')
var _ = require('lodash')
const s = require('../models/setting')
const _ = require('lodash')
s.getSettingByName('gen:customlogo', function (err, hasCustomLogo) {
if (!err && hasCustomLogo && hasCustomLogo.value) {
s.getSettingByName('gen:customlogofilename', function (err, logoFilename) {
Expand Down Expand Up @@ -368,7 +368,7 @@ function mainRoutes (router, middleware, controllers) {
router.get('/api/v1/admin/restart', middleware.api, middleware.isAdmin, function (req, res) {
if (process.env.DISABLE_RESTART) return res.json({ success: true })

var pm2 = require('pm2')
const pm2 = require('pm2')
pm2.connect(function (err) {
if (err) {
winston.error(err)
Expand All @@ -391,23 +391,23 @@ function mainRoutes (router, middleware, controllers) {
router.get('/debug/populatedb', controllers.debug.populatedatabase)
router.get('/debug/sendmail', controllers.debug.sendmail)
router.get('/debug/mailcheck/refetch', function (req, res) {
var mailCheck = require('../mailer/mailCheck')
const mailCheck = require('../mailer/mailCheck')
mailCheck.refetch()
res.send('OK')
})

router.get('/debug/cache/refresh', function (req, res) {
var _ = require('lodash')
const _ = require('lodash')

var forkProcess = _.find(global.forks, { name: 'cache' })
const forkProcess = _.find(global.forks, { name: 'cache' })
forkProcess.fork.send({ name: 'cache:refresh' })

res.send('OK')
})

router.get('/debug/restart', function (req, res) {
if (process.env.DISABLE_RESTART) return res.send('RESTART DISABLED')
var pm2 = require('pm2')
const pm2 = require('pm2')
pm2.connect(function (err) {
if (err) {
winston.error(err)
Expand All @@ -433,13 +433,13 @@ module.exports = function (app, middleware) {
app.use('/', router)

// Load Plugin routes
var dive = require('dive')
var fs = require('fs')
var pluginDir = path.join(__dirname, '../../plugins')
const dive = require('dive')
const fs = require('fs')
const pluginDir = path.join(__dirname, '../../plugins')
if (!fs.existsSync(pluginDir)) fs.mkdirSync(pluginDir)
dive(pluginDir, { directories: true, files: false, recursive: false }, function (err, dir) {
if (err) throw err
var pluginRoutes = require(path.join(dir, '/routes'))
const pluginRoutes = require(path.join(dir, '/routes'))
if (pluginRoutes) {
pluginRoutes(router, middleware)
} else {
Expand All @@ -452,7 +452,7 @@ module.exports = function (app, middleware) {
}

function handleErrors (err, req, res) {
var status = err.status || 500
const status = err.status || 500
res.status(err.status)

if (status === 500) {
Expand Down

0 comments on commit bf51da2

Please sign in to comment.