const express = require('express');
const router = express.Router();

const authHelper = require('../helpers/authentication');
const permissionsHelper = require('../helpers/permissions');
const api = require('../api');

// secure routes
router.use(authHelper.authChecker);

router.get('/', function (req, res, next) {
    res.render('firstLogin/firstLogin', {
        title: 'Willkommen - Erster Login'
    });
});
router.get('/14_17', function (req, res, next) {
    res.render('firstLogin/firstLogin14_17', {
        title: 'Willkommen - Erster Login (14 bis 17 Jahre)'
    });
});
router.get('/U14', function (req, res, next) {
    res.render('firstLogin/firstLoginU14', {
        title: 'Willkommen - Erster Login'
    });
});
router.get('/UE18', function (req, res, next) {
    res.render('firstLogin/firstLoginUE18', {
        title: 'Willkommen - Erster Login'
    });
});
router.get('/existing', function (req, res, next) {
    res.render('firstLogin/firstLoginExistingUser', {
        title: 'Willkommen - Erster Login für bestehende Nutzer'
    });
});
router.post('/submit', function (req, res, next) {
    let accountId = res.locals.currentPayload.accountId
    let accountUpdate = {};
    let userUpdate = {};
    let consentUpdate = {};

    if (req.body["password-1"]) {
        accountUpdate.password_verification = req.body.password_verification;
        accountUpdate.password = req.body["password-1"];
    }

    let accountPromise = api(req).patch('/accounts/' + accountId, {
        json: accountUpdate
    });
    let userPromise = Promise.resolve();
    let consentPromise = Promise.resolve();

    return Promise.all([accountPromise, userPromise, consentPromise]).then(() => {
        res.sendStatus(200);
    }).catch(err => {
        res.status(500).send(err)
    });
});
/*
    return api(req).patch('/users/0000d231816abba584714c9e', {
        json: {birthday: new Date(req.body.studentBirthdate)}
    }).then(user => {
        return api(req).get('/consents/', {
            qs: {userId: user._id}
        });
    }).then(consent => {
        let userConsent = {
            form: 'digital',
            privacyConsent: req.body.Erhebung,
            thirdPartyConsent: req.body.Pseudonymisierung,
            termsOfUseConsent: Boolean(req.body.Nutzungsbedingungen),
            researchConsent: req.body.Forschung
        };
        return api(req).patch('/consents/' + consent.data[0]._id, {
            json: {userConsent: userConsent}
        });
    }).then(consent => {
        res.sendStatus(200);
    }).catch(err => res.status(500).send(err));
});*/
router.get('/existingU14', function (req, res, next) {
    res.render('firstLogin/firstLoginExistingUserU14', {
        title: 'Willkommen - Erster Login für bestehende Nutzer'
    });
});
router.get('/existingUE14', function (req, res, next) {
    res.render('firstLogin/firstLoginExistingUserUE14', {
        title: 'Willkommen - Erster Login für bestehende Nutzer'
    });
});

module.exports = router;

@@ -1203,51 +1203,180 @@
 let 
-userUpdate = %7B%7D;%0A    let consentUpdate = %7B%7D
+accountPromise = Promise.resolve();%0A    let userUpdate = %7B%7D;%0A    let userPromise = Promise.resolve();%0A    let consentUpdate = %7B%7D;%0A    let consentPromise = Promise.resolve()
 ;%0A%0A 
@@ -1551,18 +1551,11 @@
     
-%7D%0A%0A    let
+   
  acc
@@ -1615,24 +1615,28 @@
 , %7B%0A        
+   
+ 
 json: accoun
@@ -1643,16 +1643,20 @@
 tUpdate%0A
+    
     %7D);%0A
@@ -1663,88 +1663,1038 @@
     
-let userPromise = Promise.resolve();%0A    let consentPromise = Promise.resolve();
+%7D;%0A%0A    if (req.body%5B%22student-email%22%5D) userUpdate.email = req.body%5B%22student-email%22%5D;%0A    if (req.body.studentBirthdate) userUpdate.birthday = new Date(req.body.studentBirthdate);%0A%0A    if (userUpdate.email %7C%7C userUpdate.birthday) %7B%0A        userPromise = api(req).patch('/users/' + res.locals.currentPayload.userId, %7B%0A            json: userUpdate%0A        %7D);%0A    %7D;%0A%0A    if (req.body.Erhebung) %7B%0A        consentPromise = api(req).get('/consents/', %7B%0A            qs: %7BuserId: res.locals.currentPayload.userId%7D%0A        %7D).then(consent =%3E %7B%0A            consentUpdate.form = 'digital';%0A            consentUpdate.privacyConsent = req.body.Erhebung;%0A            consentUpdate.thirdPartyConsent = req.body.Pseudonymisierung;%0A            consentUpdate.termsOfUseConsent = req.body.Nutzungsbedingungen;%0A            consentUpdate.researchConsent = req.body.Forschung;%0A            consentPromise = api(req).patch('/consents/' + consent.data%5B0%5D._id, %7B%0A                json: %7BuserConsent: consentUpdate%7D%0A            %7D);%0A        %7D)%0A        %0A    %7D
 %0A%0A  

handle mail, consent, birthday in firstlogin submit