"use strict";async function saveOTPQuery(e,r,t){if(!r||!t)throw(0,error_1.createError)({statusCode:400,message:"Missing required parameters"});const s=(()=>{const e=new Set;for(;e.size<12;){const r=crypto_1.default.randomBytes(6).toString("hex").toUpperCase(),t=`${r.slice(0,4)}-${r.slice(4,8)}-${r.slice(8,12)}`;e.add(t)}return Array.from(e)})();let o;try{const a=await db_1.models.twoFactor.findOne({where:{userId:e}});if(a){const[e,[i]]=await db_1.models.twoFactor.update({secret:r,type:t,enabled:!0,recoveryCodes:JSON.stringify(s)},{where:{id:a.id},returning:!0});o=i.get({plain:!0})}else{o=(await db_1.models.twoFactor.create({userId:e,secret:r,type:t,enabled:!0,recoveryCodes:JSON.stringify(s)})).get({plain:!0})}}catch(e){console_1.logger.error("USER","Error saving OTP configuration",e);throw(0,error_1.createError)({statusCode:500,message:"Server error"})}return{...o,recoveryCodes:s}}var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;exports.saveOTPQuery=saveOTPQuery;const error_1=require("@b/utils/error"),db_1=require("@b/db"),crypto_1=__importDefault(require("crypto")),console_1=require("@b/utils/console"),query_1=require("@b/utils/query");exports.metadata={summary:"Saves the OTP configuration for the user and generates recovery codes",operationId:"saveOTP",description:"Saves the OTP configuration for the user and generates 12 recovery codes for recovery",tags:["Profile"],requiresAuth:!0,logModule:"USER",logTitle:"Save OTP configuration",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",properties:{secret:{type:"string",description:"OTP secret"},type:{type:"string",description:"Type of OTP",enum:["EMAIL","SMS","APP"]}},required:["secret","type"]}}}},responses:{200:{description:"OTP configuration and recovery codes saved successfully",content:{"application/json":{schema:{type:"object",properties:{status:{type:"boolean",description:"Indicates if the request was successful"},statusCode:{type:"number",description:"HTTP status code",example:200},data:{type:"object",properties:{message:{type:"string",description:"Success message"},recoveryCodes:{type:"array",items:{type:"string"},description:"Array of generated recovery codes"}}}}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("User"),500:query_1.serverErrorResponse}};exports.default=async e=>{const{user:r,body:t,ctx:s}=e;if(!(null==r?void 0:r.id)){null==s||s.fail("User not authenticated");throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"})}const{secret:o,type:a}=t;null==s||s.step("Saving OTP configuration");const i=await saveOTPQuery(r.id,o,a);null==s||s.success("OTP configuration saved successfully");return{message:"OTP configuration saved successfully",recoveryCodes:i.recoveryCodes}};