Skip to content

Commit

Permalink
feature-5865 Able to book more discounted or access restricted ticket…
Browse files Browse the repository at this point in the history
…s than allowed (#9041)

* fix issue user request check in not admin

* feature-5865: Able to book more discounted or access restricted tickets than allowed

* feature-5865: Able to book more discounted or access restricted tickets than allowed

* fix ut

* fix ut

* fix ut

* fix ut

* fix ut

* feature-5865:
 Able to book more discounted or access restricted tickets than allowed

* feature-5865:
 Able to book more discounted or access restricted tickets than allowed

* feature-5865:
 Able to book more discounted or access restricted tickets than allowed

* fix complex function

* fix complex function

* fix complex function

* fix complex function

* add foreign key name

* fix complex code

* fix complex code

* fix complex code

* fix complex code

* fix complex code

* feature-5865: Merge code development

* feature-5865: Able to book more discounted or access restricted tickets than allowed

* fea-5865: update migration

---------

Co-authored-by: Hieu Lam - TMA <135117837+lthanhhieu@users.noreply.github.com>
Co-authored-by: lthanhhieu <lthanhhieu@tma.com.vn>
  • Loading branch information
3 people committed Sep 12, 2023
1 parent 6a2eceb commit 4d0895a
Show file tree
Hide file tree
Showing 15 changed files with 379 additions and 102 deletions.
81 changes: 75 additions & 6 deletions app/api/custom/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy.orm.exc import NoResultFound

from app.api.custom.schema.order_amount import OrderAmountInputSchema
from app.api.helpers.db import safe_query, save_to_db
from app.api.helpers.db import safe_query, safe_query_by_id, save_to_db
from app.api.helpers.errors import ForbiddenError, NotFoundError, UnprocessableEntityError
from app.api.helpers.mail import send_email_to_attendees
from app.api.helpers.order import (
Expand All @@ -21,6 +21,8 @@
from app.api.schema.orders import OrderSchema
from app.extensions.limiter import limiter
from app.models import db
from app.models.access_code import AccessCode
from app.models.discount_code import DiscountCode
from app.models.order import Order, OrderTicket
from app.models.ticket import Ticket
from app.models.ticket_holder import TicketHolder
Expand Down Expand Up @@ -91,7 +93,11 @@ def calculate_amount():
data, errors = OrderAmountInputSchema().load(request.get_json())
if errors:
return make_response(jsonify(errors), 422)
return jsonify(calculate_order_amount(data['tickets'], data.get('discount_code')))
return jsonify(
calculate_order_amount(
data['tickets'], data.get('discount_verify'), data.get('discount_code')
)
)


@order_blueprint.route('/create-order', methods=['POST'])
Expand All @@ -102,7 +108,9 @@ def create_order():
return make_response(jsonify(errors), 422)

tickets_dict = data['tickets']
order_amount = calculate_order_amount(tickets_dict, data.get('discount_code'))
order_amount = calculate_order_amount(
tickets_dict, data.get('discount_verify'), data.get('discount_code')
)
ticket_ids = {ticket['id'] for ticket in tickets_dict}
ticket_map = {int(ticket['id']): ticket for ticket in tickets_dict}
tickets = (
Expand All @@ -116,14 +124,75 @@ def create_order():
)

event = tickets[0].event

discount_code = None
access_code = None
discount_threshold = 0
access_threshold = 0
current_access_usage_count = 0
if data.get('discount_code') and (
isinstance(data.get('discount_code'), int)
or (
isinstance(data.get('discount_code'), str)
and data.get('discount_code').isdigit()
)
):
# Discount Code ID is passed
discount_code = safe_query_by_id(DiscountCode, data.get('discount_code'))
if discount_code is not None:
current_discount_usage_count = discount_code.confirmed_attendees_count
discount_threshold = (
discount_code.tickets_number - current_discount_usage_count
)
if data.get('access_code') and (
isinstance(data.get('access_code'), int)
or (
isinstance(data.get('access_code'), str) and data.get('access_code').isdigit()
)
):
# Access Code check
access_code = safe_query_by_id(AccessCode, data.get('access_code'))
if access_code is not None:
current_access_usage_count = access_code.confirmed_attendees_count
access_threshold = access_code.tickets_number - current_access_usage_count
try:
attendees = []
for ticket in tickets:
for _ in range(ticket_map[ticket.id]['quantity']):
ticket.raise_if_unavailable()
is_discount_applied = False
is_access_code_applied = False
if (
discount_code
and (ticket in discount_code.tickets)
and (discount_threshold > 0)
):
is_discount_applied = True
discount_threshold -= 1

if (
access_code
and (ticket in access_code.tickets)
and access_threshold >= 0
):
if access_threshold == 0:
raise UnprocessableEntityError(
{'source': 'access_code'},
f"Access code for ticket {ticket.name} is exhausted, "
f"only {access_code.tickets_number - current_access_usage_count} "
"quantity is available",
)
is_access_code_applied = True
access_threshold -= 1

attendees.append(
TicketHolder(firstname='', lastname='', ticket=ticket, event=event)
TicketHolder(
firstname='',
lastname='',
ticket=ticket,
event=event,
is_discount_applied=is_discount_applied,
is_access_code_applied=is_access_code_applied,
)
)
db.session.commit()
except Exception as e:
Expand All @@ -142,6 +211,7 @@ def create_order():
amount=order_amount['total'],
event=event,
discount_code_id=data.get('discount_code'),
access_code_id=data.get('access_code'),
ticket_holders=attendees,
)
db.session.commit()
Expand Down Expand Up @@ -184,7 +254,6 @@ def ticket_attendee_pdf(attendee_id):

@order_blueprint.route('/<string:order_identifier>/verify', methods=['POST'])
def verify_order_payment(order_identifier):

order = Order.query.filter_by(identifier=order_identifier).first()

if order.payment_mode == 'stripe':
Expand Down
5 changes: 5 additions & 0 deletions app/api/custom/schema/order_amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
class TicketSchema(Schema):
id = fields.Integer(required=True)
quantity = fields.Integer(default=1)
quantity_discount = fields.Integer(allow_none=True)
price = fields.Float(allow_none=True)


class OrderAmountInputSchema(Schema):
tickets = fields.Nested(TicketSchema, many=True)
discount_code = fields.Integer(load_from='discount-code')
access_code = fields.Integer(load_from='access-code')
amount = fields.Float(allow_none=True)
discount_verify = fields.Boolean(
required=False, default=True, load_from='discount-verify'
)
Loading

0 comments on commit 4d0895a

Please sign in to comment.