#How do I add two controller to one route ?

120 messages · Page 1 of 1 (latest)

lethal plaza
#

regarding medusa-extender I need to run a specific function before running a medusa core controller is there a way to do that ?

hard fjord
#

You can create a middleware or a route with just the handler

lethal plaza
#

how would it be written in case of handler ?

#

also how can I call a medusa core controller ?

#

inside hanndlers array

hard fjord
#

Yes you can, but the only thing you need to add in your function is to call next for the next handler to be called i.e the one from medusa

#

I think the easier way is to write a middleware

#

You don’t have to call the original handler yiuself

#

In both cases, you call next in your handler and express will move to the next handler for that route normally

lethal plaza
#

in POST /carts/{id}/complete if I need to add store_id value for Order, how do I approach this situation ?

#

I haven't overriden the function

#

I did a migration on the Order entity

#

and added column store_id I just need to add the store_id value to Order after calling this api

hard fjord
#

You could add the store id on the cart metadata which are assigned to the order metadata. Then you could create a subscriber on the placed event to assign it to the order

#

When you want to do such customization, i suggest you to look at the core implementation. That way you can find multiple solutions and just use the one that fits you best 😀 and you can learn 1-2 things on the way

lethal plaza
#

yeah I was searching on github

#

for the code

hard fjord
#

You can look at the order service createFromCart

lethal plaza
#

I thought maybe I can copy paste what's on github and add what needs to be added

hard fjord
#

Only for heavy customisation as it comes with some drawbacks

#

Most of the times it is not needed, it is a bad practice and only applicable in case where you want to do some heavy customisation

lethal plaza
#

aha

#

is it possible to use decorators beforeInsert on order?

hard fjord
#

Which is why it requires to be advanced in programming to avoid accumulating bad practices and ending up with something that conflicts all the time or become hard to scale or evolve 😅

#

Yes sure 👌

#

That is also a good idea 👌🤘

lethal plaza
#

how would I be able to read the request tho?

#

to fetch it

hard fjord
#

Good point

#

Before insert you don’t need the request if the store id is in the cart metadata

#

I am in vacantion so ill not be able to help a lot

lethal plaza
#

this is how I was doing it

#

ahhhh

#

on create cart

#

i just add to metadata column {store_id:1231}

hard fjord
#

Yes exactly

lethal plaza
#

then in beforeInsert in order I refetch the cart using retrieve and do what is needed?

#

or am I complicating it ?

lethal plaza
hard fjord
#

No in before insert the cart_id will be on the order metadata so you just re assign it on the order as well

#

No need to re fetch it

lethal plaza
#

ah ok

#

thanks man

#

have a wonderful vacation

#

and merry christmas

#

❤️

hard fjord
#

Thanks a lot you too 😍

lethal plaza
#

ok last question

#

now I can see in the db that the store_id is saving in the metadata of order entity

#

how to assign it ?

hard fjord
#

You can look at the doc for that 😅

lethal plaza
#

aight

lethal plaza
#

emitAsync(Cart ?

#

<InsertEvent<Cart>> ?

hard fjord
#

Order, since you want to listen before insert of the order

lethal plaza
#

weird cause in doc there are product and User

hard fjord
#

It is a typo 😂 as you might notice ahah

lethal plaza
#

ah ok

hard fjord
#

If you can either open an issue or a pr it would be cool 😀

lethal plaza
#

will do regarding it no worries

#

this is the metadata inside my cart

#

I still kinda can't understand how to read the data from the metadata :p

hard fjord
#

What do you mean?

#

You can do your order.store_id = order.metadata.store_id or something similar depending on your needs

#

It is just an object ahah

lethal plaza
#

this is what I was trying to do

hard fjord
#

Your are stringifying the metadata therefore it is a string. Why are you doing that?

lethal plaza
#

stringify is wrong

#

yeah yeah ik

#

by accident

#

I removed it

#

I just need the line that assigns store_id for order

#

I was trying to log but somehow it isnt logging in the terminal as if its not getting there

#

do I need to include subscribers in my order module ?

hard fjord
#

Yes you do

#

Oh sorry you mean the event subscriber ?

#

If yes then no you don’t. But did you add the little snippet in the service constructor?

lethal plaza
lethal plaza
#

attach(

#

yes

#

ok so its logging correctly

#

but I just need to know how to assign it to the order

#

what do I do

#

:p ?

#

the error is because

#

Record <String, unknown>

hard fjord
#

as string 😅

lethal plaza
#

store_id has an unknown type in metadata

#

I mean I could insert it in a variable of type any but I don't think I would solve any issue

hard fjord
#

Yes so cast it to a string since you know it is a string

lethal plaza
#

just + "" ?

hard fjord
#

Or just - as string - as i said

#

Or you can look at the ts doc ahah

#

Those are more related to ts than medusa or the extender

lethal plaza
#

shouldnt the record have type of String, any ?

hard fjord
#

No since since we don’t know it is better to type as unknown

#

It forces you to type it with what you know it is or to check the type of it

#

With any, it could be an object and you wouldn’t even know that you are assigning an object to a string

lethal plaza
#

fair enough

#

keeping it abstract

#

just didnt think of casting it tbh :p

hard fjord
#

Not abstract just unknown ahah

lethal plaza
#

no no

#

I meant like keeping the record type

#

sort of

#

abstract in some way

hard fjord
#

Ahah not the right word

lethal plaza
#

let the user give the type he wants

#

XD

#

que pensez vous du ChatGPT ?

hard fjord
#

I am in holiday so ill answer later to this question ahah

lethal plaza
#

deal :p

#

I would love to hear your opinion

#

you are a wise person