From 75ced51d7ec5eb522252b116b684080ba2026c81 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 5 Oct 2022 13:38:57 -0400 Subject: [PATCH] lot name contains, starts with, or ends with --- helpers/lotOccupancyDB/getLotOccupancies.d.ts | 1 + helpers/lotOccupancyDB/getLotOccupancies.js | 27 ++- helpers/lotOccupancyDB/getLotOccupancies.ts | 26 ++- helpers/lotOccupancyDB/getLots.d.ts | 1 + helpers/lotOccupancyDB/getLots.js | 28 +-- helpers/lotOccupancyDB/getLots.ts | 66 ++++--- views/lot-search.ejs | 65 ++++--- views/lotOccupancy-search.ejs | 183 +++++++++--------- 8 files changed, 224 insertions(+), 173 deletions(-) diff --git a/helpers/lotOccupancyDB/getLotOccupancies.d.ts b/helpers/lotOccupancyDB/getLotOccupancies.d.ts index fe7300d6..35ced585 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.d.ts +++ b/helpers/lotOccupancyDB/getLotOccupancies.d.ts @@ -8,6 +8,7 @@ interface GetLotOccupanciesFilters { occupantName?: string; occupancyTypeId?: number | string; mapId?: number | string; + lotNameSearchType?: "" | "startsWith" | "endsWith"; lotName?: string; lotTypeId?: number | string; workOrderId?: number | string; diff --git a/helpers/lotOccupancyDB/getLotOccupancies.js b/helpers/lotOccupancyDB/getLotOccupancies.js index 470d359b..38586795 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.js +++ b/helpers/lotOccupancyDB/getLotOccupancies.js @@ -15,10 +15,20 @@ export const getLotOccupancies = (filters, options, connectedDatabase) => { sqlParameters.push(filters.lotId); } if (filters.lotName) { - const lotNamePieces = filters.lotName.toLowerCase().split(" "); - for (const lotNamePiece of lotNamePieces) { - sqlWhereClause += " and instr(lower(l.lotName), ?)"; - sqlParameters.push(lotNamePiece); + if (filters.lotNameSearchType === "startsWith") { + sqlWhereClause += " and l.lotName like ? || '%'"; + sqlParameters.push(filters.lotName); + } + else if (filters.lotNameSearchType === "endsWith") { + sqlWhereClause += " and l.lotName like '%' || ?"; + sqlParameters.push(filters.lotName); + } + else { + const lotNamePieces = filters.lotName.toLowerCase().split(" "); + for (const lotNamePiece of lotNamePieces) { + sqlWhereClause += " and instr(lower(l.lotName), ?)"; + sqlParameters.push(lotNamePiece); + } } } if (filters.occupantName) { @@ -36,19 +46,22 @@ export const getLotOccupancies = (filters, options, connectedDatabase) => { if (filters.occupancyTime) { const currentDateString = dateToInteger(new Date()); switch (filters.occupancyTime) { - case "current": + case "current": { sqlWhereClause += " and o.occupancyStartDate <= ? and (o.occupancyEndDate is null or o.occupancyEndDate >= ?)"; sqlParameters.push(currentDateString, currentDateString); break; - case "past": + } + case "past": { sqlWhereClause += " and o.occupancyEndDate < ?"; sqlParameters.push(currentDateString); break; - case "future": + } + case "future": { sqlWhereClause += " and o.occupancyStartDate > ?"; sqlParameters.push(currentDateString); break; + } } } if (filters.occupancyStartDateString) { diff --git a/helpers/lotOccupancyDB/getLotOccupancies.ts b/helpers/lotOccupancyDB/getLotOccupancies.ts index 9d3f0ea2..39c58f71 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.ts +++ b/helpers/lotOccupancyDB/getLotOccupancies.ts @@ -20,6 +20,7 @@ interface GetLotOccupanciesFilters { occupantName?: string; occupancyTypeId?: number | string; mapId?: number | string; + lotNameSearchType?: "" | "startsWith" | "endsWith"; lotName?: string; lotTypeId?: number | string; workOrderId?: number | string; @@ -57,10 +58,18 @@ export const getLotOccupancies = ( } if (filters.lotName) { - const lotNamePieces = filters.lotName.toLowerCase().split(" "); - for (const lotNamePiece of lotNamePieces) { - sqlWhereClause += " and instr(lower(l.lotName), ?)"; - sqlParameters.push(lotNamePiece); + if (filters.lotNameSearchType === "startsWith") { + sqlWhereClause += " and l.lotName like ? || '%'"; + sqlParameters.push(filters.lotName); + } else if (filters.lotNameSearchType === "endsWith") { + sqlWhereClause += " and l.lotName like '%' || ?"; + sqlParameters.push(filters.lotName); + } else { + const lotNamePieces = filters.lotName.toLowerCase().split(" "); + for (const lotNamePiece of lotNamePieces) { + sqlWhereClause += " and instr(lower(l.lotName), ?)"; + sqlParameters.push(lotNamePiece); + } } } @@ -82,21 +91,24 @@ export const getLotOccupancies = ( const currentDateString = dateToInteger(new Date()); switch (filters.occupancyTime) { - case "current": + case "current": { sqlWhereClause += " and o.occupancyStartDate <= ? and (o.occupancyEndDate is null or o.occupancyEndDate >= ?)"; sqlParameters.push(currentDateString, currentDateString); break; + } - case "past": + case "past": { sqlWhereClause += " and o.occupancyEndDate < ?"; sqlParameters.push(currentDateString); break; + } - case "future": + case "future": { sqlWhereClause += " and o.occupancyStartDate > ?"; sqlParameters.push(currentDateString); break; + } } } diff --git a/helpers/lotOccupancyDB/getLots.d.ts b/helpers/lotOccupancyDB/getLots.d.ts index d1e4912d..2350fe9a 100644 --- a/helpers/lotOccupancyDB/getLots.d.ts +++ b/helpers/lotOccupancyDB/getLots.d.ts @@ -1,6 +1,7 @@ import sqlite from "better-sqlite3"; import type * as recordTypes from "../../types/recordTypes"; interface GetLotsFilters { + lotNameSearchType?: "" | "startsWith" | "endsWith"; lotName?: string; mapId?: number | string; lotTypeId?: number | string; diff --git a/helpers/lotOccupancyDB/getLots.js b/helpers/lotOccupancyDB/getLots.js index 1fa1b916..4a601600 100644 --- a/helpers/lotOccupancyDB/getLots.js +++ b/helpers/lotOccupancyDB/getLots.js @@ -10,10 +10,20 @@ export const getLots = (filters, options, connectedDatabase) => { let sqlWhereClause = " where l.recordDelete_timeMillis is null"; const sqlParameters = []; if (filters.lotName) { - const lotNamePieces = filters.lotName.toLowerCase().split(" "); - for (const lotNamePiece of lotNamePieces) { - sqlWhereClause += " and instr(lower(l.lotName), ?)"; - sqlParameters.push(lotNamePiece); + if (filters.lotNameSearchType === "startsWith") { + sqlWhereClause += " and l.lotName like ? || '%'"; + sqlParameters.push(filters.lotName); + } + else if (filters.lotNameSearchType === "endsWith") { + sqlWhereClause += " and l.lotName like '%' || ?"; + sqlParameters.push(filters.lotName); + } + else { + const lotNamePieces = filters.lotName.toLowerCase().split(" "); + for (const lotNamePiece of lotNamePieces) { + sqlWhereClause += " and instr(lower(l.lotName), ?)"; + sqlParameters.push(lotNamePiece); + } } } if (filters.mapId) { @@ -33,8 +43,7 @@ export const getLots = (filters, options, connectedDatabase) => { sqlWhereClause += " and lotOccupancyCount > 0"; } else if (filters.occupancyStatus === "unoccupied") { - sqlWhereClause += - " and (lotOccupancyCount is null or lotOccupancyCount = 0)"; + sqlWhereClause += " and (lotOccupancyCount is null or lotOccupancyCount = 0)"; } } if (filters.workOrderId) { @@ -88,12 +97,7 @@ export const getLots = (filters, options, connectedDatabase) => { ") o on l.lotId = o.lotId") + sqlWhereClause + " order by userFn_lotNameSortName(l.lotName), l.lotId" + - (options - ? " limit " + - options.limit + - " offset " + - options.offset - : "")) + (options ? " limit " + options.limit + " offset " + options.offset : "")) .all(sqlParameters); if (options.limit === -1) { count = lots.length; diff --git a/helpers/lotOccupancyDB/getLots.ts b/helpers/lotOccupancyDB/getLots.ts index aca3b231..4655ce92 100644 --- a/helpers/lotOccupancyDB/getLots.ts +++ b/helpers/lotOccupancyDB/getLots.ts @@ -9,6 +9,7 @@ import * as configFunctions from "../functions.config.js"; import type * as recordTypes from "../../types/recordTypes"; interface GetLotsFilters { + lotNameSearchType?: "" | "startsWith" | "endsWith"; lotName?: string; mapId?: number | string; lotTypeId?: number | string; @@ -40,13 +41,22 @@ export const getLots = ( const sqlParameters = []; if (filters.lotName) { - const lotNamePieces = filters.lotName.toLowerCase().split(" "); - for (const lotNamePiece of lotNamePieces) { - sqlWhereClause += " and instr(lower(l.lotName), ?)"; - sqlParameters.push(lotNamePiece); + if (filters.lotNameSearchType === "startsWith") { + sqlWhereClause += " and l.lotName like ? || '%'"; + sqlParameters.push(filters.lotName); + } else if (filters.lotNameSearchType === "endsWith") { + sqlWhereClause += " and l.lotName like '%' || ?"; + sqlParameters.push(filters.lotName); + } else { + const lotNamePieces = filters.lotName.toLowerCase().split(" "); + for (const lotNamePiece of lotNamePieces) { + sqlWhereClause += " and instr(lower(l.lotName), ?)"; + sqlParameters.push(lotNamePiece); + } } } + if (filters.mapId) { sqlWhereClause += " and l.mapId = ?"; sqlParameters.push(filters.mapId); @@ -66,8 +76,7 @@ export const getLots = ( if (filters.occupancyStatus === "occupied") { sqlWhereClause += " and lotOccupancyCount > 0"; } else if (filters.occupancyStatus === "unoccupied") { - sqlWhereClause += - " and (lotOccupancyCount is null or lotOccupancyCount = 0)"; + sqlWhereClause += " and (lotOccupancyCount is null or lotOccupancyCount = 0)"; } } @@ -82,25 +91,25 @@ export const getLots = ( let count: number; if (options.limit !== -1) { - count = database - .prepare( - "select count(*) as recordCount" + - " from Lots l" + - (" left join (" + - "select lotId, count(lotOccupancyId) as lotOccupancyCount" + - " from LotOccupancies" + - " where recordDelete_timeMillis is null" + - " and occupancyStartDate <= " + - currentDate + - " and (occupancyEndDate is null or occupancyEndDate >= " + - currentDate + - ")" + - " group by lotId" + - ") o on l.lotId = o.lotId") + - sqlWhereClause - ) - .get(sqlParameters).recordCount; - } + count = database + .prepare( + "select count(*) as recordCount" + + " from Lots l" + + (" left join (" + + "select lotId, count(lotOccupancyId) as lotOccupancyCount" + + " from LotOccupancies" + + " where recordDelete_timeMillis is null" + + " and occupancyStartDate <= " + + currentDate + + " and (occupancyEndDate is null or occupancyEndDate >= " + + currentDate + + ")" + + " group by lotId" + + ") o on l.lotId = o.lotId") + + sqlWhereClause + ) + .get(sqlParameters).recordCount; + } let lots: recordTypes.Lot[] = []; @@ -134,12 +143,7 @@ export const getLots = ( ") o on l.lotId = o.lotId") + sqlWhereClause + " order by userFn_lotNameSortName(l.lotName), l.lotId" + - (options - ? " limit " + - options.limit + - " offset " + - options.offset - : "") + (options ? " limit " + options.limit + " offset " + options.offset : "") ) .all(sqlParameters); diff --git a/views/lot-search.ejs b/views/lot-search.ejs index 38fc731f..735d3742 100644 --- a/views/lot-search.ejs +++ b/views/lot-search.ejs @@ -33,38 +33,47 @@
-
-
- -
- - - - -
-
-
-
-
- -
-
- +
+ +
+
+
+ +
+ + + +
+
+ +
- - -
+
+
+ +
+
+ +
+ + + +
+
-
diff --git a/views/lotOccupancy-search.ejs b/views/lotOccupancy-search.ejs index 3b3e9c6b..dc7e3260 100644 --- a/views/lotOccupancy-search.ejs +++ b/views/lotOccupancy-search.ejs @@ -18,7 +18,7 @@

- Find an <%= configFunctions.getProperty("aliases.occupancy") %> Record + Find an <%= configFunctions.getProperty("aliases.occupancy") %> Record

<% if (user.userProperties.canUpdate) { %> @@ -34,110 +34,117 @@ <% } %>
-
- - -
-
-
- -
- - - - + + + +
+
+
+ +
+ + + + +
+
+
+
+
+ +
+
+ +
+ + + +
+
+
+
+
+ +
+
+ +
+ + + +
-
-
- +
+
+
+ +
+
+ +
+ + + +
+
+
+
+
+
- +
-
-
-
-
- -
-
- -
- - -
-
-
-
-
- -
-
- -
- - - -
-
-
-
-
- + +
+
+ +
+ + + +
+
- - - -
-
-
- -
-
- -
- - - -
-
-
-
- +