File size: 2,807 Bytes
6b3ee8d
5fc01f3
6b3ee8d
 
 
 
 
380d805
 
 
 
8f0180b
380d805
4b38102
6b3ee8d
8f0180b
98519e9
8f0180b
 
 
 
 
6b3ee8d
 
 
6069f1c
6b3ee8d
 
 
 
 
4b38102
6b3ee8d
5425993
6b3ee8d
 
 
 
 
 
 
 
 
6069f1c
6b3ee8d
 
 
8f0180b
 
6b3ee8d
 
 
 
 
 
 
8f0180b
 
 
 
 
98519e9
8e36052
8f0180b
 
 
380d805
8f0180b
8e36052
3100471
380d805
3100471
 
 
 
 
 
 
 
 
 
 
8f0180b
6b3ee8d
 
 
4b2e1ca
8f0180b
6b3ee8d
 
 
 
6069f1c
8f0180b
6b3ee8d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from supabase import create_client, Client
import os

SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)

async def fetch_subscription(jwt: str):
    auth_res = supabase.auth.get_user(jwt)
    if auth_res.user is None:
        return {"error": "Invalid or expired session"}

    user = auth_res.user
    email = user.email

    cust_res = (
        supabase.schema("stripe").table("customers")
        .select("*")
        .eq("email", email)
        .execute()
    )

    if not cust_res.data:
        return {
            "email": email,
            "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
            "subscription": None
        }

    customer = cust_res.data[0]

    # Fetch subscriptions
    sub_res = (
        supabase.schema("stripe").table("subscriptions")
        .select("*")
        .eq("customer", customer["id"])
        .in_("status", ["active", "trialing", "past_due"])
        .execute()
    )

    if not sub_res.data:
        return {
            "email": email,
            "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
            "subscription": None
        }

    subscriptions = []

    for s in sub_res.data:
        price_id = (
            s["items"]["data"][0]["price"]["id"]
            if s.get("items") and s["items"].get("data")
            else None
        )

        product_name = None
        nickname = None

        if price_id:
            price_res = (
                supabase.schema("stripe").table("prices")
                .select("*")
                .eq("id", price_id)
                .execute()
            )

            if price_res.data:
                price_row = price_res.data[0]
                nickname = price_row.get("nickname")

                product_id = price_row.get("product")
                if product_id:
                    product_res = (
                        supabase.schema("stripe").table("products")
                        .select("*")
                        .eq("id", product_id)
                        .execute()
                    )
                    if product_res.data:
                        product_name = product_res.data[0].get("name")

        subscriptions.append({
            "status": s["status"],
            "subscription_id": s["id"],
            "current_period_end": s["current_period_end"],
            "product_name": product_name,
            "nickname": nickname
        })

    return {
        "email": email,
        "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
        "subscription": subscriptions
    }