-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: count query of sold tickets #6693
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| from datetime import datetime | ||
| import datetime | ||
|
|
||
| from flask import Blueprint, request, jsonify, abort, make_response | ||
| from flask_jwt_extended import current_user | ||
| from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship | ||
| from flask_rest_jsonapi.exceptions import ObjectNotFound | ||
| from sqlalchemy.orm.exc import NoResultFound | ||
| from sqlalchemy import or_, and_ | ||
|
|
||
| from app.api.bootstrap import api | ||
| from app.api.helpers.db import safe_query, get_count | ||
|
|
@@ -25,9 +26,27 @@ | |
| from app.models.ticket_holder import TicketHolder | ||
| from app.models.user import User | ||
|
|
||
| from app.settings import get_settings | ||
|
|
||
| attendee_misc_routes = Blueprint('attendee_misc', __name__, url_prefix='/v1') | ||
|
|
||
|
|
||
| def get_sold_and_reserved_tickets_count(event_id): | ||
| order_expiry_time = get_settings()['order_expiry_time'] | ||
| return db.session.query(TicketHolder.id).join(Order).filter(TicketHolder.order_id == Order.id) \ | ||
| .filter(Order.event_id == int(event_id), | ||
| Order.deleted_at.is_(None), | ||
| or_(Order.status == 'placed', | ||
| Order.status == 'completed', | ||
| and_(Order.status == 'initializing', | ||
| Order.created_at + datetime.timedelta( | ||
| minutes=order_expiry_time) > datetime.datetime.utcnow()), | ||
| and_(Order.status == 'pending', | ||
| Order.created_at + datetime.timedelta( | ||
| minutes=30 + order_expiry_time) > (datetime.datetime.utcnow())) | ||
| )).count() | ||
|
|
||
|
|
||
| class AttendeeListPost(ResourceList): | ||
| """ | ||
| List and create Attendees through direct URL | ||
|
|
@@ -56,8 +75,7 @@ def before_post(self, args, kwargs, data): | |
| "Ticket belongs to a different Event" | ||
| ) | ||
| # Check if the ticket is already sold out or not. | ||
| if get_count(db.session.query(TicketHolder.id). | ||
| filter_by(ticket_id=int(data['ticket']), deleted_at=None)) >= ticket.quantity: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See what was being counted |
||
| if get_sold_and_reserved_tickets_count(ticket.event_id) >= ticket.quantity: | ||
| raise ConflictException( | ||
| {'pointer': '/data/attributes/ticket_id'}, | ||
| "Ticket already sold out" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,7 @@ def __init__(self, | |
| transaction_id=None, | ||
| paid_via=None, | ||
| is_billing_enabled=False, | ||
| created_at=datetime.datetime.now(datetime.timezone.utc), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the problem. created_at is static - set to the starting time of the server #6703
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making the PR |
||
| user_id=None, | ||
| discount_code_id=None, | ||
| event_id=None, | ||
|
|
@@ -114,7 +115,7 @@ def __init__(self, | |
| self.transaction_id = transaction_id | ||
| self.paid_via = paid_via | ||
| self.is_billing_enabled = is_billing_enabled | ||
| self.created_at = datetime.datetime.now(datetime.timezone.utc) | ||
| self.created_at = created_at | ||
| self.discount_code_id = discount_code_id | ||
| self.status = status | ||
| self.payment_mode = payment_mode | ||
|
|
||
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.
@iamareebjamal what about pending orders then? This condition will fail to count them.
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.
How?
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.
For pending order we give a window for 30 mins to complete payment.
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.
So additional 30 minutes is granted to attendee inspite of order_expiry_time,which is not taken in account here.