custom lot name sort name function

deepsource-autofix-76c6eb20
Dan Gowans 2022-08-31 10:10:33 -04:00
parent b85a3a9ba5
commit c2ae19f4a6
16 changed files with 116 additions and 20 deletions

View File

@ -1,5 +1,26 @@
import { config as cemeteryConfig } from "./config.cemetery.ontario.js"; import { config as cemeteryConfig } from "./config.cemetery.ontario.js";
export const config = Object.assign({}, cemeteryConfig); export const config = Object.assign({}, cemeteryConfig);
config.settings.lot = {
lotNameSortNameFunction: (lotName) => {
const numericPadding = "00000";
const lotNameSplit = lotName.toUpperCase().split("-");
const cleanLotNamePieces = [];
for (const lotNamePiece of lotNameSplit) {
let numericPiece = numericPadding;
let letterPiece = "";
for (const letter of lotNamePiece) {
if (letterPiece === "" && "0123456789".includes(letter)) {
numericPiece += letter;
}
else {
letterPiece += letter;
}
}
cleanLotNamePieces.push(numericPiece.slice(-1 * numericPadding.length) + letterPiece);
}
return cleanLotNamePieces.join("-");
}
};
config.settings.lotOccupancy.occupantCityDefault = "Sault Ste. Marie"; config.settings.lotOccupancy.occupantCityDefault = "Sault Ste. Marie";
config.settings.map.mapCityDefault = "Sault Ste. Marie"; config.settings.map.mapCityDefault = "Sault Ste. Marie";
config.aliases.externalReceiptNumber = "GP Receipt Number"; config.aliases.externalReceiptNumber = "GP Receipt Number";

View File

@ -1,7 +1,39 @@
import { config as cemeteryConfig } from "./config.cemetery.ontario.js"; import {
config as cemeteryConfig
} from "./config.cemetery.ontario.js";
export const config = Object.assign({}, cemeteryConfig); export const config = Object.assign({}, cemeteryConfig);
config.settings.lot = {
lotNameSortNameFunction: (lotName) => {
const numericPadding = "00000";
const lotNameSplit = lotName.toUpperCase().split("-");
const cleanLotNamePieces: string[] = [];
for (const lotNamePiece of lotNameSplit) {
let numericPiece = numericPadding;
let letterPiece = "";
for (const letter of lotNamePiece) {
if (letterPiece === "" && "0123456789".includes(letter)) {
numericPiece += letter;
} else {
letterPiece += letter;
}
}
cleanLotNamePieces.push(numericPiece.slice(-1 * numericPadding.length) + letterPiece);
}
return cleanLotNamePieces.join("-");
}
};
config.settings.lotOccupancy.occupantCityDefault = "Sault Ste. Marie"; config.settings.lotOccupancy.occupantCityDefault = "Sault Ste. Marie";
config.settings.map.mapCityDefault = "Sault Ste. Marie"; config.settings.map.mapCityDefault = "Sault Ste. Marie";

View File

@ -26,6 +26,7 @@ export declare function getProperty(propertyName: "aliases.occupants"): string;
export declare function getProperty(propertyName: "aliases.externalReceiptNumber"): string; export declare function getProperty(propertyName: "aliases.externalReceiptNumber"): string;
export declare function getProperty(propertyName: "settings.map.mapCityDefault"): string; export declare function getProperty(propertyName: "settings.map.mapCityDefault"): string;
export declare function getProperty(propertyName: "settings.map.mapProvinceDefault"): string; export declare function getProperty(propertyName: "settings.map.mapProvinceDefault"): string;
export declare function getProperty(propertyName: "settings.lot.lotNameSortNameFunction"): (lotName: string) => string;
export declare function getProperty(propertyName: "settings.lotOccupancy.occupancyEndDateIsRequired"): boolean; export declare function getProperty(propertyName: "settings.lotOccupancy.occupancyEndDateIsRequired"): boolean;
export declare function getProperty(propertyName: "settings.lotOccupancy.occupantCityDefault"): string; export declare function getProperty(propertyName: "settings.lotOccupancy.occupantCityDefault"): string;
export declare function getProperty(propertyName: "settings.lotOccupancy.occupantProvinceDefault"): string; export declare function getProperty(propertyName: "settings.lotOccupancy.occupantProvinceDefault"): string;

View File

