OrderModifier
OrderModifier
This helper is responsible for modifying the contents of an Order.
Note:
There is not a clear separation of concerns between the OrderService and this, since
the OrderService also contains some method which modify the Order (e.g. removeItemFromOrder).
So this helper was mainly extracted to isolate the huge modifyOrder
method since the
OrderService was just growing too large. Future refactoring could improve the organization
of these Order-related methods into a more clearly-delineated set of classes.
class OrderModifier {
constructor(connection: TransactionalConnection, configService: ConfigService, orderCalculator: OrderCalculator, paymentService: PaymentService, countryService: CountryService, stockMovementService: StockMovementService, productVariantService: ProductVariantService, customFieldRelationService: CustomFieldRelationService, promotionService: PromotionService, eventBus: EventBus, shippingCalculator: ShippingCalculator, historyService: HistoryService, translator: TranslatorService)
constrainQuantityToSaleable(ctx: RequestContext, variant: ProductVariant, quantity: number, existingOrderLineQuantity: = 0, quantityInOtherOrderLines: = 0) => ;
getExistingOrderLine(ctx: RequestContext, order: Order, productVariantId: ID, customFields?: { [key: string]: any }) => Promise<OrderLine | undefined>;
getOrCreateOrderLine(ctx: RequestContext, order: Order, productVariantId: ID, customFields?: { [key: string]: any }) => ;
updateOrderLineQuantity(ctx: RequestContext, orderLine: OrderLine, quantity: number, order: Order) => Promise<OrderLine>;
cancelOrderByOrderLines(ctx: RequestContext, input: CancelOrderInput, lineInputs: OrderLineInput[]) => ;
modifyOrder(ctx: RequestContext, input: ModifyOrderInput, order: Order) => Promise<JustErrorResults<ModifyOrderResult> | { order: Order; modification: OrderModification }>;
setShippingMethods(ctx: RequestContext, order: Order, shippingMethodIds: ID[]) => ;
}
constructor
(connection: TransactionalConnection, configService: ConfigService, orderCalculator: OrderCalculator, paymentService: PaymentService, countryService: CountryService, stockMovementService: StockMovementService, productVariantService: ProductVariantService, customFieldRelationService: CustomFieldRelationService, promotionService: PromotionService, eventBus: EventBus, shippingCalculator: ShippingCalculator, historyService: HistoryService, translator: TranslatorService) => OrderModifier
constrainQuantityToSaleable
(ctx: RequestContext, variant: ProductVariant, quantity: number, existingOrderLineQuantity: = 0, quantityInOtherOrderLines: = 0) =>
Ensure that the ProductVariant has sufficient saleable stock to add the given quantity to an Order.
existingOrderLineQuantity
is used when adding an item to the order, since if an OrderLine already exists then we will be adding the new quantity to the existing quantity.quantityInOtherOrderLines
is used when we have more than 1 OrderLine containing the same ProductVariant. This occurs when there are custom fields defined on the OrderLine and the lines have differing values for one or more custom fields. In this case, we need to take all of these OrderLines into account when constraining the quantity. See https://github.com/vendure-ecommerce/vendure/issues/2702 for more on this.
getExistingOrderLine
(ctx: RequestContext, order: Order, productVariantId: ID, customFields?: { [key: string]: any }) => Promise<OrderLine | undefined>
Given a ProductVariant ID and optional custom fields, this method will return an existing OrderLine that
matches, or undefined
if no match is found.
getOrCreateOrderLine
(ctx: RequestContext, order: Order, productVariantId: ID, customFields?: { [key: string]: any }) =>
Returns the OrderLine containing the given ProductVariant, taking into account any custom field values. If no existing OrderLine is found, a new OrderLine will be created.
updateOrderLineQuantity
(ctx: RequestContext, orderLine: OrderLine, quantity: number, order: Order) => Promise<OrderLine>
Updates the quantity of an OrderLine, taking into account the available saleable stock level.
Returns the actual quantity that the OrderLine was updated to (which may be less than the
quantity
argument if insufficient stock was available.
cancelOrderByOrderLines
(ctx: RequestContext, input: CancelOrderInput, lineInputs: OrderLineInput[]) =>
modifyOrder
(ctx: RequestContext, input: ModifyOrderInput, order: Order) => Promise<JustErrorResults<ModifyOrderResult> | { order: Order; modification: OrderModification }>
setShippingMethods
(ctx: RequestContext, order: Order, shippingMethodIds: ID[]) =>