-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add basic paths into handlers + make first tests pass
- Loading branch information
Showing
21 changed files
with
578 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { | ||
TransactionStepsDefinition, | ||
WorkflowManager, | ||
} from "@medusajs/orchestration" | ||
|
||
import { InputAlias, Workflows } from "../../definitions" | ||
import { exportWorkflow, pipe } from "../../helper" | ||
import { cartHandlers } from "../../handlers" | ||
|
||
enum CreateCartActions { | ||
attachAddresses = "attachAddresses", | ||
attachCustomerDetails = "attachCustomerDetails", | ||
attachLineItems = "attachLineItems", | ||
attachRegion = "attachRegion", | ||
attachSalesChannel = "attachSalesChannel", | ||
createCart = "createCart", | ||
prepareCart = "prepareCart", | ||
removeCart = "removeCart", | ||
retrieveCart = "retrieveCart" | ||
} | ||
|
||
const workflowSteps: TransactionStepsDefinition = { | ||
next: { | ||
action: CreateCartActions.prepareCart, | ||
noCompensation: true, | ||
next: [{ | ||
action: CreateCartActions.attachCustomerDetails, | ||
noCompensation: true, | ||
}, { | ||
action: CreateCartActions.attachSalesChannel, | ||
noCompensation: true, | ||
}, { | ||
action: CreateCartActions.attachRegion, | ||
noCompensation: true, | ||
next: { | ||
action: CreateCartActions.attachAddresses, | ||
noCompensation: true, | ||
next: { | ||
action: CreateCartActions.createCart, | ||
noCompensation: true, | ||
next: { | ||
action: CreateCartActions.attachLineItems, | ||
noCompensation: true, | ||
next: { | ||
action: CreateCartActions.retrieveCart, | ||
noCompensation: true, | ||
} | ||
} | ||
} | ||
} | ||
}] | ||
}, | ||
} | ||
|
||
const handlers = new Map([ | ||
[ | ||
CreateCartActions.prepareCart, | ||
{ | ||
invoke: pipe( | ||
{ | ||
inputAlias: InputAlias.Cart, | ||
invoke: { | ||
from: InputAlias.Cart, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.prepareCart | ||
) | ||
}, | ||
], | ||
[ | ||
CreateCartActions.attachRegion, | ||
{ | ||
invoke: pipe( | ||
{ | ||
invoke: { | ||
from: CreateCartActions.prepareCart, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.attachRegionToCart | ||
), | ||
}, | ||
], | ||
[ | ||
CreateCartActions.attachSalesChannel, | ||
{ | ||
invoke: pipe( | ||
{ | ||
invoke: { | ||
from: CreateCartActions.prepareCart, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.attachSalesChannelToCart | ||
), | ||
}, | ||
], | ||
[ | ||
CreateCartActions.createCart, | ||
{ | ||
invoke: pipe( | ||
{ | ||
invoke: { | ||
from: CreateCartActions.attachRegion, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.createCart | ||
), | ||
}, | ||
], | ||
[ | ||
CreateCartActions.attachAddresses, | ||
{ | ||
invoke: pipe( | ||
{ | ||
invoke: { | ||
from: CreateCartActions.prepareCart, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.attachAddressesToCart | ||
), | ||
}, | ||
], | ||
[ | ||
CreateCartActions.attachCustomerDetails, | ||
{ | ||
invoke: pipe( | ||
{ | ||
invoke: { | ||
from: CreateCartActions.prepareCart, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.attachCustomerDetailsToCart | ||
), | ||
}, | ||
], | ||
[ | ||
CreateCartActions.attachLineItems, | ||
{ | ||
invoke: pipe( | ||
{ | ||
invoke: { | ||
from: CreateCartActions.createCart, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.attachLineItemsToCart | ||
), | ||
}, | ||
], | ||
[ | ||
CreateCartActions.retrieveCart, | ||
{ | ||
invoke: pipe( | ||
{ | ||
invoke: { | ||
from: CreateCartActions.attachLineItems, | ||
alias: InputAlias.Cart, | ||
}, | ||
}, | ||
cartHandlers.retrieveCart | ||
), | ||
}, | ||
], | ||
]) | ||
|
||
WorkflowManager.register(Workflows.CreateCart, workflowSteps, handlers) | ||
|
||
type CreateCartWorkflowInput = Record<any, any> | ||
type CreateCartWorkflowOutput = Record<any, any> | ||
|
||
export const createCart = exportWorkflow< | ||
CreateCartWorkflowInput, | ||
CreateCartWorkflowOutput | ||
>(Workflows.CreateCart, CreateCartActions.createCart) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './create-cart' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./create-products" | ||
export * from "./cart" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/workflows/src/handlers/cart/attach-addresses-to-cart.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { isDefined } from "medusa-core-utils" | ||
import { MedusaError } from "@medusajs/utils" | ||
|
||
import { InputAlias } from "../../definitions" | ||
import { PipelineHandlerResult, WorkflowArguments } from "../../helper" | ||
|
||
export async function attachAddressesToCart<T>({ | ||
container, | ||
context, | ||
data, | ||
}: WorkflowArguments): Promise<PipelineHandlerResult<T>> { | ||
const regionService = container.resolve("regionService") | ||
const addressRepository = container.resolve("addressRepository") | ||
const shippingAddress = data[InputAlias.Cart].shipping_address | ||
const shippingAddressId = data[InputAlias.Cart].shipping_address_id | ||
const billingAddress = data[InputAlias.Cart].billing_address | ||
const billingAddressId = data[InputAlias.Cart].billing_address_id | ||
|
||
const region = await regionService | ||
.retrieve(data[InputAlias.Cart].region_id!, { | ||
relations: ["countries"], | ||
}) | ||
|
||
const regionCountries = region.countries.map(({ iso_2 }) => iso_2) | ||
|
||
if (!shippingAddress && !shippingAddressId) { | ||
if (region.countries.length === 1) { | ||
data[InputAlias.Cart].shipping_address = addressRepository.create({ | ||
country_code: regionCountries[0], | ||
}) | ||
} | ||
} else { | ||
if (shippingAddress) { | ||
if (!regionCountries.includes(shippingAddress.country_code!)) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_ALLOWED, | ||
"Shipping country not in region" | ||
) | ||
} | ||
} | ||
|
||
if (shippingAddressId) { | ||
const address = await regionService.findOne({ | ||
where: { id: shippingAddressId }, | ||
}) | ||
|
||
if ( | ||
address?.country_code && | ||
!regionCountries.includes(address.country_code) | ||
) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_ALLOWED, | ||
"Shipping country not in region" | ||
) | ||
} | ||
|
||
data[InputAlias.Cart].shipping_address_id = address.id | ||
} | ||
} | ||
|
||
if (billingAddress) { | ||
if (!regionCountries.includes(billingAddress.country_code!)) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_ALLOWED, | ||
"Billing country not in region" | ||
) | ||
} | ||
} | ||
|
||
if (billingAddressId) { | ||
const address = await regionService.findOne({ | ||
where: { id: billingAddressId }, | ||
}) | ||
|
||
if (address?.country_code && !regionCountries.includes(address.country_code)) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_ALLOWED, | ||
"Billing country not in region" | ||
) | ||
} | ||
|
||
data[InputAlias.Cart].billing_address_id = billingAddressId | ||
} | ||
|
||
return data[InputAlias.Cart] | ||
} |
Oops, something went wrong.