feat(core-flows,medusa): Add API to update cart's customer (#10151)

what:

- adds an endpoint that updates a cart's customer

RESOLVES CMRC-718
This commit is contained in:
Riqwan Thamir
2024-11-19 12:44:25 +01:00
committed by GitHub
parent 41dc05d0c9
commit b7044bb3b0
13 changed files with 462 additions and 101 deletions

View File

@@ -417,7 +417,8 @@ medusaIntegrationTestRunner({
it("should create cart with logged-in customer", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
appContainer
api,
storeHeaders
)
const response = await api.post(

View File

@@ -1,6 +1,6 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ICustomerModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
generatePublishableKey,
generateStoreHeaders,
@@ -31,7 +31,8 @@ medusaIntegrationTestRunner({
it("should create a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
appContainer
api,
storeHeaders
)
const response = await api.post(

View File

@@ -1,6 +1,6 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ICustomerModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
generatePublishableKey,
generateStoreHeaders,
@@ -32,7 +32,8 @@ medusaIntegrationTestRunner({
it("should delete a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
appContainer
api,
storeHeaders
)
const address = await customerModuleService.createCustomerAddresses({
@@ -65,7 +66,7 @@ medusaIntegrationTestRunner({
})
it("should fail to delete another customer's address", async () => {
const { jwt } = await createAuthenticatedCustomer(appContainer)
const { jwt } = await createAuthenticatedCustomer(api, storeHeaders)
const otherCustomer = await customerModuleService.createCustomers({
first_name: "Jane",

View File

@@ -28,7 +28,8 @@ medusaIntegrationTestRunner({
it("should retrieve auth user's customer", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
appContainer
api,
storeHeaders
)
const response = await api.get(`/store/customers/me`, {
@@ -41,7 +42,7 @@ medusaIntegrationTestRunner({
id: customer.id,
first_name: "John",
last_name: "Doe",
email: "john@me.com",
email: "tony@start.com",
})
)
})

View File

@@ -1,6 +1,6 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ICustomerModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
generatePublishableKey,
generateStoreHeaders,
@@ -32,7 +32,8 @@ medusaIntegrationTestRunner({
it("should get all customer addresses and its count", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
appContainer
api,
storeHeaders
)
await customerModuleService.createCustomerAddresses([

View File

@@ -1,6 +1,6 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ICustomerModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
generatePublishableKey,
generateStoreHeaders,
@@ -32,7 +32,8 @@ medusaIntegrationTestRunner({
it.only("should update a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
appContainer
api,
storeHeaders
)
const address = await customerModuleService.createCustomerAddresses({
@@ -66,7 +67,7 @@ medusaIntegrationTestRunner({
})
it("should fail to update another customer's address", async () => {
const { jwt } = await createAuthenticatedCustomer(appContainer)
const { jwt } = await createAuthenticatedCustomer(api, storeHeaders)
const otherCustomer = await customerModuleService.createCustomers({
first_name: "Jane",

View File

@@ -1,43 +1,39 @@
import { CreateCustomerDTO, MedusaContainer } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import jwt from "jsonwebtoken"
import { CreateCustomerDTO } from "@medusajs/types"
export const createAuthenticatedCustomer = async (
appContainer: MedusaContainer,
api: any,
storeHeaders: Record<any, any>,
customerData: Partial<CreateCustomerDTO> = {}
) => {
const { http } = appContainer.resolve("configModule").projectConfig
const authService = appContainer.resolve(Modules.AUTH)
const customerModuleService = appContainer.resolve(Modules.CUSTOMER)
const customer = await customerModuleService.createCustomers({
first_name: "John",
last_name: "Doe",
email: "john@me.com",
...customerData,
const email = customerData.email ?? "tony@start.com"
const signup = await api.post("/auth/customer/emailpass/register", {
email,
password: "secret_password",
})
const authIdentity = await authService.createAuthIdentities({
provider_identities: [
{
entity_id: "store_user",
provider: "emailpass",
},
],
app_metadata: {
customer_id: customer.id,
},
})
const token = jwt.sign(
const {
data: { customer },
} = await api.post(
"/store/customers",
{
actor_id: customer.id,
actor_type: "customer",
auth_identity_id: authIdentity.id,
email,
first_name: "John",
last_name: "Doe",
metadata: {},
...customerData,
},
http.jwtSecret
{
headers: {
authorization: `Bearer ${signup.data.token}`,
...storeHeaders.headers,
},
}
)
return { customer, authIdentity, jwt: token }
const signin = await api.post("/auth/customer/emailpass", {
email,
password: "secret_password",
})
return { customer, jwt: signin.data.token }
}