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:
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
|
||||
describe("GET /store/:id", () => {
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -7,6 +7,8 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ICustomerModuleService, IAuthModuleService } from "@medusajs/types"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
|
||||
describe("POST /store/customers", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let customerModuleService: ICustomerModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
customerModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should create a customer", async () => {
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
const authUser = await authService.createAuthUser({
|
||||
entity_id: "store_user",
|
||||
provider_id: "test",
|
||||
})
|
||||
const jwt = await authService.generateJwtToken(authUser.id, "store")
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.post(
|
||||
`/store/customers`,
|
||||
{
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
email: "john@me.com",
|
||||
},
|
||||
{ headers: { authorization: `Bearer ${jwt}` } }
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.customer).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
email: "john@me.com",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,78 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ICustomerModuleService, IAuthModuleService } from "@medusajs/types"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
|
||||
describe("GET /store/customers", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let customerModuleService: ICustomerModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
customerModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should retrieve auth user's customer", async () => {
|
||||
const customer = await customerModuleService.create({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
email: "john@me.com",
|
||||
})
|
||||
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
const authUser = await authService.createAuthUser({
|
||||
entity_id: "store_user",
|
||||
provider_id: "test",
|
||||
app_metadata: { customer_id: customer.id },
|
||||
})
|
||||
|
||||
const jwt = await authService.generateJwtToken(authUser.id, "store")
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.get(`/store/customers/me`, {
|
||||
headers: { authorization: `Bearer ${jwt}` },
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.customer).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
email: "john@me.com",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -42,6 +42,14 @@ module.exports = {
|
||||
},
|
||||
},
|
||||
modules: {
|
||||
[Modules.AUTH]: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
resolve: "@medusajs/auth",
|
||||
options: {
|
||||
jwt_secret: "test",
|
||||
},
|
||||
},
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
"build": "babel src -d dist --extensions \".ts,.js\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/auth": "workspace:*",
|
||||
"@medusajs/cache-inmemory": "workspace:*",
|
||||
"@medusajs/customer": "workspace:^",
|
||||
"@medusajs/event-bus-local": "workspace:*",
|
||||
|
||||
@@ -1,22 +1,30 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
import { Migration } from "@mikro-orm/migrations"
|
||||
|
||||
export class Migration20240122041959 extends Migration {
|
||||
|
||||
async up(): Promise<void> {
|
||||
this.addSql('create table if not exists "auth_provider" ("provider" text not null, "name" text not null, "domain" text check ("domain" in (\'all\', \'store\', \'admin\')) not null default \'all\', "config" jsonb null, "is_active" boolean not null default false, constraint "auth_provider_pkey" primary key ("provider"));');
|
||||
this.addSql(
|
||||
'create table if not exists "auth_provider" ("provider" text not null, "name" text not null, "domain" text check ("domain" in (\'all\', \'store\', \'admin\')) not null default \'all\', "config" jsonb null, "is_active" boolean not null default false, constraint "auth_provider_pkey" primary key ("provider"));'
|
||||
)
|
||||
|
||||
this.addSql('create table if not exists "auth_user" ("id" text not null, "entity_id" text not null, "provider_id" text null, "user_metadata" jsonb null, "app_metadata" jsonb null, "provider_metadata" jsonb null, constraint "auth_user_pkey" primary key ("id"));');
|
||||
this.addSql('alter table "auth_user" add constraint "IDX_auth_user_provider_entity_id" unique ("provider_id", "entity_id");');
|
||||
this.addSql(
|
||||
'create table if not exists "auth_user" ("id" text not null, "entity_id" text not null, "provider_id" text null, "user_metadata" jsonb null, "app_metadata" jsonb null, "provider_metadata" jsonb null, constraint "auth_user_pkey" primary key ("id"));'
|
||||
)
|
||||
this.addSql(
|
||||
'alter table "auth_user" add constraint "IDX_auth_user_provider_entity_id" unique ("provider_id", "entity_id");'
|
||||
)
|
||||
|
||||
this.addSql('alter table "auth_user" add constraint if not exists "auth_user_provider_id_foreign" foreign key ("provider_id") references "auth_provider" ("provider") on delete cascade;');
|
||||
this.addSql(
|
||||
'alter table "auth_user" add constraint "auth_user_provider_id_foreign" foreign key ("provider_id") references "auth_provider" ("provider") on delete cascade;'
|
||||
)
|
||||
}
|
||||
|
||||
async down(): Promise<void> {
|
||||
this.addSql('alter table "auth_user" drop constraint if exists "auth_user_provider_id_foreign";');
|
||||
this.addSql(
|
||||
'alter table "auth_user" drop constraint if exists "auth_user_provider_id_foreign";'
|
||||
)
|
||||
|
||||
this.addSql('drop table if exists "auth_provider" cascade;');
|
||||
this.addSql('drop table if exists "auth_provider" cascade;')
|
||||
|
||||
this.addSql('drop table if exists "auth_user" cascade;');
|
||||
this.addSql('drop table if exists "auth_user" cascade;')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
1
packages/core-flows/src/auth/index.ts
Normal file
1
packages/core-flows/src/auth/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./steps"
|
||||
1
packages/core-flows/src/auth/steps/index.ts
Normal file
1
packages/core-flows/src/auth/steps/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./set-auth-app-metadata"
|
||||
59
packages/core-flows/src/auth/steps/set-auth-app-metadata.ts
Normal file
59
packages/core-flows/src/auth/steps/set-auth-app-metadata.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { isDefined } from "@medusajs/utils"
|
||||
|
||||
type StepInput = {
|
||||
authUserId: string
|
||||
key: string
|
||||
value: string
|
||||
}
|
||||
|
||||
export const setAuthAppMetadataStepId = "set-auth-app-metadata"
|
||||
export const setAuthAppMetadataStep = createStep(
|
||||
setAuthAppMetadataStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
const service = container.resolve<IAuthModuleService>(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const authUser = await service.retrieveAuthUser(data.authUserId)
|
||||
|
||||
const appMetadata = authUser.app_metadata || {}
|
||||
if (isDefined(appMetadata[data.key])) {
|
||||
throw new Error(`Key ${data.key} already exists in app metadata`)
|
||||
}
|
||||
|
||||
appMetadata[data.key] = data.value
|
||||
|
||||
await service.updateAuthUser({
|
||||
id: authUser.id,
|
||||
app_metadata: appMetadata,
|
||||
})
|
||||
|
||||
return new StepResponse(authUser, { id: authUser.id, key: data.key })
|
||||
},
|
||||
async (idAndKey, { container }) => {
|
||||
if (!idAndKey) {
|
||||
return
|
||||
}
|
||||
|
||||
const { id, key } = idAndKey
|
||||
|
||||
const service = container.resolve<IAuthModuleService>(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const authUser = await service.retrieveAuthUser(id)
|
||||
|
||||
const appMetadata = authUser.app_metadata || {}
|
||||
if (isDefined(appMetadata[key])) {
|
||||
delete appMetadata[key]
|
||||
}
|
||||
|
||||
await service.updateAuthUser({
|
||||
id: authUser.id,
|
||||
app_metadata: appMetadata,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -4,9 +4,9 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
type DeleteCustomerStepInput = string[]
|
||||
|
||||
export const deleteCustomerStepId = "delete-customer"
|
||||
export const deleteCustomerStep = createStep(
|
||||
deleteCustomerStepId,
|
||||
export const deleteCustomersStepId = "delete-customers"
|
||||
export const deleteCustomersStep = createStep(
|
||||
deleteCustomersStepId,
|
||||
async (ids: DeleteCustomerStepInput, { container }) => {
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { CreateCustomerDTO, CustomerDTO } from "@medusajs/types"
|
||||
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||
import { createCustomersStep } from "../steps"
|
||||
import { setAuthAppMetadataStep } from "../../auth/steps"
|
||||
import { transform } from "@medusajs/workflows-sdk"
|
||||
|
||||
type WorkflowInput = {
|
||||
authUserId: string
|
||||
customersData: CreateCustomerDTO
|
||||
}
|
||||
|
||||
export const createCustomerAccountWorkflowId = "create-customer-account"
|
||||
export const createCustomerAccountWorkflow = createWorkflow(
|
||||
createCustomerAccountWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerDTO> => {
|
||||
const customers = createCustomersStep([input.customersData])
|
||||
|
||||
const customer = transform(
|
||||
customers,
|
||||
(customers: CustomerDTO[]) => customers[0]
|
||||
)
|
||||
|
||||
setAuthAppMetadataStep({
|
||||
authUserId: input.authUserId,
|
||||
key: "customer_id",
|
||||
value: customer.id,
|
||||
})
|
||||
|
||||
return customer
|
||||
}
|
||||
)
|
||||
@@ -1,5 +1,5 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteCustomerStep } from "../steps"
|
||||
import { deleteCustomersStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
@@ -7,6 +7,6 @@ export const deleteCustomersWorkflowId = "delete-customers"
|
||||
export const deleteCustomersWorkflow = createWorkflow(
|
||||
deleteCustomersWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteCustomerStep(input.ids)
|
||||
return deleteCustomersStep(input.ids)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export * from "./create-customers"
|
||||
export * from "./update-customers"
|
||||
export * from "./delete-customers"
|
||||
export * from "./create-customer-account"
|
||||
export * from "./create-addresses"
|
||||
export * from "./update-addresses"
|
||||
export * from "./delete-addresses"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { MiddlewaresConfig } from "../loaders/helpers/routing/types"
|
||||
import { adminCampaignRoutesMiddlewares } from "./admin/campaigns/middlewares"
|
||||
import { adminCustomerGroupRoutesMiddlewares } from "./admin/customer-groups/middlewares"
|
||||
import { storeCustomerRoutesMiddlewares } from "./store/customers/middlewares"
|
||||
import { adminCustomerRoutesMiddlewares } from "./admin/customers/middlewares"
|
||||
import { adminPromotionRoutesMiddlewares } from "./admin/promotions/middlewares"
|
||||
import { storeCartRoutesMiddlewares } from "./store/carts/middlewares"
|
||||
@@ -11,6 +12,7 @@ export const config: MiddlewaresConfig = {
|
||||
...adminCustomerRoutesMiddlewares,
|
||||
...adminPromotionRoutesMiddlewares,
|
||||
...adminCampaignRoutesMiddlewares,
|
||||
...storeCustomerRoutesMiddlewares,
|
||||
...storeCartRoutesMiddlewares,
|
||||
],
|
||||
}
|
||||
|
||||
15
packages/medusa/src/api-v2/store/customers/me/route.ts
Normal file
15
packages/medusa/src/api-v2/store/customers/me/route.ts
Normal file
@@ -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 })
|
||||
}
|
||||
28
packages/medusa/src/api-v2/store/customers/middlewares.ts
Normal file
28
packages/medusa/src/api-v2/store/customers/middlewares.ts
Normal file
@@ -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
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
24
packages/medusa/src/api-v2/store/customers/query-config.ts
Normal file
24
packages/medusa/src/api-v2/store/customers/query-config.ts
Normal file
@@ -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,
|
||||
}
|
||||
14
packages/medusa/src/api-v2/store/customers/route.ts
Normal file
14
packages/medusa/src/api-v2/store/customers/route.ts
Normal file
@@ -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 })
|
||||
}
|
||||
29
packages/medusa/src/api-v2/store/customers/validators.ts
Normal file
29
packages/medusa/src/api-v2/store/customers/validators.ts
Normal file
@@ -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>
|
||||
}
|
||||
@@ -7889,7 +7889,7 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@medusajs/auth@workspace:packages/auth":
|
||||
"@medusajs/auth@workspace:*, @medusajs/auth@workspace:packages/auth":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@medusajs/auth@workspace:packages/auth"
|
||||
dependencies:
|
||||
@@ -31377,6 +31377,7 @@ __metadata:
|
||||
"@babel/cli": ^7.12.10
|
||||
"@babel/core": ^7.12.10
|
||||
"@babel/node": ^7.12.10
|
||||
"@medusajs/auth": "workspace:*"
|
||||
"@medusajs/cache-inmemory": "workspace:*"
|
||||
"@medusajs/customer": "workspace:^"
|
||||
"@medusajs/event-bus-local": "workspace:*"
|
||||
|
||||
Reference in New Issue
Block a user