@ -26,6 +26,7 @@ configFallbackValues.set("aliases.occupants", "Occupants");
configFallbackValues.set("aliases.externalReceiptNumber", "External Receipt Number"); configFallbackValues.set("aliases.externalReceiptNumber", "External Receipt Number");
configFallbackValues.set("settings.map.mapCityDefault", ""); configFallbackValues.set("settings.map.mapCityDefault", "");
configFallbackValues.set("settings.map.mapProvinceDefault", ""); configFallbackValues.set("settings.map.mapProvinceDefault", "");
configFallbackValues.set("settings.lot.lotNameSortNameFunction", (lotName) => lotName);
configFallbackValues.set("settings.lotOccupancy.occupancyEndDateIsRequired", true); configFallbackValues.set("settings.lotOccupancy.occupancyEndDateIsRequired", true);
configFallbackValues.set("settings.lotOccupancy.occupantCityDefault", ""); configFallbackValues.set("settings.lotOccupancy.occupantCityDefault", "");
configFallbackValues.set("settings.lotOccupancy.occupantProvinceDefault", ""); configFallbackValues.set("settings.lotOccupancy.occupantProvinceDefault", "");

View File

@ -46,6 +46,8 @@ configFallbackValues.set("aliases.externalReceiptNumber", "External Receipt Numb
configFallbackValues.set("settings.map.mapCityDefault", ""); configFallbackValues.set("settings.map.mapCityDefault", "");
configFallbackValues.set("settings.map.mapProvinceDefault", ""); configFallbackValues.set("settings.map.mapProvinceDefault", "");
configFallbackValues.set("settings.lot.lotNameSortNameFunction", (lotName: string) => lotName);
configFallbackValues.set("settings.lotOccupancy.occupancyEndDateIsRequired", true); configFallbackValues.set("settings.lotOccupancy.occupancyEndDateIsRequired", true);
configFallbackValues.set("settings.lotOccupancy.occupantCityDefault", ""); configFallbackValues.set("settings.lotOccupancy.occupantCityDefault", "");
configFallbackValues.set("settings.lotOccupancy.occupantProvinceDefault", ""); configFallbackValues.set("settings.lotOccupancy.occupantProvinceDefault", "");
@ -91,6 +93,8 @@ export function getProperty(propertyName: "aliases.externalReceiptNumber"): stri
export function getProperty(propertyName: "settings.map.mapCityDefault"): string; export function getProperty(propertyName: "settings.map.mapCityDefault"): string;
export function getProperty(propertyName: "settings.map.mapProvinceDefault"): string; export function getProperty(propertyName: "settings.map.mapProvinceDefault"): string;
export function getProperty(propertyName: "settings.lot.lotNameSortNameFunction"): (lotName: string) => string;
export function getProperty(propertyName: "settings.lotOccupancy.occupancyEndDateIsRequired"): boolean; export function getProperty(propertyName: "settings.lotOccupancy.occupancyEndDateIsRequired"): boolean;
export function getProperty(propertyName: "settings.lotOccupancy.occupantCityDefault"): string; export function getProperty(propertyName: "settings.lotOccupancy.occupantCityDefault"): string;
export function getProperty(propertyName: "settings.lotOccupancy.occupantProvinceDefault"): string; export function getProperty(propertyName: "settings.lotOccupancy.occupantProvinceDefault"): string;

View File

@ -1,3 +1,4 @@
import sqlite from "better-sqlite3";
import type * as recordTypes from "../../types/recordTypes"; import type * as recordTypes from "../../types/recordTypes";
interface GetLotsFilters { interface GetLotsFilters {
lotName?: string; lotName?: string;
@ -10,7 +11,7 @@ interface GetLotsOptions {
limit: number; limit: number;
offset: number; offset: number;
} }
export declare const getLots: (filters?: GetLotsFilters, options?: GetLotsOptions) => { export declare const getLots: (filters: GetLotsFilters, options: GetLotsOptions, connectedDatabase?: sqlite.Database) => {
count: number; count: number;
lots: recordTypes.Lot[]; lots: recordTypes.Lot[];
}; };

View File

@ -1,8 +1,9 @@
import sqlite from "better-sqlite3"; import sqlite from "better-sqlite3";
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
import { dateToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js"; import { dateToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js";
export const getLots = (filters, options) => { import * as configFunctions from "../functions.config.js";
const database = sqlite(databasePath, { export const getLots = (filters, options, connectedDatabase) => {
const database = connectedDatabase || sqlite(databasePath, {
readonly: true readonly: true
}); });
let sqlWhereClause = " where l.recordDelete_timeMillis is null"; let sqlWhereClause = " where l.recordDelete_timeMillis is null";
@ -50,6 +51,7 @@ export const getLots = (filters, options) => {
.recordCount; .recordCount;
let lots = []; let lots = [];
if (count > 0) { if (count > 0) {
database.function("userFn_lotNameSortName", configFunctions.getProperty("settings.lot.lotNameSortNameFunction"));
lots = database lots = database
.prepare("select l.lotId, l.lotName," + .prepare("select l.lotId, l.lotName," +
" t.lotType," + " t.lotType," +
@ -69,13 +71,15 @@ export const getLots = (filters, options) => {
" group by lotId" + " group by lotId" +
") o on l.lotId = o.lotId") + ") o on l.lotId = o.lotId") +
sqlWhereClause + sqlWhereClause +
" order by l.lotName, l.lotId" + " order by userFn_lotNameSortName(l.lotName), l.lotId" +
(options ? (options ?
" limit " + options.limit + " offset " + options.offset : " limit " + options.limit + " offset " + options.offset :
"")) ""))
.all(sqlParameters); .all(sqlParameters);
} }
if (!connectedDatabase) {
database.close(); database.close();
}
return { return {
count, count,
lots lots

View File

@ -8,6 +8,8 @@ import {
dateToInteger dateToInteger
} from "@cityssm/expressjs-server-js/dateTimeFns.js"; } from "@cityssm/expressjs-server-js/dateTimeFns.js";
import * as configFunctions from "../functions.config.js";
import type * as recordTypes from "../../types/recordTypes"; import type * as recordTypes from "../../types/recordTypes";
@ -25,12 +27,14 @@ interface GetLotsOptions {
} }
export const getLots = (filters ? : GetLotsFilters, options ? : GetLotsOptions): { export const getLots = (filters: GetLotsFilters,
options: GetLotsOptions,
connectedDatabase ? : sqlite.Database): {
count: number; count: number;
lots: recordTypes.Lot[]; lots: recordTypes.Lot[];
} => { } => {
const database = sqlite(databasePath, { const database = connectedDatabase || sqlite(databasePath, {
readonly: true readonly: true
}); });
@ -88,6 +92,8 @@ export const getLots = (filters ? : GetLotsFilters, options ? : GetLotsOptions):
if (count > 0) { if (count > 0) {
database.function("userFn_lotNameSortName", configFunctions.getProperty("settings.lot.lotNameSortNameFunction"));
lots = database lots = database
.prepare("select l.lotId, l.lotName," + .prepare("select l.lotId, l.lotName," +
" t.lotType," + " t.lotType," +
@ -107,14 +113,16 @@ export const getLots = (filters ? : GetLotsFilters, options ? : GetLotsOptions):
" group by lotId" + " group by lotId" +
") o on l.lotId = o.lotId") + ") o on l.lotId = o.lotId") +
sqlWhereClause + sqlWhereClause +
" order by l.lotName, l.lotId" + " order by userFn_lotNameSortName(l.lotName), l.lotId" +
(options ? (options ?
" limit " + options.limit + " offset " + options.offset : " limit " + options.limit + " offset " + options.offset :
"")) ""))
.all(sqlParameters); .all(sqlParameters);
} }
if (!connectedDatabase) {
database.close(); database.close();
}
return { return {
count, count,

View File

@ -1,14 +1,16 @@
import sqlite from "better-sqlite3"; import sqlite from "better-sqlite3";
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
import * as configFunctions from "../functions.config.js";
export const getNextLotId = (lotId) => { export const getNextLotId = (lotId) => {
const database = sqlite(databasePath, { const database = sqlite(databasePath, {
readonly: true readonly: true
}); });
database.function("userFn_lotNameSortName", configFunctions.getProperty("settings.lot.lotNameSortNameFunction"));
const result = database const result = database
.prepare("select lotId from Lots" + .prepare("select lotId from Lots" +
" where recordDelete_timeMillis is null" + " where recordDelete_timeMillis is null" +
" and lotName > (select lotName from Lots where lotId = ?)" + " and userFn_lotNameSortName(lotName) > (select userFn_lotNameSortName(lotName) from Lots where lotId = ?)" +
" order by lotName" + " order by userFn_lotNameSortName(lotName)" +
" limit 1") " limit 1")
.get(lotId); .get(lotId);
database.close(); database.close();

View File

@ -4,6 +4,8 @@ import {
lotOccupancyDB as databasePath lotOccupancyDB as databasePath
} from "../../data/databasePaths.js"; } from "../../data/databasePaths.js";
import * as configFunctions from "../functions.config.js";
export const getNextLotId = (lotId: number | string): number => { export const getNextLotId = (lotId: number | string): number => {
@ -11,13 +13,15 @@ export const getNextLotId = (lotId: number | string): number => {
readonly: true readonly: true
}); });
database.function("userFn_lotNameSortName", configFunctions.getProperty("settings.lot.lotNameSortNameFunction"));
const result: { const result: {
lotId: number lotId: number
} = database } = database
.prepare("select lotId from Lots" + .prepare("select lotId from Lots" +
" where recordDelete_timeMillis is null" + " where recordDelete_timeMillis is null" +
" and lotName > (select lotName from Lots where lotId = ?)" + " and userFn_lotNameSortName(lotName) > (select userFn_lotNameSortName(lotName) from Lots where lotId = ?)" +
" order by lotName" + " order by userFn_lotNameSortName(lotName)" +
" limit 1") " limit 1")
.get(lotId); .get(lotId);

View File

@ -1,14 +1,16 @@
import sqlite from "better-sqlite3"; import sqlite from "better-sqlite3";
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
import * as configFunctions from "../functions.config.js";
export const getPreviousLotId = (lotId) => { export const getPreviousLotId = (lotId) => {
const database = sqlite(databasePath, { const database = sqlite(databasePath, {
readonly: true readonly: true
}); });
database.function("userFn_lotNameSortName", configFunctions.getProperty("settings.lot.lotNameSortNameFunction"));
const result = database const result = database
.prepare("select lotId from Lots" + .prepare("select lotId from Lots" +
" where recordDelete_timeMillis is null" + " where recordDelete_timeMillis is null" +
" and lotName < (select lotName from Lots where lotId = ?)" + " and userFn_lotNameSortName(lotName) < (select userFn_lotNameSortName(lotName) from Lots where lotId = ?)" +
" order by lotName desc" + " order by userFn_lotNameSortName(lotName) desc" +
" limit 1") " limit 1")
.get(lotId); .get(lotId);
database.close(); database.close();

View File

@ -4,6 +4,8 @@ import {
lotOccupancyDB as databasePath lotOccupancyDB as databasePath
} from "../../data/databasePaths.js"; } from "../../data/databasePaths.js";
import * as configFunctions from "../functions.config.js";
export const getPreviousLotId = (lotId: number | string): number => { export const getPreviousLotId = (lotId: number | string): number => {
@ -11,13 +13,15 @@ export const getPreviousLotId = (lotId: number | string): number => {
readonly: true readonly: true
}); });
database.function("userFn_lotNameSortName", configFunctions.getProperty("settings.lot.lotNameSortNameFunction"));
const result: { const result: {
lotId: number lotId: number
} = database } = database
.prepare("select lotId from Lots" + .prepare("select lotId from Lots" +
" where recordDelete_timeMillis is null" + " where recordDelete_timeMillis is null" +
" and lotName < (select lotName from Lots where lotId = ?)" + " and userFn_lotNameSortName(lotName) < (select userFn_lotNameSortName(lotName) from Lots where lotId = ?)" +
" order by lotName desc" + " order by userFn_lotNameSortName(lotName) desc" +
" limit 1") " limit 1")
.get(lotId); .get(lotId);

View File

@ -459,6 +459,9 @@ function importFromPrepaidCSV() {
const possibleLots = getLots({ const possibleLots = getLots({
mapId: map.mapId, mapId: map.mapId,
lotName lotName
}, {
limit: -1,
offset: 0
}); });
if (possibleLots.lots.length > 0) { if (possibleLots.lots.length > 0) {
lot = possibleLots.lots[0]; lot = possibleLots.lots[0];

View File

@ -777,7 +777,10 @@ function importFromPrepaidCSV() {
const possibleLots = getLots({ const possibleLots = getLots({
mapId: map.mapId, mapId: map.mapId,
lotName lotName
}); }, {
limit: -1,
offset: 0
};
if (possibleLots.lots.length > 0) { if (possibleLots.lots.length > 0) {
lot = possibleLots.lots[0]; lot = possibleLots.lots[0];

View File

@ -32,6 +32,9 @@ export interface Config {
mapCityDefault?: string; mapCityDefault?: string;
mapProvinceDefault?: string; mapProvinceDefault?: string;
}; };
lot?: {
lotNameSortNameFunction?: (lotName: string) => string;
};
lotOccupancy?: { lotOccupancy?: {
lotIdIsRequired?: boolean; lotIdIsRequired?: boolean;
occupancyEndDateIsRequired?: boolean; occupancyEndDateIsRequired?: boolean;

View File

@ -32,6 +32,9 @@ export interface Config {
mapCityDefault ? : string; mapCityDefault ? : string;
mapProvinceDefault ? : string; mapProvinceDefault ? : string;
}; };
lot ? : {
lotNameSortNameFunction ? : (lotName: string) => string;
};
lotOccupancy ? : { lotOccupancy ? : {
lotIdIsRequired ? : boolean; lotIdIsRequired ? : boolean;
occupancyEndDateIsRequired ? : boolean; occupancyEndDateIsRequired ? : boolean;