-
Notifications
You must be signed in to change notification settings - Fork 540
fix: Allow to update custom field data #6613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9cacda2
5b081d7
92315f1
e8d1207
cecd05b
ec5d4c4
86a76ae
64a8c35
0ccf563
e62a0c5
d7bc98e
1dfd331
38faed1
6d13475
2ee697b
0d33912
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,15 @@ | |
| import stripe as stripe_lib | ||
| import structlog | ||
| from sqlalchemy import select | ||
| from sqlalchemy.orm import contains_eager, joinedload | ||
| from sqlalchemy.orm import contains_eager, joinedload, selectinload | ||
|
|
||
| from polar.account.repository import AccountRepository | ||
| from polar.auth.models import AuthSubject | ||
| from polar.billing_entry.service import billing_entry as billing_entry_service | ||
| from polar.checkout.eventstream import CheckoutEvent, publish_checkout_event | ||
| from polar.checkout.repository import CheckoutRepository | ||
| from polar.config import settings | ||
| from polar.custom_field.data import validate_custom_field_data | ||
| from polar.customer.repository import CustomerRepository | ||
| from polar.customer_portal.schemas.order import ( | ||
| CustomerOrderPaymentConfirmation, | ||
|
|
@@ -402,8 +403,9 @@ async def get( | |
| .options( | ||
| *repository.get_eager_options( | ||
| customer_load=contains_eager(Order.customer), | ||
| product_load=joinedload(Order.product).joinedload( | ||
| Product.organization | ||
| product_load=joinedload(Order.product).options( | ||
| joinedload(Product.organization), | ||
| selectinload(Product.attached_custom_fields), | ||
| ), | ||
| ) | ||
| ) | ||
|
|
@@ -436,10 +438,19 @@ async def update( | |
| if errors: | ||
| raise PolarRequestValidationError(errors) | ||
|
|
||
| update_dict = order_update.model_dump(exclude_unset=True) | ||
|
|
||
| if "custom_field_data" in update_dict: | ||
| # Validate custom field data against the product's attached custom fields | ||
| custom_field_data = validate_custom_field_data( | ||
| order.product.attached_custom_fields, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will likely raise an eager loading error at runtime (this is a common shortcomings of our tests which usually don't catch that). That's also why @rishi-raj-jain asked for a video showing it working. For simplicity, you should add that eager load instruction above in the async def get(
self,
session: AsyncSession,
auth_subject: AuthSubject[User | Organization],
id: uuid.UUID,
) -> Order | None:
repository = OrderRepository.from_session(session)
statement = (
repository.get_readable_statement(auth_subject)
.options(
*repository.get_eager_options(
customer_load=contains_eager(Order.customer),
product_load=joinedload(Order.product).options(
joinedload(Product.organization),
selectinload(Product.attached_custom_fields),
)
)
)
.where(Order.id == id)
)
return await repository.get_one_or_none(statement)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @frankie567 are you suggesting this because of the performance, so that update function doesnt lazy load the data? |
||
| order_update.custom_field_data, | ||
| validate_required=False, # Allow merchants to update even if required fields are missing | ||
| ) | ||
| update_dict["custom_field_data"] = custom_field_data | ||
|
|
||
| repository = OrderRepository.from_session(session) | ||
| order = await repository.update( | ||
| order, update_dict=order_update.model_dump(exclude_unset=True) | ||
| ) | ||
| order = await repository.update(order, update_dict=update_dict) | ||
|
|
||
| await self.send_webhook(session, order, WebhookEventType.order_updated) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.