/*Canopy - The next generation of stoner streaming software Copyright (C) 2024-2025 Rainbownapkin and the TTN Community 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, either version 3 of the License, or (at your option) any later version. 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 .*/ //NPM Imports const {validationResult, matchedData} = require('express-validator'); //local imports const {userModel} = require('../../../schemas/user/userSchema'); const accountUtils = require('../../../utils/sessionUtils'); const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); module.exports.post = async function(req, res){ const validResult = validationResult(req); const data = matchedData(req); var tempResult = []; //if we're not chaning the password if(data.passChange == null){ //go through validation errors validResult.errors.forEach(function(error, i){ //remove irrelevant password validation errors (i know its gross but they need to be optional as a set, not individually) if(!error.path.startsWith("passChange.")){ tempResult.push(error); } }); validResult.errors = tempResult; } try{ if(validResult.isEmpty()){ const {field, change} = data; const {user} = req.session; const userDB = await userModel.findOne(user); const update = {}; if(userDB){ if(data.img){ userDB.img = data.img; update.img = data.img; } if(data.bio){ userDB.bio = data.bio; update.bio = data.bio; } if(data.signature){ userDB.signature = data.signature; update.signature = data.signature; } if(data.passChange){ //kill active session to prevent connect-mongo from freaking out accountUtils.killSession(req.session); await userDB.changePassword(data.passChange); } await userDB.save(); res.status(200); return res.send(update); }else{ res.status(400); return res.send({errors: [{msg:"User not found!"}]}); } }else{ res.status(400); res.send({errors: validResult.array()}) } }catch(err){ exceptionHandler(res, err); } }