Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API to check if user paid for event or not #280

Merged
merged 1 commit into from
Aug 6, 2024

Conversation

lcduong
Copy link
Contributor

@lcduong lcduong commented Aug 6, 2024

As part of this issue fossasia/eventyay-talk#133
Implement an API for talk component to check if customer ordered ticket yet or not

Summary by Sourcery

Implement an API endpoint to verify if a customer has paid for a specific event by checking their orders.

New Features:

  • Introduce an API endpoint to check if a customer has paid for an event.

Copy link

sourcery-ai bot commented Aug 6, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new API endpoint to check if a customer has paid for an event. The main changes include the addition of a new view class CustomerOrderCheckView in src/pretix/api/views/event.py to handle the GET request, and the corresponding URL pattern in src/pretix/api/urls.py.

File-Level Changes

Files Changes
src/pretix/api/views/event.py
src/pretix/api/urls.py
Implemented a new API endpoint to check if a customer has paid for an event, including the view logic and URL routing.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lcduong - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using consistent response types throughout the view (either Response or JsonResponse) for better maintainability.
  • The order_list query using complex Q objects might impact performance for large datasets. Consider optimizing this query or adding appropriate database indexes.
  • Ensure proper authentication and authorization checks are in place to protect sensitive customer information.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

event = Event.objects.get(slug=kwargs["event"], organizer=organizer)
customer = Customer.objects.get(identifier=kwargs["customer_id"], organizer=organizer)

except Organizer.DoesNotExist:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Simplify error handling using a dictionary

Consider using a dictionary to map exception types to error messages. This can reduce code duplication and improve maintainability.

        exceptions = {
            Organizer.DoesNotExist: "Organizer not found.",
            Event.DoesNotExist: "Event not found.",
            Customer.DoesNotExist: "Customer not found.",
        }
        for exception, message in exceptions.items():
            try:
                raise exception
            except exception:
                return JsonResponse(status=404, data={"error": message})

return JsonResponse(status=404, data={"error": "Customer not found."})

# Get all orders of customer which belong to this event
order_list = (Order.objects.filter(Q(event=event)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Optimize and simplify the order query

The current query is complex and might be inefficient. Consider simplifying it and avoiding the use of iexact for email comparison, which could be slow for large datasets.

Suggested change
order_list = (Order.objects.filter(Q(event=event)
order_list = Order.objects.filter(
event=event,
customer__in=[customer, None],
email__in=[customer.email, '']
).select_related('event').order_by('-datetime')

if not order_list:
return JsonResponse(status=404, data={"error": "Customer has no orders for this event."})

for order in order_list:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Optimize paid order check by modifying the initial query

Instead of looping through all orders to check for paid status, consider adding a filter for paid orders in the initial database query.

Suggested change
for order in order_list:
if order_list.filter(status='p').exists():
return JsonResponse(status=200, data={"message": "Customer has paid orders."})

@mariobehling mariobehling merged commit a3c8aa4 into fossasia:development Aug 6, 2024
1 of 5 checks passed
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 14, 2024
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
lcduong added a commit to odkhang/eventyay-tickets that referenced this pull request Oct 4, 2024
odkhang pushed a commit to odkhang/eventyay-tickets that referenced this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants