-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement calculate SO price endpoint
- Loading branch information
Showing
13 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/core/core-flows/src/fulfillment/steps/calculate-shipping-options-prices.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,24 @@ | ||
import { | ||
CalculateShippingOptionPriceDTO, | ||
IFulfillmentModuleService, | ||
} from "@medusajs/framework/types" | ||
import { Modules } from "@medusajs/framework/utils" | ||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk" | ||
|
||
export const calculateShippingOptionsPricesStepId = | ||
"calculate-shipping-options-prices" | ||
/** | ||
* This step calculates the prices for one or more shipping options. | ||
*/ | ||
export const calculateShippingOptionsPricesStep = createStep( | ||
calculateShippingOptionsPricesStepId, | ||
async (input: CalculateShippingOptionPriceDTO[], { container }) => { | ||
const service = container.resolve<IFulfillmentModuleService>( | ||
Modules.FULFILLMENT | ||
) | ||
|
||
const prices = await service.calculateShippingOptionsPrices(input) | ||
|
||
return new StepResponse(prices) | ||
} | ||
) |
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
49 changes: 49 additions & 0 deletions
49
packages/core/core-flows/src/fulfillment/workflows/calculate-shipping-options-prices.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,49 @@ | ||
import { FulfillmentWorkflow } from "@medusajs/framework/types" | ||
import { | ||
createWorkflow, | ||
transform, | ||
WorkflowData, | ||
WorkflowResponse, | ||
} from "@medusajs/framework/workflows-sdk" | ||
import { calculateShippingOptionsPricesStep } from "../steps" | ||
import { useQueryGraphStep } from "../../common" | ||
|
||
export const calculateShippingOptionsPricesWorkflowId = | ||
"calculate-shipping-options-prices-workflow" | ||
/** | ||
* This workflow calculates the prices for one or more shipping options. | ||
*/ | ||
export const calculateShippingOptionsPricesWorkflow = createWorkflow( | ||
calculateShippingOptionsPricesWorkflowId, | ||
( | ||
input: WorkflowData<FulfillmentWorkflow.CalculateShippingOptionsPricesWorkflowInput> | ||
): WorkflowResponse<FulfillmentWorkflow.CalculateShippingOptionsPricesWorkflowOutput> => { | ||
const shippingOptionsQuery = useQueryGraphStep({ | ||
entity: "shipping_option", | ||
filters: { id: input.map((i) => i.shipping_option_id) }, | ||
fields: ["id", "provider_id", "data"], | ||
}) | ||
|
||
const data = transform( | ||
{ shippingOptionsQuery }, | ||
({ shippingOptionsQuery }) => { | ||
const shippingOptions = shippingOptionsQuery.data | ||
|
||
return shippingOptions.map((shippingOption) => ({ | ||
id: shippingOption.id, | ||
provider_id: shippingOption.provider_id, | ||
data: shippingOption.data, | ||
context: { | ||
cart: { | ||
id: shippingOption.cart_id, | ||
}, | ||
}, | ||
})) | ||
} | ||
) | ||
|
||
const prices = calculateShippingOptionsPricesStep(data) | ||
|
||
return new WorkflowResponse(prices) | ||
} | ||
) |
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
9 changes: 9 additions & 0 deletions
9
packages/core/types/src/workflow/fulfillment/calculate-shipping-options-prices.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,9 @@ | ||
import { CalculatedShippingOptionPrice } from "../../fulfillment" | ||
|
||
export type CalculateShippingOptionsPricesWorkflowInput = { | ||
cart_id: string | ||
shipping_option_id: string | ||
}[] | ||
|
||
export type CalculateShippingOptionsPricesWorkflowOutput = | ||
CalculatedShippingOptionPrice[] |
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
23 changes: 23 additions & 0 deletions
23
packages/medusa/src/api/store/shipping-options/[id]/calculate/route.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,23 @@ | ||
import { HttpTypes } from "@medusajs/framework/types" | ||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" | ||
import { calculateShippingOptionsPricesWorkflow } from "@medusajs/core-flows" | ||
|
||
import { StoreCalculateShippingOptionPriceType } from "../../validators" | ||
|
||
export const POST = async ( | ||
req: MedusaRequest<StoreCalculateShippingOptionPriceType>, | ||
res: MedusaResponse<HttpTypes.StoreCalculateShippingOptionPriceResponse> | ||
) => { | ||
const { result } = await calculateShippingOptionsPricesWorkflow( | ||
req.scope | ||
).run({ | ||
input: [ | ||
{ | ||
shipping_option_id: req.params.id, | ||
cart_id: req.validatedBody.cart_id, | ||
}, | ||
], | ||
}) | ||
|
||
res.status(200).json(result[0]) | ||
} |
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