Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa, core-workflows, product): slightly improve create cart workflow #5725

Merged
merged 6 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/unlucky-snails-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/core-flows": patch
---

slightly improve create cart workflow
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { WorkflowArguments } from "@medusajs/workflows-sdk"

type AddressesDTO = {
shipping_address_id?: string
shipping_address?: AddressDTO
billing_address_id?: string
billing_address?: AddressDTO
}

type HandlerInputData = {
Expand Down Expand Up @@ -48,6 +50,7 @@ export async function findOrCreateAddresses({
country_code: regionCountries[0],
})

addressesDTO.shipping_address = shippingAddress
addressesDTO.shipping_address_id = shippingAddress?.id
}
} else {
Expand Down Expand Up @@ -75,6 +78,7 @@ export async function findOrCreateAddresses({
)
}

addressesDTO.shipping_address = address
addressesDTO.shipping_address_id = address.id
}
}
Expand Down Expand Up @@ -103,6 +107,7 @@ export async function findOrCreateAddresses({
)
}

addressesDTO.billing_address = address
addressesDTO.billing_address_id = billingAddressId
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core-flows/src/handlers/cart/create-cart.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CartDTO } from "@medusajs/types"
import { AddressDTO, CartDTO } from "@medusajs/types"
import { WorkflowArguments } from "@medusajs/workflows-sdk"

