From f76f41ef63602a909492f286c87425e9118858c2 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 31 Aug 2022 10:20:40 -0400 Subject: [PATCH] get work order --- helpers/lotOccupancyDB/getLotOccupancies.d.ts | 1 + helpers/lotOccupancyDB/getLotOccupancies.js | 4 ++ helpers/lotOccupancyDB/getLotOccupancies.ts | 6 ++ helpers/lotOccupancyDB/getLots.d.ts | 1 + helpers/lotOccupancyDB/getLots.js | 4 ++ helpers/lotOccupancyDB/getLots.ts | 6 ++ helpers/lotOccupancyDB/getWorkOrder.d.ts | 3 + helpers/lotOccupancyDB/getWorkOrder.js | 40 +++++++++++ helpers/lotOccupancyDB/getWorkOrder.ts | 67 +++++++++++++++++++ 9 files changed, 132 insertions(+) create mode 100644 helpers/lotOccupancyDB/getWorkOrder.d.ts create mode 100644 helpers/lotOccupancyDB/getWorkOrder.js create mode 100644 helpers/lotOccupancyDB/getWorkOrder.ts diff --git a/helpers/lotOccupancyDB/getLotOccupancies.d.ts b/helpers/lotOccupancyDB/getLotOccupancies.d.ts index 821e93ea..6618220e 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.d.ts +++ b/helpers/lotOccupancyDB/getLotOccupancies.d.ts @@ -9,6 +9,7 @@ interface GetLotOccupanciesFilters { mapId?: number | string; lotName?: string; lotTypeId?: number | string; + workOrderId?: number | string; } interface GetLotOccupanciesOptions { limit: -1 | number; diff --git a/helpers/lotOccupancyDB/getLotOccupancies.js b/helpers/lotOccupancyDB/getLotOccupancies.js index a26baf88..fd190c31 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.js +++ b/helpers/lotOccupancyDB/getLotOccupancies.js @@ -60,6 +60,10 @@ export const getLotOccupancies = (filters, options, connectedDatabase) => { sqlWhereClause += " and l.lotTypeId = ?"; sqlParameters.push(filters.lotTypeId); } + if (filters.workOrderId) { + sqlWhereClause += " and o.lotOccupancyId in (select lotOccupancyId from WorkOrderLotOccupancies where recordDelete_timeMillis is null and workOrderId = ?)"; + sqlParameters.push(filters.workOrderId); + } const count = database.prepare("select count(*) as recordCount" + " from LotOccupancies o" + " left join Lots l on o.lotId = l.lotId" + diff --git a/helpers/lotOccupancyDB/getLotOccupancies.ts b/helpers/lotOccupancyDB/getLotOccupancies.ts index 1b3e123d..1534af23 100644 --- a/helpers/lotOccupancyDB/getLotOccupancies.ts +++ b/helpers/lotOccupancyDB/getLotOccupancies.ts @@ -26,6 +26,7 @@ interface GetLotOccupanciesFilters { mapId ? : number | string; lotName ? : string; lotTypeId ? : number | string; + workOrderId?: number | string; } @@ -117,6 +118,11 @@ export const getLotOccupancies = (filters: GetLotOccupanciesFilters, sqlParameters.push(filters.lotTypeId); } + if (filters.workOrderId) { + sqlWhereClause += " and o.lotOccupancyId in (select lotOccupancyId from WorkOrderLotOccupancies where recordDelete_timeMillis is null and workOrderId = ?)"; + sqlParameters.push(filters.workOrderId); + } + const count: number = database.prepare("select count(*) as recordCount" + " from LotOccupancies o" + " left join Lots l on o.lotId = l.lotId" + diff --git a/helpers/lotOccupancyDB/getLots.d.ts b/helpers/lotOccupancyDB/getLots.d.ts index 4b880512..b8666d3e 100644 --- a/helpers/lotOccupancyDB/getLots.d.ts +++ b/helpers/lotOccupancyDB/getLots.d.ts @@ -6,6 +6,7 @@ interface GetLotsFilters { lotTypeId?: number | string; lotStatusId?: number | string; occupancyStatus?: "" | "occupied" | "unoccupied"; + workOrderId?: number | string; } interface GetLotsOptions { limit: number; diff --git a/helpers/lotOccupancyDB/getLots.js b/helpers/lotOccupancyDB/getLots.js index c5c98162..1ea88ae2 100644 --- a/helpers/lotOccupancyDB/getLots.js +++ b/helpers/lotOccupancyDB/getLots.js @@ -35,6 +35,10 @@ export const getLots = (filters, options, connectedDatabase) => { sqlWhereClause += " and (lotOccupancyCount is null or lotOccupancyCount = 0)"; } } + if (filters.workOrderId) { + sqlWhereClause += " and l.lotId in (select lotId from WorkOrderLots where recordDelete_timeMillis is null and workOrderId = ?)"; + sqlParameters.push(filters.workOrderId); + } const currentDate = dateToInteger(new Date()); const count = database.prepare("select count(*) as recordCount" + " from Lots l" + diff --git a/helpers/lotOccupancyDB/getLots.ts b/helpers/lotOccupancyDB/getLots.ts index 449138a9..736f6b08 100644 --- a/helpers/lotOccupancyDB/getLots.ts +++ b/helpers/lotOccupancyDB/getLots.ts @@ -19,6 +19,7 @@ interface GetLotsFilters { lotTypeId ? : number | string; lotStatusId ? : number | string; occupancyStatus ? : "" | "occupied" | "unoccupied"; + workOrderId ? : number | string; } interface GetLotsOptions { @@ -72,6 +73,11 @@ export const getLots = (filters: GetLotsFilters, } } + if (filters.workOrderId) { + sqlWhereClause += " and l.lotId in (select lotId from WorkOrderLots where recordDelete_timeMillis is null and workOrderId = ?)"; + sqlParameters.push(filters.workOrderId); + } + const currentDate = dateToInteger(new Date()); const count: number = database.prepare("select count(*) as recordCount" + diff --git a/helpers/lotOccupancyDB/getWorkOrder.d.ts b/helpers/lotOccupancyDB/getWorkOrder.d.ts new file mode 100644 index 00000000..7c09e3b8 --- /dev/null +++ b/helpers/lotOccupancyDB/getWorkOrder.d.ts @@ -0,0 +1,3 @@ +import type * as recordTypes from "../../types/recordTypes"; +export declare const getWorkOrder: (workOrderId: number | string) => recordTypes.WorkOrder; +export default getWorkOrder; diff --git a/helpers/lotOccupancyDB/getWorkOrder.js b/helpers/lotOccupancyDB/getWorkOrder.js new file mode 100644 index 00000000..53b823e9 --- /dev/null +++ b/helpers/lotOccupancyDB/getWorkOrder.js @@ -0,0 +1,40 @@ +import sqlite from "better-sqlite3"; +import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; +import { dateIntegerToString } from "@cityssm/expressjs-server-js/dateTimeFns.js"; +import { getLots } from "./getLots.js"; +import { getLotOccupancies } from "./getLotOccupancies.js"; +export const getWorkOrder = (workOrderId) => { + const database = sqlite(databasePath, { + readonly: true + }); + database.function("userFn_dateIntegerToString", dateIntegerToString); + const workOrder = 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" + + " from WorkOrders w" + + " left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId" + + " where w.recordDelete_timeMillis is null" + + " and w.workOrderId = ?") + .get(workOrderId); + if (workOrder) { + workOrder.workOrderLots = getLots({ + workOrderId: workOrder.workOrderId + }, { + limit: -1, + offset: 0 + }, database).lots; + workOrder.workOrderLotOccupancies = getLotOccupancies({ + workOrderId: workOrder.workOrderId + }, { + limit: -1, + offset: 0, + includeOccupants: true + }, database).lotOccupancies; + } + database.close(); + return workOrder; +}; +export default getWorkOrder; diff --git a/helpers/lotOccupancyDB/getWorkOrder.ts b/helpers/lotOccupancyDB/getWorkOrder.ts new file mode 100644 index 00000000..04e42256 --- /dev/null +++ b/helpers/lotOccupancyDB/getWorkOrder.ts @@ -0,0 +1,67 @@ +import sqlite from "better-sqlite3"; + +import { + lotOccupancyDB as databasePath +} from "../../data/databasePaths.js"; + +import { + dateIntegerToString +} from "@cityssm/expressjs-server-js/dateTimeFns.js"; + +import { + getLots +} from "./getLots.js"; + +import { + getLotOccupancies +} from "./getLotOccupancies.js"; + +import type * as recordTypes from "../../types/recordTypes"; + + + +export const getWorkOrder = (workOrderId: number | string): recordTypes.WorkOrder => { + + const database = sqlite(databasePath, { + readonly: true + }); + + database.function("userFn_dateIntegerToString", dateIntegerToString); + + const workOrder: recordTypes.WorkOrder = 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" + + " from WorkOrders w" + + " left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId" + + " where w.recordDelete_timeMillis is null" + + " and w.workOrderId = ?") + .get(workOrderId); + + + if (workOrder) { + workOrder.workOrderLots = getLots({ + workOrderId: workOrder.workOrderId + }, { + limit: -1, + offset: 0 + }, database).lots; + + workOrder.workOrderLotOccupancies = getLotOccupancies({ + workOrderId: workOrder.workOrderId + }, { + limit: -1, + offset: 0, + includeOccupants: true + }, database).lotOccupancies; + } + + database.close(); + + return workOrder; +}; + + +export default getWorkOrder; \ No newline at end of file