Skip to main content
Skip to main content

ShippingOptionService

Provides layer to manipulate profiles.

Constructors

constructor

**new ShippingOptionService**(«destructured»)

Parameters

__namedParametersInjectedDependenciesRequired

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
featureFlagRouter_FlagRouterRequired
manager_EntityManagerRequired
methodRepository_Repository<ShippingMethod>Required
optionRepository_Repository<ShippingOption> & &#123; upsertShippingProfile: Method upsertShippingProfile &#125;Required
providerService_FulfillmentProviderServiceRequired
regionService_RegionServiceRequired
requirementRepository_Repository<ShippingOptionRequirement>Required
transactionManager_undefined | EntityManagerRequired

Accessors

activeManager_

Protected get**activeManager_**(): EntityManager

Returns

EntityManager

EntityManagerEntityManagerRequired

Methods

addRequirement

**addRequirement**(optionId, requirement): Promise&#60;[ShippingOption](/references/services/classes/ShippingOption)&#62;

Adds a requirement to a shipping option. Only 1 requirement of each type is allowed.

Parameters

optionIdstringRequired
the option to add the requirement to.
requirementShippingOptionRequirementRequired
the requirement for the option.

Returns

Promise<ShippingOption>

PromisePromise<ShippingOption>Required
the result of update

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.

TResultobjectRequired
TErrorobjectRequired

Parameters

work(transactionManager: EntityManager) => Promise<TResult>Required
the transactional work to be done
isolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>
the isolation level to be used for the work.
maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>
Potential error handler

Returns

Promise<TResult>

PromisePromise<TResult>Required
the result of the transactional work

create

**create**(data): Promise&#60;[ShippingOption](/references/services/classes/ShippingOption)&#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

the data to create shipping options

Returns

Promise<ShippingOption>

PromisePromise<ShippingOption>Required
the result of the create operation

createShippingMethod

**createShippingMethod**(optionId, data, config): Promise&#60;[ShippingMethod](/references/services/classes/ShippingMethod)&#62;

Creates a shipping method for a given cart.

Parameters

optionIdstringRequired
the id of the option to use for the method.
dataRecord<string, unknown>Required
the optional provider data to use.
the cart to create the shipping method for.

Returns

Promise<ShippingMethod>

PromisePromise<ShippingMethod>Required
the resulting shipping method.

delete

**delete**(optionId): Promise&#60;void \| [ShippingOption](/references/services/classes/ShippingOption)&#62;

Deletes a profile with a given profile id.

Parameters

optionIdstringRequired
the id of the profile to delete. Must be castable as an ObjectId

Returns

Promise<void | ShippingOption>

PromisePromise<void | ShippingOption>Required
the result of the delete operation.

deleteShippingMethods

**deleteShippingMethods**(shippingMethods): Promise&#60;[ShippingMethod](/references/services/classes/ShippingMethod)[]&#62;

Removes a given shipping method

Parameters

shippingMethodsShippingMethod | ShippingMethod[]Required
the shipping method to remove

Returns

Promise<ShippingMethod[]>

PromisePromise<ShippingMethod[]>Required
removed shipping methods

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

optionShippingOptionRequired
the shipping option to retrieve the price for.
dataRecord<string, unknown>Required
the shipping data to retrieve the price.
cartundefined | Order | CartRequired
the context in which the price should be retrieved.

Returns

Promise<number>

PromisePromise<number>Required
the price of the shipping option.

list

**list**(selector, config?): Promise&#60;[ShippingOption](/references/services/classes/ShippingOption)[]&#62;

Parameters

selectorSelector<ShippingOption>Required
the query object for find
configFindConfig<ShippingOption>Required
config object

Returns

Promise<ShippingOption[]>

PromisePromise<ShippingOption[]>Required
the result of the find operation

listAndCount

**listAndCount**(selector, config?): Promise&#60;[[ShippingOption](/references/services/classes/ShippingOption)[], number]&#62;

Parameters

selectorSelector<ShippingOption>Required
the query object for find
configFindConfig<ShippingOption>Required
config object

Returns

Promise<[ShippingOption[], number]>

PromisePromise<[ShippingOption[], number]>Required
the result of the find operation

removeRequirement

