attempt to reduce complexity
parent
71583c5d45
commit
095eb2e5f8
|
|
@ -55,50 +55,7 @@ function buildWhereClause(filters) {
|
|||
sqlParameters
|
||||
};
|
||||
}
|
||||
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)
|
||||
.get(sqlParameters).recordCount;
|
||||
let workOrders = [];
|
||||
if (count > 0) {
|
||||
workOrders = database
|
||||
.prepare(`select w.workOrderId,
|
||||
w.workOrderTypeId, t.workOrderType,
|
||||
w.workOrderNumber, w.workOrderDescription,
|
||||
w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,
|
||||
w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,
|
||||
ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,
|
||||
ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount,
|
||||
ifnull(l.workOrderLotCount, 0) as workOrderLotCount
|
||||
from WorkOrders w
|
||||
left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId
|
||||
left join (
|
||||
select workOrderId,
|
||||
count(workOrderMilestoneId) as workOrderMilestoneCount,
|
||||
sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount
|
||||
from WorkOrderMilestones
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) m on w.workOrderId = m.workOrderId
|
||||
left join (
|
||||
select workOrderId, count(lotId) as workOrderLotCount
|
||||
from WorkOrderLots
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) l on w.workOrderId = l.workOrderId
|
||||
${sqlWhereClause}
|
||||
order by w.workOrderOpenDate desc, w.workOrderNumber desc
|
||||
${options.limit === -1
|
||||
? ''
|
||||
: ` limit ${options.limit} offset ${options.offset}`}`)
|
||||
.all(sqlParameters);
|
||||
}
|
||||
const hasInclusions = (options.includeComments ?? false) ||
|
||||
(options.includeLotsAndLotOccupancies ?? false) ||
|
||||
(options.includeMilestones ?? false);
|
||||
if (hasInclusions) {
|
||||
for (const workOrder of workOrders) {
|
||||
async function addInclusions(workOrder, options, database) {
|
||||
if (options.includeComments ?? false) {
|
||||
workOrder.workOrderComments = await getWorkOrderComments(workOrder.workOrderId, database);
|
||||
}
|
||||
|
|
@ -137,6 +94,55 @@ export async function getWorkOrders(filters, options, connectedDatabase) {
|
|||
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}`)
|
||||
.get(sqlParameters).recordCount;
|
||||
let workOrders = [];
|
||||
if (count > 0) {
|
||||
workOrders = database
|
||||
.prepare(`select w.workOrderId,
|
||||
w.workOrderTypeId, t.workOrderType,
|
||||
w.workOrderNumber, w.workOrderDescription,
|
||||
w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,
|
||||
w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,
|
||||
ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,
|
||||
ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount,
|
||||
ifnull(l.workOrderLotCount, 0) as workOrderLotCount
|
||||
from WorkOrders w
|
||||
left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId
|
||||
left join (
|
||||
select workOrderId,
|
||||
count(workOrderMilestoneId) as workOrderMilestoneCount,
|
||||
sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount
|
||||
from WorkOrderMilestones
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) m on w.workOrderId = m.workOrderId
|
||||
left join (
|
||||
select workOrderId, count(lotId) as workOrderLotCount
|
||||
from WorkOrderLots
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) l on w.workOrderId = l.workOrderId
|
||||
${sqlWhereClause}
|
||||
order by w.workOrderOpenDate desc, w.workOrderNumber desc
|
||||
${options.limit === -1
|
||||
? ''
|
||||
: ` limit ${options.limit} offset ${options.offset}`}`)
|
||||
.all(sqlParameters);
|
||||
}
|
||||
const hasInclusions = (options.includeComments ?? false) ||
|
||||
(options.includeLotsAndLotOccupancies ?? false) ||
|
||||
(options.includeMilestones ?? false);
|
||||
if (hasInclusions) {
|
||||
for (const workOrder of workOrders) {
|
||||
await addInclusions(workOrder, options, database);
|
||||
}
|
||||
}
|
||||
if (connectedDatabase === undefined) {
|
||||
|
|
|
|||
|
|
@ -99,68 +99,11 @@ function buildWhereClause(filters: GetWorkOrdersFilters): {
|
|||
}
|
||||
}
|
||||
|
||||
export async function getWorkOrders(
|
||||
filters: GetWorkOrdersFilters,
|
||||
async function addInclusions(
|
||||
workOrder: recordTypes.WorkOrder,
|
||||
options: GetWorkOrdersOptions,
|
||||
connectedDatabase?: PoolConnection
|
||||
): Promise<{ count: number; workOrders: recordTypes.WorkOrder[] }> {
|
||||
const database = connectedDatabase ?? (await acquireConnection())
|
||||
|
||||
database.function('userFn_dateIntegerToString', dateIntegerToString)
|
||||
|
||||
const { sqlWhereClause, sqlParameters } = buildWhereClause(filters)
|
||||
|
||||
const count: number = database
|
||||
.prepare(
|
||||
'select count(*) as recordCount from WorkOrders w' + sqlWhereClause
|
||||
)
|
||||
.get(sqlParameters).recordCount
|
||||
|
||||
let workOrders: recordTypes.WorkOrder[] = []
|
||||
|
||||
if (count > 0) {
|
||||
workOrders = database
|
||||
.prepare(
|
||||
`select w.workOrderId,
|
||||
w.workOrderTypeId, t.workOrderType,
|
||||
w.workOrderNumber, w.workOrderDescription,
|
||||
w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,
|
||||
w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,
|
||||
ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,
|
||||
ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount,
|
||||
ifnull(l.workOrderLotCount, 0) as workOrderLotCount
|
||||
from WorkOrders w
|
||||
left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId
|
||||
left join (
|
||||
select workOrderId,
|
||||
count(workOrderMilestoneId) as workOrderMilestoneCount,
|
||||
sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount
|
||||
from WorkOrderMilestones
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) m on w.workOrderId = m.workOrderId
|
||||
left join (
|
||||
select workOrderId, count(lotId) as workOrderLotCount
|
||||
from WorkOrderLots
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) l on w.workOrderId = l.workOrderId
|
||||
${sqlWhereClause}
|
||||
order by w.workOrderOpenDate desc, w.workOrderNumber desc
|
||||
${
|
||||
options.limit === -1
|
||||
? ''
|
||||
: ` limit ${options.limit} offset ${options.offset}`
|
||||
}`
|
||||
)
|
||||
.all(sqlParameters)
|
||||
}
|
||||
|
||||
const hasInclusions =
|
||||
(options.includeComments ?? false) ||
|
||||
(options.includeLotsAndLotOccupancies ?? false) ||
|
||||
(options.includeMilestones ?? false)
|
||||
|
||||
if (hasInclusions) {
|
||||
for (const workOrder of workOrders) {
|
||||
database: PoolConnection
|
||||
): Promise<recordTypes.WorkOrder> {
|
||||
if (options.includeComments ?? false) {
|
||||
workOrder.workOrderComments = await getWorkOrderComments(
|
||||
workOrder.workOrderId!,
|
||||
|
|
@ -218,6 +161,75 @@ export async function getWorkOrders(
|
|||
database
|
||||
)
|
||||
}
|
||||
|
||||
return workOrder
|
||||
}
|
||||
|
||||
export async function getWorkOrders(
|
||||
filters: GetWorkOrdersFilters,
|
||||
options: GetWorkOrdersOptions,
|
||||
connectedDatabase?: PoolConnection
|
||||
): Promise<{ count: number; workOrders: recordTypes.WorkOrder[] }> {
|
||||
const database = connectedDatabase ?? (await acquireConnection())
|
||||
|
||||
database.function('userFn_dateIntegerToString', dateIntegerToString)
|
||||
|
||||
const { sqlWhereClause, sqlParameters } = buildWhereClause(filters)
|
||||
|
||||
const count: number = database
|
||||
.prepare(
|
||||
`select count(*) as recordCount
|
||||
from WorkOrders w
|
||||
${sqlWhereClause}`
|
||||
)
|
||||
.get(sqlParameters).recordCount
|
||||
|
||||
let workOrders: recordTypes.WorkOrder[] = []
|
||||
|
||||
if (count > 0) {
|
||||
workOrders = database
|
||||
.prepare(
|
||||
`select w.workOrderId,
|
||||
w.workOrderTypeId, t.workOrderType,
|
||||
w.workOrderNumber, w.workOrderDescription,
|
||||
w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,
|
||||
w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,
|
||||
ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,
|
||||
ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount,
|
||||
ifnull(l.workOrderLotCount, 0) as workOrderLotCount
|
||||
from WorkOrders w
|
||||
left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId
|
||||
left join (
|
||||
select workOrderId,
|
||||
count(workOrderMilestoneId) as workOrderMilestoneCount,
|
||||
sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount
|
||||
from WorkOrderMilestones
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) m on w.workOrderId = m.workOrderId
|
||||
left join (
|
||||
select workOrderId, count(lotId) as workOrderLotCount
|
||||
from WorkOrderLots
|
||||
where recordDelete_timeMillis is null
|
||||
group by workOrderId) l on w.workOrderId = l.workOrderId
|
||||
${sqlWhereClause}
|
||||
order by w.workOrderOpenDate desc, w.workOrderNumber desc
|
||||
${
|
||||
options.limit === -1
|
||||
? ''
|
||||
: ` limit ${options.limit} offset ${options.offset}`
|
||||
}`
|
||||
)
|
||||
.all(sqlParameters)
|
||||
}
|
||||
|
||||
const hasInclusions =
|
||||
(options.includeComments ?? false) ||
|
||||
(options.includeLotsAndLotOccupancies ?? false) ||
|
||||
(options.includeMilestones ?? false)
|
||||
|
||||
if (hasInclusions) {
|
||||
for (const workOrder of workOrders) {
|
||||
await addInclusions(workOrder, options, database)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue