attempt to isolate complexity
parent
0166bb33c2
commit
dacaa3feb3
|
|
@ -2,12 +2,7 @@ import sqlite from "better-sqlite3";
|
||||||
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
|
import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js";
|
||||||
import { dateIntegerToString, dateStringToInteger, dateToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js";
|
import { dateIntegerToString, dateStringToInteger, dateToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js";
|
||||||
import { getLotOccupancyOccupants } from "./getLotOccupancyOccupants.js";
|
import { getLotOccupancyOccupants } from "./getLotOccupancyOccupants.js";
|
||||||
export const getLotOccupancies = (filters, options, connectedDatabase) => {
|
const buildWhereClause = (filters) => {
|
||||||
const database = connectedDatabase ||
|
|
||||||
sqlite(databasePath, {
|
|
||||||
readonly: true
|
|
||||||
});
|
|
||||||
database.function("userFn_dateIntegerToString", dateIntegerToString);
|
|
||||||
let sqlWhereClause = " where o.recordDelete_timeMillis is null";
|
let sqlWhereClause = " where o.recordDelete_timeMillis is null";
|
||||||
const sqlParameters = [];
|
const sqlParameters = [];
|
||||||
if (filters.lotId) {
|
if (filters.lotId) {
|
||||||
|
|
@ -91,7 +86,19 @@ export const getLotOccupancies = (filters, options, connectedDatabase) => {
|
||||||
" and o.lotOccupancyId not in (select lotOccupancyId from WorkOrderLotOccupancies where recordDelete_timeMillis is null and workOrderId = ?)";
|
" and o.lotOccupancyId not in (select lotOccupancyId from WorkOrderLotOccupancies where recordDelete_timeMillis is null and workOrderId = ?)";
|
||||||
sqlParameters.push(filters.notWorkOrderId);
|
sqlParameters.push(filters.notWorkOrderId);
|
||||||
}
|
}
|
||||||
let count;
|
return {
|
||||||
|
sqlWhereClause,
|
||||||
|
sqlParameters
|
||||||
|
};
|
||||||
|
};
|
||||||
|
export const getLotOccupancies = (filters, options, connectedDatabase) => {
|
||||||
|
const database = connectedDatabase ||
|
||||||
|
sqlite(databasePath, {
|
||||||
|
readonly: true
|
||||||
|
});
|
||||||
|
database.function("userFn_dateIntegerToString", dateIntegerToString);
|
||||||
|
const { sqlWhereClause, sqlParameters } = buildWhereClause(filters);
|
||||||
|
let count = 0;
|
||||||
if (options.limit !== -1) {
|
if (options.limit !== -1) {
|
||||||
count = database
|
count = database
|
||||||
.prepare("select count(*) as recordCount" +
|
.prepare("select count(*) as recordCount" +
|
||||||
|
|
|
||||||
|
|
@ -33,24 +33,11 @@ interface GetLotOccupanciesOptions {
|
||||||
includeOccupants: boolean;
|
includeOccupants: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getLotOccupancies = (
|
const buildWhereClause = (
|
||||||
filters: GetLotOccupanciesFilters,
|
filters: GetLotOccupanciesFilters
|
||||||
options: GetLotOccupanciesOptions,
|
): { sqlWhereClause: string; sqlParameters: unknown[] } => {
|
||||||
connectedDatabase?: sqlite.Database
|
|
||||||
): {
|
|
||||||
count: number;
|
|
||||||
lotOccupancies: recordTypes.LotOccupancy[];
|
|
||||||
} => {
|
|
||||||
const database =
|
|
||||||
connectedDatabase ||
|
|
||||||
sqlite(databasePath, {
|
|
||||||
readonly: true
|
|
||||||
});
|
|
||||||
|
|
||||||
database.function("userFn_dateIntegerToString", dateIntegerToString);
|
|
||||||
|
|
||||||
let sqlWhereClause = " where o.recordDelete_timeMillis is null";
|
let sqlWhereClause = " where o.recordDelete_timeMillis is null";
|
||||||
const sqlParameters = [];
|
const sqlParameters: unknown[] = [];
|
||||||
|
|
||||||
if (filters.lotId) {
|
if (filters.lotId) {
|
||||||
sqlWhereClause += " and o.lotId = ?";
|
sqlWhereClause += " and o.lotId = ?";
|
||||||
|
|
@ -148,7 +135,31 @@ export const getLotOccupancies = (
|
||||||
sqlParameters.push(filters.notWorkOrderId);
|
sqlParameters.push(filters.notWorkOrderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
let count: number;
|
return {
|
||||||
|
sqlWhereClause,
|
||||||
|
sqlParameters
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getLotOccupancies = (
|
||||||
|
filters: GetLotOccupanciesFilters,
|
||||||
|
options: GetLotOccupanciesOptions,
|
||||||
|
connectedDatabase?: sqlite.Database
|
||||||
|
): {
|
||||||
|
count: number;
|
||||||
|
lotOccupancies: recordTypes.LotOccupancy[];
|
||||||
|
} => {
|
||||||
|
const database =
|
||||||
|
connectedDatabase ||
|
||||||
|
sqlite(databasePath, {
|
||||||
|
readonly: true
|
||||||
|
});
|
||||||
|
|
||||||
|
database.function("userFn_dateIntegerToString", dateIntegerToString);
|
||||||
|
|
||||||
|
const { sqlWhereClause, sqlParameters } = buildWhereClause(filters);
|
||||||
|
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
if (options.limit !== -1) {
|
if (options.limit !== -1) {
|
||||||
count = database
|
count = database
|
||||||
|
|
@ -192,7 +203,7 @@ export const getLotOccupancies = (
|
||||||
if (options.includeOccupants) {
|
if (options.includeOccupants) {
|
||||||
for (const lotOccupancy of lotOccupancies) {
|
for (const lotOccupancy of lotOccupancies) {
|
||||||
lotOccupancy.lotOccupancyOccupants = getLotOccupancyOccupants(
|
lotOccupancy.lotOccupancyOccupants = getLotOccupancyOccupants(
|
||||||
lotOccupancy.lotOccupancyId,
|
lotOccupancy.lotOccupancyId as number,
|
||||||
database
|
database
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue