import { getActiveJourneyValue } from '../../../utils/sessionHelper.js';
import localsHelper from '../../../utils/localsHelper.js';
import DownloadUtils from './utils.js';

class DownloadReportController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    POST = async (req, res) => {
        const { dprUser } = localsHelper.getValues(res);
        const { reportId, id, tableId } = req.params;
        const sessionKey = tableId ? { reportId, id, tableId } : { reportId, id };
        const currentReportSearch = getActiveJourneyValue(req, sessionKey, 'currentReportSearch');
        const currentReportPathname = getActiveJourneyValue(req, sessionKey, 'currentReportPathname');
        // Initialises the redirect back to the report
        let redirect = currentReportPathname && currentReportPathname.includes('/download-disabled')
            ? currentReportPathname
            : `${currentReportPathname}/download-disabled`;
        redirect = currentReportSearch ? `${redirect}${currentReportSearch}` : redirect;
        // Check if user has enabled the report for download
        const canDownloadReport = await this.services.downloadPermissionService.downloadEnabledForReport(dprUser.id, reportId, id);
        if (canDownloadReport) {
            await DownloadUtils.downloadReport({ req, res, services: this.services });
        }
        else {
            res.redirect(redirect);
        }
    };
}

export { DownloadReportController, DownloadReportController as default };
//# sourceMappingURL=controller.js.map
