From ab7b5e6867b76c18b4ddafb46df7391d84713b35 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 28 Dec 2022 13:45:49 -0500 Subject: [PATCH] attempt to reduce complexity --- helpers/lotOccupancyDB/getFeeCategories.js | 66 ++------------ helpers/lotOccupancyDB/getFeeCategories.ts | 101 ++++----------------- helpers/lotOccupancyDB/getFees.d.ts | 8 ++ helpers/lotOccupancyDB/getFees.js | 49 ++++++++++ helpers/lotOccupancyDB/getFees.ts | 81 +++++++++++++++++ 5 files changed, 163 insertions(+), 142 deletions(-) create mode 100644 helpers/lotOccupancyDB/getFees.d.ts create mode 100644 helpers/lotOccupancyDB/getFees.js create mode 100644 helpers/lotOccupancyDB/getFees.ts diff --git a/helpers/lotOccupancyDB/getFeeCategories.js b/helpers/lotOccupancyDB/getFeeCategories.js index 99ae2d2c..3e933fbd 100644 --- a/helpers/lotOccupancyDB/getFeeCategories.js +++ b/helpers/lotOccupancyDB/getFeeCategories.js @@ -1,8 +1,12 @@ import sqlite from "better-sqlite3"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; -import { updateFeeOrderNumber } from "./updateFee.js"; +import { getFees } from "./getFees.js"; import { updateFeeCategoryOrderNumber } from "./updateFeeCategory.js"; -const buildFeeCategoryWhereClause = (filters) => { +export const getFeeCategories = (filters, options) => { + const updateOrderNumbers = !(filters.lotTypeId || filters.occupancyTypeId) && options.includeFees; + const database = sqlite(databasePath, { + readonly: !updateOrderNumbers + }); let sqlWhereClause = " where recordDelete_timeMillis is null"; const sqlParameters = []; if (filters.occupancyTypeId) { @@ -21,40 +25,12 @@ const buildFeeCategoryWhereClause = (filters) => { " and (lotTypeId is null or lotTypeId = ?))"; sqlParameters.push(filters.lotTypeId); } - return { - sqlWhereClause, - sqlParameters - }; -}; -const buildFeeWhereClause = (filters, feeCategoryId) => { - let sqlWhereClause = " where f.recordDelete_timeMillis is null" + " and f.feeCategoryId = ?"; - const sqlParameters = []; - sqlParameters.push(feeCategoryId); - if (filters.occupancyTypeId) { - sqlWhereClause += " and (f.occupancyTypeId is null or f.occupancyTypeId = ?)"; - sqlParameters.push(filters.occupancyTypeId); - } - if (filters.lotTypeId) { - sqlWhereClause += " and (f.lotTypeId is null or f.lotTypeId = ?)"; - sqlParameters.push(filters.lotTypeId); - } - return { - sqlWhereClause, - sqlParameters - }; -}; -export const getFeeCategories = (filters, options) => { - const updateOrderNumbers = !(filters.lotTypeId || filters.occupancyTypeId) && options.includeFees; - const database = sqlite(databasePath, { - readonly: !updateOrderNumbers - }); - const feeCategorySqlFilter = buildFeeCategoryWhereClause(filters); const feeCategories = database .prepare("select feeCategoryId, feeCategory, orderNumber" + " from FeeCategories" + - feeCategorySqlFilter.sqlWhereClause + + sqlWhereClause + " order by orderNumber, feeCategory") - .all(feeCategorySqlFilter.sqlParameters); + .all(sqlParameters); if (options.includeFees) { let expectedFeeCategoryOrderNumber = -1; for (const feeCategory of feeCategories) { @@ -63,31 +39,7 @@ export const getFeeCategories = (filters, options) => { updateFeeCategoryOrderNumber(feeCategory.feeCategoryId, expectedFeeCategoryOrderNumber, database); feeCategory.orderNumber = expectedFeeCategoryOrderNumber; } - const feeSqlFilter = buildFeeWhereClause(filters, feeCategory.feeCategoryId); - feeCategory.fees = database - .prepare("select f.feeId, f.feeName, f.feeDescription," + - " f.occupancyTypeId, o.occupancyType," + - " f.lotTypeId, l.lotType," + - " ifnull(f.feeAmount, 0) as feeAmount, f.feeFunction," + - " f.taxAmount, f.taxPercentage," + - " f.includeQuantity, f.quantityUnit," + - " f.isRequired, f.orderNumber" + - " from Fees f" + - " left join OccupancyTypes o on f.occupancyTypeId = o.occupancyTypeId" + - " left join LotTypes l on f.lotTypeId = l.lotTypeId" + - feeSqlFilter.sqlWhereClause + - " order by f.orderNumber, f.feeName") - .all(feeSqlFilter.sqlParameters); - if (updateOrderNumbers) { - let expectedFeeOrderNumber = -1; - for (const fee of feeCategory.fees) { - expectedFeeOrderNumber += 1; - if (fee.orderNumber !== expectedFeeOrderNumber) { - updateFeeOrderNumber(fee.feeId, expectedFeeOrderNumber, database); - fee.orderNumber = expectedFeeOrderNumber; - } - } - } + feeCategory.fees = getFees(feeCategory.feeCategoryId, filters, database); } } database.close(); diff --git a/helpers/lotOccupancyDB/getFeeCategories.ts b/helpers/lotOccupancyDB/getFeeCategories.ts index f296d5be..3e992b1f 100644 --- a/helpers/lotOccupancyDB/getFeeCategories.ts +++ b/helpers/lotOccupancyDB/getFeeCategories.ts @@ -2,7 +2,7 @@ import sqlite from "better-sqlite3"; import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; -import { updateFeeOrderNumber } from "./updateFee.js"; +import { getFees } from "./getFees.js"; import { updateFeeCategoryOrderNumber } from "./updateFeeCategory.js"; import type * as recordTypes from "../../types/recordTypes"; @@ -16,10 +16,19 @@ interface GetFeeCategoriesOptions { includeFees?: boolean; } -const buildFeeCategoryWhereClause = ( - filters: GetFeeCategoriesFilters -): { sqlWhereClause: string; sqlParameters: unknown[] } => { +export const getFeeCategories = ( + filters: GetFeeCategoriesFilters, + options: GetFeeCategoriesOptions +): recordTypes.FeeCategory[] => { + const updateOrderNumbers = + !(filters.lotTypeId || filters.occupancyTypeId) && options.includeFees; + + const database = sqlite(databasePath, { + readonly: !updateOrderNumbers + }); + let sqlWhereClause = " where recordDelete_timeMillis is null"; + const sqlParameters: unknown[] = []; if (filters.occupancyTypeId) { @@ -42,61 +51,14 @@ const buildFeeCategoryWhereClause = ( sqlParameters.push(filters.lotTypeId); } - return { - sqlWhereClause, - sqlParameters - }; -}; - -const buildFeeWhereClause = ( - filters: GetFeeCategoriesFilters, - feeCategoryId: number -): { sqlWhereClause: string; sqlParameters: unknown[] } => { - let sqlWhereClause = " where f.recordDelete_timeMillis is null" + " and f.feeCategoryId = ?"; - - const sqlParameters: unknown[] = []; - - sqlParameters.push(feeCategoryId); - - if (filters.occupancyTypeId) { - sqlWhereClause += " and (f.occupancyTypeId is null or f.occupancyTypeId = ?)"; - - sqlParameters.push(filters.occupancyTypeId); - } - - if (filters.lotTypeId) { - sqlWhereClause += " and (f.lotTypeId is null or f.lotTypeId = ?)"; - - sqlParameters.push(filters.lotTypeId); - } - - return { - sqlWhereClause, - sqlParameters - }; -}; - -export const getFeeCategories = ( - filters: GetFeeCategoriesFilters, - options: GetFeeCategoriesOptions -): recordTypes.FeeCategory[] => { - const updateOrderNumbers = - !(filters.lotTypeId || filters.occupancyTypeId) && options.includeFees; - - const database = sqlite(databasePath, { - readonly: !updateOrderNumbers - }); - - const feeCategorySqlFilter = buildFeeCategoryWhereClause(filters); - const feeCategories: recordTypes.FeeCategory[] = database .prepare( "select feeCategoryId, feeCategory, orderNumber" + " from FeeCategories" + - feeCategorySqlFilter.sqlWhereClause + + sqlWhereClause + " order by orderNumber, feeCategory" ) - .all(feeCategorySqlFilter.sqlParameters); + .all(sqlParameters); if (options.includeFees) { let expectedFeeCategoryOrderNumber = -1; @@ -114,38 +76,7 @@ export const getFeeCategories = ( feeCategory.orderNumber = expectedFeeCategoryOrderNumber; } - const feeSqlFilter = buildFeeWhereClause(filters, feeCategory.feeCategoryId as number); - - feeCategory.fees = database - .prepare( - "select f.feeId, f.feeName, f.feeDescription," + - " f.occupancyTypeId, o.occupancyType," + - " f.lotTypeId, l.lotType," + - " ifnull(f.feeAmount, 0) as feeAmount, f.feeFunction," + - " f.taxAmount, f.taxPercentage," + - " f.includeQuantity, f.quantityUnit," + - " f.isRequired, f.orderNumber" + - " from Fees f" + - " left join OccupancyTypes o on f.occupancyTypeId = o.occupancyTypeId" + - " left join LotTypes l on f.lotTypeId = l.lotTypeId" + - feeSqlFilter.sqlWhereClause + - " order by f.orderNumber, f.feeName" - ) - .all(feeSqlFilter.sqlParameters); - - if (updateOrderNumbers) { - let expectedFeeOrderNumber = -1; - - for (const fee of feeCategory.fees) { - expectedFeeOrderNumber += 1; - - if (fee.orderNumber !== expectedFeeOrderNumber) { - updateFeeOrderNumber(fee.feeId, expectedFeeOrderNumber, database); - - fee.orderNumber = expectedFeeOrderNumber; - } - } - } + feeCategory.fees = getFees(feeCategory.feeCategoryId, filters, database); } } diff --git a/helpers/lotOccupancyDB/getFees.d.ts b/helpers/lotOccupancyDB/getFees.d.ts new file mode 100644 index 00000000..9b3f9850 --- /dev/null +++ b/helpers/lotOccupancyDB/getFees.d.ts @@ -0,0 +1,8 @@ +import sqlite from "better-sqlite3"; +import type * as recordTypes from "../../types/recordTypes"; +interface GetFeesFilters { + occupancyTypeId?: number | string; + lotTypeId?: number | string; +} +export declare const getFees: (feeCategoryId: number, additionalFilters: GetFeesFilters, connectedDatabase?: sqlite.Database) => recordTypes.Fee[]; +export default getFees; diff --git a/helpers/lotOccupancyDB/getFees.js b/helpers/lotOccupancyDB/getFees.js new file mode 100644 index 00000000..1866b835 --- /dev/null +++ b/helpers/lotOccupancyDB/getFees.js @@ -0,0 +1,49 @@ +import sqlite from "better-sqlite3"; +import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; +import { updateFeeOrderNumber } from "./updateFee.js"; +export const getFees = (feeCategoryId, additionalFilters, connectedDatabase) => { + const updateOrderNumbers = !(additionalFilters.lotTypeId || additionalFilters.occupancyTypeId); + const database = connectedDatabase || + sqlite(databasePath, { + readonly: !updateOrderNumbers + }); + let sqlWhereClause = " where f.recordDelete_timeMillis is null" + " and f.feeCategoryId = ?"; + const sqlParameters = [feeCategoryId]; + if (additionalFilters.occupancyTypeId) { + sqlWhereClause += " and (f.occupancyTypeId is null or f.occupancyTypeId = ?)"; + sqlParameters.push(additionalFilters.occupancyTypeId); + } + if (additionalFilters.lotTypeId) { + sqlWhereClause += " and (f.lotTypeId is null or f.lotTypeId = ?)"; + sqlParameters.push(additionalFilters.lotTypeId); + } + const fees = database + .prepare("select f.feeId, f.feeName, f.feeDescription," + + " f.occupancyTypeId, o.occupancyType," + + " f.lotTypeId, l.lotType," + + " ifnull(f.feeAmount, 0) as feeAmount, f.feeFunction," + + " f.taxAmount, f.taxPercentage," + + " f.includeQuantity, f.quantityUnit," + + " f.isRequired, f.orderNumber" + + " from Fees f" + + " left join OccupancyTypes o on f.occupancyTypeId = o.occupancyTypeId" + + " left join LotTypes l on f.lotTypeId = l.lotTypeId" + + sqlWhereClause + + " order by f.orderNumber, f.feeName") + .all(sqlParameters); + if (updateOrderNumbers) { + let expectedFeeOrderNumber = -1; + for (const fee of fees) { + expectedFeeOrderNumber += 1; + if (fee.orderNumber !== expectedFeeOrderNumber) { + updateFeeOrderNumber(fee.feeId, expectedFeeOrderNumber, database); + fee.orderNumber = expectedFeeOrderNumber; + } + } + } + if (!connectedDatabase) { + database.close(); + } + return fees; +}; +export default getFees; diff --git a/helpers/lotOccupancyDB/getFees.ts b/helpers/lotOccupancyDB/getFees.ts new file mode 100644 index 00000000..76d2422e --- /dev/null +++ b/helpers/lotOccupancyDB/getFees.ts @@ -0,0 +1,81 @@ +import sqlite from "better-sqlite3"; + +import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; + +import { updateFeeOrderNumber } from "./updateFee.js"; + +import type * as recordTypes from "../../types/recordTypes"; + +interface GetFeesFilters { + occupancyTypeId?: number | string; + lotTypeId?: number | string; +} + +export const getFees = ( + feeCategoryId: number, + additionalFilters: GetFeesFilters, + connectedDatabase?: sqlite.Database +): recordTypes.Fee[] => { + const updateOrderNumbers = !(additionalFilters.lotTypeId || additionalFilters.occupancyTypeId); + + const database = + connectedDatabase || + sqlite(databasePath, { + readonly: !updateOrderNumbers + }); + + let sqlWhereClause = " where f.recordDelete_timeMillis is null" + " and f.feeCategoryId = ?"; + + const sqlParameters: unknown[] = [feeCategoryId]; + + if (additionalFilters.occupancyTypeId) { + sqlWhereClause += " and (f.occupancyTypeId is null or f.occupancyTypeId = ?)"; + + sqlParameters.push(additionalFilters.occupancyTypeId); + } + + if (additionalFilters.lotTypeId) { + sqlWhereClause += " and (f.lotTypeId is null or f.lotTypeId = ?)"; + + sqlParameters.push(additionalFilters.lotTypeId); + } + + const fees = database + .prepare( + "select f.feeId, f.feeName, f.feeDescription," + + " f.occupancyTypeId, o.occupancyType," + + " f.lotTypeId, l.lotType," + + " ifnull(f.feeAmount, 0) as feeAmount, f.feeFunction," + + " f.taxAmount, f.taxPercentage," + + " f.includeQuantity, f.quantityUnit," + + " f.isRequired, f.orderNumber" + + " from Fees f" + + " left join OccupancyTypes o on f.occupancyTypeId = o.occupancyTypeId" + + " left join LotTypes l on f.lotTypeId = l.lotTypeId" + + sqlWhereClause + + " order by f.orderNumber, f.feeName" + ) + .all(sqlParameters); + + if (updateOrderNumbers) { + let expectedFeeOrderNumber = -1; + + for (const fee of fees) { + expectedFeeOrderNumber += 1; + + if (fee.orderNumber !== expectedFeeOrderNumber) { + updateFeeOrderNumber(fee.feeId, expectedFeeOrderNumber, database); + + fee.orderNumber = expectedFeeOrderNumber; + } + } + } + + if (!connectedDatabase) { + database.close(); + } + + return fees; +}; + +export default getFees;