"use strict";async function updateWalletBalance(e,t,a){const r=await db_1.models.wallet.findOne({where:{id:e}});if(!r)throw(0,error_1.createError)({statusCode:404,message:"Wallet not found"});const l=await db_1.models.user.findOne({where:{id:r.userId}});if(!l)throw(0,error_1.createError)({statusCode:404,message:"User not found"});if("SUBTRACT"===t&&r.balance<a)throw(0,error_1.createError)({statusCode:400,message:"Insufficient funds in wallet"});const d=`admin_balance_${e}_${t}_${a}`;let s;s="ADD"===t?await wallet_1.walletService.credit({idempotencyKey:d,userId:r.userId,walletId:r.id,walletType:r.type,currency:r.currency,amount:a,operationType:"ADMIN_ADJUSTMENT",description:`Admin added ${a} ${r.currency} to wallet`,metadata:{method:"ADMIN",adjustmentType:"ADD"}}):await wallet_1.walletService.debit({idempotencyKey:d,userId:r.userId,walletId:r.id,walletType:r.type,currency:r.currency,amount:a,operationType:"ADMIN_ADJUSTMENT",description:`Admin subtracted ${a} ${r.currency} from wallet`,metadata:{method:"ADMIN",adjustmentType:"SUBTRACT"}});const n=await db_1.models.wallet.findOne({where:{id:e}});if(!n)throw(0,error_1.createError)({statusCode:404,message:"Wallet not found"});await(0,emails_1.sendWalletBalanceUpdateEmail)(l,n,"ADD"===t?"added":"subtracted",a,s.newBalance)}Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;exports.updateWalletBalance=updateWalletBalance;const emails_1=require("@b/utils/emails"),db_1=require("@b/db"),query_1=require("@b/utils/query"),wallet_1=require("@b/services/wallet"),error_1=require("@b/utils/error");exports.metadata={summary:"Updates the balance of a wallet",operationId:"updateWalletBalance",tags:["Admin","Wallets"],parameters:[{index:0,name:"id",in:"path",required:!0,description:"ID of the wallet to update",schema:{type:"string"}}],requestBody:{required:!0,description:"Data needed to update the wallet balance",content:{"application/json":{schema:{type:"object",properties:{type:{type:"string",description:"Type of balance update (ADD or SUBTRACT)",enum:["ADD","SUBTRACT"]},amount:{type:"number",description:"Amount by which to update the wallet balance"}},required:["id","type","amount"]}}}},responses:(0,query_1.updateRecordResponses)("Wallet"),requiresAuth:!0,permission:"edit.wallet",logModule:"ADMIN_FIN",logTitle:"Update Wallet Balance"};exports.default=async e=>{const{id:t}=e.params,{type:a,amount:r}=e.body,{ctx:l}=e;null==l||l.step("Updating wallet balance");await updateWalletBalance(t,a,r);null==l||l.success("Wallet balance updated successfully");return{message:"Wallet balance updated successfully"}};