**removeRequirement**(requirementId): Promise&#60;void \| [ShippingOptionRequirement](/references/services/classes/ShippingOptionRequirement)&#62;

Removes a requirement from a shipping option

Parameters

requirementIdanyRequired
the id of the requirement to remove

Returns

Promise<void | ShippingOptionRequirement>

PromisePromise<void | ShippingOptionRequirement>Required
the result of update

retrieve

**retrieve**(optionId, options?): Promise&#60;[ShippingOption](/references/services/classes/ShippingOption)&#62;

Gets a profile by id. Throws in case of DB Error and if profile was not found.

Parameters

optionIdanyRequired
the id of the profile to get.
optionsFindConfig<ShippingOption>Required
the options to get a profile

Default: {}

Returns

Promise<ShippingOption>

PromisePromise<ShippingOption>Required
the profile document.

shouldRetryTransaction_

Protected **shouldRetryTransaction_**(err): boolean

Parameters

errRecord<string, unknown> | &#123; code: string &#125;Required

Returns

boolean

booleanboolean

update

**update**(optionId, update): Promise&#60;[ShippingOption](/references/services/classes/ShippingOption)&#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

optionIdstringRequired
the id of the option. Must be a string that can be casted to an ObjectId
an object with the update values.

Returns

Promise<ShippingOption>

PromisePromise<ShippingOption>Required
resolves to the update result.

updateShippingMethod

**updateShippingMethod**(id, update): Promise&#60;undefined \| [ShippingMethod](/references/services/classes/ShippingMethod)&#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

idstringRequired
the id of the shipping method to update
updateShippingMethodUpdateRequired
the values to update the method with

Returns

Promise<undefined | ShippingMethod>

PromisePromise<undefined | ShippingMethod>Required
the resulting shipping method

updateShippingProfile

**updateShippingProfile**(optionIds, profileId): Promise&#60;[ShippingOption](/references/services/classes/ShippingOption)[]&#62;

Parameters

optionIdsstring | string[]Required
ID or IDs of the shipping options to update
profileIdstringRequired
Shipping profile ID to update the shipping options with

Returns

Promise<ShippingOption[]>

PromisePromise<ShippingOption[]>Required
updated shipping options

validateAndMutatePrice

Private **validateAndMutatePrice**(option, priceInput): Promise&#60;[CreateShippingOptionInput](/references/services/types/CreateShippingOptionInput) \| [Omit](/references/services/types/Omit)&#60;[ShippingOption](/references/services/classes/ShippingOption), "beforeInsert"&#62;&#62;

Parameters

Returns

Promise<CreateShippingOptionInput | Omit<ShippingOption, "beforeInsert">>

PromisePromise<CreateShippingOptionInput | Omit<ShippingOption, "beforeInsert">>Required

validateCartOption

**validateCartOption**(option, cart): Promise&#60;null \| [ShippingOption](/references/services/classes/ShippingOption)&#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

optionShippingOptionRequired
the option object to check
cartCartRequired
the cart object to check against

Returns

Promise<null | ShippingOption>

PromisePromise<null | ShippingOption>Required
the validated shipping option

validatePriceType_

**validatePriceType_**(priceType, option): Promise&#60;[ShippingOptionPriceType](/references/services/enums/ShippingOptionPriceType)&#62;

Validates a shipping option price

Parameters

priceTypeShippingOptionPriceTypeRequired
the price to validate
optionShippingOptionRequired
the option to validate against

Returns

Promise<ShippingOptionPriceType>

PromisePromise<ShippingOptionPriceType>Required
the validated price

validateRequirement_

**validateRequirement_**(requirement, optionId?): Promise&#60;[ShippingOptionRequirement](/references/services/classes/ShippingOptionRequirement)&#62;

Validates a requirement

Parameters

requirementShippingOptionRequirementRequired
the requirement to validate
optionIdundefined | stringRequired
the id to validate the requirement

Default: undefined

Returns

Promise<ShippingOptionRequirement>

PromisePromise<ShippingOptionRequirement>Required
a validated shipping requirement

withTransaction

**withTransaction**(transactionManager?): [ShippingOptionService](/references/services/classes/ShippingOptionService)

Parameters

transactionManagerEntityManager

Returns

ShippingOptionService

ShippingOptionServiceShippingOptionServiceRequired
Was this section helpful?