Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Mar 7, 2024
1 parent 70983b7 commit 636826d
Show file tree
Hide file tree
Showing 30 changed files with 199 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as dateTimeFns from '@cityssm/utils-datetime';
import { dateIntegerToString, dateToInteger } from '@cityssm/utils-datetime';
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
export default function getParkingTicketsAvailableForMTOLookup(currentBatchId, issueDaysAgo) {
const database = sqlite(databasePath, {
readonly: true
});
database.function('userFn_dateIntegerToString', dateTimeFns.dateIntegerToString);
database.function('userFn_dateIntegerToString', dateIntegerToString);
let issueDateNumber = 1e8;
if (issueDaysAgo !== -1) {
const issueDate = new Date();
issueDate.setDate(issueDate.getDate() - issueDaysAgo);
issueDateNumber = dateTimeFns.dateToInteger(issueDate);
issueDateNumber = dateToInteger(issueDate);
}
const tickets = database
.prepare(`select t.ticketId, t.ticketNumber,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dateTimeFns from '@cityssm/utils-datetime'
import { dateIntegerToString, dateToInteger } from '@cityssm/utils-datetime'
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
Expand All @@ -12,17 +12,14 @@ export default function getParkingTicketsAvailableForMTOLookup(
readonly: true
})

database.function(
'userFn_dateIntegerToString',
dateTimeFns.dateIntegerToString
)
database.function('userFn_dateIntegerToString', dateIntegerToString)

let issueDateNumber = 1e8 // 10000_00_00 (a number bigger than any possible date)

if (issueDaysAgo !== -1) {
const issueDate = new Date()
issueDate.setDate(issueDate.getDate() - issueDaysAgo)
issueDateNumber = dateTimeFns.dateToInteger(issueDate) as number
issueDateNumber = dateToInteger(issueDate) as number
}

const tickets = database
Expand Down
5 changes: 2 additions & 3 deletions database/parkingDB/getLicencePlates.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ export interface GetLicencePlatesQueryOptions {
limit: number;
offset: number;
}
interface GetLicencePlatesReturn {
export interface GetLicencePlatesReturn {
count: number;
limit: number;
offset: number;
licencePlates: LicencePlate[];
}
export declare function getLicencePlates(queryOptions: GetLicencePlatesQueryOptions): GetLicencePlatesReturn;
export default getLicencePlates;
export default function getLicencePlates(queryOptions: GetLicencePlatesQueryOptions): GetLicencePlatesReturn;
3 changes: 1 addition & 2 deletions database/parkingDB/getLicencePlates.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { getSplitWhereClauseFilter } from '../parkingDB.js';
export function getLicencePlates(queryOptions) {
export default function getLicencePlates(queryOptions) {
const database = sqlite(databasePath, {
readonly: true
});
Expand Down Expand Up @@ -61,4 +61,3 @@ export function getLicencePlates(queryOptions) {
licencePlates: rows
};
}
export default getLicencePlates;
6 changes: 2 additions & 4 deletions database/parkingDB/getLicencePlates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export interface GetLicencePlatesQueryOptions {
offset: number
}

interface GetLicencePlatesReturn {
export interface GetLicencePlatesReturn {
count: number
limit: number
offset: number
licencePlates: LicencePlate[]
}

export function getLicencePlates(
export default function getLicencePlates(
queryOptions: GetLicencePlatesQueryOptions
): GetLicencePlatesReturn {
const database = sqlite(databasePath, {
Expand Down Expand Up @@ -108,5 +108,3 @@ export function getLicencePlates(
licencePlates: rows
}
}

export default getLicencePlates
3 changes: 1 addition & 2 deletions database/parkingDB/getParkingTickets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ export interface GetParkingTicketsQueryOptions {
limit: number;
offset: number;
}
interface GetParkingTicketsReturn {
export interface GetParkingTicketsReturn {
count: number;
limit: number;
offset: number;
tickets: ParkingTicket[];
}
export default function getParkingTickets(sessionUser: PTSUser, queryOptions: GetParkingTicketsQueryOptions): GetParkingTicketsReturn;
export declare function getParkingTicketsByLicencePlate(licencePlateCountry: string, licencePlateProvince: string, licencePlateNumber: string, sessionUser: PTSUser): ParkingTicket[];
export {};
2 changes: 1 addition & 1 deletion database/parkingDB/getParkingTickets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function buildWhereClause(
}
}

interface GetParkingTicketsReturn {
export interface GetParkingTicketsReturn {
count: number
limit: number
offset: number
Expand Down
8 changes: 4 additions & 4 deletions database/parkingDB/getUnreceivedLookupBatches.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dateTimeFns from '@cityssm/utils-datetime';
import { dateIntegerToString } from '@cityssm/utils-datetime';
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
export default function getUnreceivedLookupBatches(includeUnlocked) {
Expand All @@ -18,9 +18,9 @@ export default function getUnreceivedLookupBatches(includeUnlocked) {
.all();
database.close();
for (const batch of batches) {
batch.batchDateString = dateTimeFns.dateIntegerToString(batch.batchDate);
batch.lockDateString = dateTimeFns.dateIntegerToString(batch.lockDate);
batch.sentDateString = dateTimeFns.dateIntegerToString(batch.sentDate);
batch.batchDateString = dateIntegerToString(batch.batchDate);
batch.lockDateString = dateIntegerToString(batch.lockDate);
batch.sentDateString = dateIntegerToString(batch.sentDate);
}
return batches;
}
12 changes: 4 additions & 8 deletions database/parkingDB/getUnreceivedLookupBatches.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dateTimeFns from '@cityssm/utils-datetime'
import { dateIntegerToString } from '@cityssm/utils-datetime'
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
Expand Down Expand Up @@ -28,13 +28,9 @@ export default function getUnreceivedLookupBatches(
database.close()

for (const batch of batches) {
batch.batchDateString = dateTimeFns.dateIntegerToString(batch.batchDate)
batch.lockDateString = dateTimeFns.dateIntegerToString(
batch.lockDate as number
)
batch.sentDateString = dateTimeFns.dateIntegerToString(
batch.sentDate as number
)
batch.batchDateString = dateIntegerToString(batch.batchDate)
batch.lockDateString = dateIntegerToString(batch.lockDate as number)
batch.sentDateString = dateIntegerToString(batch.sentDate as number)
}

return batches
Expand Down
6 changes: 3 additions & 3 deletions database/parkingDB/lockLookupBatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dateTimeFns from '@cityssm/utils-datetime';
import { dateToInteger, dateToTimeInteger } from '@cityssm/utils-datetime';
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
export default function lockLookupBatch(batchId, sessionUser) {
Expand All @@ -12,7 +12,7 @@ export default function lockLookupBatch(batchId, sessionUser) {
where batchId = ?
and recordDelete_timeMillis is null
and lockDate is null`)
.run(dateTimeFns.dateToInteger(rightNow), sessionUser.userName, rightNow.getTime(), batchId);
.run(dateToInteger(rightNow), sessionUser.userName, rightNow.getTime(), batchId);
if (info.changes > 0) {
database
.prepare(`insert into ParkingTicketStatusLog (
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function lockLookupBatch(batchId, sessionUser) {
or (t.recordDelete_timeMillis is null and t.resolvedDate is null))
group by t.ticketId, e.licencePlateCountry, e.licencePlateProvince, e.licencePlateNumber, e.batchId
having max(case when s.statusKey in ('ownerLookupPending', 'ownerLookupMatch', 'ownerLookupError') and s.recordDelete_timeMillis is null then 1 else 0 end) = 0`)
.run(dateTimeFns.dateToInteger(rightNow), dateTimeFns.dateToTimeInteger(rightNow), sessionUser.userName, rightNow.getTime(), sessionUser.userName, rightNow.getTime(), batchId);
.run(dateToInteger(rightNow), dateToTimeInteger(rightNow), sessionUser.userName, rightNow.getTime(), sessionUser.userName, rightNow.getTime(), batchId);
}
database.close();
return {
Expand Down
8 changes: 4 additions & 4 deletions database/parkingDB/lockLookupBatch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dateTimeFns from '@cityssm/utils-datetime'
import { dateToInteger, dateToTimeInteger } from '@cityssm/utils-datetime'
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
Expand All @@ -24,7 +24,7 @@ export default function lockLookupBatch(
and lockDate is null`
)
.run(
dateTimeFns.dateToInteger(rightNow),
dateToInteger(rightNow),
sessionUser.userName,
rightNow.getTime(),
batchId
Expand Down Expand Up @@ -64,8 +64,8 @@ export default function lockLookupBatch(
having max(case when s.statusKey in ('ownerLookupPending', 'ownerLookupMatch', 'ownerLookupError') and s.recordDelete_timeMillis is null then 1 else 0 end) = 0`
)
.run(
dateTimeFns.dateToInteger(rightNow),
dateTimeFns.dateToTimeInteger(rightNow),
dateToInteger(rightNow),
dateToTimeInteger(rightNow),
sessionUser.userName,
rightNow.getTime(),
sessionUser.userName,
Expand Down
2 changes: 2 additions & 0 deletions handlers/plates-post/doGetLicencePlates.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="cookie-parser" />
import type { Request, Response } from 'express';
import { type GetLicencePlatesReturn } from '../../database/parkingDB/getLicencePlates.js';
export type DoGetLicencePlatesResponse = GetLicencePlatesReturn;
export default function handler(request: Request, response: Response): void;
4 changes: 2 additions & 2 deletions handlers/plates-post/doGetLicencePlates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as parkingDB_getLicencePlates from '../../database/parkingDB/getLicencePlates.js';
import getLicencePlates from '../../database/parkingDB/getLicencePlates.js';
export default function handler(request, response) {
const queryOptions = {
limit: Number.parseInt(request.body.limit, 10),
Expand All @@ -12,5 +12,5 @@ export default function handler(request, response) {
queryOptions.hasUnresolvedTickets =
request.body.hasUnresolvedTickets === '1';
}
response.json(parkingDB_getLicencePlates.getLicencePlates(queryOptions));
response.json(getLicencePlates(queryOptions));
}
20 changes: 12 additions & 8 deletions handlers/plates-post/doGetLicencePlates.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { Request, Response } from 'express'

import * as parkingDB_getLicencePlates from '../../database/parkingDB/getLicencePlates.js'
import getLicencePlates, {
type GetLicencePlatesQueryOptions,
type GetLicencePlatesReturn
} from '../../database/parkingDB/getLicencePlates.js'

export type DoGetLicencePlatesResponse = GetLicencePlatesReturn

export default function handler(request: Request, response: Response): void {
const queryOptions: parkingDB_getLicencePlates.GetLicencePlatesQueryOptions =
{
limit: Number.parseInt(request.body.limit as string, 10),
offset: Number.parseInt(request.body.offset as string, 10),
licencePlateNumber: request.body.licencePlateNumber
}
const queryOptions: GetLicencePlatesQueryOptions = {
limit: Number.parseInt(request.body.limit as string, 10),
offset: Number.parseInt(request.body.offset as string, 10),
licencePlateNumber: request.body.licencePlateNumber
}

if (request.body.hasOwnerRecord !== '') {
queryOptions.hasOwnerRecord = request.body.hasOwnerRecord === '1'
Expand All @@ -19,5 +23,5 @@ export default function handler(request: Request, response: Response): void {
request.body.hasUnresolvedTickets === '1'
}

response.json(parkingDB_getLicencePlates.getLicencePlates(queryOptions))
response.json(getLicencePlates(queryOptions))
}
2 changes: 2 additions & 0 deletions handlers/tickets-post/doGetTickets.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="cookie-parser" />
import type { Request, Response } from 'express';
import { type GetParkingTicketsReturn } from '../../database/parkingDB/getParkingTickets.js';
export type DoGetTicketsResponse = GetParkingTicketsReturn;
export default function handler(request: Request, response: Response): void;
5 changes: 4 additions & 1 deletion handlers/tickets-post/doGetTickets.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { Request, Response } from 'express'

import getParkingTickets, {
type GetParkingTicketsQueryOptions
type GetParkingTicketsQueryOptions,
type GetParkingTicketsReturn
} from '../../database/parkingDB/getParkingTickets.js'

export type DoGetTicketsResponse = GetParkingTicketsReturn

export default function handler(request: Request, response: Response): void {
const queryOptions: GetParkingTicketsQueryOptions = {
limit: Number.parseInt(request.body.limit as string, 10),
Expand Down
8 changes: 5 additions & 3 deletions public/javascripts/mto-plateExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
licencePlateCountry: 'CA',
licencePlateProvince: 'ON',
licencePlateNumber: batchEntry.licencePlateNumber
}, (responseJSON) => {
}, (rawResponseJSON) => {
const responseJSON = rawResponseJSON;
if (responseJSON.success) {
entryContainerElement.remove();
refreshAvailableTickets();
Expand All @@ -62,9 +63,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
function clearBatch(clickEvent) {
clickEvent.preventDefault();
function clearFunction() {
cityssm.postJSON(pts.urlPrefix + '/plates/doClearLookupBatch', {
cityssm.postJSON(`${pts.urlPrefix}/plates/doClearLookupBatch`, {
batchId
}, (responseJSON) => {
}, (rawResponseJSON) => {
const responseJSON = rawResponseJSON;
if (responseJSON.success) {
populateBatchView(responseJSON.batch);
refreshAvailableTickets();
Expand Down
21 changes: 14 additions & 7 deletions public/javascripts/mto-plateExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ declare const pts: ptsGlobal
// Set tombstone in available plates list
availableTicketsList[recordIndex] = undefined

populateBatchView(responseJSON.batch)
populateBatchView(responseJSON.batch as LicencePlateLookupBatch)
} else {
buttonElement.removeAttribute('disabled')
}
Expand Down Expand Up @@ -106,7 +106,8 @@ declare const pts: ptsGlobal
licencePlateProvince: 'ON',
licencePlateNumber: batchEntry.licencePlateNumber
},
(responseJSON: { success: boolean }) => {
(rawResponseJSON) => {
const responseJSON = rawResponseJSON as { success: boolean }
if (responseJSON.success) {
// Remove element from list
entryContainerElement.remove()
Expand All @@ -124,14 +125,20 @@ declare const pts: ptsGlobal

function clearFunction(): void {
cityssm.postJSON(
pts.urlPrefix + '/plates/doClearLookupBatch',
`${pts.urlPrefix}/plates/doClearLookupBatch`,
{
batchId
},
(responseJSON: {
success: boolean
batch?: LicencePlateLookupBatch
}) => {
(rawResponseJSON) => {
const responseJSON = rawResponseJSON as
| {
success: true
batch: LicencePlateLookupBatch
}
| {
success: false
}

if (responseJSON.success) {
populateBatchView(responseJSON.batch)
refreshAvailableTickets()
Expand Down
4 changes: 3 additions & 1 deletion public/javascripts/plate-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
<i class="fas fa-3x fa-circle-notch fa-spin" aria-hidden="true"></i><br />
<em>Loading licence plates...</em>
</p>`;
cityssm.postJSON(`${pts.urlPrefix}/plates/doGetLicencePlates`, formElement, processPlateResultsFunction);
cityssm.postJSON(`${pts.urlPrefix}/plates/doGetLicencePlates`, formElement, (rawResponseJSON) => {
processPlateResultsFunction(rawResponseJSON);
});
}
function resetOffsetAndGetLicencePlatesFunction() {
offsetElement.value = '0';
Expand Down
Loading

0 comments on commit 636826d

Please # to comment.