From 8e22ee102641a283ea11afa957445e420f548100 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 23 Apr 2025 10:31:04 -0400 Subject: [PATCH] contract paging --- database/getNextContractId.d.ts | 1 + database/getNextContractId.js | 15 +++ database/getNextContractId.ts | 23 ++++ database/getPreviousContractId.d.ts | 1 + database/getPreviousContractId.js | 15 +++ database/getPreviousContractId.ts | 23 ++++ handlers/cemeteries-get/next.d.ts | 4 +- handlers/cemeteries-get/next.ts | 2 +- handlers/contracts-get/next.d.ts | 4 + handlers/contracts-get/next.js | 11 ++ handlers/contracts-get/next.ts | 28 +++++ handlers/contracts-get/previous.d.ts | 4 + handlers/contracts-get/previous.js | 11 ++ handlers/contracts-get/previous.ts | 28 +++++ routes/contracts.js | 4 + routes/contracts.ts | 6 + views/contract-view.ejs | 159 ++++++++++++++++----------- 17 files changed, 273 insertions(+), 66 deletions(-) create mode 100644 database/getNextContractId.d.ts create mode 100644 database/getNextContractId.js create mode 100644 database/getNextContractId.ts create mode 100644 database/getPreviousContractId.d.ts create mode 100644 database/getPreviousContractId.js create mode 100644 database/getPreviousContractId.ts create mode 100644 handlers/contracts-get/next.d.ts create mode 100644 handlers/contracts-get/next.js create mode 100644 handlers/contracts-get/next.ts create mode 100644 handlers/contracts-get/previous.d.ts create mode 100644 handlers/contracts-get/previous.js create mode 100644 handlers/contracts-get/previous.ts diff --git a/database/getNextContractId.d.ts b/database/getNextContractId.d.ts new file mode 100644 index 00000000..b3fb8911 --- /dev/null +++ b/database/getNextContractId.d.ts @@ -0,0 +1 @@ +export default function getNextContractId(contractId: number | string): Promise; diff --git a/database/getNextContractId.js b/database/getNextContractId.js new file mode 100644 index 00000000..4a3813ed --- /dev/null +++ b/database/getNextContractId.js @@ -0,0 +1,15 @@ +import { acquireConnection } from './pool.js'; +export default async function getNextContractId(contractId) { + const database = await acquireConnection(); + const result = database + .prepare(`select contractId + from Contracts + where recordDelete_timeMillis is null + and contractId > ? + order by contractId + limit 1`) + .pluck() + .get(contractId); + database.release(); + return result; +} diff --git a/database/getNextContractId.ts b/database/getNextContractId.ts new file mode 100644 index 00000000..670ea42b --- /dev/null +++ b/database/getNextContractId.ts @@ -0,0 +1,23 @@ +import { acquireConnection } from './pool.js' + +export default async function getNextContractId( + contractId: number | string +): Promise { + const database = await acquireConnection() + + const result = database + .prepare( + `select contractId + from Contracts + where recordDelete_timeMillis is null + and contractId > ? + order by contractId + limit 1` + ) + .pluck() + .get(contractId) as number | undefined + + database.release() + + return result +} diff --git a/database/getPreviousContractId.d.ts b/database/getPreviousContractId.d.ts new file mode 100644 index 00000000..83d0051a --- /dev/null +++ b/database/getPreviousContractId.d.ts @@ -0,0 +1 @@ +export default function getPreviousContractId(contractId: number | string): Promise; diff --git a/database/getPreviousContractId.js b/database/getPreviousContractId.js new file mode 100644 index 00000000..14b8cd75 --- /dev/null +++ b/database/getPreviousContractId.js @@ -0,0 +1,15 @@ +import { acquireConnection } from './pool.js'; +export default async function getPreviousContractId(contractId) { + const database = await acquireConnection(); + const result = database + .prepare(`select contractId + from Contracts + where recordDelete_timeMillis is null + and contractId < ? + order by contractId desc + limit 1`) + .pluck() + .get(contractId); + database.release(); + return result; +} diff --git a/database/getPreviousContractId.ts b/database/getPreviousContractId.ts new file mode 100644 index 00000000..28818702 --- /dev/null +++ b/database/getPreviousContractId.ts @@ -0,0 +1,23 @@ +import { acquireConnection } from './pool.js' + +export default async function getPreviousContractId( + contractId: number | string +): Promise { + const database = await acquireConnection() + + const result = database + .prepare( + `select contractId + from Contracts + where recordDelete_timeMillis is null + and contractId < ? + order by contractId desc + limit 1` + ) + .pluck() + .get(contractId) as number | undefined + + database.release() + + return result +} diff --git a/handlers/cemeteries-get/next.d.ts b/handlers/cemeteries-get/next.d.ts index 19d32e4d..5e3c4b8f 100644 --- a/handlers/cemeteries-get/next.d.ts +++ b/handlers/cemeteries-get/next.d.ts @@ -1,2 +1,4 @@ import type { Request, Response } from 'express'; -export default function handler(request: Request, response: Response): Promise; +export default function handler(request: Request<{ + cemeteryId: string; +}>, response: Response): Promise; diff --git a/handlers/cemeteries-get/next.ts b/handlers/cemeteries-get/next.ts index 5788c3d8..f3a090b0 100644 --- a/handlers/cemeteries-get/next.ts +++ b/handlers/cemeteries-get/next.ts @@ -4,7 +4,7 @@ import getNextCemeteryId from '../../database/getNextCemeteryId.js' import { getConfigProperty } from '../../helpers/config.helpers.js' export default async function handler( - request: Request, + request: Request<{ cemeteryId: string }>, response: Response ): Promise { const cemeteryId = Number.parseInt(request.params.cemeteryId, 10) diff --git a/handlers/contracts-get/next.d.ts b/handlers/contracts-get/next.d.ts new file mode 100644 index 00000000..509fad91 --- /dev/null +++ b/handlers/contracts-get/next.d.ts @@ -0,0 +1,4 @@ +import type { Request, Response } from 'express'; +export default function handler(request: Request<{ + contractId: string; +}>, response: Response): Promise; diff --git a/handlers/contracts-get/next.js b/handlers/contracts-get/next.js new file mode 100644 index 00000000..a7a58478 --- /dev/null +++ b/handlers/contracts-get/next.js @@ -0,0 +1,11 @@ +import getNextContractId from '../../database/getNextContractId.js'; +import { getConfigProperty } from '../../helpers/config.helpers.js'; +export default async function handler(request, response) { + const contractId = Number.parseInt(request.params.contractId, 10); + const nextContractId = await getNextContractId(contractId); + if (nextContractId === undefined) { + response.redirect(`${getConfigProperty('reverseProxy.urlPrefix')}/contracts/?error=noNextContractIdFound`); + return; + } + response.redirect(`${getConfigProperty('reverseProxy.urlPrefix')}/contracts/${nextContractId.toString()}`); +} diff --git a/handlers/contracts-get/next.ts b/handlers/contracts-get/next.ts new file mode 100644 index 00000000..a094219b --- /dev/null +++ b/handlers/contracts-get/next.ts @@ -0,0 +1,28 @@ +import type { Request, Response } from 'express' + +import getNextContractId from '../../database/getNextContractId.js' +import { getConfigProperty } from '../../helpers/config.helpers.js' + +export default async function handler( + request: Request<{ contractId: string }>, + response: Response +): Promise { + const contractId = Number.parseInt(request.params.contractId, 10) + + const nextContractId = await getNextContractId(contractId) + + if (nextContractId === undefined) { + response.redirect( + `${getConfigProperty( + 'reverseProxy.urlPrefix' + )}/contracts/?error=noNextContractIdFound` + ) + return + } + + response.redirect( + `${getConfigProperty( + 'reverseProxy.urlPrefix' + )}/contracts/${nextContractId.toString()}` + ) +} diff --git a/handlers/contracts-get/previous.d.ts b/handlers/contracts-get/previous.d.ts new file mode 100644 index 00000000..509fad91 --- /dev/null +++ b/handlers/contracts-get/previous.d.ts @@ -0,0 +1,4 @@ +import type { Request, Response } from 'express'; +export default function handler(request: Request<{ + contractId: string; +}>, response: Response): Promise; diff --git a/handlers/contracts-get/previous.js b/handlers/contracts-get/previous.js new file mode 100644 index 00000000..e0fb0509 --- /dev/null +++ b/handlers/contracts-get/previous.js @@ -0,0 +1,11 @@ +import getPreviousContractId from '../../database/getPreviousContractId.js'; +import { getConfigProperty } from '../../helpers/config.helpers.js'; +export default async function handler(request, response) { + const contractId = Number.parseInt(request.params.contractId, 10); + const previousContractId = await getPreviousContractId(contractId); + if (previousContractId === undefined) { + response.redirect(`${getConfigProperty('reverseProxy.urlPrefix')}/contracts/?error=noPreviousContractIdFound`); + return; + } + response.redirect(`${getConfigProperty('reverseProxy.urlPrefix')}/contracts/${previousContractId.toString()}`); +} diff --git a/handlers/contracts-get/previous.ts b/handlers/contracts-get/previous.ts new file mode 100644 index 00000000..37326503 --- /dev/null +++ b/handlers/contracts-get/previous.ts @@ -0,0 +1,28 @@ +import type { Request, Response } from 'express' + +import getPreviousContractId from '../../database/getPreviousContractId.js' +import { getConfigProperty } from '../../helpers/config.helpers.js' + +export default async function handler( + request: Request<{ contractId: string }>, + response: Response +): Promise { + const contractId = Number.parseInt(request.params.contractId, 10) + + const previousContractId = await getPreviousContractId(contractId) + + if (previousContractId === undefined) { + response.redirect( + `${getConfigProperty( + 'reverseProxy.urlPrefix' + )}/contracts/?error=noPreviousContractIdFound` + ) + return + } + + response.redirect( + `${getConfigProperty( + 'reverseProxy.urlPrefix' + )}/contracts/${previousContractId.toString()}` + ) +} diff --git a/routes/contracts.js b/routes/contracts.js index 89a0b512..db9b8d81 100644 --- a/routes/contracts.js +++ b/routes/contracts.js @@ -1,6 +1,8 @@ import { Router } from 'express'; import handler_edit from '../handlers/contracts-get/edit.js'; import handler_new from '../handlers/contracts-get/new.js'; +import handler_next from '../handlers/contracts-get/next.js'; +import handler_previous from '../handlers/contracts-get/previous.js'; import handler_search from '../handlers/contracts-get/search.js'; import handler_view from '../handlers/contracts-get/view.js'; import handler_doAddContractComment from '../handlers/contracts-post/doAddContractComment.js'; @@ -36,6 +38,8 @@ router.post('/doGetContractTypeFields', updatePostHandler, handler_doGetContract router.post('/doCreateContract', updatePostHandler, handler_doCreateContract); // View router.get('/:contractId', handler_view); +router.get('/:contractId/next', handler_next); +router.get('/:contractId/previous', handler_previous); // Edit router.get('/:contractId/edit', updateGetHandler, handler_edit); router.post('/doUpdateContract', updatePostHandler, handler_doUpdateContract); diff --git a/routes/contracts.ts b/routes/contracts.ts index 4944f19c..27d60535 100644 --- a/routes/contracts.ts +++ b/routes/contracts.ts @@ -2,6 +2,8 @@ import { Router } from 'express' import handler_edit from '../handlers/contracts-get/edit.js' import handler_new from '../handlers/contracts-get/new.js' +import handler_next from '../handlers/contracts-get/next.js' +import handler_previous from '../handlers/contracts-get/previous.js' import handler_search from '../handlers/contracts-get/search.js' import handler_view from '../handlers/contracts-get/view.js' import handler_doAddContractComment from '../handlers/contracts-post/doAddContractComment.js' @@ -52,6 +54,10 @@ router.post('/doCreateContract', updatePostHandler, handler_doCreateContract) router.get('/:contractId', handler_view) +router.get('/:contractId/next', handler_next) + +router.get('/:contractId/previous', handler_previous) + // Edit router.get('/:contractId/edit', updateGetHandler, handler_edit) diff --git a/views/contract-view.ejs b/views/contract-view.ejs index 8a31ab29..dbf799d9 100644 --- a/views/contract-view.ejs +++ b/views/contract-view.ejs @@ -34,6 +34,20 @@
+ + + Previous Contract + + + Next + + <% if (contractTypePrints.length > 0) { %> <% if (contractTypePrints.length === 1) { %> @@ -244,7 +258,11 @@

- <%= contract.purchaserName %>
+ <% if (contract.purchaserName === '') { %> + (No Purchaser Set) + <% } else { %> + <%= contract.purchaserName %> + <% } %>
<% if (contract.purchaserAddress1) { %> <%= contract.purchaserAddress1 %>
@@ -398,70 +416,70 @@

-
-

Fees

-
- <% if (contract.contractFees.length === 0) { %> -
-

- There are no fees applied to this contract. -

-
- <% } else { %> - <% - let feeAmountTotal = 0; - let taxAmountTotal = 0; - %> - - - - - - - - - - - - - <% for (const contractFee of contract.contractFees) { %> - <% - feeAmountTotal += (contractFee.feeAmount * contractFee.quantity); - taxAmountTotal += (contractFee.taxAmount * contractFee.quantity); - %> - - - <% if (contractFee.quantity !== 1) { %> - - - - - <% } %> - - - <% } %> - - - - - - - - - - - - - - - -
FeeUnit Cost×Quantity=Total
"> - <%= contractFee.feeName %>
- <%= contractFee.feeCategory %> -
$<%= contractFee.feeAmount.toFixed(2) %>×<%= contractFee.quantity %>=$<%= (contractFee.feeAmount * contractFee.quantity).toFixed(2) %>
Subtotal$<%= feeAmountTotal.toFixed(2) %>
Tax$<%= taxAmountTotal.toFixed(2) %>
Grand Total$<%= (feeAmountTotal + taxAmountTotal).toFixed(2) %>
- <% } %> +
+

Fees

+
+ <% if (contract.contractFees.length === 0) { %> +
+

+ There are no fees applied to this contract. +

+ <% } else { %> + <% + let feeAmountTotal = 0; + let taxAmountTotal = 0; + %> + + + + + + + + + + + + + <% for (const contractFee of contract.contractFees) { %> + <% + feeAmountTotal += (contractFee.feeAmount * contractFee.quantity); + taxAmountTotal += (contractFee.taxAmount * contractFee.quantity); + %> + + + <% if (contractFee.quantity !== 1) { %> + + + + + <% } %> + + + <% } %> + + + + + + + + + + + + + + + +
FeeUnit Cost×Quantity=Total
"> + <%= contractFee.feeName %>
+ <%= contractFee.feeCategory %> +
$<%= contractFee.feeAmount.toFixed(2) %>×<%= contractFee.quantity %>=$<%= (contractFee.feeAmount * contractFee.quantity).toFixed(2) %>
Subtotal$<%= feeAmountTotal.toFixed(2) %>
Tax$<%= taxAmountTotal.toFixed(2) %>
Grand Total$<%= (feeAmountTotal + taxAmountTotal).toFixed(2) %>
+ <% } %>
+
@@ -537,7 +555,20 @@ const workOrderCloseDateAlias = configFunctions.getConfigProperty("aliases.workOrderCloseDate"); %>
-

Work Orders

+
+
+
+
+

Work Orders

+
+
+
+
+ +
+
+
+