From 095eb2e5f8086a66a6351a049780188f0e24652e Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Tue, 28 Mar 2023 13:05:48 -0400 Subject: [PATCH] attempt to reduce complexity --- helpers/lotOccupancyDB/getWorkOrders.js | 84 ++++++++-------- helpers/lotOccupancyDB/getWorkOrders.ts | 128 +++++++++++++----------- 2 files changed, 115 insertions(+), 97 deletions(-) diff --git a/helpers/lotOccupancyDB/getWorkOrders.js b/helpers/lotOccupancyDB/getWorkOrders.js index 1076ba7a..c02d2522 100644 --- a/helpers/lotOccupancyDB/getWorkOrders.js +++ b/helpers/lotOccupancyDB/getWorkOrders.js @@ -55,12 +55,55 @@ function buildWhereClause(filters) { sqlParameters }; } +async function addInclusions(workOrder, options, database) { + if (options.includeComments ?? false) { + workOrder.workOrderComments = await getWorkOrderComments(workOrder.workOrderId, database); + } + if (options.includeLotsAndLotOccupancies ?? false) { + if (workOrder.workOrderLotCount === 0) { + workOrder.workOrderLots = []; + } + else { + const workOrderLotsResults = await getLots({ + workOrderId: workOrder.workOrderId + }, { + limit: -1, + offset: 0, + includeLotOccupancyCount: false + }, database); + workOrder.workOrderLots = workOrderLotsResults.lots; + } + const lotOccupancies = await getLotOccupancies({ + workOrderId: workOrder.workOrderId + }, { + limit: -1, + offset: 0, + includeOccupants: true, + includeFees: false, + includeTransactions: false + }, database); + workOrder.workOrderLotOccupancies = lotOccupancies.lotOccupancies; + } + if (options.includeMilestones ?? false) { + workOrder.workOrderMilestones = + workOrder.workOrderMilestoneCount === 0 + ? [] + : await getWorkOrderMilestones({ + workOrderId: workOrder.workOrderId + }, { + orderBy: 'date' + }, database); + } + return workOrder; +} export async function getWorkOrders(filters, options, connectedDatabase) { const database = connectedDatabase ?? (await acquireConnection()); database.function('userFn_dateIntegerToString', dateIntegerToString); const { sqlWhereClause, sqlParameters } = buildWhereClause(filters); const count = database - .prepare('select count(*) as recordCount from WorkOrders w' + sqlWhereClause) + .prepare(`select count(*) as recordCount + from WorkOrders w + ${sqlWhereClause}`) .get(sqlParameters).recordCount; let workOrders = []; if (count > 0) { @@ -99,44 +142,7 @@ export async function getWorkOrders(filters, options, connectedDatabase) { (options.includeMilestones ?? false); if (hasInclusions) { for (const workOrder of workOrders) { - if (options.includeComments ?? false) { - workOrder.workOrderComments = await getWorkOrderComments(workOrder.workOrderId, database); - } - if (options.includeLotsAndLotOccupancies ?? false) { - if (workOrder.workOrderLotCount === 0) { - workOrder.workOrderLots = []; - } - else { - const workOrderLotsResults = await getLots({ - workOrderId: workOrder.workOrderId - }, { - limit: -1, - offset: 0, - includeLotOccupancyCount: false - }, database); - workOrder.workOrderLots = workOrderLotsResults.lots; - } - const lotOccupancies = await getLotOccupancies({ - workOrderId: workOrder.workOrderId - }, { - limit: -1, - offset: 0, - includeOccupants: true, - includeFees: false, - includeTransactions: false - }, database); - workOrder.workOrderLotOccupancies = lotOccupancies.lotOccupancies; - } - if (options.includeMilestones ?? false) { - workOrder.workOrderMilestones = - workOrder.workOrderMilestoneCount === 0 - ? [] - : await getWorkOrderMilestones({ - workOrderId: workOrder.workOrderId - }, { - orderBy: 'date' - }, database); - } + await addInclusions(workOrder, options, database); } } if (connectedDatabase === undefined) { diff --git a/helpers/lotOccupancyDB/getWorkOrders.ts b/helpers/lotOccupancyDB/getWorkOrders.ts index ed12973f..5eb48088 100644 --- a/helpers/lotOccupancyDB/getWorkOrders.ts +++ b/helpers/lotOccupancyDB/getWorkOrders.ts @@ -99,6 +99,72 @@ function buildWhereClause(filters: GetWorkOrdersFilters): { } } +async function addInclusions( + workOrder: recordTypes.WorkOrder, + options: GetWorkOrdersOptions, + database: PoolConnection +): Promise { + if (options.includeComments ?? false) { + workOrder.workOrderComments = await getWorkOrderComments( + workOrder.workOrderId!, + database + ) + } + + if (options.includeLotsAndLotOccupancies ?? false) { + if (workOrder.workOrderLotCount === 0) { + workOrder.workOrderLots = [] + } else { + const workOrderLotsResults = await getLots( + { + workOrderId: workOrder.workOrderId + }, + { + limit: -1, + offset: 0, + includeLotOccupancyCount: false + }, + database + ) + + workOrder.workOrderLots = workOrderLotsResults.lots + } + + const lotOccupancies = await getLotOccupancies( + { + workOrderId: workOrder.workOrderId + }, + { + limit: -1, + offset: 0, + includeOccupants: true, + includeFees: false, + includeTransactions: false + }, + database + ) + + workOrder.workOrderLotOccupancies = lotOccupancies.lotOccupancies + } + + if (options.includeMilestones ?? false) { + workOrder.workOrderMilestones = + workOrder.workOrderMilestoneCount === 0 + ? [] + : await getWorkOrderMilestones( + { + workOrderId: workOrder.workOrderId + }, + { + orderBy: 'date' + }, + database + ) + } + + return workOrder +} + export async function getWorkOrders( filters: GetWorkOrdersFilters, options: GetWorkOrdersOptions, @@ -112,7 +178,9 @@ export async function getWorkOrders( const count: number = database .prepare( - 'select count(*) as recordCount from WorkOrders w' + sqlWhereClause + `select count(*) as recordCount + from WorkOrders w + ${sqlWhereClause}` ) .get(sqlParameters).recordCount @@ -161,63 +229,7 @@ export async function getWorkOrders( if (hasInclusions) { for (const workOrder of workOrders) { - if (options.includeComments ?? false) { - workOrder.workOrderComments = await getWorkOrderComments( - workOrder.workOrderId!, - database - ) - } - - if (options.includeLotsAndLotOccupancies ?? false) { - if (workOrder.workOrderLotCount === 0) { - workOrder.workOrderLots = [] - } else { - const workOrderLotsResults = await getLots( - { - workOrderId: workOrder.workOrderId - }, - { - limit: -1, - offset: 0, - includeLotOccupancyCount: false - }, - database - ) - - workOrder.workOrderLots = workOrderLotsResults.lots - } - - const lotOccupancies = await getLotOccupancies( - { - workOrderId: workOrder.workOrderId - }, - { - limit: -1, - offset: 0, - includeOccupants: true, - includeFees: false, - includeTransactions: false - }, - database - ) - - workOrder.workOrderLotOccupancies = lotOccupancies.lotOccupancies - } - - if (options.includeMilestones ?? false) { - workOrder.workOrderMilestones = - workOrder.workOrderMilestoneCount === 0 - ? [] - : await getWorkOrderMilestones( - { - workOrderId: workOrder.workOrderId - }, - { - orderBy: 'date' - }, - database - ) - } + await addInclusions(workOrder, options, database) } }