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

Add ability to make tickets not buyable + disable paid tickets for beta #685

Merged
merged 3 commits into from
Apr 28, 2024
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
18 changes: 18 additions & 0 deletions backend/clubs/migrations/0108_ticket_buyable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-04-28 17:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("clubs", "0107_ticket_attended"),
]

operations = [
migrations.AddField(
model_name="ticket",
name="buyable",
field=models.BooleanField(default=True),
),
]
2 changes: 2 additions & 0 deletions backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,8 @@ class Ticket(models.Model):
group_size = models.PositiveIntegerField(null=True, blank=True)
transferable = models.BooleanField(default=True)
attended = models.BooleanField(default=False)
# TODO: change to enum between All, Club, None
buyable = models.BooleanField(default=True)
objects = TicketManager()

def get_qr(self):
Expand Down
17 changes: 14 additions & 3 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,11 @@ def add_to_cart(self, request, *args, **kwargs):
# We don't need a lock since we aren't changing the holder or owner
tickets = (
Ticket.objects.filter(
event=event, type=type, owner__isnull=True, holder__isnull=True
event=event,
type=type,
owner__isnull=True,
holder__isnull=True,
buyable=True,
)
.prefetch_related("carts")
.exclude(carts__owner=self.request.user)
Expand Down Expand Up @@ -2605,7 +2609,7 @@ def tickets(self, request, *args, **kwargs):
.order_by("type")
)
available = (
tickets.filter(owner__isnull=True, holder__isnull=True)
tickets.filter(owner__isnull=True, holder__isnull=True, buyable=True)
.values("type")
.annotate(price=Max("price"))
.annotate(count=Count("type"))
Expand Down Expand Up @@ -2645,6 +2649,9 @@ def create_tickets(self, request, *args, **kwargs):
required: false
transferable:
type: boolean
buyable:
type: boolean
required: false
order_limit:
type: int
required: false
Expand Down Expand Up @@ -2759,6 +2766,7 @@ def create_tickets(self, request, *args, **kwargs):
group_discount=item.get("group_discount", 0),
group_size=item.get("group_size", None),
transferable=item.get("transferable", True),
buyable=item.get("buyable", True),
)
for item in quantities
for _ in range(item["count"])
Expand Down Expand Up @@ -5131,6 +5139,7 @@ def cart(self, request, *args, **kwargs):
available_tickets = Ticket.objects.filter(
event=ticket_class["event"],
type=ticket_class["type"],
buyable=True, # should not be triggered as buyable is by ticket class
owner__isnull=True,
holder__isnull=True,
).exclude(id__in=tickets_in_cart)[: ticket_class["count"]]
Expand Down Expand Up @@ -5221,7 +5230,9 @@ def initiate_checkout(self, request, *args, **kwargs):
# skip_locked is important here because if any of the tickets in cart
# are locked, we shouldn't block.
tickets = cart.tickets.select_for_update(skip_locked=True).filter(
Q(holder__isnull=True) | Q(holder=self.request.user), owner__isnull=True
Q(holder__isnull=True) | Q(holder=self.request.user),
owner__isnull=True,
buyable=True,
)

# Assert that the filter succeeded in freezing all the tickets for checkout
Expand Down
47 changes: 32 additions & 15 deletions frontend/components/ClubEditPage/TicketsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { ReactElement, useState } from 'react'
import { toast, TypeOptions } from 'react-toastify'
import styled from 'styled-components'

import { TICKETING_PAYMENT_ENABLED } from '~/utils/branding'

