Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Nov 7, 2023
1 parent 84ad5cb commit e9a541b
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 166 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/01-admin/01-bylaw-maint.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Admin - Parking By-Laws', () => {
cy.get(".modal [name='bylawNumber']")
.invoke('val')
.then((bylawNumber) => {
const newBylawDescription = 'Updated By-Law - ' + randomString();
const newBylawDescription = `Updated By-Law - ${randomString()}`;
cy.get(".modal [name='bylawDescription']")
.clear()
.type(newBylawDescription);
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/01-admin/01-bylaw-maint.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Admin - Parking By-Laws', () => {
cy.get(".modal [name='bylawNumber']")
.invoke('val')
.then((bylawNumber: string) => {
const newBylawDescription = 'Updated By-Law - ' + randomString()
const newBylawDescription = `Updated By-Law - ${randomString()}`

cy.get(".modal [name='bylawDescription']")
.clear()
Expand Down
4 changes: 2 additions & 2 deletions cypress/support/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import 'cypress-axe';
export declare const logout: () => void;
export declare const login: (userName: string) => void;
export declare function logout(): void;
export declare function login(userName: string): void;
8 changes: 4 additions & 4 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'cypress-axe';
export const logout = () => {
export function logout() {
cy.visit('/logout');
};
export const login = (userName) => {
}
export function login(userName) {
cy.visit('/login');
cy.get('.message').contains('Testing', { matchCase: false });
cy.get("form [name='userName']").type(userName);
cy.get("form [name='password']").type(userName);
cy.get('form').submit();
cy.location('pathname').should('not.contain', '/login');
cy.get('.navbar').should('have.length', 1);
};
}
6 changes: 2 additions & 4 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* eslint-disable node/no-unpublished-import */

import 'cypress-axe'

export const logout = () => {
export function logout(): void {
cy.visit('/logout')
}

export const login = (userName: string) => {
export function login(userName: string): void {
cy.visit('/login')

cy.get('.message').contains('Testing', { matchCase: false })
Expand Down
2 changes: 1 addition & 1 deletion cypress/support/utilities.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export declare const randomString: () => string;
export declare function randomString(): string;
4 changes: 2 additions & 2 deletions cypress/support/utilities.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const randomString = () => {
export function randomString() {
return Math.ceil(Math.random() * 100000).toString();
};
}
2 changes: 1 addition & 1 deletion cypress/support/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const randomString = (): string => {
export function randomString(): string {
return Math.ceil(Math.random() * 100_000).toString()
}
31 changes: 19 additions & 12 deletions handlers/permissions.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
import * as userFunctions from '../helpers/functions.user.js';
const dashboardRedirectUrl = '/dashboard';
export const adminGetHandler = (request, response, next) => {
if (userFunctions.userIsAdmin(request)) {
return next();
next();
return;
}
return response.redirect('/dashboard');
response.redirect(dashboardRedirectUrl);
};
export const adminPostHandler = (request, response, next) => {
if (userFunctions.userIsAdmin(request)) {
return next();
next();
return;
}
return response.json(userFunctions.forbiddenJSON);
response.json(userFunctions.forbiddenJSON);
};
export const updateGetHandler = (request, response, next) => {
if (userFunctions.userCanUpdate(request)) {
return next();
next();
return;
}
return response.redirect('/dashboard');
response.redirect(dashboardRedirectUrl);
};
export const updateOrOperatorGetHandler = (request, response, next) => {
if (userFunctions.userCanUpdate(request) ||
userFunctions.userIsOperator(request)) {
return next();
next();
return;
}
return response.redirect('/dashboard');
response.redirect(dashboardRedirectUrl);
};
export const updatePostHandler = (request, response, next) => {
if (userFunctions.userCanUpdate(request)) {
return next();
next();
return;
}
return response.json(userFunctions.forbiddenJSON);
response.json(userFunctions.forbiddenJSON);
};
export const updateOrOperatorPostHandler = (request, response, next) => {
if (userFunctions.userCanUpdate(request) ||
userFunctions.userIsOperator(request)) {
return next();
next();
return;
}
return response.json(userFunctions.forbiddenJSON);
response.json(userFunctions.forbiddenJSON);
};
32 changes: 20 additions & 12 deletions handlers/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ import type { RequestHandler } from 'express'

import * as userFunctions from '../helpers/functions.user.js'

const dashboardRedirectUrl = '/dashboard'

export const adminGetHandler: RequestHandler = (request, response, next) => {
if (userFunctions.userIsAdmin(request)) {
return next()
next()
return
}

return response.redirect('/dashboard')
response.redirect(dashboardRedirectUrl)
}

export const adminPostHandler: RequestHandler = (request, response, next) => {
if (userFunctions.userIsAdmin(request)) {
return next()
next()
return
}

return response.json(userFunctions.forbiddenJSON)
response.json(userFunctions.forbiddenJSON)
}

export const updateGetHandler: RequestHandler = (request, response, next) => {
if (userFunctions.userCanUpdate(request)) {
return next()
next()
return
}

return response.redirect('/dashboard')
response.redirect(dashboardRedirectUrl)
}

export const updateOrOperatorGetHandler: RequestHandler = (
Expand All @@ -35,18 +40,20 @@ export const updateOrOperatorGetHandler: RequestHandler = (
userFunctions.userCanUpdate(request) ||
userFunctions.userIsOperator(request)
) {
return next()
next()
return
}

return response.redirect('/dashboard')
response.redirect(dashboardRedirectUrl)
}

export const updatePostHandler: RequestHandler = (request, response, next) => {
if (userFunctions.userCanUpdate(request)) {
return next()
next()
return
}

return response.json(userFunctions.forbiddenJSON)
response.json(userFunctions.forbiddenJSON)
}

export const updateOrOperatorPostHandler: RequestHandler = (
Expand All @@ -58,8 +65,9 @@ export const updateOrOperatorPostHandler: RequestHandler = (
userFunctions.userCanUpdate(request) ||
userFunctions.userIsOperator(request)
) {
return next()
next()
return
}

return response.json(userFunctions.forbiddenJSON)
response.json(userFunctions.forbiddenJSON)
}
4 changes: 2 additions & 2 deletions handlers/reports-all/reportName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as parkingDB_reporting from '../../database/parkingDB-reporting.js';
import papaparse from 'papaparse';
import * as parkingDB_reporting from '../../database/parkingDB-reporting.js';
export const handler = (request, response) => {
const reportName = request.params.reportName;
const rows = parkingDB_reporting.getReportData(reportName, request.query);
Expand All @@ -8,7 +8,7 @@ export const handler = (request, response) => {
return;
}
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);
};
Expand Down
10 changes: 6 additions & 4 deletions handlers/reports-all/reportName.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { RequestHandler } from 'express'
import papaparse from 'papaparse'

import * as parkingDB_reporting from '../../database/parkingDB-reporting.js'

import papaparse from 'papaparse'

export const handler: RequestHandler = (request, response) => {
const reportName = request.params.reportName

const rows = parkingDB_reporting.getReportData(reportName, request.query as Record<string, string>)
const rows = parkingDB_reporting.getReportData(
reportName,
request.query as Record<string, string>
)

if (!rows) {
response.redirect('/reports/?error=reportNotAvailable')
Expand All @@ -18,7 +20,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')
Expand Down
2 changes: 1 addition & 1 deletion handlers/tickets-ontario-get/convict.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as parkingDB_ontario from '../../database/parkingDB-ontario.js';
import { getConvictionBatch } from '../../database/parkingDB/getConvictionBatch.js';
import * as parkingDB_ontario from '../../database/parkingDB-ontario.js';
export const handler = (_request, response) => {
const tickets = parkingDB_ontario.getParkingTicketsAvailableForMTOConvictionBatch();
const batch = getConvictionBatch(-1);
Expand Down
3 changes: 1 addition & 2 deletions handlers/tickets-ontario-get/convict.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { RequestHandler } from 'express'

import * as parkingDB_ontario from '../../database/parkingDB-ontario.js'

import { getConvictionBatch } from '../../database/parkingDB/getConvictionBatch.js'
import * as parkingDB_ontario from '../../database/parkingDB-ontario.js'

export const handler: RequestHandler = (_request, response) => {
const tickets =
Expand Down
4 changes: 2 additions & 2 deletions handlers/tickets-post/doQuickReconcileMatches.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createParkingTicketStatus } from '../../database/parkingDB/createParkingTicketStatus.js';
import { getOwnershipReconciliationRecords } from '../../database/parkingDB/getOwnershipReconciliationRecords.js';
import * as ownerFunctions from '../../helpers/functions.owner.js';
import { getFormattedOwnerAddress } from '../../helpers/functions.owner.js';
export const handler = (request, response) => {
const records = getOwnershipReconciliationRecords();
const statusRecords = [];
for (const record of records) {
if (!record.isVehicleMakeMatch || !record.isLicencePlateExpiryDateMatch) {
continue;
}
const ownerAddress = ownerFunctions.getFormattedOwnerAddress(record);
const ownerAddress = getFormattedOwnerAddress(record);
const statusResponse = createParkingTicketStatus({
recordType: 'status',
ticketID: record.ticket_ticketID,
Expand Down
4 changes: 2 additions & 2 deletions handlers/tickets-post/doQuickReconcileMatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RequestHandler } from 'express'

import { createParkingTicketStatus } from '../../database/parkingDB/createParkingTicketStatus.js'
import { getOwnershipReconciliationRecords } from '../../database/parkingDB/getOwnershipReconciliationRecords.js'
import * as ownerFunctions from '../../helpers/functions.owner.js'
import { getFormattedOwnerAddress } from '../../helpers/functions.owner.js'

export const handler: RequestHandler = (request, response) => {
const records = getOwnershipReconciliationRecords()
Expand All @@ -14,7 +14,7 @@ export const handler: RequestHandler = (request, response) => {
continue
}

const ownerAddress = ownerFunctions.getFormattedOwnerAddress(record)
const ownerAddress = getFormattedOwnerAddress(record)

const statusResponse = createParkingTicketStatus(
{
Expand Down
4 changes: 2 additions & 2 deletions helpers/functions.config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export declare function getConfigProperty(propertyName: 'parkingTickets.ticketNu
export declare function getConfigProperty(propertyName: 'parkingTicketStatuses'): ConfigParkingTicketStatus[];
export declare function getConfigProperty(propertyName: 'users.testing' | 'users.canLogin' | 'users.canUpdate' | 'users.isAdmin' | 'users.isOperator'): string[];
export declare const keepAliveMillis: number;
export declare const getParkingTicketStatus: (statusKey: string) => ConfigParkingTicketStatus | undefined;
export declare function getParkingTicketStatus(statusKey: string): ConfigParkingTicketStatus | undefined;
interface LicencePlateLocationProperties {
licencePlateCountryAlias: string;
licencePlateProvinceAlias: string;
Expand All @@ -22,5 +22,5 @@ interface LicencePlateLocationProperties {
backgroundColor: string;
};
}
export declare const getLicencePlateLocationProperties: (originalLicencePlateCountry: string, originalLicencePlateProvince: string) => LicencePlateLocationProperties;
export declare function getLicencePlateLocationProperties(originalLicencePlateCountry: string, originalLicencePlateProvince: string): LicencePlateLocationProperties;
export {};
14 changes: 8 additions & 6 deletions helpers/functions.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const keepAliveMillis = getConfigProperty('session.doKeepAlive')
: 0;
const parkingTicketStatusMap = new Map();
let parkingTicketStatusMapIsLoaded = false;
export const getParkingTicketStatus = (statusKey) => {
export function getParkingTicketStatus(statusKey) {
if (!parkingTicketStatusMapIsLoaded) {
const parkingTicketStatusList = getConfigProperty('parkingTicketStatuses');
for (const parkingTicketStatusObject of parkingTicketStatusList) {
Expand All @@ -66,8 +66,8 @@ export const getParkingTicketStatus = (statusKey) => {
parkingTicketStatusMapIsLoaded = true;
}
return parkingTicketStatusMap.get(statusKey);
};
export const getLicencePlateLocationProperties = (originalLicencePlateCountry, originalLicencePlateProvince) => {
}
export function getLicencePlateLocationProperties(originalLicencePlateCountry, originalLicencePlateProvince) {
const licencePlateProvinceDefault = {
provinceShortName: originalLicencePlateProvince,
color: '#000',
Expand All @@ -79,16 +79,18 @@ export const getLicencePlateLocationProperties = (originalLicencePlateCountry, o
let licencePlateProvinceAlias = originalLicencePlateProvince;
if (Object.hasOwn(getConfigProperty('licencePlateProvinceAliases'), licencePlateCountryAlias)) {
licencePlateProvinceAlias =
getConfigProperty('licencePlateProvinceAliases')[licencePlateCountryAlias][originalLicencePlateProvince.toUpperCase()] || originalLicencePlateProvince;
getConfigProperty('licencePlateProvinceAliases')[licencePlateCountryAlias][originalLicencePlateProvince.toUpperCase()] ||
originalLicencePlateProvince;
}
let licencePlateProvince = licencePlateProvinceDefault;
if (Object.hasOwn(getConfigProperty('licencePlateProvinces'), licencePlateCountryAlias)) {
licencePlateProvince =
getConfigProperty('licencePlateProvinces')[licencePlateCountryAlias].provinces[licencePlateProvinceAlias] || licencePlateProvinceDefault;
getConfigProperty('licencePlateProvinces')[licencePlateCountryAlias]
.provinces[licencePlateProvinceAlias] || licencePlateProvinceDefault;
}
return {
licencePlateCountryAlias,
licencePlateProvinceAlias,
licencePlateProvince
};
};
}
Loading

0 comments on commit e9a541b

Please sign in to comment.