From c7f60fad91216858d2207a89f1e1e94fcf1c2e3b Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Thu, 20 Oct 2022 13:40:43 -0400 Subject: [PATCH] code cleanup, updated prettier rules --- bin/www.js | 6 ++---- bin/www.ts | 7 ++---- handlers/admin-get/cleanup.ts | 1 - handlers/admin-get/fees.ts | 5 +---- handlers/dashboard-get/dashboard.ts | 18 +++++++++------- handlers/lotOccupancies-get/search.ts | 8 ++----- handlers/lots-get/edit.js | 3 +-- handlers/lots-get/edit.ts | 3 +-- handlers/lots-get/next.js | 7 ++---- handlers/lots-get/next.ts | 7 ++---- handlers/lots-get/previous.js | 4 +--- handlers/lots-get/previous.ts | 4 +--- handlers/lots-get/view.js | 3 +-- handlers/lots-get/view.ts | 3 +-- handlers/maps-get/new.ts | 4 +--- handlers/reports-get/reportName.js | 9 +++----- handlers/reports-get/reportName.ts | 14 ++++-------- handlers/workOrders-get/new.ts | 5 +---- handlers/workOrders-get/search.ts | 1 - helpers/functions.authentication.js | 5 +++-- helpers/functions.authentication.ts | 31 ++++++++++----------------- helpers/functions.lotOccupancy.ts | 4 ++-- 22 files changed, 52 insertions(+), 100 deletions(-) diff --git a/bin/www.js b/bin/www.js index a8d4fb38..42a20a73 100644 --- a/bin/www.js +++ b/bin/www.js @@ -1,5 +1,5 @@ import { app } from "../app.js"; -import http from "http"; +import http from "node:http"; import * as configFunctions from "../helpers/functions.config.js"; import exitHook from "exit-hook"; import debug from "debug"; @@ -22,9 +22,7 @@ const onError = (error) => { }; const onListening = (server) => { const addr = server.address(); - const bind = typeof addr === "string" - ? "pipe " + addr - : "port " + addr.port.toString(); + const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port.toString(); debugWWW("Listening on " + bind); }; const httpPort = configFunctions.getProperty("application.httpPort"); diff --git a/bin/www.ts b/bin/www.ts index d88b96dd..c3682c03 100644 --- a/bin/www.ts +++ b/bin/www.ts @@ -2,7 +2,7 @@ import { app } from "../app.js"; -import http from "http"; +import http from "node:http"; import * as configFunctions from "../helpers/functions.config.js"; @@ -46,10 +46,7 @@ const onError = (error: ServerError) => { const onListening = (server: http.Server) => { const addr = server.address(); - const bind = - typeof addr === "string" - ? "pipe " + addr - : "port " + addr.port.toString(); + const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port.toString(); debugWWW("Listening on " + bind); }; diff --git a/handlers/admin-get/cleanup.ts b/handlers/admin-get/cleanup.ts index fc4a8e86..56df9ce5 100644 --- a/handlers/admin-get/cleanup.ts +++ b/handlers/admin-get/cleanup.ts @@ -1,7 +1,6 @@ import type { RequestHandler } from "express"; export const handler: RequestHandler = (_request, response) => { - response.render("admin-cleanup", { headTitle: "Database Cleanup" }); diff --git a/handlers/admin-get/fees.ts b/handlers/admin-get/fees.ts index eef189c1..a44b78c9 100644 --- a/handlers/admin-get/fees.ts +++ b/handlers/admin-get/fees.ts @@ -1,9 +1,6 @@ import type { RequestHandler } from "express"; -import { - getLotTypes, - getOccupancyTypes -} from "../../helpers/functions.cache.js"; +import { getLotTypes, getOccupancyTypes } from "../../helpers/functions.cache.js"; import { getFeeCategories } from "../../helpers/lotOccupancyDB/getFeeCategories.js"; diff --git a/handlers/dashboard-get/dashboard.ts b/handlers/dashboard-get/dashboard.ts index a67db931..3e1e48d7 100644 --- a/handlers/dashboard-get/dashboard.ts +++ b/handlers/dashboard-get/dashboard.ts @@ -3,10 +3,9 @@ import type { RequestHandler } from "express"; import { dateToString } from "@cityssm/expressjs-server-js/dateTimeFns.js"; import { getWorkOrderMilestones } from "../../helpers/lotOccupancyDB/getWorkOrderMilestones.js"; -import {getWorkOrders} from "../../helpers/lotOccupancyDB/getWorkOrders.js"; +import { getWorkOrders } from "../../helpers/lotOccupancyDB/getWorkOrders.js"; export const handler: RequestHandler = (_request, response) => { - const currentDateString = dateToString(new Date()); const workOrderMilestones = getWorkOrderMilestones( @@ -20,12 +19,15 @@ export const handler: RequestHandler = (_request, response) => { } ); - const workOrderCount = getWorkOrders({ - workOrderOpenDateString: currentDateString - }, { - limit: 1, - offset: 0 - }).count; + const workOrderCount = getWorkOrders( + { + workOrderOpenDateString: currentDateString + }, + { + limit: 1, + offset: 0 + } + ).count; response.render("dashboard", { headTitle: "Dashboard", diff --git a/handlers/lotOccupancies-get/search.ts b/handlers/lotOccupancies-get/search.ts index cf2e205b..2a3af308 100644 --- a/handlers/lotOccupancies-get/search.ts +++ b/handlers/lotOccupancies-get/search.ts @@ -4,10 +4,7 @@ import * as configFunctions from "../../helpers/functions.config.js"; import { getMaps } from "../../helpers/lotOccupancyDB/getMaps.js"; -import { - getLotTypes, - getOccupancyTypes -} from "../../helpers/functions.cache.js"; +import { getLotTypes, getOccupancyTypes } from "../../helpers/functions.cache.js"; export const handler: RequestHandler = (request, response) => { const maps = getMaps(); @@ -15,8 +12,7 @@ export const handler: RequestHandler = (request, response) => { const occupancyTypes = getOccupancyTypes(); response.render("lotOccupancy-search", { - headTitle: - configFunctions.getProperty("aliases.lot") + " Occupancy Search", + headTitle: configFunctions.getProperty("aliases.lot") + " Occupancy Search", maps, lotTypes, occupancyTypes, diff --git a/handlers/lots-get/edit.js b/handlers/lots-get/edit.js index b1abc8b2..3e783da0 100644 --- a/handlers/lots-get/edit.js +++ b/handlers/lots-get/edit.js @@ -5,8 +5,7 @@ import * as cacheFunctions from "../../helpers/functions.cache.js"; export const handler = (request, response) => { const lot = getLot(request.params.lotId); if (!lot) { - return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/?error=lotIdNotFound"); + return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/?error=lotIdNotFound"); } const maps = getMaps(); const lotTypes = cacheFunctions.getLotTypes(); diff --git a/handlers/lots-get/edit.ts b/handlers/lots-get/edit.ts index 0882b05a..9f8db751 100644 --- a/handlers/lots-get/edit.ts +++ b/handlers/lots-get/edit.ts @@ -11,8 +11,7 @@ export const handler: RequestHandler = (request, response) => { if (!lot) { return response.redirect( - configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/?error=lotIdNotFound" + configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/?error=lotIdNotFound" ); } diff --git a/handlers/lots-get/next.js b/handlers/lots-get/next.js index 52750f53..a90e2d1e 100644 --- a/handlers/lots-get/next.js +++ b/handlers/lots-get/next.js @@ -4,11 +4,8 @@ export const handler = (request, response) => { const lotId = request.params.lotId; const nextLotId = getNextLotId(lotId); if (!nextLotId) { - return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/?error=noNextLotIdFound"); + return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/?error=noNextLotIdFound"); } - return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/" + - nextLotId); + return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/" + nextLotId); }; export default handler; diff --git a/handlers/lots-get/next.ts b/handlers/lots-get/next.ts index a9b82b16..60bdd38e 100644 --- a/handlers/lots-get/next.ts +++ b/handlers/lots-get/next.ts @@ -11,15 +11,12 @@ export const handler: RequestHandler = (request, response) => { if (!nextLotId) { return response.redirect( - configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/?error=noNextLotIdFound" + configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/?error=noNextLotIdFound" ); } return response.redirect( - configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/" + - nextLotId + configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/" + nextLotId ); }; diff --git a/handlers/lots-get/previous.js b/handlers/lots-get/previous.js index 2e7edc6f..2f28e5ea 100644 --- a/handlers/lots-get/previous.js +++ b/handlers/lots-get/previous.js @@ -7,8 +7,6 @@ export const handler = (request, response) => { return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/?error=noPreviousLotIdFound"); } - return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/" + - previousLotId); + return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/" + previousLotId); }; export default handler; diff --git a/handlers/lots-get/previous.ts b/handlers/lots-get/previous.ts index bc1faeb7..34e68b9f 100644 --- a/handlers/lots-get/previous.ts +++ b/handlers/lots-get/previous.ts @@ -17,9 +17,7 @@ export const handler: RequestHandler = (request, response) => { } return response.redirect( - configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/" + - previousLotId + configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/" + previousLotId ); }; diff --git a/handlers/lots-get/view.js b/handlers/lots-get/view.js index 898990e4..e83384e8 100644 --- a/handlers/lots-get/view.js +++ b/handlers/lots-get/view.js @@ -3,8 +3,7 @@ import { getLot } from "../../helpers/lotOccupancyDB/getLot.js"; export const handler = (request, response) => { const lot = getLot(request.params.lotId); if (!lot) { - return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/?error=lotIdNotFound"); + return response.redirect(configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/?error=lotIdNotFound"); } return response.render("lot-view", { headTitle: lot.lotName, diff --git a/handlers/lots-get/view.ts b/handlers/lots-get/view.ts index 5cae6c5c..fb1e97c8 100644 --- a/handlers/lots-get/view.ts +++ b/handlers/lots-get/view.ts @@ -9,8 +9,7 @@ export const handler: RequestHandler = (request, response) => { if (!lot) { return response.redirect( - configFunctions.getProperty("reverseProxy.urlPrefix") + - "/lots/?error=lotIdNotFound" + configFunctions.getProperty("reverseProxy.urlPrefix") + "/lots/?error=lotIdNotFound" ); } diff --git a/handlers/maps-get/new.ts b/handlers/maps-get/new.ts index a33797ac..116f436c 100644 --- a/handlers/maps-get/new.ts +++ b/handlers/maps-get/new.ts @@ -9,9 +9,7 @@ import * as recordTypes from "../../types/recordTypes"; export const handler: RequestHandler = async (_request, response) => { const map: recordTypes.Map = { mapCity: configFunctions.getProperty("settings.map.mapCityDefault"), - mapProvince: configFunctions.getProperty( - "settings.map.mapProvinceDefault" - ) + mapProvince: configFunctions.getProperty("settings.map.mapProvinceDefault") }; const mapSVGs = await getMapSVGs(); diff --git a/handlers/reports-get/reportName.js b/handlers/reports-get/reportName.js index 49ebd7fb..da913523 100644 --- a/handlers/reports-get/reportName.js +++ b/handlers/reports-get/reportName.js @@ -4,9 +4,10 @@ export const handler = (request, response) => { const reportName = request.params.reportName; let rows; switch (reportName) { - default: + default: { rows = getReportData(reportName, request.query); break; + } } if (!rows) { return response.status(404).json({ @@ -15,11 +16,7 @@ export const handler = (request, response) => { }); } const csv = papaparse.unparse(rows); - response.setHeader("Content-Disposition", "attachment; filename=" + - reportName + - "-" + - Date.now().toString() + - ".csv"); + response.setHeader("Content-Disposition", "attachment; filename=" + reportName + "-" + Date.now().toString() + ".csv"); response.setHeader("Content-Type", "text/csv"); response.send(csv); }; diff --git a/handlers/reports-get/reportName.ts b/handlers/reports-get/reportName.ts index 42083d0e..7d501698 100644 --- a/handlers/reports-get/reportName.ts +++ b/handlers/reports-get/reportName.ts @@ -1,9 +1,6 @@ import type { RequestHandler } from "express"; -import { - getReportData, - ReportParameters -} from "../../helpers/lotOccupancyDB/getReportData.js"; +import { getReportData, ReportParameters } from "../../helpers/lotOccupancyDB/getReportData.js"; import papaparse from "papaparse"; @@ -13,9 +10,10 @@ export const handler: RequestHandler = (request, response) => { let rows: unknown[]; switch (reportName) { - default: + default: { rows = getReportData(reportName, request.query as ReportParameters); break; + } } if (!rows) { @@ -29,11 +27,7 @@ export const handler: RequestHandler = (request, response) => { response.setHeader( "Content-Disposition", - "attachment; filename=" + - reportName + - "-" + - Date.now().toString() + - ".csv" + "attachment; filename=" + reportName + "-" + Date.now().toString() + ".csv" ); response.setHeader("Content-Type", "text/csv"); diff --git a/handlers/workOrders-get/new.ts b/handlers/workOrders-get/new.ts index fcd1c8ae..208fcb58 100644 --- a/handlers/workOrders-get/new.ts +++ b/handlers/workOrders-get/new.ts @@ -1,14 +1,11 @@ import { dateToInteger, dateToString } from "@cityssm/expressjs-server-js/dateTimeFns.js"; import type { RequestHandler } from "express"; -import { - getWorkOrderTypes -} from "../../helpers/functions.cache.js"; +import { getWorkOrderTypes } from "../../helpers/functions.cache.js"; import * as recordTypes from "../../types/recordTypes"; export const handler: RequestHandler = (request, response) => { - const currentDate = new Date(); const workOrder: recordTypes.WorkOrder = { diff --git a/handlers/workOrders-get/search.ts b/handlers/workOrders-get/search.ts index d833cad0..3da4b3bc 100644 --- a/handlers/workOrders-get/search.ts +++ b/handlers/workOrders-get/search.ts @@ -3,7 +3,6 @@ import type { RequestHandler } from "express"; import { getWorkOrderTypes } from "../../helpers/functions.cache.js"; export const handler: RequestHandler = (request, response) => { - const workOrderOpenDateString = request.query.workOrderOpenDateString; const workOrderTypes = getWorkOrderTypes(); diff --git a/helpers/functions.authentication.js b/helpers/functions.authentication.js index 3841bae6..21370fb0 100644 --- a/helpers/functions.authentication.js +++ b/helpers/functions.authentication.js @@ -27,6 +27,7 @@ export const authenticate = async (userName, password) => { const safeRedirects = new Set([ "/admin/cleanup", "/admin/fees", + "/admin/lottypes", "/admin/occupancytypes", "/admin/tables", "/lotoccupancies", @@ -44,9 +45,9 @@ const safeRedirects = new Set([ export const getSafeRedirectURL = (possibleRedirectURL = "") => { const urlPrefix = configFunctions.getProperty("reverseProxy.urlPrefix"); if (typeof possibleRedirectURL === "string") { - const urlToCheck = (possibleRedirectURL.startsWith(urlPrefix) + const urlToCheck = possibleRedirectURL.startsWith(urlPrefix) ? possibleRedirectURL.slice(urlPrefix.length) - : possibleRedirectURL); + : possibleRedirectURL; const urlToCheckLowerCase = urlToCheck.toLowerCase(); if (safeRedirects.has(urlToCheckLowerCase) || /^(\/maps\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || diff --git a/helpers/functions.authentication.ts b/helpers/functions.authentication.ts index 1ab1e698..fef6465e 100644 --- a/helpers/functions.authentication.ts +++ b/helpers/functions.authentication.ts @@ -14,27 +14,20 @@ const authenticateViaActiveDirectory = async ( try { const ad = new ActiveDirectory(activeDirectoryConfig); - ad.authenticate( - userDomain + "\\" + userName, - password, - async (error, auth) => { - if (error) { - resolve(false); - } - - resolve(auth); + ad.authenticate(userDomain + "\\" + userName, password, async (error, auth) => { + if (error) { + resolve(false); } - ); + + resolve(auth); + }); } catch { resolve(false); } }); }; -export const authenticate = async ( - userName: string, - password: string -): Promise => { +export const authenticate = async (userName: string, password: string): Promise => { if (!userName || userName === "" || !password || password === "") { return false; } @@ -42,10 +35,10 @@ export const authenticate = async ( return await authenticateViaActiveDirectory(userName, password); }; - const safeRedirects = new Set([ "/admin/cleanup", "/admin/fees", + "/admin/lottypes", "/admin/occupancytypes", "/admin/tables", "/lotoccupancies", @@ -65,11 +58,9 @@ export const getSafeRedirectURL = (possibleRedirectURL = "") => { const urlPrefix = configFunctions.getProperty("reverseProxy.urlPrefix"); if (typeof possibleRedirectURL === "string") { - const urlToCheck = ( - possibleRedirectURL.startsWith(urlPrefix) - ? possibleRedirectURL.slice(urlPrefix.length) - : possibleRedirectURL - ); + const urlToCheck = possibleRedirectURL.startsWith(urlPrefix) + ? possibleRedirectURL.slice(urlPrefix.length) + : possibleRedirectURL; const urlToCheckLowerCase = urlToCheck.toLowerCase(); diff --git a/helpers/functions.lotOccupancy.ts b/helpers/functions.lotOccupancy.ts index ba189260..be984a39 100644 --- a/helpers/functions.lotOccupancy.ts +++ b/helpers/functions.lotOccupancy.ts @@ -48,10 +48,10 @@ export const getFeesByFeeCategory = ( export const getTransactionTotal = (lotOccupancy: recordTypes.LotOccupancy) => { let transactionTotal = 0; - + for (const transaction of lotOccupancy.lotOccupancyTransactions) { transactionTotal += transaction.transactionAmount; } return transactionTotal; -}; \ No newline at end of file +};