From a8981e8154e17cbacaf225f30ce27fb96824fa96 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Mon, 23 Jan 2023 13:07:29 -0500 Subject: [PATCH] linting --- handlers/lotOccupancies-get/edit.js | 2 +- handlers/lotOccupancies-get/edit.ts | 2 +- handlers/lotOccupancies-get/new.js | 4 +- handlers/lotOccupancies-get/new.ts | 4 +- handlers/lotOccupancies-get/view.js | 2 +- handlers/lotOccupancies-get/view.ts | 2 +- handlers/lots-get/edit.js | 2 +- handlers/lots-get/edit.ts | 2 +- handlers/lots-get/new.js | 4 +- handlers/lots-get/new.ts | 4 +- handlers/lots-get/next.js | 2 +- handlers/lots-get/next.ts | 2 +- handlers/lots-get/previous.js | 2 +- handlers/lots-get/previous.ts | 2 +- .../lotOccupancyDB/moveOccupancyTypeField.js | 4 +- .../lotOccupancyDB/moveOccupancyTypeField.ts | 6 +- public-typescript/dashboard.ts | 5 +- routes/login.js | 19 +- routes/login.ts | 176 +++++++++--------- temp/legacy.importFromCSV.js | 2 +- temp/legacy.importFromCSV.ts | 6 +- temp/legacy.importFromCsv.ids.ts | 3 +- 22 files changed, 134 insertions(+), 123 deletions(-) diff --git a/handlers/lotOccupancies-get/edit.js b/handlers/lotOccupancies-get/edit.js index 0760310a..93f96a07 100644 --- a/handlers/lotOccupancies-get/edit.js +++ b/handlers/lotOccupancies-get/edit.js @@ -4,7 +4,7 @@ import { getLotOccupancy } from '../../helpers/lotOccupancyDB/getLotOccupancy.js import { getMaps } from '../../helpers/lotOccupancyDB/getMaps.js'; export async function handler(request, response) { const lotOccupancy = await getLotOccupancy(request.params.lotOccupancyId); - if (!lotOccupancy) { + if (lotOccupancy === undefined) { response.redirect(`${configFunctions.getProperty('reverseProxy.urlPrefix')}/lotOccupancies/?error=lotOccupancyIdNotFound`); return; } diff --git a/handlers/lotOccupancies-get/edit.ts b/handlers/lotOccupancies-get/edit.ts index ccc49e8c..647c6e37 100644 --- a/handlers/lotOccupancies-get/edit.ts +++ b/handlers/lotOccupancies-get/edit.ts @@ -17,7 +17,7 @@ import { getMaps } from '../../helpers/lotOccupancyDB/getMaps.js' export async function handler(request: Request, response: Response): Promise { const lotOccupancy = await getLotOccupancy(request.params.lotOccupancyId) - if (!lotOccupancy) { + if (lotOccupancy === undefined) { response.redirect( `${configFunctions.getProperty( 'reverseProxy.urlPrefix' diff --git a/handlers/lotOccupancies-get/new.js b/handlers/lotOccupancies-get/new.js index 3ce0de4f..cc7e8e1e 100644 --- a/handlers/lotOccupancies-get/new.js +++ b/handlers/lotOccupancies-get/new.js @@ -9,9 +9,9 @@ export async function handler(request, response) { occupancyStartDate: dateToInteger(startDate), occupancyStartDateString: dateToString(startDate) }; - if (request.query.lotId) { + if (request.query.lotId !== undefined) { const lot = await getLot(request.query.lotId); - if (lot) { + if (lot !== undefined) { lotOccupancy.lotId = lot.lotId; lotOccupancy.lotName = lot.lotName; lotOccupancy.mapId = lot.mapId; diff --git a/handlers/lotOccupancies-get/new.ts b/handlers/lotOccupancies-get/new.ts index d7b05c00..d9241469 100644 --- a/handlers/lotOccupancies-get/new.ts +++ b/handlers/lotOccupancies-get/new.ts @@ -30,10 +30,10 @@ export async function handler( occupancyStartDateString: dateToString(startDate) } - if (request.query.lotId) { + if (request.query.lotId !== undefined) { const lot = await getLot(request.query.lotId as string) - if (lot) { + if (lot !== undefined) { lotOccupancy.lotId = lot.lotId lotOccupancy.lotName = lot.lotName lotOccupancy.mapId = lot.mapId diff --git a/handlers/lotOccupancies-get/view.js b/handlers/lotOccupancies-get/view.js index 47949a30..ef83b110 100644 --- a/handlers/lotOccupancies-get/view.js +++ b/handlers/lotOccupancies-get/view.js @@ -3,7 +3,7 @@ import * as configFunctions from '../../helpers/functions.config.js'; import { getLotOccupancy } from '../../helpers/lotOccupancyDB/getLotOccupancy.js'; export async function handler(request, response) { const lotOccupancy = await getLotOccupancy(request.params.lotOccupancyId); - if (!lotOccupancy) { + if (lotOccupancy === undefined) { response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + '/lotOccupancies/?error=lotOccupancyIdNotFound'); return; diff --git a/handlers/lotOccupancies-get/view.ts b/handlers/lotOccupancies-get/view.ts index 08fadd40..f300d5f4 100644 --- a/handlers/lotOccupancies-get/view.ts +++ b/handlers/lotOccupancies-get/view.ts @@ -11,7 +11,7 @@ export async function handler( ): Promise { const lotOccupancy = await getLotOccupancy(request.params.lotOccupancyId) - if (!lotOccupancy) { + if (lotOccupancy === undefined) { response.redirect( configFunctions.getProperty('reverseProxy.urlPrefix') + '/lotOccupancies/?error=lotOccupancyIdNotFound' diff --git a/handlers/lots-get/edit.js b/handlers/lots-get/edit.js index 4db930ee..b63baf3c 100644 --- a/handlers/lots-get/edit.js +++ b/handlers/lots-get/edit.js @@ -4,7 +4,7 @@ import { getMaps } from '../../helpers/lotOccupancyDB/getMaps.js'; import * as cacheFunctions from '../../helpers/functions.cache.js'; export async function handler(request, response) { const lot = await getLot(request.params.lotId); - if (!lot) { + if (lot === undefined) { response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/?error=lotIdNotFound'); return; diff --git a/handlers/lots-get/edit.ts b/handlers/lots-get/edit.ts index fab27577..d5ee2c91 100644 --- a/handlers/lots-get/edit.ts +++ b/handlers/lots-get/edit.ts @@ -12,7 +12,7 @@ export async function handler( ): Promise { const lot = await getLot(request.params.lotId) - if (!lot) { + if (lot === undefined) { response.redirect( configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/?error=lotIdNotFound' diff --git a/handlers/lots-get/new.js b/handlers/lots-get/new.js index fc4d431b..c1de9a88 100644 --- a/handlers/lots-get/new.js +++ b/handlers/lots-get/new.js @@ -7,12 +7,12 @@ export async function handler(request, response) { lotOccupancies: [] }; const maps = await getMaps(); - if (request.query.mapId) { + if (request.query.mapId !== undefined) { const mapId = Number.parseInt(request.query.mapId, 10); const map = maps.find((possibleMap) => { return mapId === possibleMap.mapId; }); - if (map) { + if (map !== undefined) { lot.mapId = map.mapId; lot.mapName = map.mapName; } diff --git a/handlers/lots-get/new.ts b/handlers/lots-get/new.ts index 70718a34..3b42063c 100644 --- a/handlers/lots-get/new.ts +++ b/handlers/lots-get/new.ts @@ -18,14 +18,14 @@ export async function handler( const maps = await getMaps() - if (request.query.mapId) { + if (request.query.mapId !== undefined) { const mapId = Number.parseInt(request.query.mapId as string, 10) const map = maps.find((possibleMap) => { return mapId === possibleMap.mapId }) - if (map) { + if (map !== undefined) { lot.mapId = map.mapId lot.mapName = map.mapName } diff --git a/handlers/lots-get/next.js b/handlers/lots-get/next.js index 08cc811a..863c4fda 100644 --- a/handlers/lots-get/next.js +++ b/handlers/lots-get/next.js @@ -8,6 +8,6 @@ export async function handler(request, response) { '/lots/?error=noNextLotIdFound'); return; } - response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + nextLotId); + response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + nextLotId.toString()); } export default handler; diff --git a/handlers/lots-get/next.ts b/handlers/lots-get/next.ts index 54f444a0..bb975b8e 100644 --- a/handlers/lots-get/next.ts +++ b/handlers/lots-get/next.ts @@ -21,7 +21,7 @@ export async function handler( } response.redirect( - configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + nextLotId + configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + nextLotId.toString() ) } diff --git a/handlers/lots-get/previous.js b/handlers/lots-get/previous.js index 6232d0fc..649b15d3 100644 --- a/handlers/lots-get/previous.js +++ b/handlers/lots-get/previous.js @@ -10,6 +10,6 @@ export async function handler(request, response) { } response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + - previousLotId); + previousLotId.toString()); } export default handler; diff --git a/handlers/lots-get/previous.ts b/handlers/lots-get/previous.ts index 439a27d2..6c16b65e 100644 --- a/handlers/lots-get/previous.ts +++ b/handlers/lots-get/previous.ts @@ -23,7 +23,7 @@ export async function handler( response.redirect( configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + - previousLotId + previousLotId.toString() ) } diff --git a/helpers/lotOccupancyDB/moveOccupancyTypeField.js b/helpers/lotOccupancyDB/moveOccupancyTypeField.js index 77ba3645..be1cea31 100644 --- a/helpers/lotOccupancyDB/moveOccupancyTypeField.js +++ b/helpers/lotOccupancyDB/moveOccupancyTypeField.js @@ -17,7 +17,7 @@ export async function moveOccupancyTypeFieldDown(occupancyTypeFieldId) { ' set orderNumber = orderNumber - 1' + ' where recordDelete_timeMillis is null' + (currentField.occupancyTypeId - ? " and occupancyTypeId = '" + currentField.occupancyTypeId + "'" + ? " and occupancyTypeId = '" + currentField.occupancyTypeId.toString() + "'" : ' and occupancyTypeId is null') + ' and orderNumber = ? + 1') .run(currentField.orderNumber); @@ -70,7 +70,7 @@ export async function moveOccupancyTypeFieldUp(occupancyTypeFieldId) { ' set orderNumber = orderNumber + 1' + ' where recordDelete_timeMillis is null' + (currentField.occupancyTypeId - ? " and occupancyTypeId = '" + currentField.occupancyTypeId + "'" + ? " and occupancyTypeId = '" + currentField.occupancyTypeId.toString() + "'" : ' and occupancyTypeId is null') + ' and orderNumber = ? - 1') .run(currentField.orderNumber); diff --git a/helpers/lotOccupancyDB/moveOccupancyTypeField.ts b/helpers/lotOccupancyDB/moveOccupancyTypeField.ts index fc969548..9fb8b2d4 100644 --- a/helpers/lotOccupancyDB/moveOccupancyTypeField.ts +++ b/helpers/lotOccupancyDB/moveOccupancyTypeField.ts @@ -1,5 +1,3 @@ -import sqlite from 'better-sqlite3' - import { acquireConnection } from './pool.js' import type { PoolConnection } from 'better-sqlite-pool' @@ -35,7 +33,7 @@ export async function moveOccupancyTypeFieldDown( ' set orderNumber = orderNumber - 1' + ' where recordDelete_timeMillis is null' + (currentField.occupancyTypeId - ? " and occupancyTypeId = '" + currentField.occupancyTypeId + "'" + ? " and occupancyTypeId = '" + currentField.occupancyTypeId.toString() + "'" : ' and occupancyTypeId is null') + ' and orderNumber = ? + 1' ) @@ -127,7 +125,7 @@ export async function moveOccupancyTypeFieldUp( ' set orderNumber = orderNumber + 1' + ' where recordDelete_timeMillis is null' + (currentField.occupancyTypeId - ? " and occupancyTypeId = '" + currentField.occupancyTypeId + "'" + ? " and occupancyTypeId = '" + currentField.occupancyTypeId.toString() + "'" : ' and occupancyTypeId is null') + ' and orderNumber = ? - 1' ) diff --git a/public-typescript/dashboard.ts b/public-typescript/dashboard.ts index 4b7d8bdf..eaa2a2fd 100644 --- a/public-typescript/dashboard.ts +++ b/public-typescript/dashboard.ts @@ -4,9 +4,8 @@ import type * as globalTypes from '../types/globalTypes' ;(() => { const los = exports.los as globalTypes.LOS - const workOrderNumberCircleElements: NodeListOf = document.querySelectorAll( - '.fa-circle[data-work-order-number' - ) + const workOrderNumberCircleElements: NodeListOf = + document.querySelectorAll('.fa-circle[data-work-order-number') for (const workOrderNumberCircleElement of workOrderNumberCircleElements) { workOrderNumberCircleElement.style.color = los.getRandomColor( diff --git a/routes/login.js b/routes/login.js index 5ac4b90e..174e491f 100644 --- a/routes/login.js +++ b/routes/login.js @@ -6,11 +6,10 @@ import { getApiKey } from '../helpers/functions.api.js'; import Debug from 'debug'; const debug = Debug('lot-occupancy-system:login'); export const router = Router(); -router - .route('/') - .get((request, response) => { +function getHandler(request, response) { const sessionCookieName = configFunctions.getProperty('session.cookieName'); - if (request.session.user && request.cookies[sessionCookieName]) { + if (request.session.user !== undefined && + request.cookies[sessionCookieName] !== undefined) { const redirectURL = authenticationFunctions.getSafeRedirectURL((request.query.redirect ?? '')); response.redirect(redirectURL); } @@ -22,8 +21,8 @@ router useTestDatabases }); } -}) - .post(async (request, response) => { +} +async function postHandler(request, response) { const userName = (typeof request.body.userName === 'string' ? request.body.userName : ''); const passwordPlain = (typeof request.body.password === 'string' ? request.body.password : ''); const unsafeRedirectURL = request.body.redirect; @@ -72,7 +71,7 @@ router }; } } - if (isAuthenticated && userObject) { + if (isAuthenticated && userObject !== undefined) { request.session.user = userObject; response.redirect(redirectURL); } @@ -84,5 +83,9 @@ router useTestDatabases }); } -}); +} +router + .route('/') + .get(getHandler) + .post(postHandler); export default router; diff --git a/routes/login.ts b/routes/login.ts index 253d9487..5e20fa0b 100644 --- a/routes/login.ts +++ b/routes/login.ts @@ -1,4 +1,4 @@ -import { Router } from 'express' +import { Router, RequestHandler, Request, Response } from 'express' import * as configFunctions from '../helpers/functions.config.js' @@ -16,109 +16,119 @@ const debug = Debug('lot-occupancy-system:login') export const router = Router() -router - .route('/') - .get((request, response) => { - const sessionCookieName = configFunctions.getProperty('session.cookieName') - - if (request.session.user && request.cookies[sessionCookieName]) { - const redirectURL = authenticationFunctions.getSafeRedirectURL( - (request.query.redirect ?? '') as string - ) - - response.redirect(redirectURL) - } else { - response.render('login', { - userName: '', - message: '', - redirect: request.query.redirect, - useTestDatabases - }) - } - }) - .post(async (request, response) => { - const userName = ( - typeof request.body.userName === 'string' ? request.body.userName : '' - ) as string - - const passwordPlain = ( - typeof request.body.password === 'string' ? request.body.password : '' - ) as string - - const unsafeRedirectURL = request.body.redirect +function getHandler(request: Request, response: Response): void { + const sessionCookieName = configFunctions.getProperty('session.cookieName') + if ( + request.session.user !== undefined && + request.cookies[sessionCookieName] !== undefined + ) { const redirectURL = authenticationFunctions.getSafeRedirectURL( - typeof unsafeRedirectURL === 'string' ? unsafeRedirectURL : '' + (request.query.redirect ?? '') as string ) - let isAuthenticated = false + response.redirect(redirectURL) + } else { + response.render('login', { + userName: '', + message: '', + redirect: request.query.redirect, + useTestDatabases + }) + } +} - if (userName.charAt(0) === '*') { - if (useTestDatabases && userName === passwordPlain) { - isAuthenticated = configFunctions - .getProperty('users.testing') - .includes(userName) +async function postHandler( + request: Request, + response: Response +): Promise { + const userName = ( + typeof request.body.userName === 'string' ? request.body.userName : '' + ) as string - if (isAuthenticated) { - debug('Authenticated testing user: ' + userName) - } + const passwordPlain = ( + typeof request.body.password === 'string' ? request.body.password : '' + ) as string + + const unsafeRedirectURL = request.body.redirect + + const redirectURL = authenticationFunctions.getSafeRedirectURL( + typeof unsafeRedirectURL === 'string' ? unsafeRedirectURL : '' + ) + + let isAuthenticated = false + + if (userName.charAt(0) === '*') { + if (useTestDatabases && userName === passwordPlain) { + isAuthenticated = configFunctions + .getProperty('users.testing') + .includes(userName) + + if (isAuthenticated) { + debug('Authenticated testing user: ' + userName) } - } else if (userName !== '' && passwordPlain !== '') { - isAuthenticated = await authenticationFunctions.authenticate( - userName, - passwordPlain - ) } + } else if (userName !== '' && passwordPlain !== '') { + isAuthenticated = await authenticationFunctions.authenticate( + userName, + passwordPlain + ) + } - let userObject: recordTypes.User | undefined + let userObject: recordTypes.User | undefined - if (isAuthenticated) { - const userNameLowerCase = userName.toLowerCase() + if (isAuthenticated) { + const userNameLowerCase = userName.toLowerCase() - const canLogin = configFunctions - .getProperty('users.canLogin') + const canLogin = configFunctions + .getProperty('users.canLogin') + .some((currentUserName) => { + return userNameLowerCase === currentUserName.toLowerCase() + }) + + if (canLogin) { + const canUpdate = configFunctions + .getProperty('users.canUpdate') .some((currentUserName) => { return userNameLowerCase === currentUserName.toLowerCase() }) - if (canLogin) { - const canUpdate = configFunctions - .getProperty('users.canUpdate') - .some((currentUserName) => { - return userNameLowerCase === currentUserName.toLowerCase() - }) + const isAdmin = configFunctions + .getProperty('users.isAdmin') + .some((currentUserName) => { + return userNameLowerCase === currentUserName.toLowerCase() + }) - const isAdmin = configFunctions - .getProperty('users.isAdmin') - .some((currentUserName) => { - return userNameLowerCase === currentUserName.toLowerCase() - }) + const apiKey = await getApiKey(userNameLowerCase) - const apiKey = await getApiKey(userNameLowerCase) - - userObject = { - userName: userNameLowerCase, - userProperties: { - canUpdate, - isAdmin, - apiKey - } + userObject = { + userName: userNameLowerCase, + userProperties: { + canUpdate, + isAdmin, + apiKey } } } + } - if (isAuthenticated && userObject) { - request.session.user = userObject + if (isAuthenticated && userObject !== undefined) { + request.session.user = userObject - response.redirect(redirectURL) - } else { - response.render('login', { - userName, - message: 'Login Failed', - redirect: redirectURL, - useTestDatabases - }) - } - }) + response.redirect(redirectURL) + } else { + response.render('login', { + userName, + message: 'Login Failed', + redirect: redirectURL, + useTestDatabases + }) + } +} + +router + .route('/') + .get(getHandler) + .post(postHandler as RequestHandler) export default router diff --git a/temp/legacy.importFromCSV.js b/temp/legacy.importFromCSV.js index 2035550d..985982dd 100644 --- a/temp/legacy.importFromCSV.js +++ b/temp/legacy.importFromCSV.js @@ -108,7 +108,7 @@ async function getMap(dataRow) { if (!map) { console.log('Creating map: ' + dataRow.cemetery); const mapId = await addMap({ - mapName: cemeteryToMapName[dataRow.cemetery] || dataRow.cemetery, + mapName: cemeteryToMapName[dataRow.cemetery] ?? dataRow.cemetery, mapDescription: dataRow.cemetery, mapSVG: '', mapLatitude: '', diff --git a/temp/legacy.importFromCSV.ts b/temp/legacy.importFromCSV.ts index 5004ca1f..c058d12f 100644 --- a/temp/legacy.importFromCSV.ts +++ b/temp/legacy.importFromCSV.ts @@ -291,7 +291,7 @@ const cemeteryToMapName = { WK: 'West Korah' } -const mapCache: Map = new Map() +const mapCache = new Map() async function getMap(dataRow: { cemetery: string }): Promise { const mapCacheKey = dataRow.cemetery @@ -314,7 +314,7 @@ async function getMap(dataRow: { cemetery: string }): Promise { const mapId = await addMap( { - mapName: cemeteryToMapName[dataRow.cemetery] || dataRow.cemetery, + mapName: cemeteryToMapName[dataRow.cemetery] ?? dataRow.cemetery, mapDescription: dataRow.cemetery, mapSVG: '', mapLatitude: '', @@ -329,7 +329,7 @@ async function getMap(dataRow: { cemetery: string }): Promise { user ) - map = await getMapFromDatabase(mapId) + map = await getMapFromDatabase(mapId) as recordTypes.Map } mapCache.set(mapCacheKey, map) diff --git a/temp/legacy.importFromCsv.ids.ts b/temp/legacy.importFromCsv.ids.ts index 342fceae..55c54590 100644 --- a/temp/legacy.importFromCsv.ids.ts +++ b/temp/legacy.importFromCsv.ids.ts @@ -1,4 +1,5 @@ /* eslint-disable unicorn/no-await-expression-member */ + import sqlite from 'better-sqlite3' import { lotOccupancyDB as databasePath } from '../data/databasePaths.js' @@ -8,7 +9,7 @@ import * as cacheFunctions from '../helpers/functions.cache.js' * Fee IDs */ -const feeCache: Map = new Map() +const feeCache = new Map() export function getFeeIdByFeeDescription(feeDescription: string): number { if (feeCache.keys.length === 0) {