Skip to main content
Skip to main content

SwapService

Handles swaps

constructor

Parameters

__namedParametersInjectedPropsRequired

Properties

manager_EntityManagerRequired
transactionManager_undefined | EntityManagerRequired
__container__anyRequired
EventsobjectRequired
Events.CREATEDstringRequired

Default: "swap.created"

Events.RECEIVEDstringRequired

Default: "swap.received"

Events.SHIPMENT_CREATEDstringRequired

Default: "swap.shipment_created"

Events.PAYMENT_COMPLETEDstringRequired

Default: "swap.payment_completed"

Events.PAYMENT_CAPTUREDstringRequired

Default: "swap.payment_captured"

Events.PAYMENT_CAPTURE_FAILEDstringRequired

Default: "swap.payment_capture_failed"

Events.PROCESS_REFUND_FAILEDstringRequired

Default: "swap.process_refund_failed"

Events.REFUND_PROCESSEDstringRequired

Default: "swap.refund_processed"

Events.FULFILLMENT_CREATEDstringRequired

Default: "swap.fulfillment_created"

swapRepository_Repository<Swap>Required
cartService_CartServiceRequired
eventBus_EventBusServiceRequired
orderService_OrderServiceRequired
returnService_ReturnServiceRequired
totalsService_TotalsServiceRequired
lineItemService_LineItemServiceRequired
fulfillmentService_FulfillmentServiceRequired
shippingOptionService_ShippingOptionServiceRequired
paymentProviderService_PaymentProviderServiceRequired
lineItemAdjustmentService_LineItemAdjustmentServiceRequired
customShippingOptionService_CustomShippingOptionServiceRequired
productVariantInventoryService_ProductVariantInventoryServiceRequired
__configModule__Record<string, unknown>
__moduleDeclaration__Record<string, unknown>

Accessors

activeManager_

Returns

EntityManagerEntityManagerRequired

Methods

withTransaction

Parameters

transactionManagerEntityManager

Returns

thisthisRequired

shouldRetryTransaction_

Parameters

errRecord<string, unknown> | objectRequired

Returns

booleanbooleanRequired

atomicPhase_

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

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

PromisePromise<TResult>Required
the result of the transactional work

transformQueryForCart

Transform find config object for retrieval.

Parameters

configOmit<FindConfig<Swap>, "select"> & objectRequired
parsed swap find config

Returns

OmitOmit<FindConfig<Swap>, "select">Required
cartSelectsundefined | keyof Cart[]Required
cartRelationsundefined | string[]Required
selectstring[]

retrieve

Retrieves a swap with the given id.

Parameters

swapIdstringRequired
the id of the swap to retrieve
configOmit<FindConfig<Swap>, "select"> & objectRequired
the configuration to retrieve the swap

Default: {}

Returns

PromisePromise<Swap>Required
the swap

retrieveByCartId

Retrieves a swap based on its associated cart id

Parameters

cartIdstringRequired
the cart id that the swap's cart has
relationsundefined | string[]Required
the relations to retrieve swap

Default: []

Returns

PromisePromise<Swap>Required
the swap

list

List swaps.

Parameters

selectorSelector<Swap>Required
the query object for find
configFindConfig<Swap>Required
the configuration used to find the objects. contains relations, skip, and take.

Returns

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

listAndCount

List swaps.

Parameters

selectorSelector<Swap>Required
the query object for find
configFindConfig<Swap>Required
the configuration used to find the objects. contains relations, skip, and take.

Returns

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

create

Creates a swap from an order, with given return items, additional items and an optional return shipping method.

Parameters

orderOrderRequired
the order to base the swap off
returnItemsWithRequiredProperty<Partial<ReturnItem>, "item_id">[]Required
the items to return in the swap
returnShipping.option_idstringRequired
customobjectRequired
contains relevant custom information. This object may include no_notification which will disable sending notification when creating swap. If set, it overrules the attribute inherited from the order
additionalItemsPick<LineItem, "variant_id" | "quantity">[]
the items to send to the customer
returnShippingobject
an optional shipping method for returning the returnItems
returnShipping.pricenumber
custom.no_notificationboolean
custom.idempotency_keystring
custom.allow_backorderboolean
custom.location_idstring

Returns

PromisePromise<Swap>Required
the newly created swap

processDifference

Process difference for the requested swap.

Parameters

swapIdstringRequired
id of a swap being processed

Returns

PromisePromise<Swap>Required
processed swap

update

Update the swap record.

Parameters

swapIdstringRequired
id of a swap to update
updatePartial<Swap>Required
new data

Returns

PromisePromise<Swap>Required
updated swap record

createCart

Creates a cart from the given swap. The cart can be used to pay for differences associated with the swap. The swap represented by the swapId must belong to the order. Fails if there is already a cart on the swap.

Parameters

swapIdstringRequired
the id of the swap to create the cart from
customShippingOptionsobject[]Required
the shipping options

Default: []

contextobjectRequired

Default: {}

context.sales_channel_idstring

Returns

PromisePromise<Swap>Required
the swap with its cart_id prop set to the id of the new cart.

registerCartCompletion

Register a cart completion

Parameters

swapIdstringRequired
The id of the swap

Returns

PromisePromise<Swap>Required
swap related to the cart

cancel

Cancels a given swap if possible. A swap can only be canceled if all related returns, fulfillments, and payments have been canceled. If a swap is associated with a refund, it cannot be canceled.

Parameters

swapIdstringRequired
the id of the swap to cancel.

Returns

PromisePromise<Swap>Required
the canceled swap.

createFulfillment

Fulfills the additional items associated with the swap. Will call the fulfillment providers associated with the shipping methods.

Parameters

swapIdstringRequired
the id of the swap to fulfill,
configCreateShipmentConfigRequired
optional configurations, includes optional metadata to attach to the shipment, and a no_notification flag.

Returns

PromisePromise<Swap>Required
the updated swap with new status and fulfillments.

cancelFulfillment

Cancels a fulfillment (if related to a swap)

Parameters

fulfillmentIdstringRequired
the ID of the fulfillment to cancel

Returns

PromisePromise<Swap>Required
updated swap

createShipment

Marks a fulfillment as shipped and attaches tracking numbers.

Parameters

swapIdstringRequired
the id of the swap that has been shipped.
fulfillmentIdstringRequired
the id of the specific fulfillment that has been shipped
configCreateShipmentConfigRequired
optional configurations, includes optional metadata to attach to the shipment, and a noNotification flag.
trackingLinksobject[]
the tracking numbers associated with the shipment

Returns

PromisePromise<Swap>Required
the updated swap with new fulfillments and status.

deleteMetadata

Dedicated method to delete metadata for a swap.

Parameters

swapIdstringRequired
the order to delete metadata from.
keystringRequired
key for metadata field

Returns

PromisePromise<Swap>Required
resolves to the updated result.

registerReceived

Registers the swap return items as received so that they cannot be used as a part of other swaps/returns.

Parameters

idanyRequired
the id of the order with the swap.

Returns

PromisePromise<Swap>Required
the resulting order

areReturnItemsValid

Parameters

returnItemsWithRequiredProperty<Partial<ReturnItem>, "item_id">[]Required

Returns

PromisePromise<boolean>Required
Was this section helpful?