feat(core-flows,fulfillment, fulfillment-manual, types): make fulfillment typings more specific (#10677)
**What** - attempt to add more specific type definitions around fulfillment provider APIs --- CLOSES CMRC-814 CLOSES CMRC-816 CLOSES CMRC-817
This commit is contained in:
1 parent
333ed538f5
commit
16b0672f8a
12 files changed
+1358
-68
No files matched your search
+5
-2
@@ -41,7 +41,10 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
|||||||
|
|
||||||
async createFulfillment() {
|
async createFulfillment() {
|
||||||
// No data is being sent anywhere
|
// No data is being sent anywhere
|
||||||
return {}
|
return {
|
||||||
|
data: {},
|
||||||
|
labels: [],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async cancelFulfillment() {
|
async cancelFulfillment() {
|
||||||
@@ -49,6 +52,6 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createReturnFulfillment() {
|
async createReturnFulfillment() {
|
||||||
return {}
|
return { data: {}, labels: [] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
import { Modules, promiseAll } from "@medusajs/framework/utils"
|
import { Modules, promiseAll } from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
CartDTO,
|
|
||||||
IFulfillmentModuleService,
|
IFulfillmentModuleService,
|
||||||
StockLocationDTO,
|
ValidateFulfillmentDataContext,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ export type ValidateShippingMethodsDataInput = {
|
|||||||
provider_id: string
|
provider_id: string
|
||||||
option_data: Record<string, unknown>
|
option_data: Record<string, unknown>
|
||||||
method_data: Record<string, unknown>
|
method_data: Record<string, unknown>
|
||||||
context: CartDTO & { from_location: StockLocationDTO; [k: string]: unknown }
|
context: ValidateFulfillmentDataContext
|
||||||
}[]
|
}[]
|
||||||
|
|
||||||
export const validateAndReturnShippingMethodsDataStepId =
|
export const validateAndReturnShippingMethodsDataStepId =
|
||||||
@@ -38,7 +37,7 @@ export const validateAndReturnShippingMethodsDataStep = createStep(
|
|||||||
option.provider_id,
|
option.provider_id,
|
||||||
option.option_data,
|
option.option_data,
|
||||||
option.method_data,
|
option.method_data,
|
||||||
option.context
|
option.context as ValidateFulfillmentDataContext
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { CartDTO } from "../.."
|
||||||
|
|
||||||
|
export type CartPropsForFulfillment = {
|
||||||
|
id: CartDTO["id"]
|
||||||
|
shipping_address: CartDTO["shipping_address"]
|
||||||
|
items: CartDTO["items"] & {
|
||||||
|
variant: {
|
||||||
|
id: string
|
||||||
|
weight: number
|
||||||
|
length: number
|
||||||
|
height: number
|
||||||
|
width: number
|
||||||
|
material: string
|
||||||
|
product: {
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
product: {
|
||||||
|
id: string
|
||||||
|
collection_id: string
|
||||||
|
categories: {
|
||||||
|
id: string
|
||||||
|
}[]
|
||||||
|
tags: {
|
||||||
|
id: string
|
||||||
|
}[]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,3 +10,5 @@ export * from "./fulfillment-provider"
|
|||||||
export * from "./fulfillment"
|
export * from "./fulfillment"
|
||||||
export * from "./fulfillment-item"
|
export * from "./fulfillment-item"
|
||||||
export * from "./fulfillment-label"
|
export * from "./fulfillment-label"
|
||||||
|
export * from "./cart"
|
||||||
|
export * from "./order"
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -1,7 +1,6 @@
|
|||||||
import { CreateShippingOptionTypeDTO } from "./shipping-option-type"
|
import { CreateShippingOptionTypeDTO } from "./shipping-option-type"
|
||||||
import { ShippingOptionPriceType } from "../common"
|
import { CartPropsForFulfillment, ShippingOptionPriceType } from "../common"
|
||||||
import { CreateShippingOptionRuleDTO } from "./shipping-option-rule"
|
import { CreateShippingOptionRuleDTO } from "./shipping-option-rule"
|
||||||
import { CartDTO } from "../../cart"
|
|
||||||
import { StockLocationDTO } from "../../stock-location"
|
import { StockLocationDTO } from "../../stock-location"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -148,13 +147,17 @@ export interface CalculateShippingOptionPriceDTO {
|
|||||||
/**
|
/**
|
||||||
* The calculation context needed for the associated fulfillment provider to calculate the price of a shipping option.
|
* The calculation context needed for the associated fulfillment provider to calculate the price of a shipping option.
|
||||||
*/
|
*/
|
||||||
context: CartDTO & {
|
context: CartPropsForFulfillment & {
|
||||||
/**
|
/**
|
||||||
* The location that the items will be shipped from.
|
* The location that the items will be shipped from.
|
||||||
*/
|
*/
|
||||||
from_location?: StockLocationDTO
|
from_location?: StockLocationDTO
|
||||||
} & Record<
|
[k: string]: unknown
|
||||||
string,
|
}
|
||||||
unknown
|
|
||||||
>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The calculation context needed for the associated fulfillment provider to calculate the price of a shipping option.
|
||||||
|
*/
|
||||||
|
export type CalculateShippingOptionPriceContext =
|
||||||
|
CalculateShippingOptionPriceDTO["context"]
|
||||||
@@ -1,4 +1,15 @@
|
|||||||
import { CalculateShippingOptionPriceDTO } from "./mutations"
|
import { StockLocationDTO } from "../stock-location"
|
||||||
|
import {
|
||||||
|
CartPropsForFulfillment,
|
||||||
|
FulfillmentDTO,
|
||||||
|
FulfillmentItemDTO,
|
||||||
|
FulfillmentOrderDTO,
|
||||||
|
} from "./common"
|
||||||
|
import {
|
||||||
|
CalculateShippingOptionPriceContext,
|
||||||
|
CalculateShippingOptionPriceDTO,
|
||||||
|
CreateShippingOptionDTO,
|
||||||
|
} from "./mutations"
|
||||||
|
|
||||||
export type FulfillmentOption = {
|
export type FulfillmentOption = {
|
||||||
/**
|
/**
|
||||||
@@ -28,6 +39,37 @@ export type CalculatedShippingOptionPrice = {
|
|||||||
is_calculated_price_tax_inclusive: boolean
|
is_calculated_price_tax_inclusive: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ValidateFulfillmentDataContext = CartPropsForFulfillment & {
|
||||||
|
/**
|
||||||
|
* Details about the location that items are being shipped from.
|
||||||
|
*/
|
||||||
|
from_location: StockLocationDTO
|
||||||
|
[k: string]: unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CreateFulfillmentResult = {
|
||||||
|
/**
|
||||||
|
* Additional fulfillment data from provider
|
||||||
|
*/
|
||||||
|
data: Record<string, unknown>
|
||||||
|
labels: {
|
||||||
|
/**
|
||||||
|
* The tracking number of the fulfillment label.
|
||||||
|
*/
|
||||||
|
tracking_number: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The tracking URL of the fulfillment label.
|
||||||
|
*/
|
||||||
|
tracking_url: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The label's URL.
|
||||||
|
*/
|
||||||
|
label_url: string
|
||||||
|
}[]
|
||||||
|
}
|
||||||
|
|
||||||
export interface IFulfillmentProvider {
|
export interface IFulfillmentProvider {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -46,7 +88,7 @@ export interface IFulfillmentProvider {
|
|||||||
validateFulfillmentData(
|
validateFulfillmentData(
|
||||||
optionData: Record<string, unknown>,
|
optionData: Record<string, unknown>,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
context: Record<string, unknown>
|
context: ValidateFulfillmentDataContext
|
||||||
): Promise<any>
|
): Promise<any>
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -57,7 +99,7 @@ export interface IFulfillmentProvider {
|
|||||||
*
|
*
|
||||||
* Check if the provider can calculate the fulfillment price.
|
* Check if the provider can calculate the fulfillment price.
|
||||||
*/
|
*/
|
||||||
canCalculate(data: Record<string, unknown>): Promise<boolean>
|
canCalculate(data: CreateShippingOptionDTO): Promise<boolean>
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Calculate the price for the given fulfillment option.
|
* Calculate the price for the given fulfillment option.
|
||||||
@@ -65,18 +107,18 @@ export interface IFulfillmentProvider {
|
|||||||
calculatePrice(
|
calculatePrice(
|
||||||
optionData: CalculateShippingOptionPriceDTO["optionData"],
|
optionData: CalculateShippingOptionPriceDTO["optionData"],
|
||||||
data: CalculateShippingOptionPriceDTO["data"],
|
data: CalculateShippingOptionPriceDTO["data"],
|
||||||
context: CalculateShippingOptionPriceDTO["context"]
|
context: CalculateShippingOptionPriceContext
|
||||||
): Promise<CalculatedShippingOptionPrice>
|
): Promise<CalculatedShippingOptionPrice>
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Create a fulfillment for the given data.
|
* Create a fulfillment for the given data.
|
||||||
*/
|
*/
|
||||||
createFulfillment(
|
createFulfillment(
|
||||||
data: object,
|
data: Record<string, unknown>,
|
||||||
items: object[],
|
items: Partial<Omit<FulfillmentItemDTO, "fulfillment">>[],
|
||||||
order: object | undefined,
|
order: Partial<FulfillmentOrderDTO> | undefined,
|
||||||
fulfillment: Record<string, unknown>
|
fulfillment: Partial<Omit<FulfillmentDTO, "provider_id" | "data" | "items">>
|
||||||
): Promise<Record<string, unknown>>
|
): Promise<CreateFulfillmentResult>
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Cancel the given fulfillment.
|
* Cancel the given fulfillment.
|
||||||
@@ -91,7 +133,9 @@ export interface IFulfillmentProvider {
|
|||||||
*
|
*
|
||||||
* Create a return for the given data.
|
* Create a return for the given data.
|
||||||
*/
|
*/
|
||||||
createReturnFulfillment(fromData: Record<string, unknown>): Promise<any>
|
createReturnFulfillment(
|
||||||
|
fromData: Record<string, unknown>
|
||||||
|
): Promise<CreateFulfillmentResult>
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Get the documents for the given return data.
|
* Get the documents for the given return data.
|
||||||
|
|||||||
@@ -45,7 +45,10 @@ import {
|
|||||||
CreateShippingProfileDTO,
|
CreateShippingProfileDTO,
|
||||||
UpsertShippingProfileDTO,
|
UpsertShippingProfileDTO,
|
||||||
} from "./mutations/shipping-profile"
|
} from "./mutations/shipping-profile"
|
||||||
import { CalculatedShippingOptionPrice } from "./provider"
|
import {
|
||||||
|
CalculatedShippingOptionPrice,
|
||||||
|
ValidateFulfillmentDataContext,
|
||||||
|
} from "./provider"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main service interface for the Fulfillment Module.
|
* The main service interface for the Fulfillment Module.
|
||||||
@@ -2595,7 +2598,7 @@ export interface IFulfillmentModuleService extends IModuleService {
|
|||||||
* @param {string} providerId - The fulfillment provider's ID.
|
* @param {string} providerId - The fulfillment provider's ID.
|
||||||
* @param {Record<string, unknown>} optionData - The fulfillment option data to validate.
|
* @param {Record<string, unknown>} optionData - The fulfillment option data to validate.
|
||||||
* @param {Record<string, unknown>} data - The fulfillment data to validate.
|
* @param {Record<string, unknown>} data - The fulfillment data to validate.
|
||||||
* @param {Record<string, unknown>} context - The context to validate the fulfillment option data in.
|
* @param {ValidateFulfillmentDataContext} context - The context to validate the fulfillment option data in.
|
||||||
* @returns {Promise<boolean>} Whether the fulfillment option data is valid with the specified provider.
|
* @returns {Promise<boolean>} Whether the fulfillment option data is valid with the specified provider.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
@@ -2615,7 +2618,7 @@ export interface IFulfillmentModuleService extends IModuleService {
|
|||||||
providerId: string,
|
providerId: string,
|
||||||
optionData: Record<string, unknown>,
|
optionData: Record<string, unknown>,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
context: Record<string, unknown>
|
context: ValidateFulfillmentDataContext
|
||||||
): Promise<Record<string, unknown>>
|
): Promise<Record<string, unknown>>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
import {
|
import {
|
||||||
CalculatedShippingOptionPrice,
|
CalculatedShippingOptionPrice,
|
||||||
CalculateShippingOptionPriceDTO,
|
CalculateShippingOptionPriceDTO,
|
||||||
|
CreateFulfillmentResult,
|
||||||
|
CreateShippingOptionDTO,
|
||||||
|
FulfillmentDTO,
|
||||||
|
FulfillmentItemDTO,
|
||||||
FulfillmentOption,
|
FulfillmentOption,
|
||||||
|
FulfillmentOrderDTO,
|
||||||
IFulfillmentProvider,
|
IFulfillmentProvider,
|
||||||
|
ValidateFulfillmentDataContext,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ### constructor
|
* ### constructor
|
||||||
*
|
*
|
||||||
* The constructor allows you to access resources from the [module's container](https://docs.medusajs.com/learn/fundamentals/modules/container)
|
* The constructor allows you to access resources from the [module's container](https://docs.medusajs.com/learn/fundamentals/modules/container)
|
||||||
* using the first parameter, and the module's options using the second parameter.
|
* using the first parameter, and the module's options using the second parameter.
|
||||||
*
|
*
|
||||||
* :::note
|
* :::note
|
||||||
@@ -48,7 +54,7 @@ import {
|
|||||||
*
|
*
|
||||||
* this.logger_ = logger
|
* this.logger_ = logger
|
||||||
* this.options_ = options
|
* this.options_ = options
|
||||||
*
|
*
|
||||||
* // TODO initialize your client
|
* // TODO initialize your client
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
@@ -96,7 +102,7 @@ export class AbstractFulfillmentProviderService
|
|||||||
* This method retrieves a list of fulfillment options that this provider supports. Admin users will then choose from these options when
|
* This method retrieves a list of fulfillment options that this provider supports. Admin users will then choose from these options when
|
||||||
* they're creating a shipping option. The chosen fulfillment option's object is then stored within the created shipping option's `data` property.
|
* they're creating a shipping option. The chosen fulfillment option's object is then stored within the created shipping option's `data` property.
|
||||||
* The `data` property is useful to store data relevant for the third-party provider to later process the fulfillment.
|
* The `data` property is useful to store data relevant for the third-party provider to later process the fulfillment.
|
||||||
*
|
*
|
||||||
* This method is useful if your third-party provider allows you to retrieve support options, carriers, or services from an API. You can then
|
* This method is useful if your third-party provider allows you to retrieve support options, carriers, or services from an API. You can then
|
||||||
* retrieve those and return then in the method, allowing the admin user to choose from the services provided by the third-party provider.
|
* retrieve those and return then in the method, allowing the admin user to choose from the services provided by the third-party provider.
|
||||||
*
|
*
|
||||||
@@ -112,7 +118,7 @@ export class AbstractFulfillmentProviderService
|
|||||||
* async getFulfillmentOptions(): Promise<FulfillmentOption[]> {
|
* async getFulfillmentOptions(): Promise<FulfillmentOption[]> {
|
||||||
* // assuming you have a client
|
* // assuming you have a client
|
||||||
* const services = await this.client.getServices()
|
* const services = await this.client.getServices()
|
||||||
*
|
*
|
||||||
* return services.map((service) => ({
|
* return services.map((service) => ({
|
||||||
* id: service.service_id,
|
* id: service.service_id,
|
||||||
* name: service.name,
|
* name: service.name,
|
||||||
@@ -161,7 +167,7 @@ export class AbstractFulfillmentProviderService
|
|||||||
async validateFulfillmentData(
|
async validateFulfillmentData(
|
||||||
optionData: Record<string, unknown>,
|
optionData: Record<string, unknown>,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
context: Record<string, unknown>
|
context: ValidateFulfillmentDataContext
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
throw Error("validateFulfillmentData must be overridden by the child class")
|
throw Error("validateFulfillmentData must be overridden by the child class")
|
||||||
}
|
}
|
||||||
@@ -190,7 +196,7 @@ export class AbstractFulfillmentProviderService
|
|||||||
/**
|
/**
|
||||||
* This method validates whether a shippin option's price can be calculated during checkout. It's executed when the admin user creates a shipping
|
* This method validates whether a shippin option's price can be calculated during checkout. It's executed when the admin user creates a shipping
|
||||||
* option of type `calculated`. If this method returns `false`, an error is thrown as the shipping option's price can't be calculated.
|
* option of type `calculated`. If this method returns `false`, an error is thrown as the shipping option's price can't be calculated.
|
||||||
*
|
*
|
||||||
* You can perform the checking using the third-party provider if applicable. The `data` parameter will hold the shipping option's `data` property, which
|
* You can perform the checking using the third-party provider if applicable. The `data` parameter will hold the shipping option's `data` property, which
|
||||||
* includes the data of a fulfillment option returned by {@link getFulfillmentOptions}.
|
* includes the data of a fulfillment option returned by {@link getFulfillmentOptions}.
|
||||||
*
|
*
|
||||||
@@ -200,26 +206,26 @@ export class AbstractFulfillmentProviderService
|
|||||||
* @example
|
* @example
|
||||||
* class MyFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
* class MyFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
||||||
* // ...
|
* // ...
|
||||||
* async canCalculate(data: Record<string, unknown>): Promise<boolean> {
|
* async canCalculate(data: CreateShippingOptionDTO): Promise<boolean> {
|
||||||
* // assuming you have a client
|
* // assuming you have a client
|
||||||
* return await this.client.hasRates(data.id)
|
* return await this.client.hasRates(data.id)
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
async canCalculate(data: Record<string, unknown>): Promise<boolean> {
|
async canCalculate(data: CreateShippingOptionDTO): Promise<boolean> {
|
||||||
throw Error("canCalculate must be overridden by the child class")
|
throw Error("canCalculate must be overridden by the child class")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method calculates the price of a shipping method when it's created or its cart is refreshed.
|
* This method calculates the price of a shipping method when it's created or its cart is refreshed.
|
||||||
*
|
*
|
||||||
* In this method, you can send a request to your third-party provider to retrieve the prices. The first
|
* In this method, you can send a request to your third-party provider to retrieve the prices. The first
|
||||||
* parameters holds the `data` property of the shipping method's shipping option, which has fulfillment
|
* parameters holds the `data` property of the shipping method's shipping option, which has fulfillment
|
||||||
* object data returned by {@link getFulfillmentOptions}.
|
* object data returned by {@link getFulfillmentOptions}.
|
||||||
*
|
*
|
||||||
* The second parameter holds the `data` property of the shipping method, which has data returned by {@link validateFulfillmentData}.
|
* The second parameter holds the `data` property of the shipping method, which has data returned by {@link validateFulfillmentData}.
|
||||||
* It can also hold custom data passed from the frontend during checkout.
|
* It can also hold custom data passed from the frontend during checkout.
|
||||||
*
|
*
|
||||||
* So, using both of these data, assuming you're storing in them data related to the third-party service,
|
* So, using both of these data, assuming you're storing in them data related to the third-party service,
|
||||||
* you can retrieve the calculated price of the shipping method.
|
* you can retrieve the calculated price of the shipping method.
|
||||||
*
|
*
|
||||||
@@ -281,7 +287,7 @@ export class AbstractFulfillmentProviderService
|
|||||||
* items: any,
|
* items: any,
|
||||||
* order: any,
|
* order: any,
|
||||||
* fulfillment: any
|
* fulfillment: any
|
||||||
* ): Promise<any> {
|
* ): Promise<CreateFulfillmentResult> {
|
||||||
* // assuming the client creates a fulfillment
|
* // assuming the client creates a fulfillment
|
||||||
* // in the third-party service
|
* // in the third-party service
|
||||||
* const externalData = await this.client.create(
|
* const externalData = await this.client.create(
|
||||||
@@ -299,11 +305,11 @@ export class AbstractFulfillmentProviderService
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
async createFulfillment(
|
async createFulfillment(
|
||||||
data: object,
|
data: Record<string, unknown>,
|
||||||
items: object[],
|
items: Partial<Omit<FulfillmentItemDTO, "fulfillment">>[],
|
||||||
order: object | undefined,
|
order: Partial<FulfillmentOrderDTO> | undefined,
|
||||||
fulfillment: Record<string, unknown>
|
fulfillment: Partial<Omit<FulfillmentDTO, "provider_id" | "data" | "items">>
|
||||||
): Promise<any> {
|
): Promise<CreateFulfillmentResult> {
|
||||||
throw Error("createFulfillment must be overridden by the child class")
|
throw Error("createFulfillment must be overridden by the child class")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,12 +368,12 @@ export class AbstractFulfillmentProviderService
|
|||||||
* purchasing a label for the return fulfillment.
|
* purchasing a label for the return fulfillment.
|
||||||
*
|
*
|
||||||
* @param fulfillment - The fulfillment's details.
|
* @param fulfillment - The fulfillment's details.
|
||||||
* @returns An object whose `data` property is stored in the fulfillment's `data` property.
|
* @returns An object containing `data` which is stored in the fulfillment's `data` property and `labels` array which is used to create FulfillmentLabels.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* class MyFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
* class MyFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
||||||
* // ...
|
* // ...
|
||||||
* async createReturnFulfillment(fulfillment: Record<string, unknown>): Promise<any> {
|
* async createReturnFulfillment(fulfillment: Record<string, unknown>): Promise<CreateFulfillmentResult> {
|
||||||
* // assuming the client creates a fulfillment for a return
|
* // assuming the client creates a fulfillment for a return
|
||||||
* // in the third-party service
|
* // in the third-party service
|
||||||
* const externalData = await this.client.createReturn(
|
* const externalData = await this.client.createReturn(
|
||||||
@@ -385,7 +391,7 @@ export class AbstractFulfillmentProviderService
|
|||||||
*/
|
*/
|
||||||
async createReturnFulfillment(
|
async createReturnFulfillment(
|
||||||
fulfillment: Record<string, unknown>
|
fulfillment: Record<string, unknown>
|
||||||
): Promise<any> {
|
): Promise<CreateFulfillmentResult> {
|
||||||
throw Error("createReturn must be overridden by the child class")
|
throw Error("createReturn must be overridden by the child class")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import {
|
|||||||
SoftDeleteReturn,
|
SoftDeleteReturn,
|
||||||
UpdateFulfillmentSetDTO,
|
UpdateFulfillmentSetDTO,
|
||||||
UpdateServiceZoneDTO,
|
UpdateServiceZoneDTO,
|
||||||
|
ValidateFulfillmentDataContext,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import {
|
import {
|
||||||
arrayDifference,
|
arrayDifference,
|
||||||
@@ -650,7 +651,7 @@ export default class FulfillmentModuleService
|
|||||||
fulfillmentData || {},
|
fulfillmentData || {},
|
||||||
items.map((i) => i),
|
items.map((i) => i),
|
||||||
order,
|
order,
|
||||||
fulfillmentRest
|
fulfillmentRest as unknown as Partial<FulfillmentDTO>
|
||||||
)
|
)
|
||||||
await this.fulfillmentService_.update(
|
await this.fulfillmentService_.update(
|
||||||
{
|
{
|
||||||
@@ -710,11 +711,22 @@ export default class FulfillmentModuleService
|
|||||||
sharedContext
|
sharedContext
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const shippingOption = await this.shippingOptionService_.retrieve(
|
||||||
|
fulfillment.shipping_option_id!,
|
||||||
|
{
|
||||||
|
select: ["id", "name", "data", "metadata"],
|
||||||
|
},
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const providerResult =
|
const providerResult =
|
||||||
await this.fulfillmentProviderService_.createReturn(
|
await this.fulfillmentProviderService_.createReturn(
|
||||||
fulfillment.provider_id!, // TODO: should we add a runtime check on provider_id being provided?,
|
fulfillment.provider_id!, // TODO: should we add a runtime check on provider_id being provided?,
|
||||||
fulfillment as Record<any, any>
|
{
|
||||||
|
...fulfillment,
|
||||||
|
shipping_option: shippingOption,
|
||||||
|
} as Record<any, any>
|
||||||
)
|
)
|
||||||
await this.fulfillmentService_.update(
|
await this.fulfillmentService_.update(
|
||||||
{
|
{
|
||||||
@@ -2002,7 +2014,7 @@ export default class FulfillmentModuleService
|
|||||||
providerId: string,
|
providerId: string,
|
||||||
optionData: Record<string, unknown>,
|
optionData: Record<string, unknown>,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
context: Record<string, unknown>
|
context: ValidateFulfillmentDataContext
|
||||||
): Promise<Record<string, unknown>> {
|
): Promise<Record<string, unknown>> {
|
||||||
return await this.fulfillmentProviderService_.validateFulfillmentData(
|
return await this.fulfillmentProviderService_.validateFulfillmentData(
|
||||||
providerId,
|
providerId,
|
||||||
@@ -2012,6 +2024,7 @@ export default class FulfillmentModuleService
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: seems not to be used, what is the purpose of this method?
|
||||||
async validateFulfillmentOption(
|
async validateFulfillmentOption(
|
||||||
providerId: string,
|
providerId: string,
|
||||||
data: Record<string, unknown>
|
data: Record<string, unknown>
|
||||||
@@ -2058,10 +2071,7 @@ export default class FulfillmentModuleService
|
|||||||
}
|
}
|
||||||
|
|
||||||
const promises = shippingOptionsData.map((option) =>
|
const promises = shippingOptionsData.map((option) =>
|
||||||
this.fulfillmentProviderService_.canCalculate(
|
this.fulfillmentProviderService_.canCalculate(option.provider_id, option)
|
||||||
option.provider_id,
|
|
||||||
option as unknown as Record<string, unknown>
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return await promiseAll(promises)
|
return await promiseAll(promises)
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
import {
|
import {
|
||||||
CalculateShippingOptionPriceDTO,
|
CalculateShippingOptionPriceDTO,
|
||||||
Constructor,
|
Constructor,
|
||||||
|
CreateFulfillmentResult,
|
||||||
|
CreateShippingOptionDTO,
|
||||||
DAL,
|
DAL,
|
||||||
|
FulfillmentDTO,
|
||||||
|
FulfillmentItemDTO,
|
||||||
FulfillmentOption,
|
FulfillmentOption,
|
||||||
|
FulfillmentOrderDTO,
|
||||||
FulfillmentTypes,
|
FulfillmentTypes,
|
||||||
IFulfillmentProvider,
|
IFulfillmentProvider,
|
||||||
Logger,
|
Logger,
|
||||||
|
ValidateFulfillmentDataContext,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import {
|
import {
|
||||||
MedusaError,
|
MedusaError,
|
||||||
@@ -91,7 +97,7 @@ export default class FulfillmentProviderService extends ModulesSdkUtils.MedusaIn
|
|||||||
providerId: string,
|
providerId: string,
|
||||||
optionData: Record<string, unknown>,
|
optionData: Record<string, unknown>,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
context: Record<string, unknown>
|
context: ValidateFulfillmentDataContext
|
||||||
) {
|
) {
|
||||||
const provider = this.retrieveProviderRegistration(providerId)
|
const provider = this.retrieveProviderRegistration(providerId)
|
||||||
return await provider.validateFulfillmentData(optionData, data, context)
|
return await provider.validateFulfillmentData(optionData, data, context)
|
||||||
@@ -102,7 +108,7 @@ export default class FulfillmentProviderService extends ModulesSdkUtils.MedusaIn
|
|||||||
return await provider.validateOption(data)
|
return await provider.validateOption(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
async canCalculate(providerId: string, data: Record<string, unknown>) {
|
async canCalculate(providerId: string, data: CreateShippingOptionDTO) {
|
||||||
const provider = this.retrieveProviderRegistration(providerId)
|
const provider = this.retrieveProviderRegistration(providerId)
|
||||||
return await provider.canCalculate(data)
|
return await provider.canCalculate(data)
|
||||||
}
|
}
|
||||||
@@ -119,11 +125,11 @@ export default class FulfillmentProviderService extends ModulesSdkUtils.MedusaIn
|
|||||||
|
|
||||||
async createFulfillment(
|
async createFulfillment(
|
||||||
providerId: string,
|
providerId: string,
|
||||||
data: object,
|
data: Record<string, unknown>,
|
||||||
items: object[],
|
items: Partial<Omit<FulfillmentItemDTO, "fulfillment">>[],
|
||||||
order: object | undefined,
|
order: Partial<FulfillmentOrderDTO> | undefined,
|
||||||
fulfillment: Record<string, unknown>
|
fulfillment: Partial<Omit<FulfillmentDTO, "provider_id" | "data" | "items">>
|
||||||
): Promise<Record<string, unknown>> {
|
): Promise<CreateFulfillmentResult> {
|
||||||
const provider = this.retrieveProviderRegistration(providerId)
|
const provider = this.retrieveProviderRegistration(providerId)
|
||||||
return await provider.createFulfillment(data, items, order, fulfillment)
|
return await provider.createFulfillment(data, items, order, fulfillment)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { AbstractFulfillmentProviderService } from "@medusajs/framework/utils"
|
import { AbstractFulfillmentProviderService } from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
CalculatedShippingOptionPrice,
|
CalculatedShippingOptionPrice,
|
||||||
|
CalculateShippingOptionPriceContext,
|
||||||
|
CreateFulfillmentResult,
|
||||||
FulfillmentOption,
|
FulfillmentOption,
|
||||||
|
ValidateFulfillmentDataContext,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
|
||||||
// TODO rework type and DTO's
|
// TODO rework type and DTO's
|
||||||
@@ -28,7 +31,7 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
|||||||
async validateFulfillmentData(
|
async validateFulfillmentData(
|
||||||
optionData: Record<string, unknown>,
|
optionData: Record<string, unknown>,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
context: Record<string, unknown>
|
context: ValidateFulfillmentDataContext
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
@@ -36,7 +39,7 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
|||||||
async calculatePrice(
|
async calculatePrice(
|
||||||
optionData: Record<string, unknown>,
|
optionData: Record<string, unknown>,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
context: Record<string, unknown>
|
context: CalculateShippingOptionPriceContext
|
||||||
): Promise<CalculatedShippingOptionPrice> {
|
): Promise<CalculatedShippingOptionPrice> {
|
||||||
throw new Error("Manual fulfillment does not support price calculation")
|
throw new Error("Manual fulfillment does not support price calculation")
|
||||||
}
|
}
|
||||||
@@ -49,16 +52,22 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async createFulfillment(): Promise<Record<string, any>> {
|
async createFulfillment(): Promise<CreateFulfillmentResult> {
|
||||||
// No data is being sent anywhere
|
// No data is being sent anywhere
|
||||||
return {}
|
return {
|
||||||
|
data: {},
|
||||||
|
labels: [],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async cancelFulfillment(): Promise<any> {
|
async cancelFulfillment(): Promise<any> {
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
async createReturnFulfillment(): Promise<any> {
|
async createReturnFulfillment(): Promise<CreateFulfillmentResult> {
|
||||||
return {}
|
return {
|
||||||
|
data: {},
|
||||||
|
labels: [],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user