@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
114 lines (111 loc) • 3.36 kB
JavaScript
import { StoreItemBuilder } from '../builder.js';
import { RequestStatus } from '../../../../utils/ReportStatus/types.js';
import { normalizeQueryStringArray } from '../../../../utils/queryMappers.js';
class ViewedReportBuilder extends StoreItemBuilder {
req;
filters;
sort;
reportData;
requestUrls;
interactiveQuery;
asyncQueryData;
constructor(req, res) {
super(res);
this.req = req;
}
// Public methods
withReportData = (reportData) => {
this.reportData = reportData;
return this;
};
withRequestData = (asyncQueryData, url) => {
this.withAsyncUrls(url);
this.withAsyncQuery(asyncQueryData);
return this;
};
withAsyncUrls = (url) => {
this.requestUrls = url;
};
withAsyncQuery = (asyncQueryData) => {
this.asyncQueryData = asyncQueryData;
};
withInteractiveQuery = (interactiveQueryData) => {
this.interactiveQuery = interactiveQueryData;
return this;
};
// Builder methods
buildReportData = () => {
return this.buildReportMetaData(this.reportData);
};
buildUrls = () => {
const origin = this.req.get('host') || '';
const report = this.buildReportUrls();
let polling;
let request;
if (this.requestUrls) {
polling = this.requestUrls.polling;
request = this.requestUrls.request;
}
return {
origin,
...(polling && { polling }),
...(request && { request }),
report,
};
};
buildReportUrls = () => {
const origin = this.req.get('host') || '';
const fullUrl = `${this.req.protocol}://${origin}${this.req.originalUrl}`;
const parsed = new URL(fullUrl);
const { search } = parsed;
return {
fullUrl,
search,
};
};
buildStatus = () => {
return RequestStatus.READY;
};
buildTimestamp = () => {
return {
lastViewed: new Date(),
};
};
buildInteractiveQuery = () => {
if (!this.interactiveQuery.query || !this.interactiveQuery.querySummary) {
return undefined;
}
const { query, querySummary: summary } = this.interactiveQuery;
const data = {
...query,
...(query['columns'] && {
columns: normalizeQueryStringArray(query['columns']),
}),
};
return {
data,
summary,
};
};
build = () => {
const reportData = this.buildReportData();
const url = this.buildUrls();
const status = this.buildStatus();
const timestamp = this.buildTimestamp();
const definitionsPath = this.buidDefinitionsPath();
const interactiveQuery = this.buildInteractiveQuery();
const viewedReportData = {
...reportData,
...this.executionData,
...(definitionsPath && definitionsPath),
...(this.asyncQueryData && { query: this.asyncQueryData }),
...(interactiveQuery && { interactiveQuery }),
url,
status,
timestamp,
};
return viewedReportData;
};
}
export { ViewedReportBuilder };
//# sourceMappingURL=builder.js.map