diff --git a/helpers/lotOccupancyDB/moveLotOccupantTypeDown.js b/helpers/lotOccupancyDB/moveLotOccupantTypeDown.js index 603ac71f..cbd8acd1 100644 --- a/helpers/lotOccupancyDB/moveLotOccupantTypeDown.js +++ b/helpers/lotOccupancyDB/moveLotOccupantTypeDown.js @@ -1,11 +1,11 @@ import sqlite from "better-sqlite3"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; -import { clearLotOccupantTypesCache } from "../functions.cache.js"; +import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js"; export function moveLotOccupantTypeDown(lotOccupantTypeId) { + const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId).orderNumber; const database = sqlite(databasePath); - const currentOrderNumber = database - .prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`) - .get(lotOccupantTypeId).orderNumber; database .prepare(`update LotOccupantTypes set orderNumber = orderNumber - 1 @@ -20,10 +20,10 @@ export function moveLotOccupantTypeDown(lotOccupantTypeId) { return result.changes > 0; } export function moveLotOccupantTypeDownToBottom(lotOccupantTypeId) { + const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId).orderNumber; const database = sqlite(databasePath); - const currentOrderNumber = database - .prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?") - .get(lotOccupantTypeId).orderNumber; const maxOrderNumber = database .prepare(`select max(orderNumber) as maxOrderNumber from LotOccupantTypes diff --git a/helpers/lotOccupancyDB/moveLotOccupantTypeDown.ts b/helpers/lotOccupancyDB/moveLotOccupantTypeDown.ts index f3056558..517d15ea 100644 --- a/helpers/lotOccupancyDB/moveLotOccupantTypeDown.ts +++ b/helpers/lotOccupancyDB/moveLotOccupantTypeDown.ts @@ -2,14 +2,16 @@ import sqlite from "better-sqlite3"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; -import { clearLotOccupantTypesCache } from "../functions.cache.js"; +import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js"; export function moveLotOccupantTypeDown(lotOccupantTypeId: number | string): boolean { - const database = sqlite(databasePath); + const currentOrderNumber: number = getLotOccupantTypeById( + typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId + ).orderNumber; - const currentOrderNumber: number = database - .prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`) - .get(lotOccupantTypeId).orderNumber; + const database = sqlite(databasePath); database .prepare( @@ -32,11 +34,13 @@ export function moveLotOccupantTypeDown(lotOccupantTypeId: number | string): boo } export function moveLotOccupantTypeDownToBottom(lotOccupantTypeId: number | string): boolean { - const database = sqlite(databasePath); + const currentOrderNumber: number = getLotOccupantTypeById( + typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId + ).orderNumber; - const currentOrderNumber: number = database - .prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?") - .get(lotOccupantTypeId).orderNumber; + const database = sqlite(databasePath); const maxOrderNumber: number = database .prepare( diff --git a/helpers/lotOccupancyDB/moveLotOccupantTypeUp.js b/helpers/lotOccupancyDB/moveLotOccupantTypeUp.js index 049fca7b..4747a08c 100644 --- a/helpers/lotOccupancyDB/moveLotOccupantTypeUp.js +++ b/helpers/lotOccupancyDB/moveLotOccupantTypeUp.js @@ -1,15 +1,14 @@ import sqlite from "better-sqlite3"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; -import { clearLotOccupantTypesCache } from "../functions.cache.js"; +import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js"; export function moveLotOccupantTypeUp(lotOccupantTypeId) { - const database = sqlite(databasePath); - const currentOrderNumber = database - .prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`) - .get(lotOccupantTypeId).orderNumber; + const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId).orderNumber; if (currentOrderNumber <= 0) { - database.close(); return true; } + const database = sqlite(databasePath); database .prepare(`update LotOccupantTypes set orderNumber = orderNumber + 1 @@ -26,11 +25,11 @@ export function moveLotOccupantTypeUp(lotOccupantTypeId) { return result.changes > 0; } export function moveLotOccupantTypeUpToTop(lotOccupantTypeId) { - const database = sqlite(databasePath); - const currentOrderNumber = database - .prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?") - .get(lotOccupantTypeId).orderNumber; + const currentOrderNumber = getLotOccupantTypeById(typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId).orderNumber; if (currentOrderNumber > 0) { + const database = sqlite(databasePath); database .prepare("update LotOccupantTypes set orderNumber = -1 where lotOccupantTypeId = ?") .run(lotOccupantTypeId); @@ -40,9 +39,9 @@ export function moveLotOccupantTypeUpToTop(lotOccupantTypeId) { where recordDelete_timeMillis is null and orderNumber < ?`) .run(currentOrderNumber); + database.close(); + clearLotOccupantTypesCache(); } - database.close(); - clearLotOccupantTypesCache(); return true; } export default moveLotOccupantTypeUp; diff --git a/helpers/lotOccupancyDB/moveLotOccupantTypeUp.ts b/helpers/lotOccupancyDB/moveLotOccupantTypeUp.ts index bec46909..a5d780db 100644 --- a/helpers/lotOccupancyDB/moveLotOccupantTypeUp.ts +++ b/helpers/lotOccupancyDB/moveLotOccupantTypeUp.ts @@ -2,20 +2,21 @@ import sqlite from "better-sqlite3"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; -import { clearLotOccupantTypesCache } from "../functions.cache.js"; +import { getLotOccupantTypeById, clearLotOccupantTypesCache } from "../functions.cache.js"; export function moveLotOccupantTypeUp(lotOccupantTypeId: number | string): boolean { - const database = sqlite(databasePath); - - const currentOrderNumber: number = database - .prepare(`select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?`) - .get(lotOccupantTypeId).orderNumber; + const currentOrderNumber: number = getLotOccupantTypeById( + typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId + ).orderNumber; if (currentOrderNumber <= 0) { - database.close(); return true; } + const database = sqlite(databasePath); + database .prepare( `update LotOccupantTypes @@ -41,13 +42,15 @@ export function moveLotOccupantTypeUp(lotOccupantTypeId: number | string): boole } export function moveLotOccupantTypeUpToTop(lotOccupantTypeId: number | string): boolean { - const database = sqlite(databasePath); - - const currentOrderNumber: number = database - .prepare("select orderNumber from LotOccupantTypes where lotOccupantTypeId = ?") - .get(lotOccupantTypeId).orderNumber; + const currentOrderNumber: number = getLotOccupantTypeById( + typeof lotOccupantTypeId === "string" + ? Number.parseInt(lotOccupantTypeId) + : lotOccupantTypeId + ).orderNumber; if (currentOrderNumber > 0) { + const database = sqlite(databasePath); + database .prepare("update LotOccupantTypes set orderNumber = -1 where lotOccupantTypeId = ?") .run(lotOccupantTypeId); @@ -60,12 +63,12 @@ export function moveLotOccupantTypeUpToTop(lotOccupantTypeId: number | string): and orderNumber < ?` ) .run(currentOrderNumber); + + database.close(); + + clearLotOccupantTypesCache(); } - database.close(); - - clearLotOccupantTypesCache(); - return true; }