diff --git a/src/controllers/MyController.js b/src/controllers/MyController.js deleted file mode 100644 index 2b3c7110..00000000 --- a/src/controllers/MyController.js +++ /dev/null @@ -1,11 +0,0 @@ -import hmpoFormWizard from 'hmpo-form-wizard' -const { Controller } = hmpoFormWizard - -class MyController extends Controller { - locals (req, res, callback) { - req.form.options.lastPage = req.journeyModel.get('lastVisited') - super.locals(req, res, callback) - } -} - -export default MyController diff --git a/src/controllers/datasetController.js b/src/controllers/datasetController.js index 2862c122..3b8c9bcd 100644 --- a/src/controllers/datasetController.js +++ b/src/controllers/datasetController.js @@ -1,12 +1,12 @@ 'use strict' -import MyController from './MyController.js' +import PageController from './pageController.js' // ToDo: we shouldn't hardcode these values here, should we get them from the API // maybe take from specification import { dataSubjects } from '../utils/utils.js' -class DatasetController extends MyController { +class DatasetController extends PageController { get (req, res, next) { // const dataset = req.sessionModel.get('data-subject') // const options = datasetOptions[dataset] diff --git a/src/controllers/errorsController.js b/src/controllers/errorsController.js index 241711a7..4859b987 100644 --- a/src/controllers/errorsController.js +++ b/src/controllers/errorsController.js @@ -1,10 +1,10 @@ 'use strict' -import MyController from './MyController.js' +import PageController from './pageController.js' import { severityLevels, dataSubjects } from '../utils/utils.js' -class ErrorsController extends MyController { +class ErrorsController extends PageController { get (req, res, next) { const validationResult = req.sessionModel.get('validationResult') diff --git a/src/controllers/pageController.js b/src/controllers/pageController.js new file mode 100644 index 00000000..82fb3a95 --- /dev/null +++ b/src/controllers/pageController.js @@ -0,0 +1,23 @@ +import hmpoFormWizard from 'hmpo-form-wizard' +import logger from '../utils/logger.js' +const { Controller } = hmpoFormWizard + +class PageController extends Controller { + locals (req, res, callback) { + req.form.options.lastPage = req.journeyModel.get('lastVisited') + super.locals(req, res, callback) + } + + get (req, res, next) { + logger.info({ + type: 'PageView', + pageRoute: this.options.route, + message: `page view occurred for page: ${req.originalUrl}`, + sessionId: req.sessionID, + ipAddress: req.ip + }) + super.get(req, res, next) + } +} + +export default PageController diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index 9d3c61b5..4bf78fec 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -3,7 +3,7 @@ import multer from 'multer' import axios from 'axios' import { readFile } from 'fs/promises' import { lookup } from 'mime-types' -import MyController from './MyController.js' +import PageController from './pageController.js' import config from '../../config/index.js' import { severityLevels } from '../utils/utils.js' @@ -13,7 +13,7 @@ const upload = multer({ dest: 'uploads/' }) const apiRoute = config.api.url + config.api.validationEndpoint -class UploadController extends MyController { +class UploadController extends PageController { middlewareSetup () { super.middlewareSetup() this.use('/upload', upload.single('datafile')) @@ -27,7 +27,9 @@ class UploadController extends MyController { fileName: req.file.originalname, dataset: req.sessionModel.get('dataset'), dataSubject: req.sessionModel.get('data-subject'), - organisation: 'local-authority-eng:CAT' // ToDo: this needs to be dynamic, not collected in the prototype, should it be? + organisation: 'local-authority-eng:CAT', // ToDo: this needs to be dynamic, not collected in the prototype, should it be? + sessionId: req.sessionID, + ipAddress: req.ip }) this.errorCount = jsonResult['issue-log'].filter(issue => issue.severity === severityLevels.error).length req.body.datafile = req.file @@ -39,11 +41,13 @@ class UploadController extends MyController { super.post(req, res, next) } - async validateFile ({ filePath, fileName, dataset, dataSubject, organisation }) { + async validateFile ({ filePath, fileName, dataset, dataSubject, organisation, sessionId, ipAddress }) { const formData = new FormData() formData.append('dataset', dataset) formData.append('collection', dataSubject) formData.append('organisation', organisation) + formData.append('sessionId', sessionId) + formData.append('ipAddress', ipAddress) const file = new Blob([await readFile(filePath)], { type: lookup(filePath) }) diff --git a/src/routes/form-wizard/steps.js b/src/routes/form-wizard/steps.js index 8a7a3538..380a3fef 100644 --- a/src/routes/form-wizard/steps.js +++ b/src/routes/form-wizard/steps.js @@ -1,10 +1,10 @@ -import MyController from '../../controllers/MyController.js' +import PageController from '../../controllers/pageController.js' import datasetController from '../../controllers/datasetController.js' import uploadController from '../../controllers/uploadController.js' import errorsController from '../../controllers/errorsController.js' const baseSettings = { - controller: MyController, + controller: PageController, editable: true, editBackStep: 'check' } diff --git a/test/unit/MyController.test.js b/test/unit/MyController.test.js new file mode 100644 index 00000000..b8c47809 --- /dev/null +++ b/test/unit/MyController.test.js @@ -0,0 +1,41 @@ +import PageController from '../../src/controllers/pageController.js' + +import { describe, it, vi, expect } from 'vitest' + +import logger from '../../src/utils/logger.js' + +describe('PageController', () => { + const loggerInfoMock = vi.fn() + + vi.mock('../utils/logger.js', () => { + return { + info: loggerInfoMock + } + }) + + const loggerInfoSpy = vi.spyOn(logger, 'info') + + it('Correctly creates a log when the page is viewed', () => { + const req = { + originalUrl: '/dataset', + sessionID: '123', + ip: '1234' + } + const pageController = new PageController({ + route: '/dataset' + }) + // pageController.super.get = vi.fn(); + pageController.get(req, {}, vi.fn()) + expect(loggerInfoSpy).toHaveBeenCalledOnce() + + const callArgs = loggerInfoSpy.mock.calls[0][0] + + expect(callArgs.type).toEqual('PageView') + expect(callArgs.pageRoute).toEqual('/dataset') + expect(callArgs.message).toEqual('page view occurred for page: /dataset') + expect(callArgs.sessionId).toEqual('123') + expect(callArgs.ipAddress).toEqual('1234') + expect(callArgs.level).toEqual('info') + expect(callArgs.service).toEqual('lpa-data-validation-frontend') + }) +})