Files
medusa-store/www/apps/docs/content/references/services/classes/ShippingOptionService.mdx
2023-11-29 11:58:08 +00:00

1097 lines
24 KiB
Plaintext

---
displayed_sidebar: servicesSidebar
---
import ParameterTypes from "@site/src/components/ParameterTypes"
# ShippingOptionService
Provides layer to manipulate profiles.
## constructor
`**new ShippingOptionService**(«destructured»)`
### Parameters
<ParameterTypes parameters={[
{
"name": "__namedParameters",
"type": "[InjectedDependencies](../types/InjectedDependencies-37.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
___
## Properties
<ParameterTypes parameters={[
{
"name": "__configModule__",
"type": "`Record<string, unknown>`",
"description": "",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "__container__",
"type": "`any`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "__moduleDeclaration__",
"type": "`Record<string, unknown>`",
"description": "",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "featureFlagRouter_",
"type": "[FlagRouter](FlagRouter.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "manager_",
"type": "EntityManager",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "methodRepository_",
"type": "Repository&#60;[ShippingMethod](ShippingMethod.mdx)&#62;",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "optionRepository_",
"type": "Repository&#60;[ShippingOption](ShippingOption.mdx)&#62; & ``{ upsertShippingProfile: Method upsertShippingProfile }``",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "providerService_",
"type": "[FulfillmentProviderService](FulfillmentProviderService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "regionService_",
"type": "[RegionService](RegionService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "requirementRepository_",
"type": "Repository&#60;[ShippingOptionRequirement](ShippingOptionRequirement.mdx)&#62;",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "transactionManager_",
"type": "`undefined` \\| EntityManager",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
___
## Accessors
### activeManager\_
`Protected get**activeManager_**(): EntityManager`
#### Returns
EntityManager
<ParameterTypes parameters={[
{
"name": "EntityManager",
"type": "EntityManager",
"optional": false,
"defaultValue": "",
"description": "",
"expandable": false,
"children": []
}
]} />
___
## Methods
### addRequirement
`**addRequirement**(optionId, requirement): Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;`
Adds a requirement to a shipping option. Only 1 requirement of each type
is allowed.
#### Parameters
<ParameterTypes parameters={[
{
"name": "optionId",
"type": "`string`",
"description": "the option to add the requirement to.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "requirement",
"type": "[ShippingOptionRequirement](ShippingOptionRequirement.mdx)",
"description": "the requirement for the option.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the result of update",
"expandable": false,
"children": []
}
]} />
___
### atomicPhase\_
`Protected **atomicPhase_**<TypeParameter TResult, TypeParameter TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise&#60;TResult&#62;`
Wraps some work within a transactional block. If the service already has
a transaction manager attached this will be reused, otherwise a new
transaction manager is created.
#### Type Parameters
<ParameterTypes parameters={[
{
"name": "TResult",
"type": "`object`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "TError",
"type": "`object`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Parameters
<ParameterTypes parameters={[
{
"name": "work",
"type": "(`transactionManager`: EntityManager) => Promise&#60;TResult&#62;",
"description": "the transactional work to be done",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "isolationOrErrorHandler",
"type": "`IsolationLevel` \\| (`error`: `TError`) => Promise&#60;void \\| TResult&#62;",
"description": "the isolation level to be used for the work.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "maybeErrorHandlerOrDontFail",
"type": "(`error`: `TError`) => Promise&#60;void \\| TResult&#62;",
"description": "Potential error handler",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;TResult&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;TResult&#62;",
"optional": false,
"defaultValue": "",
"description": "the result of the transactional work",
"expandable": false,
"children": []
}
]} />
___
### create
`**create**(data): Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;`
Creates a new shipping option. Used both for outbound and inbound shipping
options. The difference is registered by the `is_return` field which
defaults to false.
#### Parameters
<ParameterTypes parameters={[
{
"name": "data",
"type": "[CreateShippingOptionInput](../types/CreateShippingOptionInput.mdx)",
"description": "the data to create shipping options",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the result of the create operation",
"expandable": false,
"children": []
}
]} />
___
### createShippingMethod
`**createShippingMethod**(optionId, data, config): Promise&#60;[ShippingMethod](ShippingMethod.mdx)&#62;`
Creates a shipping method for a given cart.
#### Parameters
<ParameterTypes parameters={[
{
"name": "optionId",
"type": "`string`",
"description": "the id of the option to use for the method.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "data",
"type": "`Record<string, unknown>`",
"description": "the optional provider data to use.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "config",
"type": "[CreateShippingMethodDto](../types/CreateShippingMethodDto.mdx)",
"description": "the cart to create the shipping method for.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingMethod](ShippingMethod.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingMethod](ShippingMethod.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the resulting shipping method.",
"expandable": false,
"children": []
}
]} />
___
### delete
`**delete**(optionId): Promise&#60;void \| [ShippingOption](ShippingOption.mdx)&#62;`
Deletes a profile with a given profile id.
#### Parameters
<ParameterTypes parameters={[
{
"name": "optionId",
"type": "`string`",
"description": "the id of the profile to delete. Must be castable as an ObjectId",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;void \| [ShippingOption](ShippingOption.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;void \\| [ShippingOption](ShippingOption.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the result of the delete operation.",
"expandable": false,
"children": []
}
]} />
___
### deleteShippingMethods
`**deleteShippingMethods**(shippingMethods): Promise&#60;[ShippingMethod](ShippingMethod.mdx)[]&#62;`
Removes a given shipping method
#### Parameters
<ParameterTypes parameters={[
{
"name": "shippingMethods",
"type": "[ShippingMethod](ShippingMethod.mdx) \\| [ShippingMethod](ShippingMethod.mdx)[]",
"description": "the shipping method to remove",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingMethod](ShippingMethod.mdx)[]&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingMethod](ShippingMethod.mdx)[]&#62;",
"optional": false,
"defaultValue": "",
"description": "removed shipping methods",
"expandable": false,
"children": []
}
]} />
___
### getPrice\_
`**getPrice_**(option, data, cart): Promise&#60;number&#62;`
Returns the amount to be paid for a shipping method. Will ask the
fulfillment provider to calculate the price if the shipping option has the
price type "calculated".
#### Parameters
<ParameterTypes parameters={[
{
"name": "option",
"type": "[ShippingOption](ShippingOption.mdx)",
"description": "the shipping option to retrieve the price for.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "data",
"type": "`Record<string, unknown>`",
"description": "the shipping data to retrieve the price.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "cart",
"type": "`undefined` \\| [Order](Order.mdx) \\| [Cart](Cart.mdx)",
"description": "the context in which the price should be retrieved.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;number&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;number&#62;",
"optional": false,
"defaultValue": "",
"description": "the price of the shipping option.",
"expandable": false,
"children": []
}
]} />
___
### list
`**list**(selector, config?): Promise&#60;[ShippingOption](ShippingOption.mdx)[]&#62;`
#### Parameters
<ParameterTypes parameters={[
{
"name": "selector",
"type": "[Selector](../types/Selector.mdx)&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"description": "the query object for find",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "config",
"type": "[FindConfig](../interfaces/FindConfig.mdx)&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"description": "config object",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOption](ShippingOption.mdx)[]&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOption](ShippingOption.mdx)[]&#62;",
"optional": false,
"defaultValue": "",
"description": "the result of the find operation",
"expandable": false,
"children": []
}
]} />
___
### listAndCount
`**listAndCount**(selector, config?): Promise&#60;[[ShippingOption](ShippingOption.mdx)[], number]&#62;`
#### Parameters
<ParameterTypes parameters={[
{
"name": "selector",
"type": "[Selector](../types/Selector.mdx)&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"description": "the query object for find",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "config",
"type": "[FindConfig](../interfaces/FindConfig.mdx)&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"description": "config object",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[[ShippingOption](ShippingOption.mdx)[], number]&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[[ShippingOption](ShippingOption.mdx)[], number]&#62;",
"optional": false,
"defaultValue": "",
"description": "the result of the find operation",
"expandable": false,
"children": []
}
]} />
___
### removeRequirement
`**removeRequirement**(requirementId): Promise&#60;void \| [ShippingOptionRequirement](ShippingOptionRequirement.mdx)&#62;`
Removes a requirement from a shipping option
#### Parameters
<ParameterTypes parameters={[
{
"name": "requirementId",
"type": "`any`",
"description": "the id of the requirement to remove",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;void \| [ShippingOptionRequirement](ShippingOptionRequirement.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;void \\| [ShippingOptionRequirement](ShippingOptionRequirement.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the result of update",
"expandable": false,
"children": []
}
]} />
___
### retrieve
`**retrieve**(optionId, options?): Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;`
Gets a profile by id.
Throws in case of DB Error and if profile was not found.
#### Parameters
<ParameterTypes parameters={[
{
"name": "optionId",
"type": "`any`",
"description": "the id of the profile to get.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "options",
"type": "[FindConfig](../interfaces/FindConfig.mdx)&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"description": "the options to get a profile",
"optional": false,
"defaultValue": "{}",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the profile document.",
"expandable": false,
"children": []
}
]} />
___
### shouldRetryTransaction\_
`Protected **shouldRetryTransaction_**(err): boolean`
#### Parameters
<ParameterTypes parameters={[
{
"name": "err",
"type": "`Record<string, unknown>` \\| ``{ code: string }``",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
`boolean`
<ParameterTypes parameters={[
{
"name": "boolean",
"type": "`boolean`",
"optional": true,
"defaultValue": "",
"description": "",
"expandable": false,
"children": []
}
]} />
___
### update
`**update**(optionId, update): Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;`
Updates a profile. Metadata updates and product updates should use
dedicated methods, e.g. `setMetadata`, etc. The function
will throw errors if metadata or product updates are attempted.
#### Parameters
<ParameterTypes parameters={[
{
"name": "optionId",
"type": "`string`",
"description": "the id of the option. Must be a string that can be casted to an ObjectId",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "update",
"type": "[UpdateShippingOptionInput](../types/UpdateShippingOptionInput.mdx)",
"description": "an object with the update values.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOption](ShippingOption.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "resolves to the update result.",
"expandable": false,
"children": []
}
]} />
___
### updateShippingMethod
`**updateShippingMethod**(id, update): Promise&#60;undefined \| [ShippingMethod](ShippingMethod.mdx)&#62;`
Updates a shipping method's associations. Useful when a cart is completed
and its methods should be copied to an order/swap entity.
#### Parameters
<ParameterTypes parameters={[
{
"name": "id",
"type": "`string`",
"description": "the id of the shipping method to update",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "update",
"type": "[ShippingMethodUpdate](../types/ShippingMethodUpdate.mdx)",
"description": "the values to update the method with",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;undefined \| [ShippingMethod](ShippingMethod.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;undefined \\| [ShippingMethod](ShippingMethod.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the resulting shipping method",
"expandable": false,
"children": []
}
]} />
___
### updateShippingProfile
`**updateShippingProfile**(optionIds, profileId): Promise&#60;[ShippingOption](ShippingOption.mdx)[]&#62;`
#### Parameters
<ParameterTypes parameters={[
{
"name": "optionIds",
"type": "`string` \\| `string`[]",
"description": "ID or IDs of the shipping options to update",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "profileId",
"type": "`string`",
"description": "Shipping profile ID to update the shipping options with",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOption](ShippingOption.mdx)[]&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOption](ShippingOption.mdx)[]&#62;",
"optional": false,
"defaultValue": "",
"description": "updated shipping options",
"expandable": false,
"children": []
}
]} />
___
### validateAndMutatePrice
`Private **validateAndMutatePrice**(option, priceInput): Promise&#60;[CreateShippingOptionInput](../types/CreateShippingOptionInput.mdx) \| [Omit](../types/Omit.mdx)&#60;[ShippingOption](ShippingOption.mdx), "beforeInsert"&#62;&#62;`
#### Parameters
<ParameterTypes parameters={[
{
"name": "option",
"type": "[ShippingOption](ShippingOption.mdx) \\| [CreateShippingOptionInput](../types/CreateShippingOptionInput.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "priceInput",
"type": "[ValidatePriceTypeAndAmountInput](../types/ValidatePriceTypeAndAmountInput.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[CreateShippingOptionInput](../types/CreateShippingOptionInput.mdx) \| [Omit](../types/Omit.mdx)&#60;[ShippingOption](ShippingOption.mdx), "beforeInsert"&#62;&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[CreateShippingOptionInput](../types/CreateShippingOptionInput.mdx) \\| [Omit](../types/Omit.mdx)&#60;[ShippingOption](ShippingOption.mdx), \"beforeInsert\"&#62;&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"expandable": false,
"children": []
}
]} />
___
### validateCartOption
`**validateCartOption**(option, cart): Promise&#60;null \| [ShippingOption](ShippingOption.mdx)&#62;`
Checks if a given option id is a valid option for a cart. If it is the
option is returned with the correct price. Throws when region\_ids do not
match, or when the shipping option requirements are not satisfied.
#### Parameters
<ParameterTypes parameters={[
{
"name": "option",
"type": "[ShippingOption](ShippingOption.mdx)",
"description": "the option object to check",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "cart",
"type": "[Cart](Cart.mdx)",
"description": "the cart object to check against",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;null \| [ShippingOption](ShippingOption.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;null \\| [ShippingOption](ShippingOption.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the validated shipping option",
"expandable": false,
"children": []
}
]} />
___
### validatePriceType\_
`**validatePriceType_**(priceType, option): Promise&#60;[ShippingOptionPriceType](../enums/ShippingOptionPriceType.mdx)&#62;`
Validates a shipping option price
#### Parameters
<ParameterTypes parameters={[
{
"name": "priceType",
"type": "[ShippingOptionPriceType](../enums/ShippingOptionPriceType.mdx)",
"description": "the price to validate",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "option",
"type": "[ShippingOption](ShippingOption.mdx)",
"description": "the option to validate against",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOptionPriceType](../enums/ShippingOptionPriceType.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOptionPriceType](../enums/ShippingOptionPriceType.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "the validated price",
"expandable": false,
"children": []
}
]} />
___
### validateRequirement\_
`**validateRequirement_**(requirement, optionId?): Promise&#60;[ShippingOptionRequirement](ShippingOptionRequirement.mdx)&#62;`
Validates a requirement
#### Parameters
<ParameterTypes parameters={[
{
"name": "requirement",
"type": "[ShippingOptionRequirement](ShippingOptionRequirement.mdx)",
"description": "the requirement to validate",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "optionId",
"type": "`undefined` \\| `string`",
"description": "the id to validate the requirement",
"optional": false,
"defaultValue": "undefined",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise&#60;[ShippingOptionRequirement](ShippingOptionRequirement.mdx)&#62;
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[ShippingOptionRequirement](ShippingOptionRequirement.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "a validated shipping requirement",
"expandable": false,
"children": []
}
]} />
___
### withTransaction
`**withTransaction**(transactionManager?): [ShippingOptionService](ShippingOptionService.mdx)`
#### Parameters
<ParameterTypes parameters={[
{
"name": "transactionManager",
"type": "EntityManager",
"description": "",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
[ShippingOptionService](ShippingOptionService.mdx)
<ParameterTypes parameters={[
{
"name": "ShippingOptionService",
"type": "[ShippingOptionService](ShippingOptionService.mdx)",
"optional": false,
"defaultValue": "",
"description": "",
"expandable": false,
"children": []
}
]} />