#Overriding Enum

10 messages · Page 1 of 1 (latest)

bright field
#

Hey everyone! I have been trying to override the UserRoles enum and add a new role here. I am also using Medusa Extender. I have the migration all working, and I have created the index.d.ts file in the user module to tell the core about the change, it looks like this:

import { UserRoles } from '@medusajs/medusa';

declare module '@medusajs/medusa/dist/models/user' {
    declare enum UserRoles {
        ADMIN = "admin",
    MEMBER = "member",
    DEVELOPER = "developer",
      CONTENT_CREATOR = "content_creator"
    }
}

However, for some reason I am still not able to create a new user with role: content_creator because I get the following error:

Error: role must be a valid enum value

I tried adding a user.validator.ts file to override the role field in the AdminCreateUserRequest class, but it didn't really get me anywhere. Anyone have experience modifying enums with Medusa Extender?

crystal flax
#

Can you share your migrations and the entire error stack please

hardy cradle
bright field
#

@crystal flax thanks for the reply man! I was actually able to get it figured out haha

zealous citrus
bright field
#

@zealous citrus Hey! Yeah for sure, so the way it has to be done is by adding a compiled type to tell the core that the UserRoles enum is different. So in the user module that I created, I added a file named index.d.ts and added the following:

declare module '@medusajs/medusa/dist/models/user' {
    export declare enum UserRoles {
        ADMIN = "admin",
    MEMBER = "member",
    DEVELOPER = "developer",
      NEW_ROLE = "new_role"
    }
}

Then just replace NEW_ROLE with whatever your new role is.

dreamy sequoia
#

You could also do this

user.entity.ts

export enum UserRolesExtended {
  SUPER_ADMIN = "super_admin",
  STORE_ADMIN = "store_admin",
  BRANCH_MANAGER = "branch_manager",
  BRANCH_STAFF = "branch_staff",
}

@MedusaEntity({ override: MedusaUser })
@Entity()
export class User extends Omit(MedusaUser, ["role"]) {
  @Column({ nullable: true, default: UserRolesExtended.BRANCH_STAFF })
  role: UserRolesExtended;
}
shy stump
shy stump
chrome minnow
#

Can someone help me also.. how do I extend enum