#Error extending the Invite User Service. Any extra pair of eyes will be greatly appreciated?

14 messages · Page 1 of 1 (latest)

warm trail
#

This is my Service Extension

import { Lifetime } from "awilix"
import { InviteService as MedusaInviteService } from "@medusajs/medusa"
import { Invite } from "../models/invite"
import InviteRepository from "../repositories/invite"
import CustomerRepository from "../repositories/customer"
import { UserRoles } from "@medusajs/medusa"
import { MedusaError } from "medusa-core-utils"

const DEFAULT_VALID_DURATION = 1000 * 60 * 60 * 24 * 7

class InviteService extends MedusaInviteService {
static LIFE_TIME = Lifetime.SCOPED

static CustomerInviteEvents = {
CREATED: "customer_invite.created",
}

protected readonly loggedInUser_: Invite | null
protected readonly inviteRepository_: typeof InviteRepository;

constructor(container, options) {
// @ts-expect-error prefer-rest-params
super(...arguments)
this.inviteRepository_ = container.inviteRepository

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

}

async createCustomInvite(
email: string,
validDuration = DEFAULT_VALID_DURATION
): Promise<void> {
}

}
export default InviteService

InviteService is not exported in my local medusa version by default so I added

export { default as InviteService } from "./invite"; to @medusajs/medusa/dist/services/index.ts

#

And the error I am getting ->

1:06:36 AM [@medusajs/admin] Running in development mode. Skipping setup.
error: Error starting server
TypeError: Class extends value undefined is not a constructor or null
at Object.<anonymous> (/Desktop/marketplace/marketplace-backend/dist/services/invite.js:12:38)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)

arctic pilot
#

So multiple things, in the dist it should be invite.js not invite.ts, then in the transpile code it shouldn’t be export { default as … but it should be module.exports = InviteService.
Also, i suggest you to import it from @medusajs/medusa/dist/services/invite àd open a pr instead

warm trail
warm trail
#

The Service Issue has been solved now I am having trouble with the repository

#

import {
dataSource,
} from "@medusajs/medusa/dist/loaders/database"
import {
InviteRepository as MedusaInviteRepository,
} from "@medusajs/medusa/dist/repositories/invite"
import { Invite } from "../models/invite"

export const InviteRepository = dataSource
.getRepository(Invite)
.extend({
...Object.assign(
MedusaInviteRepository,
{ target: Invite }
),
})

export default InviteRepository

#

And here is the error -> Successfully compiled 1 file with Babel (154ms).
--------------- ERROR ---------------------
TypeError: Cannot read properties of undefined (reading 'getRepository')
at Object.<anonymous> (/Users/lxx/Desktop/marketplace/marketplace-backend/node_modules/@medusajs/medusa/dist/repositories/invite.js:6:50)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/lxxx/Desktop/marketplace/marketplace-backend/node_modules/@medusajs/medusa/dist/services/invite.js:85:16)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)

arctic pilot
#

If you are in a plugin trying to link it, Rm the @medusajs directory in your plugin node modules, otherwise use pack as npm link does not prevent dev deps to be used

arctic pilot
#

Maybe it would be easier if you share your repo

warm trail
#

@arctic pilot could you give me a step by step way of making this InviteService extendable in Medusa? I am new to Medusa and would like to just extend the Invite Service to invite customers to a store.

#

I've been blocked on this specific part for weeks now and would appreciate any help

warm trail
icy obsidian
#

I’m going to guess it’s an oversight that it’s not exported in the index, but it’s still exported from the actual service file, so you can deep import it. Just use the full path. And then extend it. In other words, Import from @medusajs/medusa/dist/services/invite or something like that.

warm trail
#

@icy obsidian @arctic pilot thank you for helping out.