attempt to reduce complexity
parent
71583c5d45
commit
095eb2e5f8
|
|
@ -55,12 +55,55 @@ function buildWhereClause(filters) {
|
||||||
sqlParameters
|
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) {
|
export async function getWorkOrders(filters, options, connectedDatabase) {
|
||||||
const database = connectedDatabase ?? (await acquireConnection());
|
const database = connectedDatabase ?? (await acquireConnection());
|
||||||
database.function('userFn_dateIntegerToString', dateIntegerToString);
|
database.function('userFn_dateIntegerToString', dateIntegerToString);
|
||||||
const { sqlWhereClause, sqlParameters } = buildWhereClause(filters);
|
const { sqlWhereClause, sqlParameters } = buildWhereClause(filters);
|
||||||
const count = database
|
const count = database
|
||||||
.prepare('select count(*) as recordCount from WorkOrders w' + sqlWhereClause)
|
.prepare(`select count(*) as recordCount
|
||||||
|
from WorkOrders w
|
||||||
|
${sqlWhereClause}`)
|
||||||
.get(sqlParameters).recordCount;
|
.get(sqlParameters).recordCount;
|
||||||
let workOrders = [];
|
let workOrders = [];
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
|
|
@ -99,44 +142,7 @@ export async function getWorkOrders(filters, options, connectedDatabase) {
|
||||||
(options.includeMilestones ?? false);
|
(options.includeMilestones ?? false);
|
||||||
if (hasInclusions) {
|
if (hasInclusions) {
|
||||||
for (const workOrder of workOrders) {
|
for (const workOrder of workOrders) {
|
||||||
if (options.includeComments ?? false) {
|
await addInclusions(workOrder, options, database);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (connectedDatabase === undefined) {
|
if (connectedDatabase === undefined) {
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,72 @@ function buildWhereClause(filters: GetWorkOrdersFilters): {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function addInclusions(
|
||||||
|
workOrder: recordTypes.WorkOrder,
|
||||||
|
options: GetWorkOrdersOptions,
|
||||||
|
database: PoolConnection
|
||||||
|
): Promise<recordTypes.WorkOrder> {
|
||||||
|
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(
|
export async function getWorkOrders(
|
||||||
filters: GetWorkOrdersFilters,
|
filters: GetWorkOrdersFilters,
|
||||||
options: GetWorkOrdersOptions,
|
options: GetWorkOrdersOptions,
|
||||||
|
|
@ -112,7 +178,9 @@ export async function getWorkOrders(
|
||||||
|
|
||||||
const count: number = database
|
const count: number = database
|
||||||
.prepare(
|
.prepare(
|
||||||
'select count(*) as recordCount from WorkOrders w' + sqlWhereClause
|
`select count(*) as recordCount
|
||||||
|
from WorkOrders w
|
||||||
|
${sqlWhereClause}`
|
||||||
)
|
)
|
||||||
.get(sqlParameters).recordCount
|
.get(sqlParameters).recordCount
|
||||||
|
|
||||||
|
|
@ -161,63 +229,7 @@ export async function getWorkOrders(
|
||||||
|
|
||||||
if (hasInclusions) {
|
if (hasInclusions) {
|
||||||
for (const workOrder of workOrders) {
|
for (const workOrder of workOrders) {
|
||||||
if (options.includeComments ?? false) {
|
await addInclusions(workOrder, options, database)
|
||||||
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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue