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
+1348
-58
No files matched your search
+5
-2
@@ -41,7 +41,10 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
||||
|
||||
async createFulfillment() {
|
||||
// No data is being sent anywhere
|
||||
return {}
|
||||
return {
|
||||
data: {},
|
||||
labels: [],
|
||||
}
|
||||
}
|
||||
|
||||
async cancelFulfillment() {
|
||||
@@ -49,6 +52,6 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
||||
}
|
||||
|
||||
async createReturnFulfillment() {
|
||||
return {}
|
||||
return { data: {}, labels: [] }
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Modules, promiseAll } from "@medusajs/framework/utils"
|
||||
import {
|
||||
CartDTO,
|
||||
IFulfillmentModuleService,
|
||||
StockLocationDTO,
|
||||
ValidateFulfillmentDataContext,
|
||||
} from "@medusajs/types"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
|
||||
@@ -11,7 +10,7 @@ export type ValidateShippingMethodsDataInput = {
|
||||
provider_id: string
|
||||
option_data: Record<string, unknown>
|
||||
method_data: Record<string, unknown>
|
||||
context: CartDTO & { from_location: StockLocationDTO; [k: string]: unknown }
|
||||
context: ValidateFulfillmentDataContext
|
||||
}[]
|
||||
|
||||
export const validateAndReturnShippingMethodsDataStepId =
|
||||
@@ -38,7 +37,7 @@ export const validateAndReturnShippingMethodsDataStep = createStep(
|
||||
option.provider_id,
|
||||
option.option_data,
|
||||
option.method_data,
|
||||
option.context
|
||||
option.context as ValidateFulfillmentDataContext
|
||||
)
|
||||
|
||||
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-item"
|
||||
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 { ShippingOptionPriceType } from "../common"
|
||||
import { CartPropsForFulfillment, ShippingOptionPriceType } from "../common"
|
||||
import { CreateShippingOptionRuleDTO } from "./shipping-option-rule"
|
||||
import { CartDTO } from "../../cart"
|
||||
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.
|
||||
*/
|
||||
context: CartDTO & {
|
||||
context: CartPropsForFulfillment & {
|
||||
/**
|
||||
* The location that the items will be shipped from.
|
||||
*/
|
||||
from_location?: StockLocationDTO
|
||||
} & Record<
|
||||
string,
|
||||
unknown
|
||||
>
|
||||
[k: 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 = {
|
||||
/**
|
||||
@@ -28,6 +39,37 @@ export type CalculatedShippingOptionPrice = {
|
||||
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 {
|
||||
/**
|
||||
*
|
||||
@@ -46,7 +88,7 @@ export interface IFulfillmentProvider {
|
||||
validateFulfillmentData(
|
||||
optionData: Record<string, unknown>,
|
||||
data: Record<string, unknown>,
|
||||
context: Record<string, unknown>
|
||||
context: ValidateFulfillmentDataContext
|
||||
): Promise<any>
|
||||
/**
|
||||
*
|
||||
@@ -57,7 +99,7 @@ export interface IFulfillmentProvider {
|
||||
*
|
||||
* 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.
|
||||
@@ -65,18 +107,18 @@ export interface IFulfillmentProvider {
|
||||
calculatePrice(
|
||||
optionData: CalculateShippingOptionPriceDTO["optionData"],
|
||||
data: CalculateShippingOptionPriceDTO["data"],
|
||||
context: CalculateShippingOptionPriceDTO["context"]
|
||||
context: CalculateShippingOptionPriceContext
|
||||
): Promise<CalculatedShippingOptionPrice>
|
||||
/**
|
||||
*
|
||||
* Create a fulfillment for the given data.
|
||||
*/
|
||||
createFulfillment(
|
||||
data: object,
|
||||
items: object[],
|
||||
order: object | undefined,
|
||||
fulfillment: Record<string, unknown>
|
||||
): Promise<Record<string, unknown>>
|
||||
data: Record<string, unknown>,
|
||||
items: Partial<Omit<FulfillmentItemDTO, "fulfillment">>[],
|
||||
order: Partial<FulfillmentOrderDTO> | undefined,
|
||||
fulfillment: Partial<Omit<FulfillmentDTO, "provider_id" | "data" | "items">>
|
||||
): Promise<CreateFulfillmentResult>
|
||||
/**
|
||||
*
|
||||
* Cancel the given fulfillment.
|
||||
@@ -91,7 +133,9 @@ export interface IFulfillmentProvider {
|
||||
*
|
||||
* 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.
|
||||
|
||||
@@ -45,7 +45,10 @@ import {
|
||||
CreateShippingProfileDTO,
|
||||
UpsertShippingProfileDTO,
|
||||
} from "./mutations/shipping-profile"
|
||||
import { CalculatedShippingOptionPrice } from "./provider"
|
||||
import {
|
||||
CalculatedShippingOptionPrice,
|
||||
ValidateFulfillmentDataContext,
|
||||
} from "./provider"
|
||||
|
||||
/**
|
||||
* 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 {Record<string, unknown>} optionData - The fulfillment option 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.
|
||||
*
|
||||
* @example
|
||||
@@ -2615,7 +2618,7 @@ export interface IFulfillmentModuleService extends IModuleService {
|
||||
providerId: string,
|
||||
optionData: Record<string, unknown>,
|
||||
data: Record<string, unknown>,
|
||||
context: Record<string, unknown>
|
||||
context: ValidateFulfillmentDataContext
|
||||
): Promise<Record<string, unknown>>
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
import {
|
||||
CalculatedShippingOptionPrice,
|
||||
CalculateShippingOptionPriceDTO,
|
||||
CreateFulfillmentResult,
|
||||
CreateShippingOptionDTO,
|
||||
FulfillmentDTO,
|
||||
FulfillmentItemDTO,
|
||||
FulfillmentOption,
|
||||
FulfillmentOrderDTO,
|
||||
IFulfillmentProvider,
|
||||
ValidateFulfillmentDataContext,
|
||||
} from "@medusajs/types"
|
||||
|
||||
/**
|
||||
@@ -161,7 +167,7 @@ export class AbstractFulfillmentProviderService
|
||||
async validateFulfillmentData(
|
||||
optionData: Record<string, unknown>,
|
||||
data: Record<string, unknown>,
|
||||
context: Record<string, unknown>
|
||||
context: ValidateFulfillmentDataContext
|
||||
): Promise<any> {
|
||||
throw Error("validateFulfillmentData must be overridden by the child class")
|
||||
}
|
||||
@@ -200,13 +206,13 @@ export class AbstractFulfillmentProviderService
|
||||
* @example
|
||||
* class MyFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
||||
* // ...
|
||||
* async canCalculate(data: Record<string, unknown>): Promise<boolean> {
|
||||
* async canCalculate(data: CreateShippingOptionDTO): Promise<boolean> {
|
||||
* // assuming you have a client
|
||||
* 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")
|
||||
}
|
||||
|
||||
@@ -281,7 +287,7 @@ export class AbstractFulfillmentProviderService
|
||||
* items: any,
|
||||
* order: any,
|
||||
* fulfillment: any
|
||||
* ): Promise<any> {
|
||||
* ): Promise<CreateFulfillmentResult> {
|
||||
* // assuming the client creates a fulfillment
|
||||
* // in the third-party service
|
||||
* const externalData = await this.client.create(
|
||||
@@ -299,11 +305,11 @@ export class AbstractFulfillmentProviderService
|
||||
* }
|
||||
*/
|
||||
async createFulfillment(
|
||||
data: object,
|
||||
items: object[],
|
||||
order: object | undefined,
|
||||
fulfillment: Record<string, unknown>
|
||||
): Promise<any> {
|
||||
data: Record<string, unknown>,
|
||||
items: Partial<Omit<FulfillmentItemDTO, "fulfillment">>[],
|
||||
order: Partial<FulfillmentOrderDTO> | undefined,
|
||||
fulfillment: Partial<Omit<FulfillmentDTO, "provider_id" | "data" | "items">>
|
||||
): Promise<CreateFulfillmentResult> {
|
||||
throw Error("createFulfillment must be overridden by the child class")
|
||||
}
|
||||
|
||||
@@ -362,12 +368,12 @@ export class AbstractFulfillmentProviderService
|
||||
* purchasing a label for the return fulfillment.
|
||||
*
|
||||
* @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
|
||||
* 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
|
||||
* // in the third-party service
|
||||
* const externalData = await this.client.createReturn(
|
||||
@@ -385,7 +391,7 @@ export class AbstractFulfillmentProviderService
|
||||
*/
|
||||
async createReturnFulfillment(
|
||||
fulfillment: Record<string, unknown>
|
||||
): Promise<any> {
|
||||
): Promise<CreateFulfillmentResult> {
|
||||
throw Error("createReturn must be overridden by the child class")
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
SoftDeleteReturn,
|
||||
UpdateFulfillmentSetDTO,
|
||||
UpdateServiceZoneDTO,
|
||||
ValidateFulfillmentDataContext,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
arrayDifference,
|
||||
@@ -650,7 +651,7 @@ export default class FulfillmentModuleService
|
||||
fulfillmentData || {},
|
||||
items.map((i) => i),
|
||||
order,
|
||||
fulfillmentRest
|
||||
fulfillmentRest as unknown as Partial<FulfillmentDTO>
|
||||
)
|
||||
await this.fulfillmentService_.update(
|
||||
{
|
||||
@@ -710,11 +711,22 @@ export default class FulfillmentModuleService
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const shippingOption = await this.shippingOptionService_.retrieve(
|
||||
fulfillment.shipping_option_id!,
|
||||
{
|
||||
select: ["id", "name", "data", "metadata"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
try {
|
||||
const providerResult =
|
||||
await this.fulfillmentProviderService_.createReturn(
|
||||
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(
|
||||
{
|
||||
@@ -2002,7 +2014,7 @@ export default class FulfillmentModuleService
|
||||
providerId: string,
|
||||
optionData: Record<string, unknown>,
|
||||
data: Record<string, unknown>,
|
||||
context: Record<string, unknown>
|
||||
context: ValidateFulfillmentDataContext
|
||||
): Promise<Record<string, unknown>> {
|
||||
return await this.fulfillmentProviderService_.validateFulfillmentData(
|
||||
providerId,
|
||||
@@ -2012,6 +2024,7 @@ export default class FulfillmentModuleService
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: seems not to be used, what is the purpose of this method?
|
||||
async validateFulfillmentOption(
|
||||
providerId: string,
|
||||
data: Record<string, unknown>
|
||||
@@ -2058,10 +2071,7 @@ export default class FulfillmentModuleService
|
||||
}
|
||||
|
||||
const promises = shippingOptionsData.map((option) =>
|
||||
this.fulfillmentProviderService_.canCalculate(
|
||||
option.provider_id,
|
||||
option as unknown as Record<string, unknown>
|
||||
)
|
||||
this.fulfillmentProviderService_.canCalculate(option.provider_id, option)
|
||||
)
|
||||
|
||||
return await promiseAll(promises)
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import {
|
||||
CalculateShippingOptionPriceDTO,
|
||||
Constructor,
|
||||
CreateFulfillmentResult,
|
||||
CreateShippingOptionDTO,
|
||||
DAL,
|
||||
FulfillmentDTO,
|
||||
FulfillmentItemDTO,
|
||||
FulfillmentOption,
|
||||
FulfillmentOrderDTO,
|
||||
FulfillmentTypes,
|
||||
IFulfillmentProvider,
|
||||
Logger,
|
||||
ValidateFulfillmentDataContext,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
MedusaError,
|
||||
@@ -91,7 +97,7 @@ export default class FulfillmentProviderService extends ModulesSdkUtils.MedusaIn
|
||||
providerId: string,
|
||||
optionData: Record<string, unknown>,
|
||||
data: Record<string, unknown>,
|
||||
context: Record<string, unknown>
|
||||
context: ValidateFulfillmentDataContext
|
||||
) {
|
||||
const provider = this.retrieveProviderRegistration(providerId)
|
||||
return await provider.validateFulfillmentData(optionData, data, context)
|
||||
@@ -102,7 +108,7 @@ export default class FulfillmentProviderService extends ModulesSdkUtils.MedusaIn
|
||||
return await provider.validateOption(data)
|
||||
}
|
||||
|
||||
async canCalculate(providerId: string, data: Record<string, unknown>) {
|
||||
async canCalculate(providerId: string, data: CreateShippingOptionDTO) {
|
||||
const provider = this.retrieveProviderRegistration(providerId)
|
||||
return await provider.canCalculate(data)
|
||||
}
|
||||
@@ -119,11 +125,11 @@ export default class FulfillmentProviderService extends ModulesSdkUtils.MedusaIn
|
||||
|
||||
async createFulfillment(
|
||||
providerId: string,
|
||||
data: object,
|
||||
items: object[],
|
||||
order: object | undefined,
|
||||
fulfillment: Record<string, unknown>
|
||||
): Promise<Record<string, unknown>> {
|
||||
data: Record<string, unknown>,
|
||||
items: Partial<Omit<FulfillmentItemDTO, "fulfillment">>[],
|
||||
order: Partial<FulfillmentOrderDTO> | undefined,
|
||||
fulfillment: Partial<Omit<FulfillmentDTO, "provider_id" | "data" | "items">>
|
||||
): Promise<CreateFulfillmentResult> {
|
||||
const provider = this.retrieveProviderRegistration(providerId)
|
||||
return await provider.createFulfillment(data, items, order, fulfillment)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { AbstractFulfillmentProviderService } from "@medusajs/framework/utils"
|
||||
import {
|
||||
CalculatedShippingOptionPrice,
|
||||
CalculateShippingOptionPriceContext,
|
||||
CreateFulfillmentResult,
|
||||
FulfillmentOption,
|
||||
ValidateFulfillmentDataContext,
|
||||
} from "@medusajs/types"
|
||||
|
||||
// TODO rework type and DTO's
|
||||
@@ -28,7 +31,7 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
||||
async validateFulfillmentData(
|
||||
optionData: Record<string, unknown>,
|
||||
data: Record<string, unknown>,
|
||||
context: Record<string, unknown>
|
||||
context: ValidateFulfillmentDataContext
|
||||
): Promise<any> {
|
||||
return data
|
||||
}
|
||||
@@ -36,7 +39,7 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
||||
async calculatePrice(
|
||||
optionData: Record<string, unknown>,
|
||||
data: Record<string, unknown>,
|
||||
context: Record<string, unknown>
|
||||
context: CalculateShippingOptionPriceContext
|
||||
): Promise<CalculatedShippingOptionPrice> {
|
||||
throw new Error("Manual fulfillment does not support price calculation")
|
||||
}
|
||||
@@ -49,16 +52,22 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
|
||||
return true
|
||||
}
|
||||
|
||||
async createFulfillment(): Promise<Record<string, any>> {
|
||||
async createFulfillment(): Promise<CreateFulfillmentResult> {
|
||||
// No data is being sent anywhere
|
||||
return {}
|
||||
return {
|
||||
data: {},
|
||||
labels: [],
|
||||
}
|
||||
}
|
||||
|
||||
async cancelFulfillment(): Promise<any> {
|
||||
return {}
|
||||
}
|
||||
|
||||
async createReturnFulfillment(): Promise<any> {
|
||||
return {}
|
||||
async createReturnFulfillment(): Promise<CreateFulfillmentResult> {
|
||||
return {
|
||||
data: {},
|
||||
labels: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user