lot name contains, starts with, or ends with

deepsource-autofix-76c6eb20
Dan Gowans 2022-10-05 13:38:57 -04:00
parent 69c97d0d02
commit 75ced51d7e
8 changed files with 224 additions and 173 deletions

View File

@ -8,6 +8,7 @@ interface GetLotOccupanciesFilters {
occupantName?: string; occupantName?: string;
occupancyTypeId?: number | string; occupancyTypeId?: number | string;
mapId?: number | string; mapId?: number | string;
lotNameSearchType?: "" | "startsWith" | "endsWith";
lotName?: string; lotName?: string;
lotTypeId?: number | string; lotTypeId?: number | string;
workOrderId?: number | string; workOrderId?: number | string;

View File

@ -15,12 +15,22 @@ export const getLotOccupancies = (filters, options, connectedDatabase) => {
sqlParameters.push(filters.lotId); sqlParameters.push(filters.lotId);
} }
if (filters.lotName) { if (filters.lotName) {
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(" "); const lotNamePieces = filters.lotName.toLowerCase().split(" ");
for (const lotNamePiece of lotNamePieces) { for (const lotNamePiece of lotNamePieces) {
sqlWhereClause += " and instr(lower(l.lotName), ?)"; sqlWhereClause += " and instr(lower(l.lotName), ?)";
sqlParameters.push(lotNamePiece); sqlParameters.push(lotNamePiece);
} }
} }
}
if (filters.occupantName) { if (filters.occupantName) {
const occupantNamePieces = filters.occupantName.toLowerCase().split(" "); const occupantNamePieces = filters.occupantName.toLowerCase().split(" ");
for (const occupantNamePiece of occupantNamePieces) { for (const occupantNamePiece of occupantNamePieces) {
@ -36,21 +46,24 @@ export const getLotOccupancies = (filters, options, connectedDatabase) => {
if (filters.occupancyTime) { if (filters.occupancyTime) {
const currentDateString = dateToInteger(new Date()); const currentDateString = dateToInteger(new Date());
switch (filters.occupancyTime) { switch (filters.occupancyTime) {
case "current": case "current": {
sqlWhereClause += sqlWhereClause +=
" and o.occupancyStartDate <= ? and (o.occupancyEndDate is null or o.occupancyEndDate >= ?)"; " and o.occupancyStartDate <= ? and (o.occupancyEndDate is null or o.occupancyEndDate >= ?)";
sqlParameters.push(currentDateString, currentDateString); sqlParameters.push(currentDateString, currentDateString);
break; break;
case "past": }
case "past": {
sqlWhereClause += " and o.occupancyEndDate < ?"; sqlWhereClause += " and o.occupancyEndDate < ?";
sqlParameters.push(currentDateString); sqlParameters.push(currentDateString);
break; break;
case "future": }
case "future": {
sqlWhereClause += " and o.occupancyStartDate > ?"; sqlWhereClause += " and o.occupancyStartDate > ?";
sqlParameters.push(currentDateString); sqlParameters.push(currentDateString);
break; break;
} }
} }
}
if (filters.occupancyStartDateString) { if (filters.occupancyStartDateString) {
sqlWhereClause += " and o.occupancyStartDate = ?"; sqlWhereClause += " and o.occupancyStartDate = ?";
sqlParameters.push(dateStringToInteger(filters.occupancyStartDateString)); sqlParameters.push(dateStringToInteger(filters.occupancyStartDateString));

View File

@ -20,6 +20,7 @@ interface GetLotOccupanciesFilters {
occupantName?: string; occupantName?: string;
occupancyTypeId?: number | string; occupancyTypeId?: number | string;
mapId?: number | string; mapId?: number | string;
lotNameSearchType?: "" | "startsWith" | "endsWith";
lotName?: string; lotName?: string;
lotTypeId?: number | string; lotTypeId?: number | string;
workOrderId?: number | string; workOrderId?: number | string;
@ -57,12 +58,20 @@ export const getLotOccupancies = (
} }
if (filters.lotName) { if (filters.lotName) {
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(" "); const lotNamePieces = filters.lotName.toLowerCase().split(" ");
for (const lotNamePiece of lotNamePieces) { for (const lotNamePiece of lotNamePieces) {
sqlWhereClause += " and instr(lower(l.lotName), ?)"; sqlWhereClause += " and instr(lower(l.lotName), ?)";
sqlParameters.push(lotNamePiece); sqlParameters.push(lotNamePiece);
} }
} }
}
if (filters.occupantName) { if (filters.occupantName) {
const occupantNamePieces = filters.occupantName.toLowerCase().split(" "); const occupantNamePieces = filters.occupantName.toLowerCase().split(" ");
@ -82,23 +91,26 @@ export const getLotOccupancies = (
const currentDateString = dateToInteger(new Date()); const currentDateString = dateToInteger(new Date());
switch (filters.occupancyTime) { switch (filters.occupancyTime) {
case "current": case "current": {
sqlWhereClause += sqlWhereClause +=
" and o.occupancyStartDate <= ? and (o.occupancyEndDate is null or o.occupancyEndDate >= ?)"; " and o.occupancyStartDate <= ? and (o.occupancyEndDate is null or o.occupancyEndDate >= ?)";
sqlParameters.push(currentDateString, currentDateString); sqlParameters.push(currentDateString, currentDateString);
break; break;
}
case "past": case "past": {
sqlWhereClause += " and o.occupancyEndDate < ?"; sqlWhereClause += " and o.occupancyEndDate < ?";
sqlParameters.push(currentDateString); sqlParameters.push(currentDateString);
break; break;
}
case "future": case "future": {
sqlWhereClause += " and o.occupancyStartDate > ?"; sqlWhereClause += " and o.occupancyStartDate > ?";
sqlParameters.push(currentDateString); sqlParameters.push(currentDateString);
break; break;
} }
} }
}
if (filters.occupancyStartDateString) { if (filters.occupancyStartDateString) {
sqlWhereClause += " and o.occupancyStartDate = ?"; sqlWhereClause += " and o.occupancyStartDate = ?";

View File

@ -1,6 +1,7 @@
import sqlite from "better-sqlite3"; import sqlite from "better-sqlite3";
import type * as recordTypes from "../../types/recordTypes"; import type * as recordTypes from "../../types/recordTypes";
interface GetLotsFilters { interface GetLotsFilters {
lotNameSearchType?: "" | "startsWith" | "endsWith";
lotName?: string; lotName?: string;
mapId?: number | string; mapId?: number | string;
lotTypeId?: number | string; lotTypeId?: number | string;

View File

@ -10,12 +10,22 @@ export const getLots = (filters, options, connectedDatabase) => {
let sqlWhereClause = " where l.recordDelete_timeMillis is null"; let sqlWhereClause = " where l.recordDelete_timeMillis is null";
const sqlParameters = []; const sqlParameters = [];
if (filters.lotName) { if (filters.lotName) {
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(" "); const lotNamePieces = filters.lotName.toLowerCase().split(" ");
for (const lotNamePiece of lotNamePieces) { for (const lotNamePiece of lotNamePieces) {
sqlWhereClause += " and instr(lower(l.lotName), ?)"; sqlWhereClause += " and instr(lower(l.lotName), ?)";
sqlParameters.push(lotNamePiece); sqlParameters.push(lotNamePiece);
} }
} }
}
if (filters.mapId) { if (filters.mapId) {
sqlWhereClause += " and l.mapId = ?"; sqlWhereClause += " and l.mapId = ?";
sqlParameters.push(filters.mapId); sqlParameters.push(filters.mapId);
@ -33,8 +43,7 @@ export const getLots = (filters, options, connectedDatabase) => {
sqlWhereClause += " and lotOccupancyCount > 0"; sqlWhereClause += " and lotOccupancyCount > 0";
} }
else if (filters.occupancyStatus === "unoccupied") { else if (filters.occupancyStatus === "unoccupied") {
sqlWhereClause += sqlWhereClause += " and (lotOccupancyCount is null or lotOccupancyCount = 0)";
" and (lotOccupancyCount is null or lotOccupancyCount = 0)";
} }
} }
if (filters.workOrderId) { if (filters.workOrderId) {
@ -88,12 +97,7 @@ export const getLots = (filters, options, connectedDatabase) => {
") o on l.lotId = o.lotId") + ") o on l.lotId = o.lotId") +
sqlWhereClause + sqlWhereClause +
" order by userFn_lotNameSortName(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 (options.limit === -1) { if (options.limit === -1) {
count = lots.length; count = lots.length;

View File

@ -9,6 +9,7 @@ import * as configFunctions from "../functions.config.js";
import type * as recordTypes from "../../types/recordTypes"; import type * as recordTypes from "../../types/recordTypes";
interface GetLotsFilters { interface GetLotsFilters {
lotNameSearchType?: "" | "startsWith" | "endsWith";
lotName?: string; lotName?: string;
mapId?: number | string; mapId?: number | string;
lotTypeId?: number | string; lotTypeId?: number | string;
@ -40,12 +41,21 @@ export const getLots = (
const sqlParameters = []; const sqlParameters = [];
if (filters.lotName) { if (filters.lotName) {
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(" "); const lotNamePieces = filters.lotName.toLowerCase().split(" ");
for (const lotNamePiece of lotNamePieces) { for (const lotNamePiece of lotNamePieces) {
sqlWhereClause += " and instr(lower(l.lotName), ?)"; sqlWhereClause += " and instr(lower(l.lotName), ?)";
sqlParameters.push(lotNamePiece); sqlParameters.push(lotNamePiece);
} }
} }
}
if (filters.mapId) { if (filters.mapId) {
sqlWhereClause += " and l.mapId = ?"; sqlWhereClause += " and l.mapId = ?";
@ -66,8 +76,7 @@ export const getLots = (
if (filters.occupancyStatus === "occupied") { if (filters.occupancyStatus === "occupied") {
sqlWhereClause += " and lotOccupancyCount > 0"; sqlWhereClause += " and lotOccupancyCount > 0";
} else if (filters.occupancyStatus === "unoccupied") { } else if (filters.occupancyStatus === "unoccupied") {
sqlWhereClause += sqlWhereClause += " and (lotOccupancyCount is null or lotOccupancyCount = 0)";
" and (lotOccupancyCount is null or lotOccupancyCount = 0)";
} }
} }
@ -134,12 +143,7 @@ export const getLots = (
") o on l.lotId = o.lotId") + ") o on l.lotId = o.lotId") +
sqlWhereClause + sqlWhereClause +
" order by userFn_lotNameSortName(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);

View File

@ -34,14 +34,23 @@
<input id="searchFilter--offset" name="offset" type="hidden" value="0" /> <input id="searchFilter--offset" name="offset" type="hidden" value="0" />
<div class="columns"> <div class="columns">
<div class="column"> <div class="column">
<div class="field">
<label class="label" for="searchFilter--lotName"><%= configFunctions.getProperty("aliases.lot") %></label> <label class="label" for="searchFilter--lotName"><%= configFunctions.getProperty("aliases.lot") %></label>
<div class="field has-addons">
<div class="control has-icons-left"> <div class="control has-icons-left">
<input class="input" id="searchFilter--lotName" name="lotName" accesskey="f" /> <div class="select">
<select id="selectFilter--lotNameSearchType" name="lotNameSearchType">
<option value="">contains</option>
<option value="startsWith">starts with</option>
<option value="endsWith">ends with</option>
</select>
</div>
<span class="icon is-small is-left"> <span class="icon is-small is-left">
<i class="fas fa-search" aria-hidden="true"></i> <i class="fas fa-search" aria-hidden="true"></i>
</span> </span>
</div> </div>
<div class="control is-expanded">
<input class="input" id="searchFilter--lotName" name="lotName" />
</div>
</div> </div>
</div> </div>
<div class="column"> <div class="column">

View File

@ -107,17 +107,6 @@
</div> </div>
</div> </div>
</div> </div>
<div class="column">
<div class="field">
<label class="label" for="searchFilter--lotName"><%= configFunctions.getProperty("aliases.lot") %></label>
<div class="control has-icons-left">
<input class="input" id="searchFilter--lotName" name="lotName" />
<span class="icon is-small is-left">
<i class="fas fa-search" aria-hidden="true"></i>
</span>
</div>
</div>
</div>
<div class="column"> <div class="column">
<div class="field"> <div class="field">
<label class="label" for="searchFilter--lotTypeId"><%= configFunctions.getProperty("aliases.lot") %> Type</label> <label class="label" for="searchFilter--lotTypeId"><%= configFunctions.getProperty("aliases.lot") %> Type</label>
@ -137,6 +126,24 @@
</div> </div>
</div> </div>
</div> </div>
<label class="label" for="searchFilter--lotName"><%= configFunctions.getProperty("aliases.lot") %></label>
<div class="field has-addons">
<div class="control has-icons-left">
<div class="select">
<select id="selectFilter--lotNameSearchType" name="lotNameSearchType">
<option value="">contains</option>
<option value="startsWith">starts with</option>
<option value="endsWith">ends with</option>
</select>
</div>
<span class="icon is-small is-left">
<i class="fas fa-search" aria-hidden="true"></i>
</span>
</div>
<div class="control is-expanded">
<input class="input" id="searchFilter--lotName" name="lotName" />
</div>
</div>
</form> </form>
</div> </div>