Back to EIP
Composed Message Processor
⋯
service /api/v1 on new http:Listener(8080) {
resource function post dashboard(SalesByStateRequest salesRequest) returns AggregratedSales|error {
AggregratedSales summary = {};
foreach string state in salesRequest.states {
http:Client? stateClient = stateRoutes[state];
if stateClient == () {
return error("Invalid state provided");
}
SalesByState salesByState = check stateClient->/sales();
aggregateSales(summary, state, salesByState);
}
return summary;
}
}
function aggregateSales(AggregratedSales summary, string state, SalesByState salesByState) {
summary.revenueByState[state] = salesByState.revenue;
summary.totalRevenue += salesByState.revenue;
summary.operatingExpensesByState[state] = salesByState.operatingExpenses;
summary.totalOperatingExpenses += salesByState.operatingExpenses;
summary.totalProduction += salesByState.production;
summary.maxRevenueState = summary.maxRevenue < salesByState.revenue ? state : summary.maxRevenueState;
summary.maxRevenue = summary.maxRevenue < salesByState.revenue ? salesByState.revenue : summary.maxRevenue;
summary.productivityByState[state] = salesByState.production / salesByState.totalEmployees;
}