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: Discount code quantity calculation #6878

Merged
merged 4 commits into from
Mar 7, 2020
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
20 changes: 9 additions & 11 deletions app/api/helpers/ticketing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ def get_order_expiry():
def match_discount_quantity(discount_code, tickets=None, ticket_holders=None):
qty = 0
ticket_ids = [ticket.id for ticket in discount_code.tickets]
old_holders = get_count(
TicketHolder.query.filter(
TicketHolder.ticket_id.in_(ticket_ids) # pytype: disable=attribute-error
)
.join(Order)
.filter(Order.status.in_(['completed', 'placed']))
)
old_holders = discount_code.confirmed_attendees_count
if ticket_holders:
for holder in ticket_holders:
ticket_holder = TicketHolder.query.filter_by(id=holder).one()
if ticket_holder.ticket.id in ticket_ids:
qty += 1
# pytype: disable=attribute-error
qty = get_count(
TicketHolder.query.filter(
TicketHolder.id.in_(ticket_holders),
TicketHolder.ticket_id.in_(ticket_ids),
)
)
# pytype: enable=attribute-error
elif tickets:
for ticket in tickets:
if int(ticket['id']) in ticket_ids:
Expand Down
2 changes: 1 addition & 1 deletion app/factories/discount_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Meta:
type = "amount"
is_active = True
tickets_number = 30
min_quantity = 10
min_quantity = 1
max_quantity = 20
valid_from = common.date_
valid_till = common.dateEnd_
Expand Down
19 changes: 19 additions & 0 deletions app/models/discount_code.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from datetime import datetime
from sqlalchemy.sql import func
from app.api.helpers.db import get_count
from app.models import db
from app.models.base import SoftDeletionModel
from app.models.order import Order
from app.models.ticket_holder import TicketHolder

TICKET = 'ticket'
EVENT = 'event'
Expand Down Expand Up @@ -75,6 +78,22 @@ def __repr__(self):
def __str__(self):
return self.__repr__()

def get_confirmed_attendees_query(self):
return (
TicketHolder.query.filter_by(deleted_at=None)
.join(Order)
.filter_by(discount_code_id=self.id, deleted_at=None)
.filter(Order.status.in_(['completed', 'placed']))
)

@property
def confirmed_attendees(self):
return self.get_confirmed_attendees_query().all()

@property
def confirmed_attendees_count(self):
return get_count(self.get_confirmed_attendees_query())

@property
def serialize(self):
"""Return object data in easily serializable format"""
Expand Down
51 changes: 51 additions & 0 deletions tests/all/integration/api/helpers/test_ticketing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from app.api.helpers.ticketing import TicketingManager
from app.factories.attendee import AttendeeFactoryBase
from app.factories.order import OrderFactory
from app.factories.ticket import TicketFactory
from app.factories.discount_code import DiscountCodeTicketFactory
from app.models import db

from tests.all.integration.utils import OpenEventTestCase


class TestTicketing(OpenEventTestCase):
def test_match_discount_quantity(self):
"""Method to test the quantity calculation of discount code"""

with self.app.test_request_context():
ticket = TicketFactory()
discount_code = DiscountCodeTicketFactory(tickets_number=5)
discount_code.tickets.append(ticket)

order_without_discount = OrderFactory(status='completed')

Choose a reason for hiding this comment

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

Black would make changes.


db.session.commit()

# Attendees associated with the order without discount code should not be counted

Choose a reason for hiding this comment

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

line too long (93 > 90 characters)

AttendeeFactoryBase.create_batch(
10, order_id=order_without_discount.id, ticket_id=ticket.id
)

self.assertTrue(
TicketingManager.match_discount_quantity(
discount_code, ticket_holders=[1]
)
)

order_with_discount = OrderFactory(
status='completed', discount_code_id=discount_code.id
)

db.session.commit()

# Attendees associated with the order with discount code should be counted
AttendeeFactoryBase.create_batch(
5, order_id=order_with_discount.id, ticket_id=ticket.id
)

self.assertFalse(
TicketingManager.match_discount_quantity(
discount_code, ticket_holders=[1]
)
)
self.assertEqual(5, discount_code.confirmed_attendees_count)