feat: Support invites in CLI for V2 (#6798)
**What** - Add invite support to cli for 2.0 - Allow email to be passed upon accepting an invite
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import { acceptInviteWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IUserModuleService, InviteWorkflow } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
|
||||
import { acceptInviteWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IUserModuleService, InviteWorkflow } from "@medusajs/types"
|
||||
import { AdminPostInvitesInviteAcceptReq } from "../validators"
|
||||
|
||||
export const POST = async (
|
||||
@@ -21,8 +20,6 @@ export const POST = async (
|
||||
return
|
||||
}
|
||||
|
||||
const workflow = acceptInviteWorkflow(req.scope)
|
||||
|
||||
const input = {
|
||||
invite_token: req.filterableFields.token as string,
|
||||
auth_user_id: req.auth?.auth_user_id,
|
||||
@@ -31,7 +28,7 @@ export const POST = async (
|
||||
|
||||
let users
|
||||
try {
|
||||
const { result } = await workflow.run({ input })
|
||||
const { result } = await acceptInviteWorkflow(req.scope).run({ input })
|
||||
users = result
|
||||
} catch (e) {
|
||||
res.status(401).json({ message: "Unauthorized" })
|
||||
|
||||
@@ -77,11 +77,6 @@ export class AdminCreateInviteRequest {
|
||||
email: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Details of the use accepting the invite.
|
||||
*/
|
||||
export class AdminPostInvitesInviteAcceptUserReq {}
|
||||
|
||||
/**
|
||||
* @schema AdminPostInvitesInviteAcceptReq
|
||||
* type: object
|
||||
@@ -113,6 +108,13 @@ export class AdminPostInvitesInviteAcceptUserReq {}
|
||||
* format: password
|
||||
*/
|
||||
export class AdminPostInvitesInviteAcceptReq {
|
||||
/**
|
||||
* The invite's first name.
|
||||
* If email is not passed, we default to using the email of the invite.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
email: string
|
||||
/**
|
||||
* The invite's first name.
|
||||
*/
|
||||
|
||||
@@ -11,25 +11,38 @@ import featureFlagLoader from "../loaders/feature-flags"
|
||||
import configModuleLoader from "../loaders/config"
|
||||
import { MedusaV2Flag } from "@medusajs/utils"
|
||||
|
||||
// TEMP: Only supporting emailpass
|
||||
const createV2User = async ({ email, password }, { container }) => {
|
||||
const authService = container.resolve(ModuleRegistrationName.AUTH)
|
||||
const useV2Command = async (
|
||||
{ email, password, isInvite, provider = "emailpass" },
|
||||
{ container }
|
||||
) => {
|
||||
const userService = container.resolve(ModuleRegistrationName.USER)
|
||||
const user = await userService.create({ email })
|
||||
const { authUser } = await authService.authenticate("emailpass", {
|
||||
body: {
|
||||
email,
|
||||
password,
|
||||
},
|
||||
authScope: "admin",
|
||||
})
|
||||
const authService = container.resolve(ModuleRegistrationName.AUTH)
|
||||
|
||||
await authService.update({
|
||||
id: authUser.id,
|
||||
app_metadata: {
|
||||
user_id: user.id,
|
||||
},
|
||||
})
|
||||
if (isInvite) {
|
||||
// The invite flow only works with the V2 version of packages/admin-next/dashboard, so enable the V2 feature flag in admin before using this command
|
||||
const invite = await userService.createInvites({ email })
|
||||
|
||||
Logger.info(`
|
||||
Invite token: ${invite.token}
|
||||
Open the invite in Medusa Admin at: [your-admin-url]/invite?token=${invite.token}`)
|
||||
} else {
|
||||
const user = await userService.create({ email })
|
||||
|
||||
const { authUser } = await authService.authenticate(provider, {
|
||||
body: {
|
||||
email,
|
||||
password,
|
||||
},
|
||||
authScope: "admin",
|
||||
})
|
||||
|
||||
await authService.update({
|
||||
id: authUser.id,
|
||||
app_metadata: {
|
||||
user_id: user.id,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default async function ({
|
||||
@@ -48,21 +61,21 @@ export default async function ({
|
||||
expressApp: app,
|
||||
})
|
||||
|
||||
if (invite) {
|
||||
const inviteService = container.resolve("inviteService")
|
||||
await inviteService.create(email, "admin")
|
||||
const invite = await inviteService.list({
|
||||
user_email: email,
|
||||
})
|
||||
Logger.info(`
|
||||
Invite token: ${invite[0].token}
|
||||
Open the invite in Medusa Admin at: [your-admin-url]/invite?token=${invite[0].token}`)
|
||||
} else {
|
||||
const configModule = configModuleLoader(directory)
|
||||
const featureFlagRouter = featureFlagLoader(configModule)
|
||||
const configModule = configModuleLoader(directory)
|
||||
const featureFlagRouter = featureFlagLoader(configModule)
|
||||
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
await createV2User({ email, password }, { container })
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
await useV2Command({ email, password, isInvite: invite }, { container })
|
||||
} else {
|
||||
if (invite) {
|
||||
const inviteService = container.resolve("inviteService")
|
||||
await inviteService.create(email, "admin")
|
||||
const invite = await inviteService.list({
|
||||
user_email: email,
|
||||
})
|
||||
Logger.info(`
|
||||
Invite token: ${invite[0].token}
|
||||
Open the invite in Medusa Admin at: [your-admin-url]/invite?token=${invite[0].token}`)
|
||||
} else {
|
||||
const userService = container.resolve("userService")
|
||||
await userService.create({ id, email }, password)
|
||||
|
||||
Reference in New Issue
Block a user