"use strict";async function checkLicenseFileExists(e){if(!e)return!1;const t=process.cwd(),a=t.endsWith("backend")||t.endsWith("backend/")||t.endsWith("backend\\")?path_1.default.dirname(t):t,s=path_1.default.join(a,"lic",`${e}.lic`);try{await fs_1.promises.access(s);return!0}catch(e){return!1}}var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const db_1=require("@b/db"),query_1=require("@b/utils/query"),sequelize_1=require("sequelize"),fs_1=require("fs"),path_1=__importDefault(require("path"));exports.metadata={summary:"Updates the status of an Exchange",operationId:"updateExchangeStatus",tags:["Admin","Exchanges"],parameters:[{index:0,name:"id",in:"path",required:!0,description:"ID of the exchange to update",schema:{type:"string"}}],requestBody:{required:!0,content:{"application/json":{schema:{type:"object",properties:{status:{type:"boolean",description:"New status to apply (true for active, false for inactive)"}},required:["status"]}}}},responses:(0,query_1.updateRecordResponses)("Exchange"),requiresAuth:!0,permission:"edit.exchange",logModule:"ADMIN_FIN",logTitle:"Update Exchange Provider Status"};exports.default=async e=>{const{body:t,params:a,ctx:s}=e,{id:i}=a,{status:n}=t;if(!0===n){null==s||s.step("Checking exchange provider license");const e=await db_1.models.exchange.findByPk(i);if(null==e?void 0:e.productId){if(!await checkLicenseFileExists(e.productId))return{statusCode:403,body:{message:"Cannot enable exchange provider: License not activated. Please activate your license first.",licenseRequired:!0,productId:e.productId}}}}null==s||s.step("Starting transaction");const r=await db_1.sequelize.transaction();try{if(n){null==s||s.step("Deactivating other exchanges");await db_1.models.exchange.update({status:!1},{where:{id:{[sequelize_1.Op.ne]:i}},transaction:r})}null==s||s.step("Updating exchange status");await db_1.models.exchange.update({status:n},{where:{id:i},transaction:r});null==s||s.step("Committing transaction");await r.commit();null==s||s.success();return{statusCode:200,body:{message:"Exchange status updated successfully"}}}catch(e){await r.rollback();return{statusCode:500,body:{message:"Failed to update exchange status",error:e.message}}}};