feat: Cart Customer link + create cart with customer (#6426)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
StoreGetCartsCartParams,
|
||||
@@ -8,6 +9,15 @@ import {
|
||||
} from "./validators"
|
||||
|
||||
export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: "ALL",
|
||||
matcher: "/store/carts*",
|
||||
middlewares: [
|
||||
authenticate("store", ["session", "bearer"], {
|
||||
allowUnauthenticated: true,
|
||||
}),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/store/carts/:id",
|
||||
|
||||
@@ -10,6 +10,8 @@ export const defaultStoreCartFields = [
|
||||
"items.title",
|
||||
"items.quantity",
|
||||
"items.unit_price",
|
||||
"customer.id",
|
||||
"customer.email",
|
||||
"shipping_address.id",
|
||||
"shipping_address.first_name",
|
||||
"shipping_address.last_name",
|
||||
@@ -39,6 +41,7 @@ export const defaultStoreCartFields = [
|
||||
export const defaultStoreCartRelations = [
|
||||
"items",
|
||||
"region",
|
||||
"customer",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"shipping_methods",
|
||||
@@ -47,6 +50,7 @@ export const defaultStoreCartRelations = [
|
||||
export const allowedRelations = [
|
||||
"items",
|
||||
"region",
|
||||
"customer",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"shipping_methods",
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
import { createCartWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateCartDTO } from "@medusajs/types"
|
||||
import { CreateCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { defaultStoreCartFields } from "../carts/query-config"
|
||||
import { StorePostCartReq } from "./validators"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const workflow = createCartWorkflow(req.scope)
|
||||
const input = req.validatedBody as StorePostCartReq
|
||||
const workflowInput: CreateCartWorkflowInputDTO = {
|
||||
...input,
|
||||
}
|
||||
|
||||
const { result, errors } = await workflow.run({
|
||||
input: req.validatedBody as CreateCartDTO,
|
||||
// If the customer is logged in, we auto-assign them to the cart
|
||||
if (req.auth_user?.app_metadata?.customer_id) {
|
||||
workflowInput.customer_id = req.auth_user!.app_metadata?.customer_id
|
||||
}
|
||||
|
||||
const { result, errors } = await createCartWorkflow(req.scope).run({
|
||||
input: workflowInput,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
@@ -18,7 +27,7 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { id: result[0].id }
|
||||
const variables = { id: result.id }
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "cart",
|
||||
|
||||
@@ -29,15 +29,10 @@ export class StorePostCartReq {
|
||||
@IsString()
|
||||
region_id?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
customer_id?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
email?: string
|
||||
|
||||
// TODO: Remove in favor of using region currencies, as in the core
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
currency_code?: string
|
||||
@@ -74,10 +69,6 @@ export class StorePostCartsCartReq {
|
||||
@IsType([AddressPayload, String])
|
||||
shipping_address?: AddressPayload | string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
customer_id?: string
|
||||
|
||||
@IsEmail()
|
||||
@IsOptional()
|
||||
sales_channel_id?: string
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
import "reflect-metadata"
|
||||
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import {
|
||||
IsDate,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from "class-validator"
|
||||
import {
|
||||
FindManyOptions,
|
||||
FindOneOptions,
|
||||
@@ -8,20 +16,12 @@ import {
|
||||
FindOptionsWhere,
|
||||
OrderByCondition,
|
||||
} from "typeorm"
|
||||
import {
|
||||
IsDate,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from "class-validator"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
|
||||
import { BaseEntity } from "../interfaces"
|
||||
import { ClassConstructor } from "./global"
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations"
|
||||
import { BaseEntity } from "../interfaces"
|
||||
import { transformDate } from "../utils/validators/date-transform"
|
||||
import { ClassConstructor } from "./global"
|
||||
|
||||
/**
|
||||
* Utility type used to remove some optional attributes (coming from K) from a type T
|
||||
|
||||
Reference in New Issue
Block a user