"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:"Block a user account",description:"Block a user account with reason and optional duration",operationId:"blockUser",tags:["Admin","CRM","User"],logModule:"ADMIN_CRM",logTitle:"Block user",parameters:[{index:0,name:"id",in:"path",required:!0,description:"ID of the user to block",schema:{type:"string"}}],requestBody:{required:!0,content:{"application/json":{schema:{type:"object",properties:{reason:{type:"string",description:"Reason for blocking the user"},isTemporary:{type:"boolean",description:"Whether this is a temporary block",default:!1},duration:{type:"number",description:"Duration in hours (only for temporary blocks)"}},required:["reason"]}}}},responses:{200:{description:"User blocked successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"},blockId:{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,body:o,user:t,ctx:s}=e,{id:i}=r,{reason:a,isTemporary:n=!1,duration:d}=o;null==s||s.step("Validating user authorization");if(!(null==t?void 0:t.id))throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"});null==s||s.step("Validating block parameters");if(n&&(!d||d<1||d>8760))throw(0,error_1.createError)({statusCode:400,message:"Duration must be between 1 and 8760 hours for temporary blocks"});null==s||s.step("Fetching target user");const u=await db_1.models.user.findOne({where:{id:i},include:[{model:db_1.models.role,as:"role",attributes:["name"]}]});if(!u)throw(0,error_1.createError)({statusCode:404,message:"User not found"});if(u.role&&"Super Admin"===u.role.name)throw(0,error_1.createError)({statusCode:403,message:"Cannot block Super Admin accounts"});if(u.id===t.id)throw(0,error_1.createError)({statusCode:403,message:"You cannot block your own account"});null==s||s.step("Checking existing blocks");if(await db_1.models.userBlock.findOne({where:{userId:i,isActive:!0,[sequelize_1.Op.or]:[{isTemporary:!1},{isTemporary:!0,blockedUntil:{[sequelize_1.Op.gt]:new Date}}]}}))throw(0,error_1.createError)({statusCode:400,message:"User is already blocked"});null==s||s.step("Creating block record");let l=null;n&&d&&(l=new Date(Date.now()+60*d*60*1e3));const c=await db_1.models.userBlock.create({userId:i,adminId:t.id,reason:a,isTemporary:n,duration:n?d:null,blockedUntil:l,isActive:!0});null==s||s.step("Updating user status");const p=n?"SUSPENDED":"BANNED";await db_1.models.user.update({status:p},{where:{id:i}});null==s||s.success();return{message:`User ${n?"temporarily blocked":"blocked"} successfully`,blockId:c.id}};