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

fix: prevent deletion of ticket that has sales #7392

Merged
merged 12 commits into from
Nov 5, 2020
Merged

fix: prevent deletion of ticket that has sales #7392

merged 12 commits into from
Nov 5, 2020

Conversation

maze-runnar
Copy link
Contributor

Fixes fossasia/open-event-frontend#5195

Checklist

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

@codecov
Copy link

codecov bot commented Oct 30, 2020

Codecov Report

Merging #7392 into development will increase coverage by 0.00%.
The diff coverage is 57.14%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ
app/api/tickets.py 45.28% <33.33%> (-0.88%) ⬇️
app/models/ticket.py 78.08% <75.00%> (-0.18%) ⬇️
app/api/helpers/tasks.py 16.75% <0.00%> (-0.44%) ⬇️
app/models/event.py 78.63% <0.00%> (-0.11%) ⬇️
app/api/helpers/mail_recorder.py 70.00% <0.00%> (ø)
app/api/custom/calendars.py 50.00% <0.00%> (ø)
app/api/helpers/calendar/ical.py 97.29% <0.00%> (ø)
app/instance.py 88.04% <0.00%> (+0.26%) ⬆️
app/api/helpers/permissions.py 36.20% <0.00%> (+0.30%) ⬆️
app/api/event_invoices.py 63.54% <0.00%> (+0.38%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5459387...ae3cfbf. Read the comment docs.

@iamareebjamal iamareebjamal changed the title Fix: can't delete ticket that has sales fix: can't delete ticket that has sales Oct 30, 2020
@auto-label auto-label bot added the fix label Oct 30, 2020
@@ -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):
Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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)

Copy link
Member

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

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()

@iamareebjamal
Copy link
Member

@maze-runnar Status?

@maze-runnar
Copy link
Contributor Author

maze-runnar commented Nov 4, 2020

@iamareebjamal it is giving this error on deletion of ticket -

 if (data.get('deleted_at') and ticket.has_current_orders):
    raise TypeError("Boolean value of this clause is not defined")

File "/home/saurabh/Desktop/extra/open-event-server/app/api/tickets.py", line 244, in before_update_object
if (data.get('deleted_at') and ticket.has_current_orders):
File "/home/saurabh/Desktop/extra/open-event-server/.venv/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 535, in bool
raise TypeError("Boolean value of this clause is not defined")
TypeError: Boolean value of this clause is not defined

or_(Order.status == 'completed',
Order.status == 'placed',
Order.status == 'pending',
Order.status == 'initializing')).exists()
Copy link
Member

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

Copy link
Member

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()

@iamareebjamal
Copy link
Member

@maze-runnar That's because you didn't write what I commented. You can't just skip things randomly and expect everything to work

"Can't delete a ticket that has sales",
)

if (ticket.has_current_orders and data.get('deleted_at')):
Copy link
Member

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

iamareebjamal
iamareebjamal previously approved these changes Nov 5, 2020
@iamareebjamal iamareebjamal changed the title fix: can't delete ticket that has sales fix: prevent deletion of ticket that has sales Nov 5, 2020
@iamareebjamal iamareebjamal merged commit 87b58df into fossasia:development Nov 5, 2020
@maze-runnar maze-runnar deleted the patch-4 branch December 27, 2020 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tickets: Prevent deletion of tickets with sales
2 participants