Skip to content

Commit 9377cf2

Browse files
committed
Added tools - Refund analytics and filter orders by amount
1 parent fe37ea7 commit 9377cf2

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

app/agents/voice/automatic/tools/juspay/analytics.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,85 @@ def get_average_ticket_payment_wise_by_time(
476476
return _make_genius_api_request(params, payload_details)
477477

478478

479+
async def get_refund_analytics_by_time(params: FunctionCallParams):
480+
"""
481+
Get comprehensive refund analytics from the dedicated refund domain including
482+
total refund volume, successful refunds, manual review counts, and breakdown
483+
by payment gateway and method.
484+
"""
485+
try:
486+
input_dimension = params.arguments.get("dimension")
487+
488+
actual_dimensions = []
489+
if input_dimension:
490+
actual_dimensions = [input_dimension]
491+
492+
logger.info(
493+
f"Fetching refund analytics with params: {params.arguments} and dimension: {actual_dimensions}"
494+
)
495+
496+
# Main refund analytics by gateway
497+
analytics_payload = {
498+
"domain": "kvrefundtxns",
499+
"metric": [
500+
"total_volume",
501+
"success_rate",
502+
"manual_review_rate",
503+
"manual_review_count",
504+
"pending_rate",
505+
"refund_pending_5days",
506+
"total_amount",
507+
"mean_turn_around_time",
508+
],
509+
"dimensions": actual_dimensions,
510+
"sortedOn": {"sortDimension": "total_volume", "ordering": "Desc"},
511+
}
512+
513+
analytics_result = await _make_genius_api_request(params, analytics_payload)
514+
if isinstance(analytics_result, ApiFailure):
515+
await params.result_callback(analytics_result.error)
516+
return
517+
518+
await params.result_callback({"data": analytics_result.data})
519+
520+
except Exception as e:
521+
logger.error(
522+
f"Unexpected error in get_refund_analytics_by_time: {e}", exc_info=True
523+
)
524+
await params.result_callback({"error": f"An unexpected error occurred: {e}"})
525+
526+
527+
async def get_orders_by_amount(params: FunctionCallParams) -> GeniusApiResponse:
528+
"""
529+
Retrieves orders filtered by transaction amount using flexible comparison operators. Allows filtering orders above, below, or at specific amount thresholds within a specified time range. Results are grouped by payment method type showing order counts and success volumes. Useful for analyzing high-value transactions, small purchases, or specific spending patterns.
530+
"""
531+
logger.info(f"Fetching orders filtered by amount with params: {params.arguments}")
532+
try:
533+
input_amount = params.arguments.get("amount")
534+
input_operator = params.arguments.get("operator")
535+
536+
analytics_payload = {
537+
"domain": "kvorders",
538+
"metric": ["order_with_transactions", "success_volume"],
539+
"dimensions": ["payment_method_type"],
540+
"filters": {
541+
"condition": input_operator,
542+
"field": "ticket_size",
543+
"val": input_amount,
544+
},
545+
}
546+
547+
analytics_result = await _make_genius_api_request(params, analytics_payload)
548+
if isinstance(analytics_result, ApiFailure):
549+
await params.result_callback(analytics_result.error)
550+
return
551+
552+
await params.result_callback({"data": analytics_result.data})
553+
except Exception as e:
554+
logger.error(f"Unexpected error in get_orders_by_amount: {e}", exc_info=True)
555+
await params.result_callback({"error": f"An unexpected error occurred: {e}"})
556+
557+
479558
async def create_euler_offer(params: FunctionCallParams):
480559
"""
481560
Creates discount offers, cashbacks, and other promotional offers in the platform. IMPORTANT: Before calling this function, you MUST first present all the offer details to the user in a clear, formatted way and explicitly ask for their confirmation. Only proceed with calling this function after the user has explicitly confirmed they want to create the offer. Do not call this function without explicit user confirmation. To set the offer's active period, always use the get_current_time() tool for accurate start and end times in IST.
@@ -1868,6 +1947,43 @@ async def update_euler_offer(params: FunctionCallParams):
18681947
required=["offerCode"],
18691948
)
18701949

1950+
get_refund_analytics_function = FunctionSchema(
1951+
name="get_refund_analytics_by_time",
1952+
description="Get comprehensive refund analytics from the dedicated refund domain including total refund volume, successful refunds, manual review counts, and breakdown by payment gateway and method within a specified time range. Provides insights into refund processing efficiency and patterns.",
1953+
properties={
1954+
**time_input_schema["properties"],
1955+
"dimension": {
1956+
"type": "string",
1957+
"description": "The dimension to group the analytics by. If provided, the results will be grouped by this dimension. Can be empty. How to slice the data: 'payment_gateway' for each gateway (Stripe, Razorpay), 'payment_method_type' for UPI, Cards, NB, etc., currency for different currencies (INR, USD) and banks for each bank (HDFC, SBI) ",
1958+
"enum": [
1959+
"currency",
1960+
"payment_gateway",
1961+
"payment_method_type",
1962+
"bank",
1963+
],
1964+
},
1965+
},
1966+
required=time_input_schema["required"],
1967+
)
1968+
1969+
get_orders_by_amount_function = FunctionSchema(
1970+
name="get_orders_by_amount",
1971+
description="Retrieves orders filtered by transaction amount using flexible comparison operators. Allows filtering orders above, below, or at specific amount thresholds within a specified time range. Results are grouped by payment method type showing order counts and success volumes. Useful for analyzing high-value transactions, small purchases, or specific spending patterns.",
1972+
properties={
1973+
**time_input_schema["properties"],
1974+
"operator": {
1975+
"type": "string",
1976+
"description": "The comparison operator to apply against the ticket size. 'Greater' finds orders above the amount, 'GreaterThanEqual' includes the exact amount and above, 'Less' finds orders below the amount, 'LessThanEqual' includes the exact amount and below.",
1977+
"enum": ["Greater", "GreaterThanEqual", "Less", "LessThanEqual"],
1978+
},
1979+
"amount": {
1980+
"type": "string",
1981+
"description": "The monetary threshold value in the base currency (typically INR) to compare against order ticket sizes. This value will be used with the specified operator to filter transactions.",
1982+
},
1983+
},
1984+
required=["startTime", "endTime", "amount", "operator"],
1985+
)
1986+
18711987
tools = ToolsSchema(
18721988
standard_tools=[
18731989
get_sr_success_rate_function,
@@ -1881,6 +1997,8 @@ async def update_euler_offer(params: FunctionCallParams):
18811997
list_offers_by_filter_function,
18821998
delete_euler_offer_function,
18831999
update_euler_offer_function,
2000+
get_refund_analytics_function,
2001+
get_orders_by_amount_function,
18842002
]
18852003
)
18862004

@@ -1897,4 +2015,6 @@ async def update_euler_offer(params: FunctionCallParams):
18972015
"list_offers_by_filter": list_offers_by_filter,
18982016
"delete_euler_offer": delete_euler_offer,
18992017
"update_euler_offer": update_euler_offer,
2018+
"get_refund_analytics_by_time": get_refund_analytics_by_time,
2019+
"get_orders_by_amount": get_orders_by_amount,
19002020
}

0 commit comments

Comments
 (0)