-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
fix: prevent deletion of ticket that has sales #7392
Conversation
Codecov Report
@@ Coverage Diff @@
## development #7392 +/- ##
============================================
Coverage 64.74% 64.74%
============================================
Files 262 265 +3
Lines 13235 13319 +84
============================================
+ Hits 8569 8624 +55
- Misses 4666 4695 +29
Continue to review full report at Codecov.
|
app/api/tickets.py
Outdated
@@ -241,6 +241,12 @@ def before_update_object(self, ticket, data, view_kwargs): | |||
{'event_id': ticket.event.id}, | |||
"Event having paid ticket must have a payment method", | |||
) | |||
else: | |||
if (data.get('deleted_at') and ticket.has_order_tickets): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should only check completed, placed, pending and initializing tickets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expired, cancelled orders may prevent deletion this way which will create issues like this fossasia/open-event-frontend#5430
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can I change here something to implement above condition.
@property
def has_order_tickets(self):
"""Returns True if ticket has already placed orders.
Else False.
"""
from app.api.helpers.db import get_count
orders = Order.id.in_(
OrderTicket.query.with_entities(OrderTicket.order_id)
.filter_by(ticket_id=self.id)
.all()
)
count = get_count(Order.query.filter(orders).filter(Order.status != 'deleted'))
# Count is zero if no completed orders are present
return bool(count > 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a new function as this will not just check if the ticket has orders. Name it has_current orders
and use
Order.query.join(TicketHolder)
.filter(TicketHolder.ticket_id == self.id,
or_(Order.status == 'completed',
Order.status == 'placed',
Order.status == 'pending',
Order.status == 'initializing'))
Don't fetch all the orders like done above. Neither use get_count
. Use exists
query like here
open-event-server/app/models/video_stream.py
Lines 41 to 46 in d3582cb
return db.session.query( | |
TicketHolder.query.filter_by(event_id=event_id, user=user) | |
.join(Order) | |
.filter(or_(Order.status == 'completed', Order.status == 'placed')) | |
.exists() | |
).scalar() |
@maze-runnar Status? |
@iamareebjamal it is giving this error on deletion of ticket -
File "/home/saurabh/Desktop/extra/open-event-server/app/api/tickets.py", line 244, in before_update_object |
app/models/ticket.py
Outdated
or_(Order.status == 'completed', | ||
Order.status == 'placed', | ||
Order.status == 'pending', | ||
Order.status == 'initializing')).exists() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it has something missing from the query I wrote
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is db.session.query(...).exists()
@maze-runnar That's because you didn't write what I commented. You can't just skip things randomly and expect everything to work |
app/api/tickets.py
Outdated
"Can't delete a ticket that has sales", | ||
) | ||
|
||
if (ticket.has_current_orders and data.get('deleted_at')): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deleted_at should be checked first. There is no need to make an SQL query everytime there is a change in tickets. It only needs to be checked if ticket is being deleted
Fixes fossasia/open-event-frontend#5195
Checklist
development
branch.