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

This commit is contained in:
Adrien de Peretti
2023-12-07 17:05:23 +01:00
committed by GitHub
parent 946db51a9b
commit 85cda7ce37
14 changed files with 136 additions and 43 deletions

View File

@@ -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 = {
@@ -48,6 +50,7 @@ export async function findOrCreateAddresses({
country_code: regionCountries[0],
})
addressesDTO.shipping_address = shippingAddress
addressesDTO.shipping_address_id = shippingAddress?.id
}
} else {
@@ -75,6 +78,7 @@ export async function findOrCreateAddresses({
)
}
addressesDTO.shipping_address = address
addressesDTO.shipping_address_id = address.id
}
}
@@ -103,6 +107,7 @@ export async function findOrCreateAddresses({
)
}
addressesDTO.billing_address = address
addressesDTO.billing_address_id = billingAddressId
}

View File

@@ -1,4 +1,4 @@
import { CartDTO } from "@medusajs/types"
import { AddressDTO, CartDTO, CustomerDTO, RegionDTO } from "@medusajs/types"
import { WorkflowArguments } from "@medusajs/workflows-sdk"
enum Aliases {
@@ -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?: CustomerDTO
customer_id?: string
email?: string
}
region: {
region?: RegionDTO
region_id: string
}
context: {

View File

@@ -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
}
@@ -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)
@@ -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
@@ -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

View File

@@ -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 = {
@@ -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(
@@ -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
}