feat(customer): Add create and retrieve customer from store side (#6267)
**What** - GET /store/customers/me - POST /store/customers - Workflow for customer account creation - Authentication middleware on customer routes
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const id = req.auth_user!.app_metadata.customer_id
|
||||
|
||||
const customerModule = req.scope.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
|
||||
const customer = await customerModule.retrieve(id, {
|
||||
select: req.retrieveConfig.select,
|
||||
relations: req.retrieveConfig.relations,
|
||||
})
|
||||
|
||||
res.json({ customer })
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { StorePostCustomersReq, StoreGetCustomersMeParams } from "./validators"
|
||||
import authenticate from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
export const storeCustomerRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: "ALL",
|
||||
matcher: "/store/customers*",
|
||||
middlewares: [authenticate("store", ["session", "bearer"])],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/customers",
|
||||
middlewares: [transformBody(StorePostCustomersReq)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/store/customers/me",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
StoreGetCustomersMeParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,24 @@
|
||||
import { CustomerDTO } from "@medusajs/types"
|
||||
|
||||
export const defaultStoreCustomersRelations = []
|
||||
export const allowedStoreCustomersRelations = ["addresses", "groups"]
|
||||
export const defaultStoreCustomersFields: (keyof CustomerDTO)[] = [
|
||||
"id",
|
||||
"email",
|
||||
"company_name",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"phone",
|
||||
"metadata",
|
||||
"created_by",
|
||||
"deleted_at",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultStoreCustomersFields,
|
||||
defaultRelations: defaultStoreCustomersRelations,
|
||||
allowedRelations: allowedStoreCustomersRelations,
|
||||
isList: false,
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { createCustomerAccountWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateCustomerDTO } from "@medusajs/types"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const createCustomers = createCustomerAccountWorkflow(req.scope)
|
||||
const customersData = req.validatedBody as CreateCustomerDTO
|
||||
|
||||
const { result } = await createCustomers.run({
|
||||
input: { customersData, authUserId: req.auth_user!.id },
|
||||
})
|
||||
|
||||
res.status(200).json({ customer: result })
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { IsEmail, IsObject, IsOptional, IsString } from "class-validator"
|
||||
import { FindParams } from "../../../types/common"
|
||||
|
||||
export class StoreGetCustomersMeParams extends FindParams {}
|
||||
|
||||
export class StorePostCustomersReq {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
first_name: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
last_name: string
|
||||
|
||||
@IsEmail()
|
||||
email: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
phone?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
company_name?: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
Reference in New Issue
Block a user