"use strict";async function getActiveInvestment(e,t){let s,r;switch(t.toLowerCase()){case"general":s=db_1.models.investment;r=db_1.models.investmentDuration;break;case"forex":s=db_1.models.forexInvestment;r=db_1.models.forexDuration}const n=await s.findAll({where:{userId:e,status:"ACTIVE"},include:[{model:r,as:"duration"}],attributes:{exclude:["userId"]}});if(!n||0===n.length)throw(0,error_1.createError)({statusCode:404,message:"No active investments found"});return n.map(e=>e.get({plain:!0}))}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("./utils"),error_1=require("@b/utils/error"),constants_1=require("@b/utils/constants");exports.metadata={summary:"Lists all investments",description:"Fetches a comprehensive list of all investments made by users, including details of the investment plan and user information.",operationId:"listAllInvestments",tags:["Finance","Investment"],parameters:[...constants_1.crudParameters,{name:"type",in:"query",description:"The type of investment to retrieve",schema:{type:"string"}}],responses:{200:{description:"Investments retrieved successfully",content:{"application/json":{schema:{type:"object",properties:{data:{type:"array",items:{type:"object",properties:{...utils_1.baseInvestmentSchema,user:{type:"object",properties:utils_1.baseUserSchema},plan:{type:"object",properties:utils_1.baseInvestmentPlanSchema}}}},pagination:constants_1.paginationSchema}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("Investment"),500:query_1.serverErrorResponse},requiresAuth:!0};exports.default=async e=>{const{user:t}=e;if(!t)throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"});const{type:s,...r}=e.query;if(s&&!r.page)return await getActiveInvestment(t.id,s);let n,a,o;switch(null==s?void 0:s.toLowerCase()){case"general":n=db_1.models.investment;a=db_1.models.investmentPlan;o=db_1.models.investmentDuration;break;case"forex":n=db_1.models.forexInvestment;a=db_1.models.forexPlan;o=db_1.models.forexDuration;break;default:throw(0,error_1.createError)({statusCode:400,message:"Invalid investment type"})}return(0,query_1.getFiltered)({model:n,query:r,where:{userId:t.id},sortField:r.sortField||"createdAt",includeModels:[{model:a,as:"plan",attributes:["id","title","image","currency"]},{model:o,as:"duration",attributes:["id","duration","timeframe"]}],numericFields:["amount","profit"]})};