import { Icon, Line, Text, Title } from '../../components/common'
import {
ALLBIRDS_GRAY,
Expand Down Expand Up @@ -113,18 +115,20 @@ const TicketItem: React.FC<TicketItemProps> = ({
onChange?.({ ...ticket, count })
}}
/>
<Input
type="number"
className="input"
value={ticket.price ?? ''}
placeholder="Ticket Price"
onChange={(e) => {
const price = e.target.value
setTicket({ ...ticket, price })
onChange?.({ ...ticket, price })
}}
/>

{ticket.buyable && (
<Input
type="number"
className="input"
value={ticket.price ?? ''}
placeholder="Ticket Price"
disabled={!TICKETING_PAYMENT_ENABLED}
onChange={(e) => {
const price = e.target.value
setTicket({ ...ticket, price })
onChange?.({ ...ticket, price })
}}
/>
)}
<button
className="button is-danger"
disabled={!deletable}
Expand All @@ -147,6 +151,12 @@ const TicketItem: React.FC<TicketItemProps> = ({
justifyContent: 'center',
}}
>
<button
className="button is-info is-small mr-2"
onClick={() => setTicket({ ...ticket, buyable: !ticket.buyable })}
>
{ticket.buyable ? 'Disable Buying' : 'Enable Buying'}
</button>
{openGroupDiscount ? (
<>
<div style={{ maxWidth: '75px' }}>
Expand Down Expand Up @@ -205,6 +215,7 @@ type Ticket = {
price: string | null // Free if null
groupDiscount: string | null // If null, no group discount
groupNumber: string | null // If null, no group discount
buyable: boolean
}

const TicketsModal = ({
Expand All @@ -222,9 +233,10 @@ const TicketsModal = ({
{
name: 'Regular Ticket',
count: null,
price: null,
price: '0.00',
groupDiscount: null,
groupNumber: null,
buyable: true,
},
])

Expand All @@ -235,9 +247,10 @@ const TicketsModal = ({
{
name: '',
count: null,
price: null,
price: '0.00',
groupDiscount: null,
groupNumber: null,
buyable: true,
},
])
}
Expand All @@ -258,6 +271,7 @@ const TicketsModal = ({
groupNumber: usingGroupPricing
? parseFloat(ticket.groupNumber!)
: null,
buyable: ticket.buyable,
}
})
doApiRequest(`/events/${id}/tickets/?format=json`, {
Expand Down Expand Up @@ -300,7 +314,10 @@ const TicketsModal = ({
/>
<ModalBody>
<Title>{name}</Title>
<Text>Create new tickets for this event.</Text>
<Text>
Create new tickets for this event. For our alpha, only free tickets
will be supported for now: stay tuned for payments integration!
</Text>
<Line />
<SectionContainer>
<h1>Tickets</h1>
Expand Down
43 changes: 28 additions & 15 deletions frontend/components/Tickets/CartTickets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,23 @@ const combineTickets = (tickets: EventTicket[]): CountedEventTicket[] =>
),
)

const useCheckout = () => {
const useCheckout = (paid: boolean) => {
const [showModal, setShowModal] = useState(false)
const [captureContext, setCaptureContext] = useState<string>()
const navigate = useRouter()

const onModalClose = (force: boolean) => {
if (force) {
setShowModal(false)
return
}
const confirmed = confirm(
'Are you sure you want to exit the checkout process? You could lose items in your cart!',
)
if (confirmed) {
setShowModal(false)
}
}

const fetchToken = async () => {
const res = await doApiRequest(`/tickets/initiate_checkout/?format=json`, {
Expand All @@ -143,26 +157,24 @@ const useCheckout = () => {
toast.error(data.detail, {
style: { color: WHITE },
})
} else if (data.sold_free_tickets) {
onModalClose(true)
toast.success('Free tickets purchased successfully!')
setTimeout(() => {
navigate.push('/settings#Tickets')
}, 500)
}
return data.detail
}

return {
showModal,
onClose: (force: boolean) => {
if (force) {
setShowModal(false)
return
}
const confirmed = confirm(
'Are you sure you want to exit the checkout process? You could lose items in your cart!',
)
if (confirmed) {
setShowModal(false)
}
},
onClose: onModalClose,
checkout: async () => {
setShowModal(true)
if (paid) {
// heuristic on frontend to skip modal if checking out free tickets
setShowModal(true)
}
const token = await fetchToken()
setCaptureContext(token)
},
Expand All @@ -177,12 +189,13 @@ const CartTickets: React.FC<CartTicketsProps> = ({ tickets, soldOut }) => {
)
const [countedTickets, setCountedTickets] = useState<CountedEventTicket[]>([])

const atLeastOnePaid = tickets.some((ticket) => parseFloat(ticket.price) > 0)
const {
showModal,
onClose: onModalClose,
checkout,
captureContext,
} = useCheckout()
} = useCheckout(atLeastOnePaid)

useEffect(() => {
setCountedTickets(combineTickets(tickets))
Expand Down
1 change: 1 addition & 0 deletions frontend/utils/branding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ const sites = {
},
}

export const TICKETING_PAYMENT_ENABLED = false
export const SITE_ID = site
export const SITE_NAME = sites[site].SITE_NAME
export const SITE_SUBTITLE = sites[site].SITE_SUBTITLE
Expand Down
Loading