Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/handlers/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from lib.types import StripeCheckout

db = Database(url=os.getenv("DATABASE_URL"))
db = Database(url=os.environ["DATABASE_URL"])
stripe.api_key = get_api_key()


Expand Down
2 changes: 1 addition & 1 deletion src/handlers/printful.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

load_dotenv()

db = Database(url=os.getenv("DATABASE_URL"))
db = Database(url=os.environ["DATABASE_URL"])
log = Logs(log_group=os.environ["LOG_GROUP"])
notify = Notify(topic_arn=os.environ["SNS_TOPIC_ARN"], phone=os.environ["NOTIFY_PHONE"])
queue = Queue(queue_url=os.environ["PRINTFUL_QUEUE_URL"])
Expand Down
57 changes: 57 additions & 0 deletions src/handlers/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import json
import os

from dotenv import load_dotenv

from lib.db import Database
from lib.errors import HttpError

load_dotenv()

db = Database(url=os.environ["DATABASE_URL"])


def handler(event, context):
"""
Wrap a user order query with error-handling exception logic.
"""
try:
return _handle(event)
except HttpError as e:
return {"statusCode": e.status, "body": json.dumps({"error": e.message})}


def _handle(event):
"""
Serve user order lookups given an order ID and email.
"""
path_params = event.get("pathParameters") or {}
query_params = event.get("queryStringParameters") or {}

order_id = path_params.get("id")
email = query_params.get("email")

if not order_id or not email:
raise HttpError(400, "missing required parameters")

try:
order_id = int(order_id)
except ValueError:
raise HttpError(400, "invalid order id")

order = db.get_order(order_id)

if order is None or order.email != email:
raise HttpError(404, "order not found or email is invalid")

tracking_urls = [s.tracking_url for s in order.shipments if s.tracking_url]

return {
"statusCode": 200,
"body": json.dumps(
{
"receipt_url": order.receipt_url,
"tracking_urls": tracking_urls,
}
),
}
6 changes: 6 additions & 0 deletions src/lib/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class HttpError(Exception):
def __init__(self, status: int, message: str):
self.status = status
self.message = message


class StripeException(Exception):
"""
This error type stands for an error processing a
Expand Down