diff --git a/controllers/users.js b/controllers/users.js index 88137ed..24cab6b 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -8,8 +8,11 @@ const router = express.Router(); const authHelper = require('../helpers/authentication'); const { api } = require('../api'); const moment = require('moment'); +const { isFeatureFlagTrue } = require('../helpers/featureFlagHelper'); moment.locale('de'); +const USER_MIGRATION_ENABLED = isFeatureFlagTrue(process.env.FEATURE_SCHOOL_SANIS_USER_MIGRATION_ENABLED); + const getTableActions = (item, path) => { let tableActions = [ { @@ -48,6 +51,14 @@ const getTableActions = (item, path) => { title: 'Registrierungslink generieren', }); } + if (USER_MIGRATION_ENABLED) { + tableActions.push({ + link: path + item._id, + class: 'btn-migration-rollback', + icon: 'rotate-left', + title: 'Migration rückgängig machen', + }); + } return tableActions; }; @@ -577,4 +588,21 @@ const generateRegistrationLink = () => { router.get('/registrationlink/:id', generateRegistrationLink()); +router.post('/:id/rollback-migration', (req, res, next) => { + const userId = req.params.id; + + api(req, { version: 'v3' }).post(`/user-login-migrations/users/${userId}/rollback-migration`) + .then(async () => { + req.session.notification = { + type: 'success', + message: `Die Migration für den Nutzer wurde erfolgreich zurückgesetzt`, + }; + + res.redirect(req.header('Referer')); + }) + .catch((err) => { + next(err); + }); +}); + module.exports = router; diff --git a/static/scripts/users.js b/static/scripts/users.js index f4cd335..fd2fa6f 100644 --- a/static/scripts/users.js +++ b/static/scripts/users.js @@ -1,5 +1,29 @@ "use strict"; +$(document).ready(function () { + const $rollbackModal = $('.migration-rollback-modal'); + + $('.btn-migration-rollback').on('click', function (rollbackBtnClickEvent) { + rollbackBtnClickEvent.preventDefault(); + + const url = $(this).attr('href'); + $.getJSON(url, function (user) { + populateModalForm($rollbackModal, { + action: url + '/rollback-migration', + title: 'Migration rückgängig machen', + closeLabel: 'Schließen', + submitLabel: 'Zurücksetzen', + fields: user + }); + + const $btnSubmit = $rollbackModal.find('.btn-submit'); + $btnSubmit.prop('disabled', !user.lastLoginSystemChange); + + $rollbackModal.modal('show'); + }); + }); +}); + function toggleSilentArea(status){ const area = document.querySelector('.silent-area'); const inputs = area.querySelectorAll('input, select'); diff --git a/views/users/forms/rollback-user-migration.hbs b/views/users/forms/rollback-user-migration.hbs new file mode 100644 index 0000000..9d2a8bd --- /dev/null +++ b/views/users/forms/rollback-user-migration.hbs @@ -0,0 +1,42 @@ +
Möchten sie die Migration dieses Nutzers wirklich rückgängig machen?
\ No newline at end of file diff --git a/views/users/users.hbs b/views/users/users.hbs index 3f14b80..c2263df 100644 --- a/views/users/users.hbs +++ b/views/users/users.hbs @@ -64,5 +64,10 @@ {{> "users/forms/form-invitation"}} {{/content}} {{/embed}} + {{#embed "lib/components/modal-form" method="post" class="migration-rollback-modal"}} + {{#content "fields"}} + {{> "users/forms/rollback-user-migration"}} + {{/content}} + {{/embed}} {{/content}} {{/extend}}