From 687a7635c9559f8efc28ad0f85f4ef27804db51c Mon Sep 17 00:00:00 2001 From: davwas Date: Mon, 11 Sep 2023 16:58:10 +0200 Subject: [PATCH] refactor controllers --- controllers/dataprivacy.js | 18 ++---------------- controllers/termsofuse.js | 18 ++---------------- helpers/consentVersionHelper.js | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 32 deletions(-) create mode 100644 helpers/consentVersionHelper.js diff --git a/controllers/dataprivacy.js b/controllers/dataprivacy.js index 07685a02e5..562e5813c0 100644 --- a/controllers/dataprivacy.js +++ b/controllers/dataprivacy.js @@ -1,10 +1,9 @@ const express = require('express'); const { URL } = require('url'); -const api = require('../api'); -const authHelper = require('../helpers/authentication'); const { DOCUMENT_BASE_DIR, SC_THEME } = require('../config/global'); const { specificFiles } = require('../config/documents'); const { getBase64File } = require('../helpers/fileHelper'); +const { getConsentVersion } = require('../helpers/consentVersionHelper'); const router = express.Router(); @@ -12,20 +11,7 @@ const privacyUrl = () => new URL(`${SC_THEME}/${specificFiles.privacyExemplary}` router.get('/', async (req, res, next) => { try { - const isAuthenticated = await authHelper.isAuthenticated(req); - const qs = { - $limit: 1, - consentTypes: ['privacy'], - $sort: { - publishedAt: -1, - }, - }; - - if (isAuthenticated && res.locals.currentSchool) { - qs.schoolId = res.locals.currentSchool; - } - - const consentVersions = await api(req).get('/consentVersions', { qs }); + const consentVersions = await getConsentVersion(req, res, 'privacy'); if (consentVersions.data.length) { const fileId = consentVersions.data[0].consentDataId; diff --git a/controllers/termsofuse.js b/controllers/termsofuse.js index bed7820a2d..4060a1435f 100644 --- a/controllers/termsofuse.js +++ b/controllers/termsofuse.js @@ -1,10 +1,9 @@ const express = require('express'); const { URL } = require('url'); -const api = require('../api'); -const authHelper = require('../helpers/authentication'); const { DOCUMENT_BASE_DIR, SC_THEME } = require('../config/global'); const { specificFiles } = require('../config/documents'); const { getBase64File } = require('../helpers/fileHelper'); +const { getConsentVersion } = require('../helpers/consentVersionHelper'); const router = express.Router(); @@ -12,20 +11,7 @@ const termsUrl = () => new URL(`${SC_THEME}/${specificFiles.termsOfUseSchool}`, router.get('/', async (req, res, next) => { try { - const isAuthenticated = await authHelper.isAuthenticated(req); - const qs = { - $limit: 1, - consentTypes: ['termsOfUse'], - $sort: { - publishedAt: -1, - }, - }; - - if (isAuthenticated && res.locals.currentSchool) { - qs.schoolId = res.locals.currentSchool; - } - - const consentVersions = await api(req).get('/consentVersions', { qs }); + const consentVersions = await getConsentVersion(req, res, 'termsOfUse'); if (consentVersions.data.length) { const fileId = consentVersions.data[0].consentDataId; diff --git a/helpers/consentVersionHelper.js b/helpers/consentVersionHelper.js new file mode 100644 index 0000000000..42683c07e4 --- /dev/null +++ b/helpers/consentVersionHelper.js @@ -0,0 +1,24 @@ +const api = require('../api'); +const authHelper = require('./authentication'); + +const getConsentVersion = async (req, res, consentType) => { + const isAuthenticated = await authHelper.isAuthenticated(req); + const qs = { + $limit: 1, + consentTypes: [consentType], + $sort: { + publishedAt: -1, + }, + }; + + if (isAuthenticated && res.locals.currentSchool) { + qs.schoolId = res.locals.currentSchool; + } + + const consentVersion = await api(req).get('/consentVersions', { qs }); + return consentVersion; +}; + +module.exports = { + getConsentVersion, +};