Skip to content

Commit

Permalink
custom reports
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Nov 29, 2022
1 parent 79e6e58 commit 4ec9e85
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 460 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ bin/daemon/
data/*.db
data/*.db-journal
data/*.min.js
data/config.*
!data/config.testing.*
!data/config.example.*
data/config.d.ts
data/config.js
data/config.ts
data/backups/*
!data/backups/README.md
handlers/*.min.js
Expand Down
2 changes: 2 additions & 0 deletions data/customReportsSsm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import type { ReportDefinition } from "../types/configTypes";
export declare const customReports_general: ReportDefinition[];
18 changes: 18 additions & 0 deletions data/customReportsSsm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const customReports_general = [
{
reportName: "ssm-taxiLicences",
reportTitle: "Taxi, Limo, Shuttle, and Rideshare Licences for Police Services",
reportDescription: "Includes licence categories and number, licensee names, and expiration dates.",
sql: "select l.licenceNumber, lc.licenceCategory," +
" l.licenseeName, l.licenseeBusinessName, userFn_dateIntegerToString(l.endDate) as endDateString," +
" max(case when lcf.licenceField = 'Insurance Expiration' then lf.licenceFieldValue else '' end) as insuranceExpiration" +
" from Licences l" +
" left join LicenceCategories lc on l.licenceCategoryKey = lc.licenceCategoryKey" +
" left join LicenceFields lf on l.licenceId = lf.licenceId" +
" left join LicenceCategoryFields lcf on lf.licenceFieldKey = lcf.licenceFieldKey" +
" where l.recordDelete_timeMillis is null" +
" and l.issueDate is not null" +
" and l.licenceCategoryKey in ('01', '02', '04', '05', '06', '37')" +
" group by l.licenceNumber, lc.licenceCategory, l.licenseeName, l.licenseeBusinessName, l.endDate"
}
];
22 changes: 22 additions & 0 deletions data/customReportsSsm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { ReportDefinition } from "../types/configTypes";

export const customReports_general: ReportDefinition[] = [
{
reportName: "ssm-taxiLicences",
reportTitle: "Taxi, Limo, Shuttle, and Rideshare Licences for Police Services",
reportDescription:
"Includes licence categories and number, licensee names, and expiration dates.",
sql:
"select l.licenceNumber, lc.licenceCategory," +
" l.licenseeName, l.licenseeBusinessName, userFn_dateIntegerToString(l.endDate) as endDateString," +
" max(case when lcf.licenceField = 'Insurance Expiration' then lf.licenceFieldValue else '' end) as insuranceExpiration" +
" from Licences l" +
" left join LicenceCategories lc on l.licenceCategoryKey = lc.licenceCategoryKey" +
" left join LicenceFields lf on l.licenceId = lf.licenceId" +
" left join LicenceCategoryFields lcf on lf.licenceFieldKey = lcf.licenceFieldKey" +
" where l.recordDelete_timeMillis is null" +
" and l.issueDate is not null" +
" and l.licenceCategoryKey in ('01', '02', '04', '05', '06', '37')" +
" group by l.licenceNumber, lc.licenceCategory, l.licenseeName, l.licenseeBusinessName, l.endDate"
}
];
4 changes: 1 addition & 3 deletions handlers/reports-get/reportName.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export const handler = (request, response) => {
break;
}
if (!rows) {
return response
.status(404)
.json({
return response.status(404).json({
success: false,
message: "Report Not Found"
});
Expand Down
44 changes: 20 additions & 24 deletions handlers/reports-get/reportName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,34 @@ import { getReportData } from "../../helpers/licencesDB/getReportData.js";

import papaparse from "papaparse";


export const handler: RequestHandler = (request, response) => {
const reportName = request.params.reportName;

const reportName = request.params.reportName;

let rows: unknown[];
let rows: unknown[];

switch (reportName) {
switch (reportName) {
default:
rows = getReportData(reportName, request.query);
break;
}

default:
rows = getReportData(reportName, request.query);
break;
}
if (!rows) {
return response.status(404).json({
success: false,
message: "Report Not Found"
});
}

if (!rows) {
return response
.status(404)
.json({
success: false,
message: "Report Not Found"
});
}
const csv = papaparse.unparse(rows);

const csv = papaparse.unparse(rows);
response.setHeader(
"Content-Disposition",
"attachment; filename=" + reportName + "-" + Date.now().toString() + ".csv"
);

response.setHeader("Content-Disposition",
"attachment; filename=" + reportName + "-" + Date.now().toString() + ".csv");
response.setHeader("Content-Type", "text/csv");

response.setHeader("Content-Type", "text/csv");

response.send(csv);
response.send(csv);
};


export default handler;
4 changes: 3 additions & 1 deletion helpers/functions.config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export declare function getProperty(propertyName: "licenceLengthFunctions"): {
[licenceLengthFunctionName: string]: configTypes.LicenceLengthFunction;
};
export declare function getProperty(propertyName: "additionalFeeFunctions"): {
[additonalFeeFunctionName: string]: configTypes.AdditionalFeeFunction;
[additionalFeeFunctionName: string]: configTypes.AdditionalFeeFunction;
};
export declare function getProperty(propertyName: "customReports"): configTypes.ReportDefinition[];
export declare function getProperty(propertyName: "reverseProxy.disableCompression"): boolean;
export declare function getProperty(propertyName: "reverseProxy.disableEtag"): boolean;
export declare function getProperty(propertyName: "reverseProxy.urlPrefix"): string;
Expand All @@ -40,3 +41,4 @@ export declare const getLicenceLengthFunctionNames: () => string[];
export declare const getLicenceLengthFunction: (licenceLengthFunctionName: string) => configTypes.LicenceLengthFunction;
export declare const getAdditionalFeeFunctionNames: () => string[];
export declare const getAdditionalFeeFunction: (additionalFeeFunctionName: string) => configTypes.AdditionalFeeFunction;
export declare const getCustomReport: (reportName: string) => configTypes.ReportDefinition;
8 changes: 7 additions & 1 deletion helpers/functions.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ configFallbackValues.set("settings.includeReplacementFee", true);
configFallbackValues.set("settings.includeYearEnd", false);
configFallbackValues.set("licenceLengthFunctions", {});
configFallbackValues.set("additionalFeeFunctions", {});
configFallbackValues.set("customReports", []);
export function getProperty(propertyName) {
const propertyNameSplit = propertyName.split(".");
let currentObject = config;
Expand All @@ -42,7 +43,7 @@ export function getProperty(propertyName) {
return currentObject;
}
export const keepAliveMillis = getProperty("session.doKeepAlive")
? Math.max(getProperty("session.maxAgeMillis") / 2, getProperty("session.maxAgeMillis") - (10 * 60 * 1000))
? Math.max(getProperty("session.maxAgeMillis") / 2, getProperty("session.maxAgeMillis") - 10 * 60 * 1000)
: 0;
export const getLicenceLengthFunctionNames = () => {
return Object.keys(getProperty("licenceLengthFunctions"));
Expand All @@ -56,3 +57,8 @@ export const getAdditionalFeeFunctionNames = () => {
export const getAdditionalFeeFunction = (additionalFeeFunctionName) => {
return getProperty("additionalFeeFunctions")[additionalFeeFunctionName];
};
export const getCustomReport = (reportName) => {
return getProperty("customReports").find((possibleReportDefinition) => {
return possibleReportDefinition.reportName === reportName;
});
};
65 changes: 37 additions & 28 deletions helpers/functions.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { config } from "../data/config.js";

import type * as configTypes from "../types/configTypes";


/*
* SET UP FALLBACK VALUES
*/
Expand Down Expand Up @@ -45,7 +44,7 @@ configFallbackValues.set("settings.includeYearEnd", false);

