"use strict";async function updateUserWalletBalance(e,t,a,r,s){var l,i,n,o,d;null===(l=null==s?void 0:s.step)||void 0===l||l.call(s,`Updating wallet balance: ${e} (${r})`);const c=await db_1.models.wallet.findOne({where:{id:e}});if(!c){null===(i=null==s?void 0:s.fail)||void 0===i||i.call(s,"Wallet not found");throw(0,error_1.createError)({statusCode:404,message:"Wallet not found"})}const u=`admin_wallet_${e}_${r}`;let p,w;switch(r){case"WITHDRAWAL":p=t+a;w="WITHDRAW";if(c.balance<p){null===(n=null==s?void 0:s.fail)||void 0===n||n.call(s,"Insufficient balance");throw(0,error_1.createError)({statusCode:400,message:"Insufficient balance"})}await wallet_1.walletService.debit({idempotencyKey:u,userId:c.userId,walletId:c.id,walletType:c.type,currency:c.currency,amount:p,operationType:w,description:`Admin withdrawal - ${t} + ${a} fee`});break;case"DEPOSIT":p=t-a;w="DEPOSIT";await wallet_1.walletService.credit({idempotencyKey:u,userId:c.userId,walletId:c.id,walletType:c.type,currency:c.currency,amount:p,operationType:w,description:`Admin deposit - ${t} - ${a} fee`});break;case"REFUND_WITHDRAWAL":p=t+a;w="REFUND_WITHDRAWAL";await wallet_1.walletService.credit({idempotencyKey:u,userId:c.userId,walletId:c.id,walletType:c.type,currency:c.currency,amount:p,operationType:w,description:`Withdrawal refund - ${t} + ${a} fee`});break;default:throw(0,error_1.createError)({statusCode:400,message:"Invalid operation type"})}const m=await db_1.models.wallet.findOne({where:{id:c.id}});if(!m){null===(o=null==s?void 0:s.fail)||void 0===o||o.call(s,"Failed to update wallet balance");throw(0,error_1.createError)({message:"Failed to update wallet balance",statusCode:500})}null===(d=null==s?void 0:s.success)||void 0===d||d.call(s,`Wallet balance updated: ${m.id} - ${m.balance}`);return m}Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;exports.updateUserWalletBalance=updateUserWalletBalance;const emails_1=require("@b/utils/emails"),error_1=require("@b/utils/error"),db_1=require("@b/db"),query_1=require("@b/utils/query"),console_1=require("@b/utils/console"),wallet_1=require("@b/services/wallet");exports.metadata={summary:"Rejects a spot wallet withdrawal request",operationId:"rejectSpotWalletWithdrawal",tags:["Admin","Wallets"],parameters:[{index:0,name:"id",in:"path",required:!0,description:"ID of the withdrawal transaction to reject",schema:{type:"string",format:"uuid"}}],requestBody:{required:!0,content:{"application/json":{schema:{type:"object",properties:{message:{type:"string",description:"Reason for rejecting the withdrawal request"}},required:["message"]}}}},responses:{200:{description:"Withdrawal request rejected successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"}}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("Wallet"),500:query_1.serverErrorResponse},permission:"edit.wallet",requiresAuth:!0,logModule:"ADMIN_FIN",logTitle:"Reject Withdrawal"};exports.default=async e=>{var t;const{params:a,body:r,ctx:s}=e,{id:l}=a,{message:i}=r;try{null==s||s.step("Fetching transaction");const e=await db_1.models.transaction.findOne({where:{id:l}});if(!e)throw(0,error_1.createError)({statusCode:404,message:"Transaction not found"});if("PENDING"!==e.status)throw(0,error_1.createError)({statusCode:400,message:"Transaction is not pending"});const{walletId:a}=e;null==s||s.step("Updating transaction status to rejected");await db_1.models.transaction.update({status:"REJECTED",metadata:{...e.metadata,note:i||"Withdrawal request rejected"}},{where:{id:l}});const r=await db_1.models.transaction.findOne({where:{id:l}});if(!r)throw(0,error_1.createError)({statusCode:500,message:"Failed to update transaction status"});const n=r.get({plain:!0});null==s||s.step("Refunding wallet balance");const o=await updateUserWalletBalance(a,Number(n.amount),Number(n.fee),"REFUND_WITHDRAWAL",s);try{null==s||s.step("Sending rejection email");const a=await db_1.models.user.findOne({where:{id:e.userId}});await(0,emails_1.sendTransactionStatusUpdateEmail)(a,n,o,o.balance,(null===(t=n.metadata)||void 0===t?void 0:t.note)||"Withdrawal request rejected")}catch(e){console_1.logger.error("WALLET","Error sending withdrawal rejection email",e)}null==s||s.success("Withdrawal rejected successfully");return{message:"Withdrawal rejected successfully"}}catch(e){throw(0,error_1.createError)({statusCode:e.statusCode||500,message:e.message})}};