@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
262 lines (259 loc) • 8.79 kB
JavaScript
import logger from '../utils/logger.js';
import { RestClient } from './restClient.js';
class ReportingClient {
restClient;
constructor(config) {
this.restClient = new RestClient('Reporting API Client', config);
}
getCount(resourceName, token, countRequest) {
logger.info(`Reporting client: Get count. { resourceName: ${resourceName} }`);
return this.restClient
.get({
path: `/${resourceName}/count`,
query: countRequest.toRecordWithFilterPrefix(true),
token,
})
.then(response => response.count);
}
getList(resourceName, token, listRequest) {
return this.getListWithWarnings(resourceName, token, listRequest).then(response => response.data);
}
getListWithWarnings(resourceName, token, listRequest) {
logger.info(`Reporting client: Get list. { resourceName: ${resourceName} }`);
return this.restClient
.getWithHeaders({
path: `/${resourceName}`,
query: listRequest.toRecordWithFilterPrefix(true),
token,
})
.then((response) => ({
data: response.data,
warnings: {
noDataAvailable: response.headers['x-no-data-warning'],
},
}));
}
getDefinitionSummary(token, reportId, definitionsPath) {
this.logInfo('Get definition summary', { reportId });
const queryParams = {
...(definitionsPath && { dataProductDefinitionsPath: definitionsPath }),
};
return this.restClient
.get({
path: `/definitions/${reportId}`,
query: queryParams,
token,
})
.then(response => response);
}
getDefinitions(token, definitionsPath) {
this.logInfo('Get definitions');
const queryParams = {
renderMethod: 'HTML',
...(definitionsPath && { dataProductDefinitionsPath: definitionsPath }),
};
return this.restClient
.get({
path: '/definitions',
query: queryParams,
token,
})
.then(response => response);
}
getDefinition(token, reportId, variantId, definitionsPath, queryData) {
const query = {
...queryData,
dataProductDefinitionsPath: definitionsPath,
};
this.logInfo('Get definition', { reportId, variantId, ...query });
return this.restClient
.get({
path: `/definitions/${reportId}/${variantId}`,
query,
token,
})
.then(response => response);
}
requestAsyncReport(token, reportId, variantId, query) {
this.logInfo('Request report', { reportId, variantId });
return this.restClient
.get({
path: `/async/reports/${reportId}/${variantId}`,
token,
query,
})
.then(response => response);
}
cancelAsyncRequest(token, reportId, variantId, executionId, dataProductDefinitionsPath) {
this.logInfo('Cancel Request', { reportId, variantId, executionId });
return this.restClient
.delete({
path: `/reports/${reportId}/${variantId}/statements/${executionId}`,
token,
query: {
dataProductDefinitionsPath,
},
})
.then(response => response);
}
downloadAsyncReport(token, reportId, variantId, tableId, query, res) {
this.logInfo('Streaming download data', { reportId, variantId, tableId });
return this.restClient.getStream({
path: `/reports/${reportId}/${variantId}/tables/${tableId}/download`,
query,
token,
}, res);
}
downloadSyncReport(token, resourceName, query, res) {
this.logInfo('Streaming download data', { resourceName });
return this.restClient.getStream({
path: `/${resourceName}/download`,
query,
token,
}, res);
}
getAsyncReport(token, reportId, variantId, tableId, query) {
this.logInfo('Get Data', { reportId, variantId, tableId });
return this.restClient
.get({
path: `/reports/${reportId}/${variantId}/tables/${tableId}/result`,
token,
query,
})
.then(response => response);
}
getAsyncSummaryReport(token, reportId, variantId, tableId, summaryId, query) {
this.logInfo('Get summary data', { reportId, variantId, tableId, summaryId });
return this.restClient
.get({
path: `/reports/${reportId}/${variantId}/tables/${tableId}/result/summary/${summaryId}`,
token,
query,
})
.then(response => response);
}
getAsyncReportStatus(token, reportId, variantId, executionId, dataProductDefinitionsPath, tableId) {
this.logInfo('Get status', { reportId, variantId, tableId, executionId });
return this.restClient
.get({
path: `/reports/${reportId}/${variantId}/statements/${executionId}/status`,
token,
query: {
dataProductDefinitionsPath,
tableId,
},
})
.then(response => response);
}
getAsyncCount(token, tableId, dataProductDefinitionsPath) {
this.logInfo('Get count', { tableId });
return this.restClient
.get({
path: `/report/tables/${tableId}/count`,
token,
query: {
dataProductDefinitionsPath,
},
})
.then(response => response.count);
}
getAsyncInteractiveCount(token, tableId, reportId, id, filters) {
this.logInfo('Get interactive count', { tableId, reportId, id });
return this.restClient
.get({
path: `/reports/${reportId}/${id}/tables/${tableId}/count`,
token,
query: filters.toRecordWithFilterPrefix(true),
})
.then(response => response.count);
}
/**
* Gets the expiry state for tables
*
* @param {string} token
* @param {string[]} tableIds
* @return {*}
* @memberof ReportingClient
*/
getTableExpiryState(token, tableIds) {
return this.restClient
.post({
path: `/reports/tableExpiryState`,
data: { tableIds },
}, token)
.then(response => response);
}
/**
* Subscribe to a scheduled report
*
* @param {string} token
* @param {string} reportId
* @param {string} id
* @return {*} {Promise<{ tableId: string }>}
* @memberof ReportingClient
*/
subscribe(token, reportId, id) {
return this.restClient
.post({
path: `/user/subscribe`,
data: { reportId, id },
}, token)
.then(response => response);
}
/**
* Unsubscribe from a scheduled report
*
* @param {string} token
* @param {string} reportId
* @param {string} id
* @return {*} {Promise<{ success: boolean }>}
* @memberof ReportingClient
*/
unsubscribe(token, reportId, id) {
return this.restClient
.post({
path: `/user/unsubscribe`,
data: { reportId, id },
}, token)
.then(response => response);
}
/**
* Get a single subscription data
*
* @param {string} token
* @param {string} reportId
* @param {string} id
* @return {*} {Promise<{ reportId: string; id: string; tableId: string; createdAt: string; addedAt: string }[]>}
* @memberof ReportingClient
*/
getSubscription(token, reportId, id) {
return this.restClient
.get({
path: `/user/subscription/${reportId}/${id}`,
token,
})
.then(response => response);
}
/**
* Get all a user subscriptions data
*
* @param {string} token
* @return {*} {Promise<{ reportId: string; id: string; tableId: string; createdAt: string; addedAt: string }[]>}
* @memberof ReportingClient
*/
getSubscriptions(token) {
return this.restClient
.get({
path: `/user/subscriptions`,
token,
})
.then(response => response);
}
logInfo(title, args) {
const query = args && Object.keys(args).length ? JSON.stringify(args) : '';
const message = `Reporting Client: ${title}: ${query}`;
logger.info(message);
}
}
export { ReportingClient, ReportingClient as default };
//# sourceMappingURL=reportingClient.js.map