enum Aliases {
Expand All @@ -14,14 +14,18 @@ type HandlerInputData = {
sales_channel_id?: string
}
addresses: {
shipping_address?: AddressDTO
shipping_address_id: string
billing_address?: AddressDTO
billing_address_id: string
}
customer: {
customer?: any
customer_id?: string
adrien2p marked this conversation as resolved.
Show resolved Hide resolved
email?: string
}
region: {
region?: any
region_id: string
}
context: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { validateEmail } from "@medusajs/utils"

import { WorkflowArguments } from "@medusajs/workflows-sdk"
import { CustomerTypes } from "@medusajs/types"

type CustomerDTO = {
type CustomerResultDTO = {
customer?: CustomerTypes.CustomerDTO
customer_id?: string
email?: string
}
Expand All @@ -22,12 +24,12 @@ export async function findOrCreateCustomer({
container,
context,
data,
}: WorkflowArguments<HandlerInputData>): Promise<CustomerDTO> {
}: WorkflowArguments<HandlerInputData>): Promise<CustomerResultDTO> {
const { manager } = context

const customerService = container.resolve("customerService")

const customerDTO: CustomerDTO = {}
const customerDataDTO: CustomerResultDTO = {}
const customerId = data[Aliases.Customer].customer_id
const customerServiceTx = customerService.withTransaction(manager)

Expand All @@ -36,8 +38,9 @@ export async function findOrCreateCustomer({
.retrieve(customerId)
.catch(() => undefined)

customerDTO.customer_id = customer?.id
customerDTO.email = customer?.email
customerDataDTO.customer = customer
customerDataDTO.customer_id = customer?.id
customerDataDTO.email = customer?.email
}

const customerEmail = data[Aliases.Customer].email
Expand All @@ -53,11 +56,12 @@ export async function findOrCreateCustomer({
customer = await customerServiceTx.create({ email: validatedEmail })
}

customerDTO.customer_id = customer.id
customerDTO.email = customer.email
customerDataDTO.customer = customer
customerDataDTO.customer_id = customer.id
customerDataDTO.email = customer.email
}

return customerDTO
return customerDataDTO
}

findOrCreateCustomer.aliases = Aliases
25 changes: 17 additions & 8 deletions packages/core-flows/src/handlers/region/find-region.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { MedusaError } from "@medusajs/utils"
import { RegionTypes } from "@medusajs/types"
import { isDefined } from "medusa-core-utils"

import { WorkflowArguments } from "@medusajs/workflows-sdk"

type RegionDTO = {
type RegionResultDTO = {
region_id?: string
region?: RegionTypes.RegionDTO
}

type HandlerInputData = {
Expand All @@ -20,16 +22,24 @@ enum Aliases {
export async function findRegion({
container,
data,
}: WorkflowArguments<HandlerInputData>): Promise<RegionDTO> {
}: WorkflowArguments<HandlerInputData>): Promise<RegionResultDTO> {
const regionService = container.resolve("regionService")

let regionId: string
const regionDTO: RegionDTO = {}
const regionDTO: RegionResultDTO = {}

if (isDefined(data[Aliases.Region].region_id)) {
regionId = data[Aliases.Region].region_id
regionDTO.region_id = data[Aliases.Region].region_id
regionDTO.region = await regionService.retrieve(regionDTO.region_id, {
relations: ["countries"],
})
} else {
const regions = await regionService.list({}, {})
const regions = await regionService.list(
{},
{
relations: ["countries"],
}
)

if (!regions?.length) {
throw new MedusaError(
Expand All @@ -38,11 +48,10 @@ export async function findRegion({
)
}

regionId = regions[0].id
regionDTO.region_id = regions[0].id
regionDTO.region = regions[0]
}

regionDTO.region_id = regionId

return regionDTO
}

Expand Down
14 changes: 9 additions & 5 deletions packages/medusa/src/services/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,15 @@ class CartService extends TransactionBaseService {
).id
}

if (data.customer_id) {
const customer = await this.customerService_
.withTransaction(transactionManager)
.retrieve(data.customer_id)
.catch(() => undefined)
if (data.customer_id || data.customer) {
const customer =
(data.customer ??
(data.customer_id &&
(await this.customerService_
.withTransaction(transactionManager)
.retrieve(data.customer_id)
.catch(() => undefined)))) as Customer

rawCart.customer = customer
rawCart.customer_id = customer?.id
rawCart.email = customer?.email
Expand Down
2 changes: 2 additions & 0 deletions packages/medusa/src/types/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Cart, CartType } from "../models/cart"
import { IsType } from "../utils/validators/is-type"
import { Region } from "../models"
import { ValidateNested } from "class-validator"
import { CustomerTypes } from "@medusajs/types"

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isCart(object: any): object is Cart {
Expand Down Expand Up @@ -60,6 +61,7 @@ export type CartCreateProps = {
shipping_address?: Partial<AddressPayload>
gift_cards?: GiftCard[]
discounts?: Discount[]
customer?: CustomerTypes.CustomerDTO
customer_id?: string
type?: CartType
context?: object
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/bundles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * as CacheTypes from "./cache"
export * as CommonTypes from "./common"
export * as CustomerTypes from "./customer"
export * as DAL from "./dal"
export * as EventBusTypes from "./event-bus"
export * as FeatureFlagTypes from "./feature-flag"
Expand All @@ -8,6 +9,7 @@ export * as LoggerTypes from "./logger"
export * as ModulesSdkTypes from "./modules-sdk"
export * as PricingTypes from "./pricing"
export * as ProductTypes from "./product"
export * as RegionTypes from "./region"
export * as SalesChannelTypes from "./sales-channel"
export * as SearchTypes from "./search"
export * as StockLocationTypes from "./stock-location"
Expand Down
24 changes: 24 additions & 0 deletions packages/types/src/customer/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AddressDTO } from "../address"

export interface CustomerDTO {
id: string
email: string
billing_address_id?: string | null
shipping_address_id?: string | null
first_name?: string | null
last_name?: string | null
billing_address?: AddressDTO
shipping_address?: AddressDTO
phone?: string | null
has_account: boolean
groups?: {
id: string
}[]
orders: {
id: string
}[]
metadata?: Record<string, unknown>
deleted_at?: Date | string
created_at?: Date | string
updated_at?: Date | string
}
1 change: 1 addition & 0 deletions packages/types/src/customer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./common"
2 changes: 2 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./bundles"
export * from "./cache"
export * from "./cart"
export * from "./common"
export * from "./customer"
export * from "./dal"
export * from "./event-bus"
export * from "./feature-flag"
Expand All @@ -15,6 +16,7 @@ export * from "./modules-sdk"
export * from "./pricing"
export * from "./product"
export * from "./product-category"
export * from "./region"
export * from "./sales-channel"
export * from "./search"
export * from "./shared-context"
Expand Down
11 changes: 11 additions & 0 deletions packages/types/src/region/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type RegionDTO = {
name: string
currency_code: string
tax_rate?: number
tax_code?: string | null
gift_cards_taxable?: boolean
automatic_taxes?: boolean
tax_provider_id?: string | null
metadata?: Record<string, unknown>
includes_tax?: boolean
}
1 change: 1 addition & 0 deletions packages/types/src/region/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./common"
Loading