configFallbackValues.set("licenceLengthFunctions", {});
configFallbackValues.set("additionalFeeFunctions", {});

configFallbackValues.set("customReports", []);

/*
* Set up function overloads
Expand All @@ -66,7 +65,9 @@ export function getProperty(propertyName: "users.isAdmin"): string[];

export function getProperty(propertyName: "defaults.licenseeCity"): string;
export function getProperty(propertyName: "defaults.licenseeProvince"): string;
export function getProperty(propertyName: "defaults.licenceNumberFunction"): configTypes.LicenceNumberFunction;
export function getProperty(
propertyName: "defaults.licenceNumberFunction"
): configTypes.LicenceNumberFunction;

export function getProperty(propertyName: "settings.licenceAlias"): string;
export function getProperty(propertyName: "settings.licenceAliasPlural"): string;
Expand All @@ -81,13 +82,15 @@ export function getProperty(propertyName: "settings.includeYearEnd"): boolean;
export function getProperty(propertyName: "exports.batches"): configTypes.ConfigBatchExport;

export function getProperty(propertyName: "licenceLengthFunctions"): {
[licenceLengthFunctionName: string]: configTypes.LicenceLengthFunction;
[licenceLengthFunctionName: string]: configTypes.LicenceLengthFunction;
};

export function getProperty(propertyName: "additionalFeeFunctions"): {
[additonalFeeFunctionName: string]: configTypes.AdditionalFeeFunction;
[additionalFeeFunctionName: string]: configTypes.AdditionalFeeFunction;
};

export function getProperty(propertyName: "customReports"): configTypes.ReportDefinition[];

export function getProperty(propertyName: "reverseProxy.disableCompression"): boolean;
export function getProperty(propertyName: "reverseProxy.disableEtag"): boolean;
export function getProperty(propertyName: "reverseProxy.urlPrefix"): string;
Expand All @@ -98,45 +101,51 @@ export function getProperty(propertyName: "session.maxAgeMillis"): number;
export function getProperty(propertyName: "session.secret"): string;

export function getProperty(propertyName: string): unknown {
const propertyNameSplit = propertyName.split(".");

const propertyNameSplit = propertyName.split(".");

let currentObject = config;
let currentObject = config;

for (const propertyNamePiece of propertyNameSplit) {
for (const propertyNamePiece of propertyNameSplit) {
if (Object.prototype.hasOwnProperty.call(currentObject, propertyNamePiece)) {
currentObject = currentObject[propertyNamePiece];
continue;
}

if (Object.prototype.hasOwnProperty.call(currentObject, propertyNamePiece)) {
currentObject = currentObject[propertyNamePiece];
continue;
return configFallbackValues.get(propertyName);
}

return configFallbackValues.get(propertyName);
}

return currentObject;

return currentObject;
}

export const keepAliveMillis =
getProperty("session.doKeepAlive")
export const keepAliveMillis = getProperty("session.doKeepAlive")
? Math.max(
getProperty("session.maxAgeMillis") / 2,
getProperty("session.maxAgeMillis") - (10 * 60 * 1000)
)
getProperty("session.maxAgeMillis") / 2,
getProperty("session.maxAgeMillis") - 10 * 60 * 1000
)
: 0;

export const getLicenceLengthFunctionNames = (): string[] => {
return Object.keys(getProperty("licenceLengthFunctions"));
return Object.keys(getProperty("licenceLengthFunctions"));
};

export const getLicenceLengthFunction = (licenceLengthFunctionName: string): configTypes.LicenceLengthFunction => {
return getProperty("licenceLengthFunctions")[licenceLengthFunctionName];
export const getLicenceLengthFunction = (
licenceLengthFunctionName: string
): configTypes.LicenceLengthFunction => {
return getProperty("licenceLengthFunctions")[licenceLengthFunctionName];
};

export const getAdditionalFeeFunctionNames = (): string[] => {
return Object.keys(getProperty("additionalFeeFunctions")) || [];
return Object.keys(getProperty("additionalFeeFunctions")) || [];
};

export const getAdditionalFeeFunction = (
additionalFeeFunctionName: string
): configTypes.AdditionalFeeFunction => {
return getProperty("additionalFeeFunctions")[additionalFeeFunctionName];
};

export const getAdditionalFeeFunction = (additionalFeeFunctionName: string): configTypes.AdditionalFeeFunction => {
return getProperty("additionalFeeFunctions")[additionalFeeFunctionName];
export const getCustomReport = (reportName: string): configTypes.ReportDefinition => {
return getProperty("customReports").find((possibleReportDefinition) => {
return possibleReportDefinition.reportName === reportName;
});
};
Loading

0 comments on commit 4ec9e85

Please sign in to comment.