"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const error_1=require("@b/utils/error"),db_1=require("@b/db"),passwords_1=require("@b/utils/passwords"),utils_1=require("./utils"),query_1=require("@b/utils/query");exports.metadata={summary:"Creates a new user",operationId:"createUser",tags:["Admin","CRM","User"],logModule:"ADMIN_CRM",logTitle:"Create user",requestBody:{required:!0,content:{"application/json":{schema:utils_1.userUpdateSchema}}},responses:(0,query_1.storeRecordResponses)(utils_1.userStoreSchema,"Page"),requiresAuth:!0,permission:"create.user"};exports.default=async e=>{const{body:r,user:s,ctx:a}=e,{firstName:t,lastName:o,email:i,roleId:u,avatar:l,phone:d,emailVerified:n,status:m="ACTIVE",profile:c}=r;null==a||a.step("Validating user authorization");if(!(null==s?void 0:s.id))throw(0,error_1.createError)({statusCode:401,message:"Unauthorized access"});null==a||a.step("Checking for existing user");if(await db_1.models.user.findOne({where:{email:i},include:[{model:db_1.models.role,as:"role"}]}))throw(0,error_1.createError)({statusCode:400,message:"User already exists"});const p=await(0,passwords_1.hashPassword)("12345678");null==a||a.step("Validating role assignment");const _=await db_1.models.role.findOne({where:{name:"Super Admin"}});if(u===(null==_?void 0:_.id))throw(0,error_1.createError)({statusCode:400,message:"You cannot create a Super Admin"});null==a||a.step("Creating user");await db_1.models.user.create({firstName:t,lastName:o,email:i,roleId:Number(u),password:p,avatar:l,phone:d,emailVerified:n,status:m,profile:c});null==a||a.success();return{message:"User created successfully, Password is 12345678"}};