Medusa Marketplace #2.1 | Extending StoreService

Medusa Marketplace #2.1 | Extending StoreService

·

2 min read

Hello everyone!

In this part of our Medusa.js marketplace journey, we'll continue building on the groundwork we established in the previous sections. This time, our focus will be on extending the StoreService and ProductService to ensure a seamless user experience.

What is the goal here ?

The core objective here is to guarantee that when a user logs into the admin ui, they can seamlessly access their own store based on their store_id. This level of personalization is crucial for providing a tailored experience within our marketplace. As you've noted, the approach involves extending the existing services to achieve this functionality. Let's dive into the details.

Extend the Store Service

We'll create a new file in the services folder, named store.ts, where we'll extend the StoreService to override the retrieve function.

// src/services/store.ts
import { type FindConfig, StoreService as MedusaStoreService, buildQuery } from "@medusajs/medusa"
import { Lifetime } from 'awilix'

import type { User } from "../models/user"
import type { Store } from "../models/store"

class StoreService extends MedusaStoreService {
    static LIFE_TIME = Lifetime.TRANSIENT
    protected readonly loggedInUser_: User | null

    constructor(container, options) {
        // @ts-ignore
        super(...arguments)

        try {
            this.loggedInUser_ = container.loggedInUser
        } catch (e) {
            // avoid errors when backend first runs
        }
    }

     async retrieve(config?: FindConfig<Store>): Promise<Store> {
            if (!this.loggedInUser_ || !this.loggedInUser_.store_id) {
                // In case there is no loggedInUser or no store_id, 
                // we just use the original function 
                // that retrieves the default store
                return await super.retrieve(config)
            }

            const storeRepo = this.activeManager_.withRepository(this.storeRepository_)

            const query = buildQuery<Partial<Store>, Store>(
                {
                    id: this.loggedInUser_.store_id,
                },
                {
                    ...config,
                    relations: [...(config?.relations || []), 'members'],
                },
            )

            return await storeRepo.findOne(query)
        }
}

export default StoreService

In this extended StoreService, we override the retrieve method to check if the logged-in user is defined and have a store_id associated with their account. If so, we use that store_id to retrieve that specific store information. Otherwise, we fall back to the default behavior of the StoreService (which will just use the default store created by the Medusa core loaders)

💡
I recommend that you adjust the store names you currently have in your database so that when you log in with a vendor account, you see the correct shop name. In reality, the default store name is Medusa Store, thus it might be easier for you to have alternative names so you don't struggle with it.

What's Next ?

Well done, now that the right store is displayed, we will extend the products in the next part!

GitHub Branch

You can access the complete part's code here.

Contact

You can contact me on Discord and X with the same username : @adevinwild

Did you find this article valuable?

Support perseides by becoming a sponsor. Any amount is appreciated!