feat: implement reminder preferences and withdrawal (Stories 6.3, 6.2)

Implement Phase 3 participant self-management features:

Story 6.3 - Reminder Preferences:
- Add ReminderPreferenceForm to participant forms
- Add update_preferences route for preference updates
- Update dashboard template with reminder preference toggle
- Participants can enable/disable reminder emails at any time

Story 6.2 - Withdrawal from Exchange:
- Add can_withdraw utility function for state validation
- Create WithdrawalService to handle withdrawal process
- Add WithdrawForm with explicit confirmation requirement
- Add withdraw route with GET (confirmation) and POST (process)
- Add withdrawal confirmation email template
- Update dashboard to show withdraw link when allowed
- Withdrawal only allowed before registration closes
- Session cleared after withdrawal, user redirected to registration

All acceptance criteria met for both stories.
Test coverage: 90.02% (156 tests passing)
Linting and type checking: passed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-22 21:18:18 -07:00
co-authored by Claude Opus 4.5
parent 4fbb681e03
commit c2b3641d74
12 changed files with 725 additions and 2 deletions
+50
View File
@@ -174,3 +174,53 @@ class EmailService:
"""
return self.send_email(to=to, subject=subject, html_body=html_body)
def send_withdrawal_confirmation(self, participant: Any) -> Any:
"""Send withdrawal confirmation email to participant.
Args:
participant: The participant who withdrew
Returns:
Response from email service
"""
exchange = participant.exchange
subject = f"Withdrawal Confirmed - {exchange.name}"
html_body = f"""
<html>
<body>
<h2>Withdrawal Confirmed</h2>
<p>Hello {participant.name},</p>
<p>
This email confirms that you have withdrawn from the
Secret Santa exchange <strong>{exchange.name}</strong>.
</p>
<div style="background-color: #f8f9fa;
border-left: 4px solid #e74c3c;
padding: 15px; margin: 20px 0;">
<p style="margin: 0;"><strong>What happens now:</strong></p>
<ul style="margin: 10px 0;">
<li>You have been removed from the participant list</li>
<li>Your profile information has been archived</li>
<li>
You will not receive further emails about this exchange
</li>
</ul>
</div>
<p>
If you withdrew by mistake, you can re-register using a
different email address while registration is still open.
</p>
<hr style="border: none; border-top: 1px solid #ddd;
margin: 30px 0;">
<p style="font-size: 12px; color: #666;">
This is an automated message from Sneaky Klaus.
</p>
</body>
</html>
"""
return self.send_email(
to=participant.email, subject=subject, html_body=html_body
)
+73
View File
@@ -0,0 +1,73 @@
"""Participant withdrawal service."""
from datetime import UTC, datetime
from flask import current_app
from src.app import db
from src.models.participant import Participant
from src.services.email import EmailService
from src.utils.participant import can_withdraw
class WithdrawalError(Exception):
"""Raised when withdrawal operation fails."""
pass
def withdraw_participant(participant: Participant) -> None:
"""Withdraw a participant from their exchange.
This performs a soft delete by setting withdrawn_at timestamp.
Participant record is retained for audit trail.
Args:
participant: The participant to withdraw
Raises:
WithdrawalError: If withdrawal is not allowed
Side effects:
- Sets participant.withdrawn_at to current UTC time
- Commits database transaction
- Sends withdrawal confirmation email
"""
# Validate withdrawal is allowed
if not can_withdraw(participant):
if participant.withdrawn_at is not None:
raise WithdrawalError("You have already withdrawn from this exchange.")
exchange = participant.exchange
if exchange.state == "registration_closed":
raise WithdrawalError(
"Registration has closed. Please contact the admin to withdraw."
)
elif exchange.state in ["matched", "completed"]:
raise WithdrawalError(
"Matching has already occurred. Please contact the admin."
)
else:
raise WithdrawalError("Withdrawal is not allowed at this time.")
# Perform withdrawal
participant.withdrawn_at = datetime.now(UTC)
try:
db.session.commit()
current_app.logger.info(
f"Participant {participant.id} withdrawn from "
f"exchange {participant.exchange_id}"
)
except Exception as e:
db.session.rollback()
current_app.logger.error(f"Failed to withdraw participant: {e}")
raise WithdrawalError("Failed to process withdrawal. Please try again.") from e
# Send confirmation email
try:
email_service = EmailService()
email_service.send_withdrawal_confirmation(participant)
except Exception as e:
current_app.logger.error(f"Failed to send withdrawal email: {e}")
# Don't raise - withdrawal already committed