Skip to content

Commit

Permalink
refactor controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
davwas committed Sep 11, 2023
1 parent 5cfb190 commit 687a763
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
18 changes: 2 additions & 16 deletions controllers/dataprivacy.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
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();

const privacyUrl = () => new URL(`${SC_THEME}/${specificFiles.privacyExemplary}`, DOCUMENT_BASE_DIR);

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;
Expand Down
18 changes: 2 additions & 16 deletions controllers/termsofuse.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
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();

const termsUrl = () => new URL(`${SC_THEME}/${specificFiles.termsOfUseSchool}`, DOCUMENT_BASE_DIR);

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;
Expand Down
24 changes: 24 additions & 0 deletions helpers/consentVersionHelper.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit 687a763

Please sign in to comment.