feat(user, types): add invite and user properties + migration (#6327)
**What** - Add invite model - Populate user model
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { deleteUsersWorkflow, updateUsersWorkflow } from "@medusajs/core-flows"
|
||||
import { IUserModuleService, UpdateUserDTO } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "../../../../../../modules-sdk/dist"
|
||||
import { AdminUpdateUserRequest } from "../validators"
|
||||
|
||||
// Get user
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id } = req.params
|
||||
|
||||
const moduleService: IUserModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
const user = await moduleService.retrieve(id, req.retrieveConfig)
|
||||
|
||||
res.status(200).json({ user })
|
||||
}
|
||||
|
||||
// update user
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const workflow = updateUsersWorkflow(req.scope)
|
||||
|
||||
const input = {
|
||||
updates: [
|
||||
{
|
||||
id: req.params.id,
|
||||
...(req.validatedBody as AdminUpdateUserRequest),
|
||||
} as UpdateUserDTO,
|
||||
],
|
||||
}
|
||||
|
||||
const { result } = await workflow.run({ input })
|
||||
|
||||
const [user] = result
|
||||
|
||||
res.status(200).json({ user })
|
||||
}
|
||||
|
||||
// delete user
|
||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id } = req.params
|
||||
const workflow = deleteUsersWorkflow(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: "user",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import {
|
||||
AdminCreateUserRequest,
|
||||
AdminGetUsersParams,
|
||||
AdminGetUsersUserParams,
|
||||
AdminUpdateUserRequest,
|
||||
} from "./validators"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import { MiddlewareRoute } from "../../../types/middlewares"
|
||||
|
||||
export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/users",
|
||||
middlewares: [
|
||||
transformQuery(AdminGetUsersParams, QueryConfig.listTransformQueryConfig),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/users",
|
||||
middlewares: [transformBody(AdminCreateUserRequest)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/users/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetUsersUserParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/users/:id",
|
||||
middlewares: [transformBody(AdminUpdateUserRequest)],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
export const defaultAdminUserRelations = []
|
||||
export const allowedAdminUserRelations = []
|
||||
export const defaultAdminUserFields = [
|
||||
"id",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"email",
|
||||
"avatar_url",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminUserFields,
|
||||
defaultRelations: defaultAdminUserRelations,
|
||||
allowedRelations: allowedAdminUserRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { createUsersWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateUserDTO } from "@medusajs/types"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "user",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
order: req.listConfig.order,
|
||||
skip: req.listConfig.skip,
|
||||
take: req.listConfig.take,
|
||||
},
|
||||
fields: req.listConfig.select as string[],
|
||||
})
|
||||
|
||||
const { rows: users, metadata } = await remoteQuery({
|
||||
...query,
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
users,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const workflow = createUsersWorkflow(req.scope)
|
||||
|
||||
const input = {
|
||||
input: {
|
||||
users: [req.validatedBody as CreateUserDTO],
|
||||
},
|
||||
}
|
||||
|
||||
const { result } = await workflow.run(input)
|
||||
|
||||
const [user] = result
|
||||
res.status(200).json({ user })
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
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 AdminGetUsersUserParams extends FindParams {}
|
||||
|
||||
export class AdminGetUsersParams extends extendedFindParamsMixin({
|
||||
limit: 50,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* IDs to filter users 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 users' `update_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
updated_at?: DateComparisonOperator
|
||||
|
||||
/**
|
||||
* Date filters to apply on the customer users' `created_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
created_at?: DateComparisonOperator
|
||||
|
||||
/**
|
||||
* Date filters to apply on the users' `deleted_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
deleted_at?: DateComparisonOperator
|
||||
|
||||
/**
|
||||
* Filter to apply on the users' `email` field.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
email?: string
|
||||
|
||||
/**
|
||||
* Filter to apply on the users' `first_name` field.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
first_name?: string
|
||||
|
||||
/**
|
||||
* Filter to apply on the users' `last_name` field.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
last_name?: string
|
||||
|
||||
/**
|
||||
* Comma-separated fields that should be included in the returned users.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
fields?: string
|
||||
}
|
||||
|
||||
export class AdminCreateUserRequest {
|
||||
@IsEmail()
|
||||
email: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
first_name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
last_name?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
avatar_url: string
|
||||
}
|
||||
|
||||
export class AdminUpdateUserRequest {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
first_name?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
last_name?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
avatar_url: string
|
||||
}
|
||||
Reference in New Issue
Block a user