Closed stephanboersma closed 10 months ago
Unfortunately, I was unable to reproduce your issue with the information provided.
Can I get you to ensure all your Medusa packages are running the latest version? The issue is likely due to a versioning mismatch between two of our packages.
Also, happy to further debug, if you can provide a repository with a consistent reproduction.
Hi again @olivermrbl ,
Thank you for the quick response!
After some dependency hell, I managed to get rid of the type error. Although, I am still not able to run the workflow according to the example of the createWorkflow
documentation.
The logs give this feedback when attempting to run the workflow in the subscriber.
info: Processing cart.updated which has 0 subscribers
info: Processing product-variant.updated which has 1 subscribers
info: Processing payment.updated which has 0 subscribers
info: Processing order.placed which has 3 subscribers
warn: An error occurred while processing order.placed: TypeError: create_shipmondo_order_1.createShipmondoSalesOrder.run is not a function
info: Processing cart.updated which has 0 subscribers
info: Processing cart.created which has 0 subscribers
import {
type SubscriberConfig,
type SubscriberArgs,
OrderService,
Logger,
} from "@medusajs/medusa"
import { createShipmondoSalesOrder } from "../workflows/create-shipmondo-order";
export default async function orderPlacedHandler({
data, eventName, container, pluginOptions,
}: SubscriberArgs<Record<string, any>>) {
const logger: Logger = container.resolve("logger");
const { result } = await createShipmondoSalesOrder.run({ input: { orderId: data.id } })
logger.info("Created Shipmondo sales order: " + result.id);
}
export const config: SubscriberConfig = {
event: OrderService.Events.PLACED,
context: {
subscriberId: "order-placed-shipmondo",
},
}
I made my repo public - if you have time, please have a look: https://github.com/stephanboersma/metalvinyl/tree/feat/shipmondo.
The workflow is on the feat/shipmondo branch located in apps/backend/src/workflows
and I am attempting to run it in a subscriber at apps/backend/src/subscribers/create-shipmondo-order.ts
.
See apps/backend/docker-compose.yml
as reference for my development environment.
I believe you are missing the workflow initialization step. Can I get you to try the follow:
const { result } = await createShipmondoSalesOrder(container).run({ input: { orderId: data.id } })
Here, our dependency container, container
, is passed upon initializing the workflow, so the workflow know what dependencies it can work with. We are looking into abstracting this step away, as we should be able to do it automagically in the average use case (and because it is evidently error-prone).
Let me know how it goes.
Oh god, you are right. The workflow runs and executes the steps correctly. I see that last step actually manages to communicate with third party, however a new typeerror is thrown during the workflow execution.
warn: An error occurred while processing order.placed: TypeError: object is not a function
I was able to console.log just before returning the final StepResult.
export const syncSalesOrder = createStep<
StepInput, StepOutput, string>("sync-shipmondo-sales-order", async (input) => {
const shipmondoClient = new ShipmondoClient({
USERNAME: process.env.SHIPMONDO_USER,
PASSWORD: process.env.SHIPMONDO_KEY,
})
const { preparedSalesOrder } = input;
const salesOrder = await shipmondoClient.salesOrders.salesOrdersPost(preparedSalesOrder);
console.log("this works: " + salesOrder.id);
return new StepResponse({ salesOrder });
}, async (error, context) => {
const logger: Logger = context.container.resolve("logger");
logger.error("Failed to sync sales order", error);
})
@adrien2p, @carlos-r-l-rodrigues – one of you know what might be wrong here?
@carlos-r-l-rodrigues, the exception is thrown within my event handler/subscriber:
import {
type SubscriberConfig,
type SubscriberArgs,
OrderService,
Logger,
} from "@medusajs/medusa"
import { createShipmondoSalesOrder } from "../workflows/create-shipmondo-order";
export default async function orderPlacedHandler({
data, eventName, container, pluginOptions,
}: SubscriberArgs<Record<string, any>>) {
const logger: Logger = container.resolve("logger");
const { result } = await createShipmondoSalesOrder(container).run({ input: { orderId: data.id } }); // exception thrown here
logger.info(`Created Shipmondo order ${result.id} for Medusa order ${result.order_id}`); // this never runs
}
export const config: SubscriberConfig = {
event: OrderService.Events.PLACED,
context: {
subscriberId: "order-placed-shipmondo",
},
}
I manage to reproduce the issue. Returning a destructured reference is not working.
To unblock you while we work in a fix for that, I suggest that in the definition of your workflow, you return the last step and handle the property salesOrder
on your side.
function (input) {
const { preparedSalesOrder } = prepareSalesOrder(input);
return syncSalesOrder({ preparedSalesOrder });
},
It works, thanks a lot! 🙏
Also just to add some information, no need to use the templates for typing, the templates are there to grab and infer all the types based on what you will type the input of the step and what will be returned.
E.g in my playground https://github.com/medusajs/medusa/blob/develop/packages/workflows-sdk/src/utils/_playground.ts
Another note, in a step, the second function is the compensation step, the first argument is mot the error, but it is what is passed as the second argument of the step response. For example you create something, you would like to to have the id of the object that have been created to be reverted in the compensation function.
Also, dont forget that the compensation function is called as soon as the step itself fail or a later step. This is to allow full compensation of a workflow
Bug report
Describe the bug
Hi Team,
Playing around with the newly announced workflow-sdk to integrate medusa with Shipmondo but I am encountering a Typescript issue and I am not able to determine where or if I screwed up.
System information
node: 18.17 database: postgres latest
tsconfig
The code
index.ts
prepare-sales-order.ts
sync-sales-order.ts
I am open to share code regarding shipmondo fulfillment when I am further in the process and if you are interested in sharing it as a community package.