From fff285f8d2f3020a24b83066fa4250130fdcf229 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Sun, 11 May 2025 17:17:19 +0200 Subject: [PATCH] feat(core-flows): Emit cart trasnferred customer (#12425) **What** When a cart is transferred emit an event notifying of the action with the cart id and the customer id --- .changeset/serious-plums-hope.md | 6 + .../cart/workflows/transfer-cart-customer.ts | 11 +- packages/core/utils/src/core-flows/events.ts | 139 ++++++++++-------- 3 files changed, 92 insertions(+), 64 deletions(-) create mode 100644 .changeset/serious-plums-hope.md diff --git a/.changeset/serious-plums-hope.md b/.changeset/serious-plums-hope.md new file mode 100644 index 0000000000..5cdbebf5e3 --- /dev/null +++ b/.changeset/serious-plums-hope.md @@ -0,0 +1,6 @@ +--- +"@medusajs/core-flows": patch +"@medusajs/utils": patch +--- + +feat(core-flows): Emit cart trasnferred customer diff --git a/packages/core/core-flows/src/cart/workflows/transfer-cart-customer.ts b/packages/core/core-flows/src/cart/workflows/transfer-cart-customer.ts index 1b6765a2fc..45fa328d59 100644 --- a/packages/core/core-flows/src/cart/workflows/transfer-cart-customer.ts +++ b/packages/core/core-flows/src/cart/workflows/transfer-cart-customer.ts @@ -6,9 +6,10 @@ import { WorkflowData, WorkflowResponse, } from "@medusajs/framework/workflows-sdk" -import { useQueryGraphStep } from "../../common" +import { emitEventStep, useQueryGraphStep } from "../../common" import { updateCartsStep } from "../steps" import { refreshCartItemsWorkflow } from "./refresh-cart-items" +import { CartWorkflowEvents } from "@medusajs/framework/utils" /** * The cart ownership transfer details. @@ -110,6 +111,14 @@ export const transferCartCustomerWorkflow = createWorkflow( refreshCartItemsWorkflow.runAsStep({ input: { cart_id: input.id, force_refresh: true }, }) + + emitEventStep({ + eventName: CartWorkflowEvents.CUSTOMER_TRANSFERRED, + data: { + id: input.id, + customer_id: customer.customer_id, + }, + }) } ) diff --git a/packages/core/utils/src/core-flows/events.ts b/packages/core/utils/src/core-flows/events.ts index f359d0fa95..8fbb79ccc1 100644 --- a/packages/core/utils/src/core-flows/events.ts +++ b/packages/core/utils/src/core-flows/events.ts @@ -5,7 +5,7 @@ export const CartWorkflowEvents = { /** * Emitted when a cart is created. - * + * * @eventPayload * ```ts * { @@ -16,7 +16,7 @@ export const CartWorkflowEvents = { CREATED: "cart.created", /** * Emitted when a cart's details are updated. - * + * * @eventPayload * ```ts * { @@ -27,7 +27,7 @@ export const CartWorkflowEvents = { UPDATED: "cart.updated", /** * Emitted when the customer in the cart is updated. - * + * * @eventPayload * ```ts * { @@ -39,7 +39,7 @@ export const CartWorkflowEvents = { /** * Emitted when the cart's region is updated. This * event is emitted alongside the `cart.updated` event. - * + * * @eventPayload * ```ts * { @@ -48,6 +48,19 @@ export const CartWorkflowEvents = { * ``` */ REGION_UPDATED: "cart.region_updated", + + /** + * Emitted when the customer in the cart is transferred. + * + * @eventPayload + * ```ts + * { + * id, // The ID of the cart + * customer_id, // The ID of the customer + * } + * ``` + */ + CUSTOMER_TRANSFERRED: "cart.customer_transferred", } /** @@ -57,7 +70,7 @@ export const CartWorkflowEvents = { export const CustomerWorkflowEvents = { /** * Emitted when a customer is created. - * + * * @eventPayload * ```ts * [{ @@ -68,7 +81,7 @@ export const CustomerWorkflowEvents = { CREATED: "customer.created", /** * Emitted when a customer is updated. - * + * * @eventPayload * ```ts * [{ @@ -79,7 +92,7 @@ export const CustomerWorkflowEvents = { UPDATED: "customer.updated", /** * Emitted when a customer is deleted. - * + * * @eventPayload * ```ts * [{ @@ -98,7 +111,7 @@ export const OrderWorkflowEvents = { /** * Emitted when the details of an order or draft order is updated. This * doesn't include updates made by an edit. - * + * * @eventPayload * ```ts * { @@ -111,7 +124,7 @@ export const OrderWorkflowEvents = { /** * Emitted when an order is placed, or when a draft order is converted to an * order. - * + * * @eventPayload * ```ts * { @@ -122,7 +135,7 @@ export const OrderWorkflowEvents = { PLACED: "order.placed", /** * Emitted when an order is canceld. - * + * * @eventPayload * ```ts * { @@ -133,7 +146,7 @@ export const OrderWorkflowEvents = { CANCELED: "order.canceled", /** * Emitted when orders are completed. - * + * * @eventPayload * ```ts * [{ @@ -144,7 +157,7 @@ export const OrderWorkflowEvents = { COMPLETED: "order.completed", /** * Emitted when an order is archived. - * + * * @eventPayload * ```ts * [{ @@ -156,7 +169,7 @@ export const OrderWorkflowEvents = { /** * Emitted when a fulfillment is created for an order. - * + * * @eventPayload * ```ts * { @@ -169,7 +182,7 @@ export const OrderWorkflowEvents = { FULFILLMENT_CREATED: "order.fulfillment_created", /** * Emitted when an order's fulfillment is canceled. - * + * * @eventPayload * ```ts * { @@ -183,7 +196,7 @@ export const OrderWorkflowEvents = { /** * Emitted when a return request is confirmed. - * + * * @eventPayload * ```ts * { @@ -195,7 +208,7 @@ export const OrderWorkflowEvents = { RETURN_REQUESTED: "order.return_requested", /** * Emitted when a return is marked as received. - * + * * @eventPayload * ```ts * { @@ -208,7 +221,7 @@ export const OrderWorkflowEvents = { /** * Emitted when a claim is created for an order. - * + * * @eventPayload * ```ts * { @@ -220,7 +233,7 @@ export const OrderWorkflowEvents = { CLAIM_CREATED: "order.claim_created", /** * Emitted when an exchange is created for an order. - * + * * @eventPayload * ```ts * { @@ -234,7 +247,7 @@ export const OrderWorkflowEvents = { /** * Emitted when an order is requested to be transferred to * another customer. - * + * * @eventPayload * ```ts * { @@ -253,9 +266,9 @@ export const OrderWorkflowEvents = { export const OrderEditWorkflowEvents = { /** * Emitted when an order edit is requested. - * + * * @version 2.8.0 - * + * * @eventPayload * ```ts * { @@ -267,9 +280,9 @@ export const OrderEditWorkflowEvents = { REQUESTED: "order-edit.requested", /** * Emitted when an order edit request is confirmed. - * + * * @version 2.8.0 - * + * * @eventPayload * ```ts * { @@ -281,9 +294,9 @@ export const OrderEditWorkflowEvents = { CONFIRMED: "order-edit.confirmed", /** * Emitted when an order edit request is canceled. - * + * * @version 2.8.0 - * + * * @eventPayload * ```ts * { @@ -302,7 +315,7 @@ export const OrderEditWorkflowEvents = { export const UserWorkflowEvents = { /** * Emitted when users are created. - * + * * @eventPayload * ```ts * [{ @@ -313,7 +326,7 @@ export const UserWorkflowEvents = { CREATED: "user.created", /** * Emitted when users are updated. - * + * * @eventPayload * ```ts * [{ @@ -324,7 +337,7 @@ export const UserWorkflowEvents = { UPDATED: "user.updated", /** * Emitted when users are deleted. - * + * * @eventPayload * ```ts * [{ @@ -343,7 +356,7 @@ export const AuthWorkflowEvents = { /** * Emitted when a reset password token is generated. You can listen to this event * to send a reset password email to the user or customer, for example. - * + * * @eventPayload * ```ts * { @@ -363,7 +376,7 @@ export const AuthWorkflowEvents = { export const SalesChannelWorkflowEvents = { /** * Emitted when sales channels are created. - * + * * @eventPayload * ```ts * [{ @@ -374,7 +387,7 @@ export const SalesChannelWorkflowEvents = { CREATED: "sales-channel.created", /** * Emitted when sales channels are updated. - * + * * @eventPayload * ```ts * [{ @@ -385,7 +398,7 @@ export const SalesChannelWorkflowEvents = { UPDATED: "sales-channel.updated", /** * Emitted when sales channels are deleted. - * + * * @eventPayload * ```ts * [{ @@ -403,7 +416,7 @@ export const SalesChannelWorkflowEvents = { export const ProductCategoryWorkflowEvents = { /** * Emitted when product categories are created. - * + * * @eventPayload * ```ts * [{ @@ -414,7 +427,7 @@ export const ProductCategoryWorkflowEvents = { CREATED: "product-category.created", /** * Emitted when product categories are updated. - * + * * @eventPayload * ```ts * [{ @@ -425,7 +438,7 @@ export const ProductCategoryWorkflowEvents = { UPDATED: "product-category.updated", /** * Emitted when product categories are deleted. - * + * * @eventPayload * ```ts * [{ @@ -443,7 +456,7 @@ export const ProductCategoryWorkflowEvents = { export const ProductCollectionWorkflowEvents = { /** * Emitted when product collections are created. - * + * * @eventPayload * ```ts * [{ @@ -454,7 +467,7 @@ export const ProductCollectionWorkflowEvents = { CREATED: "product-collection.created", /** * Emitted when product collections are updated. - * + * * @eventPayload * ```ts * [{ @@ -465,7 +478,7 @@ export const ProductCollectionWorkflowEvents = { UPDATED: "product-collection.updated", /** * Emitted when product collections are deleted. - * + * * @eventPayload * ```ts * [{ @@ -483,7 +496,7 @@ export const ProductCollectionWorkflowEvents = { export const ProductVariantWorkflowEvents = { /** * Emitted when product variants are updated. - * + * * @eventPayload * ```ts * [{ @@ -494,7 +507,7 @@ export const ProductVariantWorkflowEvents = { UPDATED: "product-variant.updated", /** * Emitted when product variants are created. - * + * * @eventPayload * ```ts * [{ @@ -505,7 +518,7 @@ export const ProductVariantWorkflowEvents = { CREATED: "product-variant.created", /** * Emitted when product variants are deleted. - * + * * @eventPayload * ```ts * [{ @@ -523,7 +536,7 @@ export const ProductVariantWorkflowEvents = { export const ProductWorkflowEvents = { /** * Emitted when products are updated. - * + * * @eventPayload * ```ts * [{ @@ -534,7 +547,7 @@ export const ProductWorkflowEvents = { UPDATED: "product.updated", /** * Emitted when products are created. - * + * * @eventPayload * ```ts * [{ @@ -545,7 +558,7 @@ export const ProductWorkflowEvents = { CREATED: "product.created", /** * Emitted when products are deleted. - * + * * @eventPayload * ```ts * [{ @@ -563,7 +576,7 @@ export const ProductWorkflowEvents = { export const ProductTypeWorkflowEvents = { /** * Emitted when product types are updated. - * + * * @eventPayload * ```ts * [{ @@ -574,7 +587,7 @@ export const ProductTypeWorkflowEvents = { UPDATED: "product-type.updated", /** * Emitted when product types are created. - * + * * @eventPayload * ```ts * [{ @@ -585,7 +598,7 @@ export const ProductTypeWorkflowEvents = { CREATED: "product-type.created", /** * Emitted when product types are deleted. - * + * * @eventPayload * ```ts * [{ @@ -603,7 +616,7 @@ export const ProductTypeWorkflowEvents = { export const ProductTagWorkflowEvents = { /** * Emitted when product tags are updated. - * + * * @eventPayload * ```ts * [{ @@ -614,7 +627,7 @@ export const ProductTagWorkflowEvents = { UPDATED: "product-tag.updated", /** * Emitted when product tags are created. - * + * * @eventPayload * ```ts * [{ @@ -625,7 +638,7 @@ export const ProductTagWorkflowEvents = { CREATED: "product-tag.created", /** * Emitted when product tags are deleted. - * + * * @eventPayload * ```ts * [{ @@ -643,7 +656,7 @@ export const ProductTagWorkflowEvents = { export const ProductOptionWorkflowEvents = { /** * Emitted when product options are updated. - * + * * @eventPayload * ```ts * [{ @@ -654,7 +667,7 @@ export const ProductOptionWorkflowEvents = { UPDATED: "product-option.updated", /** * Emitted when product options are created. - * + * * @eventPayload * ```ts * [{ @@ -665,7 +678,7 @@ export const ProductOptionWorkflowEvents = { CREATED: "product-option.created", /** * Emitted when product options are deleted. - * + * * @eventPayload * ```ts * [{ @@ -683,7 +696,7 @@ export const ProductOptionWorkflowEvents = { export const InviteWorkflowEvents = { /** * Emitted when an invite is accepted. - * + * * @eventPayload * ```ts * { @@ -695,7 +708,7 @@ export const InviteWorkflowEvents = { /** * Emitted when invites are created. You can listen to this event * to send an email to the invited users, for example. - * + * * @eventPayload * ```ts * [{ @@ -706,7 +719,7 @@ export const InviteWorkflowEvents = { CREATED: "invite.created", /** * Emitted when invites are deleted. - * + * * @eventPayload * ```ts * [{ @@ -719,7 +732,7 @@ export const InviteWorkflowEvents = { * Emitted when invites should be resent because their token was * refreshed. You can listen to this event to send an email to the invited users, * for example. - * + * * @eventPayload * ```ts * [{ @@ -737,7 +750,7 @@ export const InviteWorkflowEvents = { export const RegionWorkflowEvents = { /** * Emitted when regions are updated. - * + * * @eventPayload * ```ts * [{ @@ -748,7 +761,7 @@ export const RegionWorkflowEvents = { UPDATED: "region.updated", /** * Emitted when regions are created. - * + * * @eventPayload * ```ts * [{ @@ -759,7 +772,7 @@ export const RegionWorkflowEvents = { CREATED: "region.created", /** * Emitted when regions are deleted. - * + * * @eventPayload * ```ts * [{ @@ -777,7 +790,7 @@ export const RegionWorkflowEvents = { export const FulfillmentWorkflowEvents = { /** * Emitted when a shipment is created for an order. - * + * * @eventPayload * ```ts * { @@ -789,7 +802,7 @@ export const FulfillmentWorkflowEvents = { SHIPMENT_CREATED: "shipment.created", /** * Emitted when a fulfillment is marked as delivered. - * + * * @eventPayload * ```ts * { @@ -798,4 +811,4 @@ export const FulfillmentWorkflowEvents = { * ``` */ DELIVERY_CREATED: "delivery.created", -} \ No newline at end of file +}