Skip to content

Commit

Permalink
fix: check workshop/presentation capacity on payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Adibov authored and azare242 committed Nov 24, 2023
1 parent 821149a commit ed6fa30
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
19 changes: 19 additions & 0 deletions backend/backend_api/migrations/0049_alter_payment_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.4 on 2023-11-24 19:06

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('backend_api', '0048_presentation_capacity'),
]

operations = [
migrations.AlterField(
model_name='payment',
name='id',
field=models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False),
),
]
19 changes: 16 additions & 3 deletions backend/backend_api/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import uuid
from urllib.parse import urljoin

from django.contrib.auth.models import AbstractBaseUser
Expand All @@ -9,12 +10,15 @@
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import ValidationError
from rest_framework import status

from aaiss_backend import settings
from aaiss_backend.settings import BASE_URL
from backend_api import validators
from backend_api.email import MailerThread
from utils.random import create_random_string
from utils.renderers import new_detailed_response

SMALL_MAX_LENGTH = 255
BIG_MAX_LENGTH = 65535
Expand Down Expand Up @@ -132,7 +136,7 @@ def remaining_capacity(self) -> int:
@property
def participants(self):
participants = []
for participant in WorkshopRegistration.objects.filter(presentation=self,
for participant in WorkshopRegistration.objects.filter(workshop=self,
status=
WorkshopRegistration.StatusChoices.PURCHASED):
participants += participant.user
Expand Down Expand Up @@ -310,7 +314,7 @@ class PaymentStatus(models.IntegerChoices):
PAYMENT_CONFIRMED = 1, _('Payment confirmed')
PAYMENT_REJECTED = 2, _('Payment rejected')

id = models.UUIDField(primary_key=True)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
amount = models.PositiveIntegerField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
workshops = models.ManyToManyField(Workshop, blank=True)
Expand Down Expand Up @@ -354,6 +358,10 @@ def create_payment_for_user(user: User):
workshop_registration = workshop.workshopregistration_set.get(workshop_id=workshop.id)
if workshop_registration.status != WorkshopRegistration.StatusChoices.AWAITING_PAYMENT:
continue
if workshop.remaining_capacity <= 0:
raise ValidationError(
new_detailed_response(status.HTTP_400_BAD_REQUEST,
f"Workshop {workshop.id} is full"))
total_cost += workshop.cost
workshops.append(workshop)
except ObjectDoesNotExist:
Expand All @@ -364,12 +372,17 @@ def create_payment_for_user(user: User):
presentation_id=presentation.id)
if presentation_participation.status != PresentationParticipation.StatusChoices.AWAITING_PAYMENT:
continue
if presentation.remaining_capacity <= 0:
raise ValidationError(
new_detailed_response(status.HTTP_400_BAD_REQUEST,
f"Presentation {presentation.id} is full"))
total_cost += presentation.cost
presentations.append(presentation)
except ObjectDoesNotExist:
raise ValueError(f"User {user} is registered for presentation {presentation} but has no registration")
if len(workshops) == 0 and len(presentations) == 0:
return None
raise ValidationError(
new_detailed_response(status.HTTP_400_BAD_REQUEST, f"User {user} has no unpaid registrations"))
payment = Payment.objects.create(user=user, amount=total_cost, year=datetime.date.today().year,
date=datetime.datetime.now())
payment.workshops.set(workshops)
Expand Down
6 changes: 1 addition & 5 deletions backend/backend_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,7 @@ def payment(self, request):
status.HTTP_400_BAD_REQUEST, "User not found"))

payment = Payment.create_payment_for_user(user)
if payment is None:
return Response(new_detailed_response(
status.HTTP_400_BAD_REQUEST, "User has no unpaid item"))

response = ZIFYRequest().create_payment(payment.pk, payment.amount, user.name, user.phone_number,
response = ZIFYRequest().create_payment(str(payment.pk), payment.amount, user.name, user.phone_number,
user.account.email)
if response['status'] == ZIFY_STATUS_OK:
payment.track_id = response['data']['order']
Expand Down

0 comments on commit ed6fa30

Please sign in to comment.