Skip to content

Commit

Permalink
add axios logic in getCombinedData controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Sushant-Coder-01 committed Aug 17, 2024
1 parent 440d85c commit ae1dc0d
Showing 1 changed file with 31 additions and 52 deletions.
83 changes: 31 additions & 52 deletions backend/src/controllers/transaction.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Transaction } from "../models/Transaction.js";
import { ApiError } from "../utils/ApiError.js";
import { ApiResponse } from "../utils/ApiResponse.js";
import { asyncHandler } from "../utils/AsyncHandler.js";
import axios from "axios";

// list transactions API

const listTransactions = asyncHandler( async(req, res) => {

const { search = '', page = 1, perPage = 10, month } = req.query;

console.log('month:', month);

// validate page and perPage.
const pageNum = parseInt(page,10);
const perPageNum = parseInt(perPage,10);
Expand All @@ -36,10 +35,6 @@ const listTransactions = asyncHandler( async(req, res) => {
}
};

console.log('Query:', query);
console.log('Start Date:', startDate.toISOString());
console.log('End Date:', endDate.toISOString());

// added search criteria
if(search){
query.$or = [
Expand All @@ -55,8 +50,6 @@ const listTransactions = asyncHandler( async(req, res) => {
.skip((pageNum - 1)*perPageNum)
.limit(parseInt(perPageNum));

console.log("transaction:", transaction);

if(!transaction.length){
throw new ApiError(404,"No transaction found!");
}
Expand Down Expand Up @@ -106,19 +99,16 @@ const getTransactionStatistics = asyncHandler( async(req, res) => {
// Execute the aggregation pipeline
const stats = await Transaction.aggregate(pipeline);

console.log("stats:",stats);
console.log('Start Date:', startDate.toISOString());
console.log('End Date:', endDate.toISOString());
console.log('Aggregation Pipeline:', JSON.stringify(pipeline));


// Check if statistics were found
if (!stats.length) {
throw new ApiError(404, "No statistics found for the given month.");
}


// Send response
res.status(200).json(
res.status(200).json(
new ApiResponse(200, stats[0], "Transaction statistics fetched successfully!")
);
})
Expand Down Expand Up @@ -226,52 +216,41 @@ const getPieChartData = asyncHandler( async(req, res) => {

// combined data.

const getCombinedData = asyncHandler( async(req, res) =>{
const getCombinedData = asyncHandler(async (req, res) => {

const{month} = req.query;
const { month } = req.query ;

// Define async functions with error handling
// validate month parameter.

const fetchTransactionStaticstics = async () => {
try {
return await getTransactionStatistics(req, res);
} catch (error) {
throw new ApiError(500,"Error fechting transaction staticstics.");
}
if(!month){
throw new ApiError(400,"Month is required!");
}

const fetchBarChartData = async () => {
try {
return await getBarChartData(req, res);
} catch (error) {
throw new ApiError(500, "Error fetching bar chart data.");
}
};

const fetchPieChartData = async () => {
try {
return await getPieChartData(req, res);
} catch (error) {
throw new ApiError(500, "Error fetching pie chart data.");
}
};

// Run queries in parallel
const [stats, barChartData, pieChartData] = await Promise.all([
fetchTransactionStaticstics(),
fetchBarChartData(),
fetchPieChartData()
]);

// Send response
res.status(200).json({
statistics: new ApiResponse(200, stats, "Transaction statistics fetched successfully!"),
barChart: new ApiResponse(200, barChartData, "Bar chart data fetched successfully!"),
pieChart: new ApiResponse(200, pieChartData, "Pie chart data fetched successfully!")
});
try {
const [ transactionListResponse, statsResponse, barChartResponse, pieChartResponse ] = await Promise.all([
axios.get(`http://localhost:8000/api/transactions/?month=${month}`),
axios.get(`http://localhost:8000/api/transactions/statistics?month=${month}`),
axios.get(`http://localhost:8000/api/transactions/barchart?month=${month}`),
axios.get(`http://localhost:8000/api/transactions/piechart?month=${month}`)
]);

const transactionList = transactionListResponse.data ;
const stats = statsResponse.data ;
const barChartData = barChartResponse.data ;
const pieChartData = pieChartResponse.data ;

console.log("transactionList:",transactionList);

res.status(200).json(
new ApiResponse(200,transactionList, stats, barChartData, pieChartData, "combined data fetched successfully !"),
);
} catch (error) {

throw new ApiError(500,"data is not fetched!");
}

});

})

export {
listTransactions,
Expand Down

0 comments on commit ae1dc0d

Please sign in to comment.