From bd70eaa955c73e4fb93675314b9cdca2e2a2504f Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 28 Dec 2022 16:14:03 -0500 Subject: [PATCH] fix occupantname filter --- helpers/functions.sqlFilters.js | 5 +---- helpers/functions.sqlFilters.ts | 5 +---- helpers/lotOccupancyDB/getLotOccupancies.js | 6 ++++-- helpers/lotOccupancyDB/getLotOccupancies.ts | 9 +++++---- helpers/lotOccupancyDB/getWorkOrders.js | 2 ++ helpers/lotOccupancyDB/getWorkOrders.ts | 2 ++ 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/helpers/functions.sqlFilters.js b/helpers/functions.sqlFilters.js index 68cccf46..3e73a39f 100644 --- a/helpers/functions.sqlFilters.js +++ b/helpers/functions.sqlFilters.js @@ -69,10 +69,7 @@ export const getOccupantNameWhereClause = (occupantName, tableAlias = "o") => { if (occupantName) { const occupantNamePieces = occupantName.toLowerCase().split(" "); for (const occupantNamePiece of occupantNamePieces) { - sqlWhereClause += - " and " + - tableAlias + - ".lotOccupancyId in (select lotOccupancyId from LotOccupancyOccupants where recordDelete_timeMillis is null and instr(lower(occupantName), ?))"; + sqlWhereClause += " and instr(lower(" + tableAlias + ".occupantName), ?)"; sqlParameters.push(occupantNamePiece); } } diff --git a/helpers/functions.sqlFilters.ts b/helpers/functions.sqlFilters.ts index 902e90ae..0dd535ec 100644 --- a/helpers/functions.sqlFilters.ts +++ b/helpers/functions.sqlFilters.ts @@ -91,10 +91,7 @@ export const getOccupantNameWhereClause = (occupantName: string, tableAlias = "o if (occupantName) { const occupantNamePieces = occupantName.toLowerCase().split(" "); for (const occupantNamePiece of occupantNamePieces) { - sqlWhereClause += - " and " + - tableAlias + - ".lotOccupancyId in (select lotOccupancyId from LotOccupancyOccupants where recordDelete_timeMillis is null and instr(lower(occupantName), ?))"; + sqlWhereClause += " and instr(lower(" + tableAlias + ".occupantName), ?)"; sqlParameters.push(occupantNamePiece); } } diff --git a/helpers/lotOccupancyDB/getLotOccupancies.js b/helpers/lotOccupancyDB/getLotOccupancies.js index 6dd1a9a8..64c8ba84 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.js +++ b/helpers/lotOccupancyDB/getLotOccupancies.js @@ -16,8 +16,10 @@ const buildWhereClause = (filters) => { sqlWhereClause += lotNameFilters.sqlWhereClause; sqlParameters.push(...lotNameFilters.sqlParameters); const occupantNameFilters = getOccupantNameWhereClause(filters.occupantName, "o"); - sqlWhereClause += occupantNameFilters.sqlWhereClause; - sqlParameters.push(...occupantNameFilters.sqlParameters); + if (occupantNameFilters.sqlParameters.length > 0) { + sqlWhereClause += " and o.lotOccupancyId in (select lotOccupancyId from LotOccupancyOccupants o where recordDelete_timeMillis is null" + occupantNameFilters.sqlWhereClause + ")"; + sqlParameters.push(...occupantNameFilters.sqlParameters); + } if (filters.occupancyTypeId) { sqlWhereClause += " and o.occupancyTypeId = ?"; sqlParameters.push(filters.occupancyTypeId); diff --git a/helpers/lotOccupancyDB/getLotOccupancies.ts b/helpers/lotOccupancyDB/getLotOccupancies.ts index a67fe3ae..2fbfaef1 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.ts +++ b/helpers/lotOccupancyDB/getLotOccupancies.ts @@ -4,8 +4,7 @@ import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; import { dateIntegerToString, - dateStringToInteger, - dateToInteger + dateStringToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js"; import * as configFunctions from "../functions.config.js"; @@ -53,8 +52,10 @@ const buildWhereClause = ( sqlParameters.push(...lotNameFilters.sqlParameters); const occupantNameFilters = getOccupantNameWhereClause(filters.occupantName, "o"); - sqlWhereClause += occupantNameFilters.sqlWhereClause; - sqlParameters.push(...occupantNameFilters.sqlParameters); + if (occupantNameFilters.sqlParameters.length > 0) { + sqlWhereClause += " and o.lotOccupancyId in (select lotOccupancyId from LotOccupancyOccupants o where recordDelete_timeMillis is null" + occupantNameFilters.sqlWhereClause + ")"; + sqlParameters.push(...occupantNameFilters.sqlParameters); + } if (filters.occupancyTypeId) { sqlWhereClause += " and o.occupancyTypeId = ?"; diff --git a/helpers/lotOccupancyDB/getWorkOrders.js b/helpers/lotOccupancyDB/getWorkOrders.js index fc9dc2ec..0b1bb5ea 100644 --- a/helpers/lotOccupancyDB/getWorkOrders.js +++ b/helpers/lotOccupancyDB/getWorkOrders.js @@ -31,7 +31,9 @@ const buildWhereClause = (filters) => { " and w.workOrderId in (" + "select workOrderId from WorkOrderLotOccupancies o" + " where recordDelete_timeMillis is null" + + " and o.lotOccupancyId in (select lotOccupancyId from LotOccupancyOccupants o where recordDelete_timeMillis is null" + occupantNameFilters.sqlWhereClause + + ")" + ")"; sqlParameters.push(...occupantNameFilters.sqlParameters); } diff --git a/helpers/lotOccupancyDB/getWorkOrders.ts b/helpers/lotOccupancyDB/getWorkOrders.ts index 86720dc0..b1c2b5a6 100644 --- a/helpers/lotOccupancyDB/getWorkOrders.ts +++ b/helpers/lotOccupancyDB/getWorkOrders.ts @@ -62,7 +62,9 @@ const buildWhereClause = ( " and w.workOrderId in (" + "select workOrderId from WorkOrderLotOccupancies o" + " where recordDelete_timeMillis is null" + + " and o.lotOccupancyId in (select lotOccupancyId from LotOccupancyOccupants o where recordDelete_timeMillis is null" + occupantNameFilters.sqlWhereClause + + ")" + ")"; sqlParameters.push(...occupantNameFilters.sqlParameters); }