#Fetch regions by country

11 messages · Page 1 of 1 (latest)

radiant pollen
#

Hi!

I see Medusa works with regions to determine shipping etc.
Im creating an order portal for a business and I want to use draft orders to start with. I see we need a region ID.
In the UI I make the business select a customer first, then a shipping & billing address. By then, I already know region because a country is known. Which makes me wonder, why is there no filter in the https://api.medusa-commerce.com/admin/regions endpoint to filter by countryCode for example?

Now I have to fetch them all, and do a frontend filter to display the proper regions they can select.

#

Same goes when I want to create an ecommerce frontend for consumers. Tehy would have to select a region. That's uncommon right? They enter their address and then the shipping options appear and they pay.

radiant pollen
#

Anybody?

reef ravine
#

Hi @radiant pollen I would advice you to create a custom endpoint https://docs.medusajs.com/development/endpoints/create and the use the regionService to find the region based on a country code.
It has a retrieveByCountryCode method https://docs.medusajs.com/references/services/classes/RegionService#retrievebycountrycode

Learn how to create endpoints in Medusa. This guide also includes how to add CORS configurations, creating multiple endpoints, adding protected routes, and more.

Provides layer to manipulate regions.

radiant pollen
radiant pollen
#

Hi @reef ravine I've added the custome endpoint like this:

  router.get("/admin/regions/country-code/:code", authenticate(), (req, res) => {
    const regionService = req.scope.resolve("regionService")

    regionService.retrieveByCountryCode(req.params.code)
      .then((region) => {
        res.json({
          region,
        })
      })
      .catch((e) => {
        console.dir(res)
      })
  })

And it works great. However, there's so little documentation about creating custom endpoint.
For example, how do I return a 404 when no region is found (in catch)?

For another example I needed to parse the request body (post request) and it took me very long online to bump across import bodyParser from 'body-parser'. Got that working now but it would be very helpful if the docs mention that.

random bough
#

@radiant pollen It's based on express, you can check express docs

radiant pollen
# random bough <@874338316751888384> It's based on express, you can check express docs

Thanks! Got it!
For anyone interested:

router.get("/admin/regions/country-code/:code", authenticate(), (req, res, next) => {
    const regionService = req.scope.resolve("regionService")

    regionService.retrieveByCountryCode(req.params.code)
      .then((region) => {
        res.json({
          region,
        })
      })
      .catch((e) => {
        console.dir(e)
        // var e = new Error('No region found for country code \'' + req.params.code + '\'.')
        if (e?.message === 'Country does not belong to a region') {
          res.status(404).send({
            error: e?.message,
          })
        } else if (e?.message.includes('not found')) {
          res.status(400).send({
            error: e?.message,
          })
        } else {
          res.status(500).send({
            error: e?.message,
          })
        }
      })
  })
dreamy frigate
#

That would be a good PR, in my opinion. Seems like an endpoint that should be in core.

radiant pollen
#

Yeah I have more examples

#

I have created this draft orders list endpoint:

router.get("/admin/draft-orders/filter", authenticate(), (req, res) => {
    const draftOrderService = req.scope.resolve("draftOrderService")

    const config = {
      skip: req.query.offset ?? 0,
      take: req.query.limit ?? 50,
      order: { created_at: "DESC" },
    }

    const query = req.query
    delete req.query.limit
    delete req.query.offset

    draftOrderService.listAndCount(req.query, config)
      .then((arr) => {
        res.json({
          draft_orders: arr?.[0],
          count: arr?.[1],
          offset: config?.skip,
          limit: config?.take,
        })
      })
      .catch((e) => {
        if (e) {
          res.status(400).send({
            error: e?.message,
          })
        }
      })
  })