"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const error_1=require("@b/utils/error"),db_1=require("@b/db"),query_1=require("@b/utils/query"),utils_1=require("../utils");exports.metadata={summary:"Updates a specific user by UUID",operationId:"updateUserByUuid",tags:["Admin","CRM","User"],logModule:"ADMIN_CRM",logTitle:"Update user",parameters:[{index:0,name:"id",in:"path",required:!0,description:"ID of the user to update",schema:{type:"string"}}],requestBody:{required:!0,content:{"application/json":{schema:utils_1.userUpdateSchema}}},responses:(0,query_1.updateRecordResponses)("User"),requiresAuth:!0,permission:"edit.user"};exports.default=async e=>{const{params:r,body:t,user:s,ctx:a}=e,{id:o}=r,{firstName:i,lastName:d,email:u,roleId:l,avatar:n,phone:p,emailVerified:m,twoFactor:c,status:f,profile:h}=t;null==a||a.step("Validating user authorization");if(!(null==s?void 0:s.id))throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"});const _=await db_1.models.user.findOne({where:{id:s.id},include:[{model:db_1.models.role,as:"role"}]});null==a||a.step("Fetching target user");const b=await db_1.models.user.findOne({where:{id:o},include:[{model:db_1.models.role,as:"role"}]});if(!b)throw(0,error_1.createError)({statusCode:404,message:"User not found"});if(b.id===_.id&&"Super Admin"!==_.role.name)throw(0,error_1.createError)({statusCode:400,message:"You cannot update your own account"});null==a||a.step("Updating user details");await db_1.models.user.update({firstName:i,lastName:d,email:u,avatar:n,phone:p,emailVerified:m,status:f,profile:h,..."Super Admin"===_.role.name&&{roleId:l}},{where:{id:o}});if(c){null==a||a.step("Disabling two-factor authentication");await db_1.models.twoFactor.update({enabled:!1},{where:{userId:o}})}null==a||a.success();return{message:"User updated successfully"}};