"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const db_1=require("@b/db"),query_1=require("@b/utils/query"),error_1=require("@b/utils/error"),BinaryOrderService_1=require("../util/BinaryOrderService"),binaryProfit=parseFloat(process.env.NEXT_PUBLIC_BINARY_PROFIT||"87");exports.metadata={summary:"Cancel Binary Order",operationId:"cancelBinaryOrder",tags:["Binary","Orders"],description:"Cancels a binary order for the authenticated user.",parameters:[{name:"id",in:"path",description:"ID of the binary order to cancel.",required:!0,schema:{type:"string"}}],requestBody:{description:"Cancellation percentage data.",content:{"application/json":{schema:{type:"object",properties:{percentage:{type:"number"}}}}},required:!1},responses:{200:{description:"Binary order cancelled",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"}}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("Binary Order"),500:query_1.serverErrorResponse},logModule:"BINARY",logTitle:"Cancel binary order",requiresAuth:!0};exports.default=async e=>{const{body:r,params:t,user:n,ctx:a}=e;if(!(null==n?void 0:n.id))throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"});const{id:o}=t,{percentage:i}=r;try{null==a||a.step("Validating order existence");const e=await db_1.models.binaryOrder.findOne({where:{id:o}});if(!e)throw(0,error_1.createError)(404,"Order not found");null==a||a.step("Checking order status and eligibility for cancellation");null==a||a.step("Fetching current market price");null==a||a.step("Calculating refund amount");null==a||a.step("Refunding wallet balance");null==a||a.step("Updating order status to cancelled");BinaryOrderService_1.BinaryOrderService.cancelOrder(n.id,o,i);null==a||a.success(`Cancelled binary order on ${e.symbol}${i?` with ${i}% penalty`:""}`)}catch(e){null==a||a.fail(e.message||"Failed to cancel binary order");if(503===e.statusCode)throw e;console.error("Error cancelling binary order:",e);throw(0,error_1.createError)(500,"An error occurred while cancelling the order")}};