Skip to content

Commit

Permalink
server/customer_session: add customer portal URL to API output
Browse files Browse the repository at this point in the history
Fix #4752
  • Loading branch information
frankie567 committed Jan 2, 2025
1 parent ab841e9 commit 30a6252
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions server/polar/customer/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sqlalchemy import Select, UnaryExpression, asc, desc, func, or_, select
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.sql.base import ExecutableOption
from stripe import Customer as StripeCustomer

from polar.auth.models import AuthSubject, is_organization, is_user
Expand Down Expand Up @@ -64,10 +65,14 @@ async def get_by_id(
session: AsyncSession,
auth_subject: AuthSubject[User | Organization],
id: uuid.UUID,
*,
options: Sequence[ExecutableOption] | None = None,
) -> Customer | None:
statement = self._get_readable_customer_statement(auth_subject).where(
Customer.id == id
)
if options is not None:
statement = statement.options(*options)
result = await session.execute(statement)
return result.unique().scalar_one_or_none()

Expand Down
1 change: 1 addition & 0 deletions server/polar/customer_session/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ class CustomerSession(IDSchema, TimestampedSchema):

token: str = Field(validation_alias="raw_token")
expires_at: datetime
customer_portal_url: str
customer_id: UUID4
customer: Customer
6 changes: 5 additions & 1 deletion server/polar/customer_session/service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy import delete, select
from sqlalchemy.orm import joinedload

from polar.auth.models import AuthSubject, Organization, User
from polar.config import settings
Expand All @@ -23,7 +24,10 @@ async def create(
customer_create: CustomerSessionCreate,
) -> CustomerSession:
customer = await customer_service.get_by_id(
session, auth_subject, customer_create.customer_id
session,
auth_subject,
customer_create.customer_id,
options=(joinedload(Customer.organization),),
)
if customer is None:
raise PolarRequestValidationError(
Expand Down
9 changes: 8 additions & 1 deletion server/polar/models/customer_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from polar.config import settings
from polar.kit.db.models.base import RecordModel
from polar.kit.utils import utc_now
from polar.models.customer import Customer

from .customer import Customer


def get_expires_at() -> datetime:
Expand Down Expand Up @@ -37,3 +38,9 @@ def raw_token(self) -> str | None:
@raw_token.setter
def raw_token(self, value: str) -> None:
self._raw_token = value

@property
def customer_portal_url(self) -> str:
return settings.generate_frontend_url(
f"/{self.customer.organization.slug}/portal?customer_session_token={self.raw_token}"
)
1 change: 1 addition & 0 deletions server/tests/customer_session/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ async def test_valid(

assert json["token"].startswith(CUSTOMER_SESSION_TOKEN_PREFIX)
assert json["customer_id"] == str(customer.id)
assert json["customer_portal_url"].endswith(json["token"])

0 comments on commit 30a6252

Please sign in to comment.