Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add FULFILLMENT_APPROVED webhook
IKarbowiakc7207e71e0d3
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 0584be8a02..69d2a8eaac 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1544,6 +1544,9 @@ enum WebhookEventTypeEnum {"""A fulfillment is cancelled."""FULFILLMENT_CANCELED+ """A fulfillment is approved."""+ FULFILLMENT_APPROVED+"""User notification triggered."""NOTIFY_USER@@ -1952,6 +1955,9 @@ enum WebhookEventTypeAsyncEnum {"""A fulfillment is cancelled."""FULFILLMENT_CANCELED+ """A fulfillment is approved."""+ FULFILLMENT_APPROVED+"""User notification triggered."""NOTIFY_USER@@ -2578,6 +2584,7 @@ enum WebhookSampleEventTypeEnum {CHECKOUT_UPDATEDFULFILLMENT_CREATEDFULFILLMENT_CANCELED+ FULFILLMENT_APPROVEDNOTIFY_USERPAGE_CREATEDPAGE_UPDATED@@ -22913,6 +22920,33 @@ type FulfillmentCanceled implements Event {order: Order}+"""+Event sent when fulfillment is approved.++Added in Saleor 3.7.++Note: this API is currently in Feature Preview and can be subject to changes at later point.+"""+type FulfillmentApproved implements Event {+ """Time of the event."""+ issuedAt: DateTime++ """Saleor version that triggered the event."""+ version: String++ """The user or application that triggered the event."""+ issuingPrincipal: IssuingPrincipal++ """The application receiving the webhook."""+ recipient: App++ """The fulfillment the event relates to."""+ fulfillment: Fulfillment++ """The order the fulfillment belongs to."""+ order: Order+}+"""Event sent when new customer user is created.diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.pyindex 0b2c1e8e04..1ad0244fdc 100644--- a/saleor/graphql/webhook/enums.py+++ b/saleor/graphql/webhook/enums.py@@ -81,6 +81,7 @@ WEBHOOK_EVENT_DESCRIPTION = {WebhookEventAsyncType.SALE_TOGGLE: "A sale is activated or deactivated.",WebhookEventAsyncType.FULFILLMENT_CREATED: "A new fulfillment is created.",WebhookEventAsyncType.FULFILLMENT_CANCELED: "A fulfillment is cancelled.",+ WebhookEventAsyncType.FULFILLMENT_APPROVED: "A fulfillment is approved.",WebhookEventAsyncType.PAGE_CREATED: "A new page is created.",WebhookEventAsyncType.PAGE_UPDATED: "A page is updated.",WebhookEventAsyncType.PAGE_DELETED: "A page is deleted.",diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.pyindex 9e858e8576..3f8a27f962 100644--- a/saleor/graphql/webhook/subscription_types.py+++ b/saleor/graphql/webhook/subscription_types.py@@ -819,6 +819,14 @@ class FulfillmentCanceled(ObjectType, FulfillmentBase):)+class FulfillmentApproved(ObjectType, FulfillmentBase):+ class Meta:+ interfaces = (Event,)+ description = (+ "Event sent when fulfillment is approved." + ADDED_IN_37 + PREVIEW_FEATURE+ )++class UserBase(AbstractType):user = graphene.Field("saleor.graphql.account.types.User",@@ -1520,6 +1528,7 @@ WEBHOOK_TYPES_MAP = {WebhookEventAsyncType.INVOICE_SENT: InvoiceSent,WebhookEventAsyncType.FULFILLMENT_CREATED: FulfillmentCreated,WebhookEventAsyncType.FULFILLMENT_CANCELED: FulfillmentCanceled,+ WebhookEventAsyncType.FULFILLMENT_APPROVED: FulfillmentApproved,WebhookEventAsyncType.CUSTOMER_CREATED: CustomerCreated,WebhookEventAsyncType.CUSTOMER_UPDATED: CustomerUpdated,WebhookEventAsyncType.COLLECTION_CREATED: CollectionCreated,diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.pyindex 27b2c2e079..e51a99dc01 100644--- a/saleor/plugins/base_plugin.py+++ b/saleor/plugins/base_plugin.py@@ -490,6 +490,11 @@ class BasePlugin:# cancelled.fulfillment_canceled: Callable[["Fulfillment", Any], Any]+ # Trigger when fulfillemnt is approved.+ # Overwrite this method if you need to trigger specific logic when a fulfillment is+ # approved.+ fulfillment_approved: Callable[["Fulfillment", Any], Any]+get_checkout_line_tax_rate: Callable[["CheckoutInfo",diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.pyindex 95ece16318..1e8a4e0583 100644--- a/saleor/plugins/manager.py+++ b/saleor/plugins/manager.py@@ -791,6 +791,15 @@ class PluginsManager(PaymentInterface):channel_slug=fulfillment.order.channel.slug,)+ def fulfillment_approved(self, fulfillment: "Fulfillment"):+ default_value = None+ return self.__run_method_on_plugins(+ "fulfillment_approved",+ default_value,+ fulfillment,+ channel_slug=fulfillment.order.channel.slug,+ )+def tracking_number_updated(self, fulfillment: "Fulfillment"):default_value = Nonereturn self.__run_method_on_plugins(diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.pyindex f9cfdb4d98..c055f6ee65 100644--- a/saleor/plugins/webhook/plugin.py+++ b/saleor/plugins/webhook/plugin.py@@ -648,6 +648,16 @@ class WebhookPlugin(BasePlugin):fulfillment_data, event_type, webhooks, fulfillment, self.requestor)+ def fulfillment_approved(self, fulfillment: "Fulfillment", previous_value):+ if not self.active:+ return previous_value+ event_type = WebhookEventAsyncType.FULFILLMENT_APPROVED+ if webhooks := get_webhooks_for_event(event_type):+ fulfillment_data = generate_fulfillment_payload(fulfillment, self.requestor)+ trigger_webhooks_async(+ fulfillment_data, event_type, webhooks, fulfillment, self.requestor+ )+def customer_created(self, customer: "User", previous_value: Any) -> Any:if not self.active:return previous_valuediff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.pyindex 8e854f65ff..3d81cd89bf 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py@@ -470,6 +470,14 @@ def subscription_fulfillment_canceled_webhook(subscription_webhook):)+def subscription_fulfillment_approved_webhook(subscription_webhook):+ return subscription_webhook(+ queries.FULFILLMENT_APPROVED,+ WebhookEventAsyncType.FULFILLMENT_APPROVED,+ )++@pytest.fixturedef subscription_customer_created_webhook(subscription_webhook):return subscription_webhook(diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.pyindex 95c457a6d7..6acf77d4c1 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py@@ -940,6 +940,24 @@ FULFILLMENT_CANCELED = (""")+FULFILLMENT_APPROVED = (+ fragments.FULFILLMENT_DETAILS+ + """+ subscription{+ event{+ ...on FulfillmentApproved{+ fulfillment{+ ...FulfillmentDetails+ }+ order{+ id+ }+ }+ }+ }+"""+)+CUSTOMER_CREATED = (fragments.CUSTOMER_DETAILS+ """diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.pyindex d37b05d78b..c2a056035c 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py@@ -1215,6 +1215,21 @@ def test_fulfillment_canceled(fulfillment, subscription_fulfillment_canceled_webassert deliveries[0].webhook == webhooks[0]+def test_fulfillment_approved(fulfillment, subscription_fulfillment_approved_webhook):+ # given+ webhooks = [subscription_fulfillment_approved_webhook]+ event_type = WebhookEventAsyncType.FULFILLMENT_APPROVED+ expected_payload = generate_fulfillment_payload(fulfillment)++ # when+ deliveries = create_deliveries_for_subscriptions(event_type, fulfillment, webhooks)++ # then+ assert deliveries[0].payload.payload == json.dumps(expected_payload)+ assert len(deliveries) == len(webhooks)+ assert deliveries[0].webhook == webhooks[0]++def test_customer_created(customer_user, subscription_customer_created_webhook):# givenwebhooks = [subscription_customer_created_webhook]diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.pyindex 0cc728aa1f..f7577d6b4d 100644--- a/saleor/webhook/event_types.py+++ b/saleor/webhook/event_types.py@@ -62,9 +62,12 @@ class WebhookEventAsyncType:ORDER_FULLY_PAID = "order_fully_paid"ORDER_UPDATED = "order_updated"ORDER_CANCELLED = "order_cancelled"- FULFILLMENT_CANCELED = "fulfillment_canceled"ORDER_FULFILLED = "order_fulfilled"+ FULFILLMENT_CREATED = "fulfillment_created"+ FULFILLMENT_CANCELED = "fulfillment_canceled"+ FULFILLMENT_APPROVED = "fulfillment_approved"+DRAFT_ORDER_CREATED = "draft_order_created"DRAFT_ORDER_UPDATED = "draft_order_updated"DRAFT_ORDER_DELETED = "draft_order_deleted"@@ -78,8 +81,6 @@ class WebhookEventAsyncType:INVOICE_DELETED = "invoice_deleted"INVOICE_SENT = "invoice_sent"- FULFILLMENT_CREATED = "fulfillment_created"-CUSTOMER_CREATED = "customer_created"CUSTOMER_UPDATED = "customer_updated"CUSTOMER_DELETED = "customer_deleted"@@ -209,6 +210,7 @@ class WebhookEventAsyncType:CHECKOUT_UPDATED: "Checkout updated",FULFILLMENT_CREATED: "Fulfillment created",FULFILLMENT_CANCELED: "Fulfillment cancelled",+ FULFILLMENT_APPROVED: "Fulfillment approved",NOTIFY_USER: "Notify user",PAGE_CREATED: "Page Created",PAGE_UPDATED: "Page Updated",@@ -306,6 +308,7 @@ class WebhookEventAsyncType:(CHECKOUT_UPDATED, DISPLAY_LABELS[CHECKOUT_UPDATED]),(FULFILLMENT_CREATED, DISPLAY_LABELS[FULFILLMENT_CREATED]),(FULFILLMENT_CANCELED, DISPLAY_LABELS[FULFILLMENT_CANCELED]),+ (FULFILLMENT_APPROVED, DISPLAY_LABELS[FULFILLMENT_APPROVED]),(NOTIFY_USER, DISPLAY_LABELS[NOTIFY_USER]),(PAGE_CREATED, DISPLAY_LABELS[PAGE_CREATED]),(PAGE_UPDATED, DISPLAY_LABELS[PAGE_UPDATED]),@@ -404,6 +407,7 @@ class WebhookEventAsyncType:CHECKOUT_UPDATED: CheckoutPermissions.MANAGE_CHECKOUTS,FULFILLMENT_CREATED: OrderPermissions.MANAGE_ORDERS,FULFILLMENT_CANCELED: OrderPermissions.MANAGE_ORDERS,+ FULFILLMENT_APPROVED: OrderPermissions.MANAGE_ORDERS,NOTIFY_USER: AccountPermissions.MANAGE_USERS,PAGE_CREATED: PagePermissions.MANAGE_PAGES,PAGE_UPDATED: PagePermissions.MANAGE_PAGES,