Adding more shit

This commit is contained in:
James Pattinson
2025-11-10 15:42:09 +00:00
parent f1c4ff19d6
commit 43b13ef52d
10 changed files with 682 additions and 21 deletions

View File

@@ -4,11 +4,12 @@ from typing import List
from datetime import datetime
from ...core.database import get_db
from ...models.models import Payment, PaymentStatus, User, Membership
from ...models.models import Payment, PaymentStatus, User, Membership, MembershipStatus
from ...schemas import (
PaymentCreate, PaymentUpdate, PaymentResponse, MessageResponse
)
from ...api.dependencies import get_current_active_user, get_admin_user
from ...services.email_service import email_service
router = APIRouter()
@@ -114,6 +115,31 @@ async def update_payment(
db.commit()
db.refresh(payment)
# If payment was just marked as completed and has an associated membership,
# activate the membership and send activation email
if (update_data.get("status") == PaymentStatus.COMPLETED and
payment.membership_id and
payment.membership.status == MembershipStatus.PENDING):
# Activate the membership
payment.membership.status = MembershipStatus.ACTIVE
db.commit()
# Send activation email (non-blocking)
try:
await email_service.send_membership_activation_email(
to_email=payment.membership.user.email,
first_name=payment.membership.user.first_name,
membership_tier=payment.membership.tier.name,
annual_fee=payment.membership.tier.annual_fee,
payment_amount=payment.amount,
payment_method=payment.payment_method.value,
renewal_date=payment.membership.end_date.strftime("%d %B %Y")
)
except Exception as e:
# Log error but don't fail the payment update
print(f"Failed to send membership activation email: {e}")
return payment
@@ -152,6 +178,7 @@ async def record_manual_payment(
)
# Verify membership if provided
membership = None
if payment_data.membership_id:
membership = db.query(Membership).filter(
Membership.id == payment_data.membership_id,
@@ -178,4 +205,24 @@ async def record_manual_payment(
db.commit()
db.refresh(payment)
# If payment has an associated membership that's pending, activate it and send email
if membership and membership.status == MembershipStatus.PENDING:
membership.status = MembershipStatus.ACTIVE
db.commit()
# Send activation email (non-blocking)
try:
await email_service.send_membership_activation_email(
to_email=membership.user.email,
first_name=membership.user.first_name,
membership_tier=membership.tier.name,
annual_fee=membership.tier.annual_fee,
payment_amount=payment.amount,
payment_method=payment.payment_method.value,
renewal_date=membership.end_date.strftime("%d %B %Y")
)
except Exception as e:
# Log error but don't fail the payment creation
print(f"Failed to send membership activation email: {e}")
return payment