"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const db_1=require("@b/db"),error_1=require("@b/utils/error"),sequelize_1=require("sequelize");exports.metadata={summary:"Unblock a user account",description:"Unblock a user account and restore access",operationId:"unblockUser",tags:["Admin","CRM","User"],logModule:"ADMIN_CRM",logTitle:"Unblock user",parameters:[{index:0,name:"id",in:"path",required:!0,description:"ID of the user to unblock",schema:{type:"string"}}],responses:{200:{description:"User unblocked successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"}}}}}},400:{description:"Bad request"},401:{description:"Unauthorized"},403:{description:"Forbidden"},404:{description:"User not found"}},requiresAuth:!0,permission:"edit.user"};exports.default=async e=>{const{params:r,user:s,ctx:t}=e,{id:o}=r;null==t||t.step("Validating user authorization");if(!(null==s?void 0:s.id))throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"});null==t||t.step("Fetching target user");if(!await db_1.models.user.findOne({where:{id:o},include:[{model:db_1.models.role,as:"role",attributes:["name"]}]}))throw(0,error_1.createError)({statusCode:404,message:"User not found"});null==t||t.step("Checking active block");const i=await db_1.models.userBlock.findOne({where:{userId:o,isActive:!0,[sequelize_1.Op.or]:[{isTemporary:!1},{isTemporary:!0,blockedUntil:{[sequelize_1.Op.gt]:new Date}}]}});if(!i)throw(0,error_1.createError)({statusCode:400,message:"User is not currently blocked"});null==t||t.step("Deactivating block record");await db_1.models.userBlock.update({isActive:!1},{where:{id:i.id}});null==t||t.step("Restoring user access");await db_1.models.user.update({status:"ACTIVE"},{where:{id:o}});null==t||t.success();return{message:"User unblocked successfully"}};