UNPKG

@medusajs/core-flows

Version:

Set of workflow definitions for Medusa

87 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transferCartCustomerWorkflow = exports.transferCartCustomerWorkflowId = void 0; const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk"); const common_1 = require("../../common"); const steps_1 = require("../steps"); const refresh_cart_items_1 = require("./refresh-cart-items"); const utils_1 = require("@medusajs/framework/utils"); exports.transferCartCustomerWorkflowId = "transfer-cart-customer"; /** * This workflow transfers a cart's customer ownership to another customer. It's useful if a customer logs in after * adding the items to their cart, allowing you to transfer the cart's ownership to the logged-in customer. This workflow is used * by the [Set Cart's Customer Store API Route](https://docs.medusajs.com/api/store#carts_postcartsidcustomer). * * You can use this workflow within your own customizations or custom workflows, allowing you to set the cart's customer within your custom flows. * * @example * const { result } = await transferCartCustomerWorkflow(container) * .run({ * input: { * id: "cart_123", * customer_id: "cus_123" * } * }) * * @summary * * Refresh a cart's payment collection details. * * @property hooks.validate - This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution. */ exports.transferCartCustomerWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.transferCartCustomerWorkflowId, (input) => { const cartQuery = (0, common_1.useQueryGraphStep)({ entity: "cart", filters: { id: input.id }, fields: [ "id", "email", "customer_id", "customer.has_account", "shipping_address.*", "region.*", "region.countries.*", ], options: { throwIfKeyNotFound: true }, }).config({ name: "get-cart" }); const cart = (0, workflows_sdk_1.transform)({ cartQuery }, ({ cartQuery }) => cartQuery.data[0]); const validate = (0, workflows_sdk_1.createHook)("validate", { input, cart, }); const customerQuery = (0, common_1.useQueryGraphStep)({ entity: "customer", filters: { id: input.customer_id }, fields: ["id", "email"], options: { throwIfKeyNotFound: true }, }).config({ name: "get-customer" }); const customer = (0, workflows_sdk_1.transform)({ customerQuery }, ({ customerQuery }) => customerQuery.data[0]); // If its the same customer, we don't want the email to be overridden, so we skip the // update entirely. When the customer is different, we also override the email. // The customer will have an opportunity to edit email again through update cart endpoint. const shouldTransfer = (0, workflows_sdk_1.transform)({ cart, customer }, ({ cart, customer }) => cart.customer?.id !== customer.id); (0, workflows_sdk_1.when)({ shouldTransfer }, ({ shouldTransfer }) => shouldTransfer).then(() => { const cartInput = (0, workflows_sdk_1.transform)({ cart, customer }, ({ cart, customer }) => [ { id: cart.id, customer_id: customer.id, email: customer.email, }, ]); (0, steps_1.updateCartsStep)(cartInput); refresh_cart_items_1.refreshCartItemsWorkflow.runAsStep({ input: { cart_id: input.id, force_refresh: true }, }); (0, common_1.emitEventStep)({ eventName: utils_1.CartWorkflowEvents.CUSTOMER_TRANSFERRED, data: { id: input.id, customer_id: customer.customer_id, }, }); }); return new workflows_sdk_1.WorkflowResponse(void 0, { hooks: [validate], }); }); //# sourceMappingURL=transfer-cart-customer.js.map