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 script to create customer account for customer placed order before #277

Merged
merged 3 commits into from
Aug 6, 2024

Conversation

lcduong
Copy link
Contributor

@lcduong lcduong commented Aug 6, 2024

This PR is partly of this issue fossasia/eventyay-talk#166

Implement a script to create customer account for customer who placed order before.
By execute this script create_customer_account: python manage.py create_customer_account
then input organizer and event short name.

  1. It will scan all the order of event.
  2. detect order with email not have customer account
  3. create customer account and link to that order
  4. for order already have account, link that customer account to order as well

Summary by Sourcery

Add a script to create customer accounts for orders that do not have an associated customer account. The script scans all orders for a specified event and links existing customer accounts or creates new ones if necessary.

New Features:

  • Introduce a management command to create customer accounts for orders without associated customer accounts.

Copy link

sourcery-ai bot commented Aug 6, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new management command script to create customer accounts for orders that do not have an associated customer account. The script takes organizer and event slugs as input, fetches the relevant orders, checks for existing customer accounts based on order emails, creates new customer accounts if necessary, and links the customer accounts to the orders.

File-Level Changes

Files Changes
src/pretix/base/management/commands/create_customer_account.py Added a new management command script to create customer accounts for orders without an associated customer account, including argument parsing, error handling, and logic to create and link customer accounts.

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 database transactions to ensure atomicity of operations. This will prevent partial updates if the script fails midway.
  • For better performance with large datasets, consider implementing batch processing for customer creation and order updates.
Here's what I looked at during the review
  • 🟡 General issues: 4 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.



class Command(BaseCommand):
help = 'Create SocialApp entries for Eventyay-ticket Provider'
Copy link

Choose a reason for hiding this comment

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

suggestion: Update the command's help text to accurately describe its functionality

The current help text doesn't match the command's actual functionality. Consider updating it to something like 'Create customer accounts for existing orders in an event'.

Suggested change
help = 'Create SocialApp entries for Eventyay-ticket Provider'
help = 'Create customer accounts for existing orders in an event'

event = Event.objects.get(slug=event_slug, organizer=organizer)
except Organizer.DoesNotExist:
self.stderr.write(self.style.ERROR('Organizer not found.'))
sys.exit(1)
Copy link

Choose a reason for hiding this comment

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

suggestion: Replace sys.exit(1) with raising CommandError for better error handling

Using sys.exit(1) is not the recommended way to handle errors in Django management commands. Consider raising CommandError instead, which allows for better error reporting and doesn't abruptly terminate the process.

from django.core.management.base import CommandError

# ... (earlier in the code)

except Organizer.DoesNotExist:
    raise CommandError('Organizer not found.')
except Event.DoesNotExist:
    raise CommandError('Event not found.')

if not customer:
name_parts_data = {
"_scheme": "full",
"full_name":order.email.split("@")[0]
Copy link

Choose a reason for hiding this comment

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

suggestion: Reconsider using email local part as full name

Using the email's local part as the full name might lead to inappropriate or nonsensical names. Consider using a placeholder like 'Unknown' or leaving it blank, allowing users to update it later.

Suggested change
"full_name":order.email.split("@")[0]
"full_name": "Unknown Customer"

with scopes_disabled():
orders = Order.objects.filter(event=event)
# Get all orders email and check if they have a customer account or not
for order in orders:
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 database queries for better performance

For events with many orders, querying the database for each order could be slow. Consider optimizing by first fetching all unique emails from orders, then bulk creating customers for emails without accounts.

Suggested change
for order in orders:
unique_emails = orders.values_list('email', flat=True).distinct()
existing_customers = set(Customer.objects.filter(email__in=unique_emails).values_list('email', flat=True))
new_customers = [Customer(email=email) for email in unique_emails if email and email not in existing_customers]
Customer.objects.bulk_create(new_customers)

Copy link
Member

@norbusan norbusan left a comment

Choose a reason for hiding this comment

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

Looks fine to me, although the suggestions by sourcery-ai also sound reasonable

@mariobehling mariobehling merged commit a2decff 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
… before (fossasia#277)

* implement script to create customer account for customer placed order before
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
… before (fossasia#277)

* implement script to create customer account for customer placed order before
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
… before (fossasia#277)

* implement script to create customer account for customer placed order before
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
… before (fossasia#277)

* implement script to create customer account for customer placed order before
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
… before (fossasia#277)

* implement script to create customer account for customer placed order before
lcduong added a commit to lcduong/eventyay-tickets that referenced this pull request Aug 15, 2024
… before (fossasia#277)

* implement script to create customer account for customer placed order before
odkhang pushed a commit that referenced this pull request Sep 24, 2024
… before (#277)

* implement script to create customer account for customer placed order before
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.

3 participants