feat(medusa, types, core-flows): Add invite endpoints for api-v2 (#6395)
**What** - Add invite endpoints for simple operations on invites
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { deleteInvitesWorkflow } from "@medusajs/core-flows"
|
||||
import { IUserModuleService, UpdateUserDTO } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "../../../../../../modules-sdk/dist"
|
||||
|
||||
// Get invite
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id } = req.params
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "invite",
|
||||
variables: {
|
||||
id,
|
||||
},
|
||||
fields: req.retrieveConfig.select as string[],
|
||||
})
|
||||
|
||||
const [invite] = await remoteQuery(query)
|
||||
|
||||
if (!invite) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Invite with id: ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
res.status(200).json({ invite })
|
||||
}
|
||||
|
||||
// delete invite
|
||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id } = req.params
|
||||
const workflow = deleteInvitesWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "invite",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import {
|
||||
AdminCreateInviteRequest,
|
||||
AdminGetInvitesParams,
|
||||
AdminGetInvitesInviteParams,
|
||||
} from "./validators"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import { MiddlewareRoute } from "../../../types/middlewares"
|
||||
|
||||
export const adminInviteRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/invites",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetInvitesParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/invites",
|
||||
middlewares: [transformBody(AdminCreateInviteRequest)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/invites/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetInvitesInviteParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
export const defaultAdminInviteRelations = []
|
||||
export const allowedAdminInviteRelations = []
|
||||
export const defaultAdminInviteFields = [
|
||||
"id",
|
||||
"email",
|
||||
"accepted",
|
||||
"token",
|
||||
"expires_at",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminInviteFields,
|
||||
defaultRelations: defaultAdminInviteRelations,
|
||||
allowedRelations: allowedAdminInviteRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { createInvitesWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateInviteDTO, CreateUserDTO } from "@medusajs/types"
|
||||
|
||||
// List invites
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "invite",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
order: req.listConfig.order,
|
||||
skip: req.listConfig.skip,
|
||||
take: req.listConfig.take,
|
||||
},
|
||||
fields: req.listConfig.select as string[],
|
||||
})
|
||||
|
||||
const { rows: invites, metadata } = await remoteQuery({
|
||||
...query,
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
invites,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
// Create invite
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const workflow = createInvitesWorkflow(req.scope)
|
||||
|
||||
const input = {
|
||||
input: {
|
||||
invites: [req.validatedBody as CreateInviteDTO],
|
||||
},
|
||||
}
|
||||
|
||||
const { result } = await workflow.run(input)
|
||||
|
||||
const [invite] = result
|
||||
res.status(200).json({ invite })
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { Type } from "class-transformer"
|
||||
import { IsEmail, IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import {
|
||||
DateComparisonOperator,
|
||||
FindParams,
|
||||
extendedFindParamsMixin,
|
||||
} from "../../../types/common"
|
||||
import { IsType } from "../../../utils"
|
||||
|
||||
export class AdminGetInvitesInviteParams extends FindParams {}
|
||||
|
||||
export class AdminGetInvitesParams extends extendedFindParamsMixin({
|
||||
limit: 50,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* IDs to filter invites by.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsType([String, [String]])
|
||||
id?: string | string[]
|
||||
|
||||
/**
|
||||
* The field to sort the data by. By default, the sort order is ascending. To change the order to descending, prefix the field name with `-`.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
order?: string
|
||||
|
||||
/**
|
||||
* Date filters to apply on the invites' `update_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
updated_at?: DateComparisonOperator
|
||||
|
||||
/**
|
||||
* Date filters to apply on the customer invites' `created_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
created_at?: DateComparisonOperator
|
||||
|
||||
/**
|
||||
* Date filters to apply on the invites' `deleted_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
deleted_at?: DateComparisonOperator
|
||||
|
||||
/**
|
||||
* Filter to apply on the invites' `email` field.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
email?: string
|
||||
|
||||
/**
|
||||
* Comma-separated fields that should be included in the returned invites.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
fields?: string
|
||||
}
|
||||
|
||||
export class AdminCreateInviteRequest {
|
||||
@IsEmail()
|
||||
email: string
|
||||
}
|
||||
Reference in New Issue
Block a user