"use strict";
/*
 * server component for the TimeLimit App
 * Copyright (C) 2019 - 2026 Jonas Lochmann
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.dispatchChangeParentPassword = dispatchChangeParentPassword;
const changeparentpassword_1 = require("../../../../action/changeparentpassword");
const index_1 = require("../exception/index");
const missing_item_1 = require("../exception/missing-item");
async function dispatchChangeParentPassword({ action, cache }) {
    const parentEntry = await cache.database.user.findOne({
        where: {
            familyId: cache.familyId,
            userId: action.parentUserId,
            type: 'parent'
        },
        transaction: cache.transaction
    });
    if (!parentEntry) {
        throw new missing_item_1.MissingUserException();
    }
    try {
        action.assertIntegrityValid({ oldPasswordSecondHash: parentEntry.secondPasswordHash });
    }
    catch (ex) {
        if (ex instanceof changeparentpassword_1.InvalidChangeParentPasswordIntegrityException) {
            throw new index_1.ApplyActionException({ staticMessage: 'invalid new password integrity' });
        }
        else
            throw ex;
    }
    const newSecondPasswordHash = action.decryptSecondHash({ oldPasswordSecondHash: parentEntry.secondPasswordHash });
    parentEntry.passwordHash = action.newPasswordFirstHash;
    parentEntry.secondPasswordSalt = action.newPasswordSecondSalt;
    parentEntry.secondPasswordHash = newSecondPasswordHash;
    await parentEntry.save({ transaction: cache.transaction });
    {
        const clear = cache.getSecondPasswordHashOfParent.cache.clear;
        if (clear)
            clear();
    }
    cache.invalidiateUserList = true;
    cache.incrementTriggeredSyncLevel(2);
}
//# sourceMappingURL=changeparentpassword.js.map