"use strict";async function getLastCandles(){try{return(await Promise.resolve().then(()=>__importStar(require("@b/api/(ext)/ecosystem/utils/scylla/queries")))).getLastCandles()}catch(e){return[]}}function parseMetadata(e){let t={};try{e=e.replace(/\\/g,"");t=JSON.parse(e)||{}}catch(e){console_1.logger.error("TRANSFER","Invalid JSON in metadata",e)}return t}var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,a){void 0===a&&(a=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,a,n)}:function(e,t,r,a){void 0===a&&(a=r);e[a]=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 a=e(t),n=0;n<a.length;n++)"default"!==a[n]&&__createBinding(r,t,a[n]);__setModuleDefault(r,t);return r}}(),__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const query_1=require("@b/utils/query"),db_1=require("@b/db"),utils_1=require("@b/api/finance/transaction/utils"),exchange_1=__importDefault(require("@b/utils/exchange")),redis_1=require("@b/utils/redis"),console_1=require("@b/utils/console"),wallet_1=require("@b/services/wallet"),error_1=require("@b/utils/error");exports.metadata={summary:"Updates an existing transaction",operationId:"updateTransaction",tags:["Admin","Wallets","Transactions"],parameters:[{index:0,name:"id",in:"path",description:"The ID of the transaction to update",required:!0,schema:{type:"string"}}],requestBody:{required:!0,description:"Updated data for the transaction",content:{"application/json":{schema:utils_1.transactionUpdateSchema}}},responses:(0,query_1.updateRecordResponses)("Transaction"),requiresAuth:!0,permission:"edit.transfer",logModule:"ADMIN_FIN",logTitle:"Update Transfer"};exports.default=async e=>{const{body:t,params:r,ctx:a}=e,{id:n}=r,{status:s,amount:i,fee:o,description:c,referenceId:u,metadata:d}=t,l=await db_1.models.transaction.findOne({where:{id:n}});if(!l)throw(0,error_1.createError)({statusCode:404,message:"Transaction not found"});if("PENDING"!==l.status)throw(0,error_1.createError)({statusCode:400,message:"Only pending transactions can be updated"});l.amount=i;l.fee=o;l.description=c;l.referenceId=u;return await db_1.sequelize.transaction(async e=>{const t=parseMetadata(l.metadata),r=await db_1.models.wallet.findOne({where:{id:l.walletId},transaction:e});if(!r)throw(0,error_1.createError)({statusCode:404,message:"Wallet not found"});let n=1;const i=t.fromCurrency;if(!i)throw(0,error_1.createError)({statusCode:400,message:"From currency not found"});const o=l.description.split(" ")[2];if("FIAT"===o){const t=await db_1.models.currency.findOne({where:{id:i},transaction:e});if(!t)throw(0,error_1.createError)({statusCode:404,message:"Currency not found"});if(null===t.price||void 0===t.price)throw(0,error_1.createError)({statusCode:400,message:"Currency price not found"});n=t.price}else if("SPOT"===o){if("USDT"!==i){const e=redis_1.RedisSingleton.getInstance(),t=await e.get("exchange:tickers"),r=t?JSON.parse(t):{};if(r[i])n=r[i].last;else{const e=await exchange_1.default.startExchange(a),t=await e.fetchTicker(`${i}/USDT`);if(!t||!t.last)throw(0,error_1.createError)({statusCode:500,message:"Unable to fetch current market price"});n=t.last}}}else if("ECO"===o){const e=(await getLastCandles()).find(e=>e.symbol===i);if(!e)throw(0,error_1.createError)({statusCode:500,message:"Unable to fetch candle data for the currency"});n=e.close}const c=Number(l.amount)*n,u=`admin_transfer_approve_${l.id}`;await wallet_1.walletService.credit({idempotencyKey:u,userId:l.userId,walletId:r.id,walletType:r.type,currency:r.currency,amount:c,operationType:"TRANSFER",referenceId:l.id,description:`Transfer approved - ${c} ${r.currency}`,metadata:{transactionId:l.id,originalAmount:l.amount,price:n,fromCurrency:t.fromCurrency,fromType:o},transaction:e});d&&(t.message=d.message);l.metadata=JSON.stringify(t);l.status=s;await l.save({transaction:e});return{message:"Transaction updated successfully"}})};