add type to reportdata
parent
a7380707fa
commit
f1ffdefbf8
|
|
@ -1,32 +1,26 @@
|
|||
import path from 'node:path';
|
||||
import { convertHTMLToPDF } from '@cityssm/pdf-puppeteer';
|
||||
import * as dateTimeFunctions from '@cityssm/utils-datetime';
|
||||
import camelcase from 'camelcase';
|
||||
import { renderFile as renderEjsFile } from 'ejs';
|
||||
import * as configFunctions from '../../helpers/functions.config.js';
|
||||
import * as lotOccupancyFunctions from '../../helpers/functions.lotOccupancy.js';
|
||||
import { getConfigProperty } from '../../helpers/functions.config.js';
|
||||
import { getPdfPrintConfig, getReportData } from '../../helpers/functions.print.js';
|
||||
const attachmentOrInline = configFunctions.getConfigProperty('settings.printPdf.contentDisposition');
|
||||
const attachmentOrInline = getConfigProperty('settings.printPdf.contentDisposition');
|
||||
export async function handler(request, response, next) {
|
||||
const printName = request.params.printName;
|
||||
if (!configFunctions
|
||||
.getConfigProperty('settings.lotOccupancy.prints')
|
||||
.includes(`pdf/${printName}`) &&
|
||||
!configFunctions
|
||||
.getConfigProperty('settings.workOrders.prints')
|
||||
.includes(`pdf/${printName}`)) {
|
||||
response.redirect(`${configFunctions.getConfigProperty('reverseProxy.urlPrefix')}/dashboard/?error=printConfigNotAllowed`);
|
||||
if (!getConfigProperty('settings.lotOccupancy.prints').includes(`pdf/${printName}`) &&
|
||||
!getConfigProperty('settings.workOrders.prints').includes(`pdf/${printName}`)) {
|
||||
response.redirect(`${getConfigProperty('reverseProxy.urlPrefix')}/dashboard/?error=printConfigNotAllowed`);
|
||||
return;
|
||||
}
|
||||
const printConfig = getPdfPrintConfig(printName);
|
||||
if (printConfig === undefined) {
|
||||
response.redirect(`${configFunctions.getConfigProperty('reverseProxy.urlPrefix')}/dashboard/?error=printConfigNotFound`);
|
||||
response.redirect(`${getConfigProperty('reverseProxy.urlPrefix')}/dashboard/?error=printConfigNotFound`);
|
||||
return;
|
||||
}
|
||||
const reportData = await getReportData(printConfig, request.query);
|
||||
const reportPath = path.join('views', 'print', 'pdf', `${printName}.ejs`);
|
||||
function pdfCallbackFunction(pdf) {
|
||||
response.setHeader('Content-Disposition', `${attachmentOrInline}; filename=${camelcase(printConfig.title)}.pdf`);
|
||||
response.setHeader('Content-Disposition', `${attachmentOrInline}; filename=${camelcase(printConfig?.title ?? 'export')}.pdf`);
|
||||
response.setHeader('Content-Type', 'application/pdf');
|
||||
response.send(pdf);
|
||||
}
|
||||
|
|
@ -42,9 +36,6 @@ export async function handler(request, response, next) {
|
|||
});
|
||||
pdfCallbackFunction(pdf);
|
||||
}
|
||||
reportData.configFunctions = configFunctions;
|
||||
reportData.dateTimeFunctions = dateTimeFunctions;
|
||||
reportData.lotOccupancyFunctions = lotOccupancyFunctions;
|
||||
await renderEjsFile(reportPath, reportData, {}, ejsCallbackFunction);
|
||||
}
|
||||
export default handler;
|
||||
|
|
|
|||
|
|
@ -1,19 +1,17 @@
|
|||
import path from 'node:path'
|
||||
|
||||
import { convertHTMLToPDF } from '@cityssm/pdf-puppeteer'
|
||||
import * as dateTimeFunctions from '@cityssm/utils-datetime'
|
||||
import camelcase from 'camelcase'
|
||||
import { renderFile as renderEjsFile } from 'ejs'
|
||||
import type { NextFunction, Request, Response } from 'express'
|
||||
|
||||
import * as configFunctions from '../../helpers/functions.config.js'
|
||||
import * as lotOccupancyFunctions from '../../helpers/functions.lotOccupancy.js'
|
||||
import { getConfigProperty } from '../../helpers/functions.config.js'
|
||||
import {
|
||||
getPdfPrintConfig,
|
||||
getReportData
|
||||
} from '../../helpers/functions.print.js'
|
||||
|
||||
const attachmentOrInline = configFunctions.getConfigProperty(
|
||||
const attachmentOrInline = getConfigProperty(
|
||||
'settings.printPdf.contentDisposition'
|
||||
)
|
||||
|
||||
|
|
@ -25,15 +23,15 @@ export async function handler(
|
|||
const printName = request.params.printName
|
||||
|
||||
if (
|
||||
!configFunctions
|
||||
.getConfigProperty('settings.lotOccupancy.prints')
|
||||
.includes(`pdf/${printName}`) &&
|
||||
!configFunctions
|
||||
.getConfigProperty('settings.workOrders.prints')
|
||||
.includes(`pdf/${printName}`)
|
||||
!getConfigProperty('settings.lotOccupancy.prints').includes(
|
||||
`pdf/${printName}`
|
||||
) &&
|
||||
!getConfigProperty('settings.workOrders.prints').includes(
|
||||
`pdf/${printName}`
|
||||
)
|
||||
) {
|
||||
response.redirect(
|
||||
`${configFunctions.getConfigProperty(
|
||||
`${getConfigProperty(
|
||||
'reverseProxy.urlPrefix'
|
||||
)}/dashboard/?error=printConfigNotAllowed`
|
||||
)
|
||||
|
|
@ -44,7 +42,7 @@ export async function handler(
|
|||
|
||||
if (printConfig === undefined) {
|
||||
response.redirect(
|
||||
`${configFunctions.getConfigProperty('reverseProxy.urlPrefix')}/dashboard/?error=printConfigNotFound`
|
||||
`${getConfigProperty('reverseProxy.urlPrefix')}/dashboard/?error=printConfigNotFound`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
|
@ -56,7 +54,7 @@ export async function handler(
|
|||
function pdfCallbackFunction(pdf: Buffer): void {
|
||||
response.setHeader(
|
||||
'Content-Disposition',
|
||||
`${attachmentOrInline}; filename=${camelcase(printConfig!.title)}.pdf`
|
||||
`${attachmentOrInline}; filename=${camelcase(printConfig?.title ?? 'export')}.pdf`
|
||||
)
|
||||
|
||||
response.setHeader('Content-Type', 'application/pdf')
|
||||
|
|
@ -82,10 +80,6 @@ export async function handler(
|
|||
pdfCallbackFunction(pdf)
|
||||
}
|
||||
|
||||
reportData.configFunctions = configFunctions
|
||||
reportData.dateTimeFunctions = dateTimeFunctions
|
||||
reportData.lotOccupancyFunctions = lotOccupancyFunctions
|
||||
|
||||
await renderEjsFile(reportPath, reportData, {}, ejsCallbackFunction)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,19 @@
|
|||
import type { Lot, LotOccupancy, WorkOrder } from '../types/recordTypes.js';
|
||||
interface PrintConfig {
|
||||
title: string;
|
||||
params: string[];
|
||||
}
|
||||
interface ReportData {
|
||||
headTitle: string;
|
||||
lot?: Lot;
|
||||
lotOccupancy?: LotOccupancy;
|
||||
workOrder?: WorkOrder;
|
||||
configFunctions: unknown;
|
||||
dateTimeFunctions: unknown;
|
||||
lotOccupancyFunctions: unknown;
|
||||
}
|
||||
export declare function getScreenPrintConfig(printName: string): PrintConfig | undefined;
|
||||
export declare function getPdfPrintConfig(printName: string): PrintConfig | undefined;
|
||||
export declare function getPrintConfig(screenOrPdfPrintName: string): PrintConfig | undefined;
|
||||
export declare function getReportData(printConfig: PrintConfig, requestQuery: Record<string, unknown>): Promise<Record<string, unknown>>;
|
||||
export declare function getReportData(printConfig: PrintConfig, requestQuery: Record<string, unknown>): Promise<ReportData>;
|
||||
export {};
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import * as dateTimeFunctions from '@cityssm/utils-datetime';
|
||||
import getLot from '../database/getLot.js';
|
||||
import getLotOccupancy from '../database/getLotOccupancy.js';
|
||||
import getWorkOrder from '../database/getWorkOrder.js';
|
||||
import { getConfigProperty } from './functions.config.js';
|
||||
import * as configFunctions from './functions.config.js';
|
||||
import * as lotOccupancyFunctions from './functions.lotOccupancy.js';
|
||||
const screenPrintConfigs = {
|
||||
lotOccupancy: {
|
||||
title: `${getConfigProperty('aliases.lot')} ${getConfigProperty('aliases.occupancy')} Print`,
|
||||
title: `${configFunctions.getConfigProperty('aliases.lot')} ${configFunctions.getConfigProperty('aliases.occupancy')} Print`,
|
||||
params: ['lotOccupancyId']
|
||||
}
|
||||
};
|
||||
|
|
@ -46,13 +48,16 @@ export function getPrintConfig(screenOrPdfPrintName) {
|
|||
}
|
||||
export async function getReportData(printConfig, requestQuery) {
|
||||
const reportData = {
|
||||
headTitle: printConfig.title
|
||||
headTitle: printConfig.title,
|
||||
configFunctions,
|
||||
dateTimeFunctions,
|
||||
lotOccupancyFunctions
|
||||
};
|
||||
if (printConfig.params.includes('lotOccupancyId') &&
|
||||
typeof requestQuery.lotOccupancyId === 'string') {
|
||||
const lotOccupancy = await getLotOccupancy(requestQuery.lotOccupancyId);
|
||||
if ((lotOccupancy?.lotId ?? -1) !== -1) {
|
||||
reportData.lot = getLot(lotOccupancy.lotId);
|
||||
if (lotOccupancy !== undefined && (lotOccupancy?.lotId ?? -1) !== -1) {
|
||||
reportData.lot = await getLot(lotOccupancy.lotId ?? -1);
|
||||
}
|
||||
reportData.lotOccupancy = lotOccupancy;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,35 @@
|
|||
import * as dateTimeFunctions from '@cityssm/utils-datetime'
|
||||
|
||||
import getLot from '../database/getLot.js'
|
||||
import getLotOccupancy from '../database/getLotOccupancy.js'
|
||||
import getWorkOrder from '../database/getWorkOrder.js'
|
||||
import type { Lot, LotOccupancy, WorkOrder } from '../types/recordTypes.js'
|
||||
|
||||
import { getConfigProperty } from './functions.config.js'
|
||||
import * as configFunctions from './functions.config.js'
|
||||
import * as lotOccupancyFunctions from './functions.lotOccupancy.js'
|
||||
|
||||
interface PrintConfig {
|
||||
title: string
|
||||
params: string[]
|
||||
}
|
||||
|
||||
interface ReportData {
|
||||
headTitle: string
|
||||
|
||||
lot?: Lot
|
||||
lotOccupancy?: LotOccupancy
|
||||
workOrder?: WorkOrder
|
||||
|
||||
configFunctions: unknown
|
||||
dateTimeFunctions: unknown
|
||||
lotOccupancyFunctions: unknown
|
||||
}
|
||||
|
||||
const screenPrintConfigs: Record<string, PrintConfig> = {
|
||||
lotOccupancy: {
|
||||
title: `${getConfigProperty(
|
||||
title: `${configFunctions.getConfigProperty(
|
||||
'aliases.lot'
|
||||
)} ${getConfigProperty('aliases.occupancy')} Print`,
|
||||
)} ${configFunctions.getConfigProperty('aliases.occupancy')} Print`,
|
||||
params: ['lotOccupancyId']
|
||||
}
|
||||
}
|
||||
|
|
@ -69,9 +85,12 @@ export function getPrintConfig(
|
|||
export async function getReportData(
|
||||
printConfig: PrintConfig,
|
||||
requestQuery: Record<string, unknown>
|
||||
): Promise<Record<string, unknown>> {
|
||||
const reportData: Record<string, unknown> = {
|
||||
headTitle: printConfig.title
|
||||
): Promise<ReportData> {
|
||||
const reportData: ReportData = {
|
||||
headTitle: printConfig.title,
|
||||
configFunctions,
|
||||
dateTimeFunctions,
|
||||
lotOccupancyFunctions
|
||||
}
|
||||
|
||||
if (
|
||||
|
|
@ -80,8 +99,8 @@ export async function getReportData(
|
|||
) {
|
||||
const lotOccupancy = await getLotOccupancy(requestQuery.lotOccupancyId)
|
||||
|
||||
if ((lotOccupancy?.lotId ?? -1) !== -1) {
|
||||
reportData.lot = getLot(lotOccupancy!.lotId!)
|
||||
if (lotOccupancy !== undefined && (lotOccupancy?.lotId ?? -1) !== -1) {
|
||||
reportData.lot = await getLot(lotOccupancy.lotId ?? -1)
|
||||
}
|
||||
|
||||
reportData.lotOccupancy = lotOccupancy
|
||||
|
|
|
|||
Loading…
Reference in New Issue