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 { dateIntegerToString, dateStringToInteger, dateToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js";
|
||||
import { getLotOccupancyOccupants } from "./getLotOccupancyOccupants.js";
|
||||
export const getLotOccupancies = (filters, options, connectedDatabase) => {
|
||||
const database = connectedDatabase ||
|
||||
sqlite(databasePath, {
|
||||
readonly: true
|
||||
});
|
||||
database.function("userFn_dateIntegerToString", dateIntegerToString);
|
||||
const buildWhereClause = (filters) => {
|
||||
let sqlWhereClause = " where o.recordDelete_timeMillis is null";
|
||||
const sqlParameters = [];
|
||||
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 = ?)";
|
||||
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) {
|
||||
count = database
|
||||
.prepare("select count(*) as recordCount" +
|
||||
|
|
|
|||
|
|
@ -33,24 +33,11 @@ interface GetLotOccupanciesOptions {
|
|||
includeOccupants: boolean;
|
||||
}
|
||||
|
||||
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 buildWhereClause = (
|
||||
filters: GetLotOccupanciesFilters
|
||||
): { sqlWhereClause: string; sqlParameters: unknown[] } => {
|
||||
let sqlWhereClause = " where o.recordDelete_timeMillis is null";
|
||||
const sqlParameters = [];
|
||||
const sqlParameters: unknown[] = [];
|
||||
|
||||
if (filters.lotId) {
|
||||
sqlWhereClause += " and o.lotId = ?";
|
||||
|
|
@ -148,7 +135,31 @@ export const getLotOccupancies = (
|
|||
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) {
|
||||
count = database
|
||||
|
|
@ -192,7 +203,7 @@ export const getLotOccupancies = (
|
|||
if (options.includeOccupants) {
|
||||
for (const lotOccupancy of lotOccupancies) {
|
||||
lotOccupancy.lotOccupancyOccupants = getLotOccupancyOccupants(
|
||||
lotOccupancy.lotOccupancyId,
|
||||
lotOccupancy.lotOccupancyId as number,
|
||||
database
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue