"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}});Object.defineProperty(e,o,n)}:function(e,t,r,o){void 0===o&&(o=r);e[o]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(){var e=function(t){e=Object.getOwnPropertyNames||function(e){var t=[];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[t.length]=r);return t};return e(t)};return function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var o=e(t),n=0;n<o.length;n++)"default"!==o[n]&&__createBinding(r,t,o[n]);__setModuleDefault(r,t);return r}}();Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const error_1=require("@b/utils/error"),db_1=require("@b/db"),constants_1=require("@b/utils/constants");exports.metadata={summary:"Send phone verification code",operationId:"sendPhoneVerificationCode",tags:["User","Phone"],requiresAuth:!0,logModule:"USER",logTitle:"Send phone verification code",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",properties:{phoneNumber:{type:"string",description:"Phone number to verify"}},required:["phoneNumber"]}}}},responses:{200:{description:"Code sent"},400:{description:"Bad request"},401:{description:"Unauthorized"}}};exports.default=async e=>{const{user:t,body:r,ctx:o}=e;if(!t){null==o||o.fail("User not authenticated");throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"})}const{phoneNumber:n}=r;if(!n){null==o||o.fail("Phone number missing");throw(0,error_1.createError)({statusCode:400,message:"Phone required"})}null==o||o.step("Generating verification code");const i=Math.floor(1e5+9e5*Math.random()).toString();null==o||o.step("Sending SMS via Twilio");try{const e=(0,(await Promise.resolve().then(()=>__importStar(require("twilio")))).default)(constants_1.APP_TWILIO_ACCOUNT_SID,constants_1.APP_TWILIO_AUTH_TOKEN);await e.messages.create({body:`Your verification code is: ${i}`,from:process.env.APP_TWILIO_PHONE_NUMBER,to:n})}catch(e){null==o||o.fail("Error sending SMS");throw(0,error_1.createError)({statusCode:500,message:"Error sending SMS"})}null==o||o.step("Storing verification code");await db_1.models.user.update({phoneVerificationCode:i,phoneVerificationExpiresAt:new Date(Date.now()+6e5),phoneTemp:n},{where:{id:t.id}});null==o||o.success("Verification code sent successfully");return{message:"Verification code sent to phone."}};