-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement fare calculation with missed trips discount logic
- Loading branch information
1 parent
d9aa313
commit 086da1c
Showing
1 changed file
with
74 additions
and
5 deletions.
There are no files selected for viewing
79 changes: 74 additions & 5 deletions
79
swift-lift-fare-calculation/swift-lift-fare-calculation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,91 @@ | ||
import boto3 | ||
import json | ||
|
||
from math import ceil | ||
from botocore.exceptions import ClientError | ||
|
||
print("Packages have imported successfully") | ||
|
||
# Initialize DynamoDB | ||
dynamodb = boto3.resource('dynamodb') | ||
|
||
# DynamoDB table name | ||
TRIPS_TABLE = "trips" # Replace with your table name | ||
|
||
def lambda_handler(event, context): | ||
body = {} | ||
statusCode = 200 | ||
headers = { | ||
"Content-Type": "application/json" | ||
} | ||
try: | ||
print("Processing") | ||
print(event) | ||
print(context) | ||
|
||
# Extract passenger_id and total trips from the event | ||
passenger_id = event.get('passenger_id') | ||
total_trips = event.get('total_trips') | ||
if not passenger_id or not total_trips: | ||
raise ValueError("passenger_id and total_trips must be provided") | ||
|
||
# Fetch missed trips for the passenger | ||
trips_table = dynamodb.Table(TRIPS_TABLE) | ||
response = trips_table.get_item(Key={"passenger_id": passenger_id}) | ||
if 'Item' not in response: | ||
raise ValueError("Passenger data not found") | ||
|
||
missed_trips = response['Item'].get('missed_trips', 0) | ||
print(f"Total trips: {total_trips}, Missed trips: {missed_trips}") | ||
|
||
# Calculate the discount threshold | ||
discount_threshold = ceil(total_trips * 0.4) | ||
|
||
# Calculate eligible missed trips for discount | ||
eligible_missed_trips = max(0, missed_trips - discount_threshold) | ||
discount_per_trip = 0.15 * 350 # 15% of R350 | ||
total_discount = eligible_missed_trips * discount_per_trip | ||
|
||
# Calculate the final fare | ||
final_fare = 350 - total_discount | ||
final_fare = max(final_fare, 0) # Ensure fare is not negative | ||
|
||
print(f"Final fare calculated: R{final_fare}") | ||
|
||
body = { | ||
"passenger_id": passenger_id, | ||
"total_trips": total_trips, | ||
"missed_trips": missed_trips, | ||
"discount_threshold": discount_threshold, | ||
"eligible_missed_trips": eligible_missed_trips, | ||
"total_discount": total_discount, | ||
"final_fare": final_fare | ||
} | ||
|
||
return { | ||
'statusCode': statusCode, | ||
'body': json.dumps(body), | ||
'headers': headers | ||
} | ||
|
||
except ValueError as ve: | ||
print(f"Value error occurred: {ve}") | ||
return { | ||
'statusCode': 200, | ||
'body': json.dumps('Process completed successfully') | ||
'statusCode': 400, | ||
'body': json.dumps(str(ve)), | ||
'headers': headers | ||
} | ||
|
||
except ClientError as ce: | ||
print(f"Client error occurred: {ce}") | ||
return { | ||
'statusCode': 500, | ||
'body': json.dumps("Error accessing DynamoDB"), | ||
'headers': headers | ||
} | ||
|
||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
return { | ||
'statusCode': 500, | ||
'body': json.dumps('Error occurred during processing') | ||
'body': json.dumps('Error occurred during processing'), | ||
'headers': headers | ||
} |