|
| 1 | +from fastapi import APIRouter, Depends |
| 2 | +from typing import Annotated |
| 3 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 4 | + |
| 5 | +from app.auth.dependency import get_current_user |
| 6 | +from database.dependency import get_db |
| 7 | +from domain.user.entity.user import User |
| 8 | +from domain.membership.service.membership_service import MembershipService |
| 9 | +from app.utils.dto.success import ok |
| 10 | +from pydantic import BaseModel, Field |
| 11 | + |
| 12 | + |
| 13 | +router = APIRouter() |
| 14 | + |
| 15 | + |
| 16 | +class UpgradeMembershipRequest(BaseModel): |
| 17 | + """λ©€λ²μ μ
κ·Έλ μ΄λ μμ² DTO""" |
| 18 | + membership_tier: str = Field(..., description="λ©€λ²μ λ±κΈ (FREE, PREMIUM)") |
| 19 | + payment_amount: int = Field(default=0, description="κ²°μ κΈμ‘ (μ)") |
| 20 | + duration_days: int = Field(default=30, description="ꡬλ
κΈ°κ° (μΌ)") |
| 21 | + |
| 22 | + |
| 23 | +class MembershipInfoResponse(BaseModel): |
| 24 | + """λ©€λ²μ μ 보 μλ΅ DTO""" |
| 25 | + user_id: int |
| 26 | + membership_tier: str |
| 27 | + is_active: bool |
| 28 | + is_expired: bool |
| 29 | + subscription_expire_date: str | None |
| 30 | + |
| 31 | + |
| 32 | +@router.get("/info") |
| 33 | +async def get_membership_info( |
| 34 | + current_user: Annotated[User, Depends(get_current_user)] |
| 35 | +): |
| 36 | + """ |
| 37 | + λ©€λ²μ μ 보 μ‘°ν |
| 38 | +
|
| 39 | + Returns: |
| 40 | + { |
| 41 | + "code": 200, |
| 42 | + "message": "λ©€λ²μ μ 보 μ‘°ν μ±κ³΅", |
| 43 | + "data": { |
| 44 | + "user_id": 1, |
| 45 | + "membership_tier": "FREE", |
| 46 | + "is_active": false, |
| 47 | + "is_expired": false, |
| 48 | + "subscription_expire_date": null |
| 49 | + } |
| 50 | + } |
| 51 | + """ |
| 52 | + subscription_status = await MembershipService.check_subscription_status(current_user) |
| 53 | + |
| 54 | + membership_tier = getattr(current_user, 'membership_tier', 'FREE') |
| 55 | + |
| 56 | + response = MembershipInfoResponse( |
| 57 | + user_id=current_user.id, |
| 58 | + membership_tier=membership_tier, |
| 59 | + is_active=subscription_status["is_active"], |
| 60 | + is_expired=subscription_status["is_expired"], |
| 61 | + subscription_expire_date=subscription_status["expire_date"].isoformat() if subscription_status["expire_date"] else None |
| 62 | + ) |
| 63 | + |
| 64 | + return ok(data=response.model_dump(), message="λ©€λ²μ μ 보 μ‘°ν μ±κ³΅") |
| 65 | + |
| 66 | + |
| 67 | +@router.patch("/upgrade") |
| 68 | +async def upgrade_membership( |
| 69 | + request: UpgradeMembershipRequest, |
| 70 | + current_user: Annotated[User, Depends(get_current_user)], |
| 71 | + db: Annotated[AsyncSession, Depends(get_db)] |
| 72 | +): |
| 73 | + """ |
| 74 | + λ©€λ²μ μ
κ·Έλ μ΄λ (ν
μ€νΈμ©) |
| 75 | +
|
| 76 | + Request Body: |
| 77 | + { |
| 78 | + "membership_tier": "PREMIUM", |
| 79 | + "payment_amount": 9900, |
| 80 | + "duration_days": 30 |
| 81 | + } |
| 82 | +
|
| 83 | + Returns: |
| 84 | + { |
| 85 | + "code": 200, |
| 86 | + "message": "λ©€λ²μ μ
κ·Έλ μ΄λ μ±κ³΅", |
| 87 | + "data": { |
| 88 | + "user_id": 1, |
| 89 | + "previous_tier": "FREE", |
| 90 | + "new_tier": "PREMIUM", |
| 91 | + "subscription_expire_date": "2026-01-11T12:00:00" |
| 92 | + } |
| 93 | + } |
| 94 | + """ |
| 95 | + result = await MembershipService.upgrade_membership( |
| 96 | + db=db, |
| 97 | + user=current_user, |
| 98 | + membership_tier=request.membership_tier, |
| 99 | + payment_amount=request.payment_amount, |
| 100 | + duration_days=request.duration_days |
| 101 | + ) |
| 102 | + |
| 103 | + # subscription_expire_dateκ° datetimeμ΄λ©΄ λ¬Έμμ΄λ‘ λ³ν |
| 104 | + if result["subscription_expire_date"]: |
| 105 | + result["subscription_expire_date"] = result["subscription_expire_date"].isoformat() |
| 106 | + |
| 107 | + return ok(data=result, message="λ©€λ²μ μ
κ·Έλ μ΄λ μ±κ³΅") |
| 108 | + |
| 109 | + |
| 110 | +@router.get("/payment-history") |
| 111 | +async def get_payment_history( |
| 112 | + current_user: Annotated[User, Depends(get_current_user)], |
| 113 | + db: Annotated[AsyncSession, Depends(get_db)] |
| 114 | +): |
| 115 | + """ |
| 116 | + κ²°μ μ΄λ ₯ μ‘°ν |
| 117 | +
|
| 118 | + Returns: |
| 119 | + { |
| 120 | + "code": 200, |
| 121 | + "message": "κ²°μ μ΄λ ₯ μ‘°ν μ±κ³΅", |
| 122 | + "data": [ |
| 123 | + { |
| 124 | + "id": 1, |
| 125 | + "membership_tier": "PREMIUM", |
| 126 | + "payment_amount": 9900, |
| 127 | + "payment_method": "TEST", |
| 128 | + "payment_status": "SUCCESS", |
| 129 | + "subscription_start_date": "2025-11-30T00:00:00", |
| 130 | + "subscription_end_date": "2025-12-30T00:00:00", |
| 131 | + "created_at": "2025-11-30T00:00:00", |
| 132 | + "transaction_id": "TEST_1_1234567890", |
| 133 | + "pg_provider": "TEST" |
| 134 | + } |
| 135 | + ] |
| 136 | + } |
| 137 | + """ |
| 138 | + payment_history = await MembershipService.get_payment_history( |
| 139 | + db=db, |
| 140 | + user_id=current_user.id |
| 141 | + ) |
| 142 | + |
| 143 | + return ok(data=payment_history, message="κ²°μ μ΄λ ₯ μ‘°ν μ±κ³΅") |
| 144 | + |
| 145 | + |
| 146 | +@router.patch("/cancel") |
| 147 | +async def cancel_membership( |
| 148 | + current_user: Annotated[User, Depends(get_current_user)], |
| 149 | + db: Annotated[AsyncSession, Depends(get_db)] |
| 150 | +): |
| 151 | + """ |
| 152 | + λ©€λ²μ ꡬλ
ν΄μ§ |
| 153 | +
|
| 154 | + ꡬλ
μ ν΄μ§ν©λλ€. λ§λ£μΌκΉμ§λ ν리미μ κΈ°λ₯μ κ³μ μ¬μ©ν μ μμ΅λλ€. |
| 155 | +
|
| 156 | + Returns: |
| 157 | + { |
| 158 | + "code": 200, |
| 159 | + "message": "ꡬλ
ν΄μ§κ° μλ£λμμ΅λλ€", |
| 160 | + "data": { |
| 161 | + "user_id": 1, |
| 162 | + "membership_tier": "PREMIUM", |
| 163 | + "subscription_expire_date": "2026-01-11T12:00:00", |
| 164 | + "days_remaining": 29, |
| 165 | + "message": "λ§λ£μΌκΉμ§ ν리미μ κΈ°λ₯μ κ³μ μ¬μ©ν μ μμ΅λλ€." |
| 166 | + } |
| 167 | + } |
| 168 | + """ |
| 169 | + from datetime import datetime |
| 170 | + from exception.client_exception import BadRequestException |
| 171 | + |
| 172 | + membership_tier = getattr(current_user, 'membership_tier', 'FREE') |
| 173 | + subscription_expire_date = getattr(current_user, 'subscription_expire_date', None) |
| 174 | + |
| 175 | + # μ΄λ―Έ FREE λ±κΈμ΄λ©΄ ν΄μ§ν ꡬλ
μ΄ μμ |
| 176 | + if membership_tier == "FREE": |
| 177 | + raise BadRequestException(message="ν΄μ§ν ꡬλ
μ΄ μμ΅λλ€.") |
| 178 | + |
| 179 | + # ꡬλ
μν νμΈ |
| 180 | + subscription_status = await MembershipService.check_subscription_status(current_user) |
| 181 | + |
| 182 | + if subscription_status["is_expired"]: |
| 183 | + raise BadRequestException(message="μ΄λ―Έ λ§λ£λ ꡬλ
μ
λλ€.") |
| 184 | + |
| 185 | + # λ¨μ μΌμ κ³μ° |
| 186 | + if subscription_expire_date: |
| 187 | + now = datetime.now() |
| 188 | + days_remaining = (subscription_expire_date - now).days |
| 189 | + else: |
| 190 | + days_remaining = 0 |
| 191 | + |
| 192 | + # μ€μ ꡬλ
ν΄μ§ λ‘μ§μ μ¬κΈ°μ ꡬν |
| 193 | + # (μ: auto_renewal νλκ·Έ μ€μ , λλ μ¦μ FREEλ‘ λ³κ²½ λ±) |
| 194 | + # νμ¬λ μ λ³΄λ§ λ°ν |
| 195 | + |
| 196 | + response = { |
| 197 | + "user_id": current_user.id, |
| 198 | + "membership_tier": membership_tier, |
| 199 | + "subscription_expire_date": subscription_expire_date.isoformat() if subscription_expire_date else None, |
| 200 | + "days_remaining": days_remaining, |
| 201 | + "message": f"λ§λ£μΌ({subscription_expire_date.date()})κΉμ§ ν리미μ κΈ°λ₯μ κ³μ μ¬μ©ν μ μμ΅λλ€." if subscription_expire_date else "ꡬλ
ν΄μ§κ° μλ£λμμ΅λλ€." |
| 202 | + } |
| 203 | + |
| 204 | + return ok(data=response, message="ꡬλ
ν΄μ§κ° μλ£λμμ΅λλ€") |
0 commit comments