diff --git a/.gitignore b/.gitignore index 31c9362c..31fd8c22 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,4 @@ uploads/* .idea .DS_Store +volume/cache/* diff --git a/src/assets/emailTemplates/newEndpoint.yml b/src/assets/emailTemplates/newEndpoint.yml new file mode 100644 index 00000000..abd2b64e --- /dev/null +++ b/src/assets/emailTemplates/newEndpoint.yml @@ -0,0 +1,15 @@ +subject: "${organisation} submitted a ${dataset} endpoint" + +body: | + Hello data management team, + + A new endpoint has been submitted with these details and is ready to check: + + Name: ${name} + Organisation: ${organisation} + Email: ${email} + Endpoint URL: ${endpoint} + Documentation URL: ${documentationUrl} + Dataset Type: ${dataset} + + I confirm this dataset is provided under the Open Government Licence diff --git a/src/controllers/CheckAnswersController.js b/src/controllers/CheckAnswersController.js deleted file mode 100644 index 1f5602e8..00000000 --- a/src/controllers/CheckAnswersController.js +++ /dev/null @@ -1,91 +0,0 @@ -import PageController from './pageController.js' -import notifyClient from '../services/mailClient.js' -import config from '../../config/index.js' - -const dataManagementEmail = process.env.DATA_MANAGEMENT_EMAIL || config.email.dataManagementEmail - -class CheckAnswersController extends PageController { - /** - * Handles the HTTP POST request for choosing a dataset. - * during this, we will perform a few actions - * firstly, we will email Jira causing the creation of a ticket (this will be implemented at a later date) - * secondly, we will email the management team to inform them of the request - * finally, we will email the LPA/organisation to inform them that their request has been acknowledged - * @param {Object} req - The HTTP request object. - * @param {Object} res - The HTTP response object. - * @param {Function} next - The next middleware function. - */ - async post (req, res, next) { - const result = await this.sendEmails(req, res, next) - for (const err of (result.errors ?? [])) { - console.error(err) - } - - super.post(req, res, next) - } - - /** - * Attempts to send email notifications. - * - * @param {*} req - * @param {*} res - * @param {*} next - * @returns {Promise<{} | {errors: string[]}>} - */ - async sendEmails (req, res, next) { - const name = req.sessionModel.get('name') - const email = req.sessionModel.get('email') - const organisation = req.sessionModel.get('lpa') - const dataset = req.sessionModel.get('dataset') - const documentationUrl = req.sessionModel.get('documentation-url') - const endpoint = req.sessionModel.get('endpoint-url') - - const { RequestTemplateId, AcknowledgementTemplateId } = - config.email.templates - - const personalisation = { - name, - email, - organisation, - endpoint, - 'documentation-url': documentationUrl, - dataset - } - - const [reqResult, ackResult] = await Promise.allSettled([ - notifyClient.sendEmail(RequestTemplateId, dataManagementEmail, { - personalisation - }), - notifyClient.sendEmail(AcknowledgementTemplateId, email, { - personalisation - }) - ]) - - const errors = [] - if (reqResult.status === 'rejected') { - const msg = emailFailureMessage(RequestTemplateId, personalisation) - errors.push(msg) - } - if (ackResult.status === 'rejected') { - const msg = emailFailureMessage(AcknowledgementTemplateId, personalisation) - errors.push(msg) - } - - if (errors.length !== 0) { - return { errors } - } - return {} - } -} - -/** - * - * @param {string} templateId - * @param {{organisation: string, name: string, email: string}} metadata - * @returns - */ -function emailFailureMessage (templateId, { organisation, name, email }) { - return `Failed to send email template=${templateId} to (org: ${organisation}, name: ${name}, email: ${email}):` -} - -export default CheckAnswersController diff --git a/src/controllers/confirmationController.js b/src/controllers/confirmationController.js new file mode 100644 index 00000000..a2b690b4 --- /dev/null +++ b/src/controllers/confirmationController.js @@ -0,0 +1,30 @@ +import PageController from './pageController.js' +import yaml from 'js-yaml'; +import fs from 'fs'; + +const templateFile = 'src/assets/emailTemplates/newEndpoint.yml' + +const templateContent = fs.readFileSync(templateFile, 'utf8'); +const template = yaml.load(templateContent); + +class confirmationController extends PageController { + locals(req, res, next) { + const name = req.sessionModel.get('name') + const email = req.sessionModel.get('email') + const organisation = req.sessionModel.get('lpa') + const dataset = req.sessionModel.get('dataset') + const documentationUrl = req.sessionModel.get('documentation-url') + const endpoint = req.sessionModel.get('endpoint-url') + + const recipient = 'digitalland@levellingup.gov.uk' + const templateVars = { name, organisation, email, endpoint, documentationUrl, dataset }; + const subject = template.subject.replace(/\${(.*?)}/g, (match, key) => templateVars[key]); + const body = template.body.replace(/\${(.*?)}/g, (match, key) => templateVars[key]); + + req.form.options.mailTo = `mailto:${recipient}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}` + + super.locals(req, res, next) + } +} + +export default confirmationController diff --git a/src/routes/form-wizard/endpoint-submission-form/steps.js b/src/routes/form-wizard/endpoint-submission-form/steps.js index 1462cd97..b5bf8f30 100644 --- a/src/routes/form-wizard/endpoint-submission-form/steps.js +++ b/src/routes/form-wizard/endpoint-submission-form/steps.js @@ -1,7 +1,7 @@ import chooseDatasetController from '../../../controllers/chooseDatasetController.js' import LpaDetailsController from '../../../controllers/lpaDetailsController.js' import PageController from '../../../controllers/pageController.js' -import CheckAnswersController from '../../../controllers/CheckAnswersController.js' +import confirmationController from '../../../controllers/confirmationController.js' const defaultParams = { entryPoint: false, @@ -38,12 +38,12 @@ export default { }, '/check-answers': { ...defaultParams, - controller: CheckAnswersController, next: 'confirmation', backLink: '/dataset-details' }, '/confirmation': { ...defaultParams, + controller: confirmationController, template: 'submit/confirmation.html' } } diff --git a/src/views/submit/check.html b/src/views/submit/check.html index f10ddbf9..64dfcb96 100644 --- a/src/views/submit/check.html +++ b/src/views/submit/check.html @@ -122,7 +122,7 @@