Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order totalQuantity, total and shippingLines not updated after an OrderLine is removed #2548

Closed
geckozr opened this issue Nov 25, 2023 · 1 comment
Assignees
Labels
type: bug 🐛 Something isn't working

Comments

@geckozr
Copy link

geckozr commented Nov 25, 2023

While working on the checkout flow, we discovered that the order is not updated correctly after an orderLine is removed from an Order.

To Reproduce

  • Add two items from two different sellers.
  • Assign the shipping methods from the eligible shipping options.
  • Remove one of the orderLines.
  • The response will return successfully, but with some properties not correctly updated, such as subTotalWithTax or totalQuantity.

Expected behavior
The response should return the updated order with the correct information

Environment:

  • @vendure/core version: 2.1.4
  • Nodejs version: 18.16.0
  • Database PostgreSQL 14.5 (Debian 14.5-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

Additional context
Possible solution:
Remove the "orphans" shippingLines and update the Order in the database before calling the applyPriceAdjustments method."

I have tried to implement a fix:

/**
 * @description
 * Removes the specified OrderLine from the Order.
 */
async removeItemFromOrder(
    ctx: RequestContext,
    orderId: ID,
    orderLineId: ID,
): Promise<ErrorResultUnion<RemoveOrderItemsResult, Order>> {
    const order = await this.getOrderOrThrow(ctx, orderId);
    const validationError = this.assertAddingItemsState(order);
    if (validationError) {
        return validationError;
    }
    const orderLine = this.getOrderLineOrThrow(order, orderLineId);
    order.lines = order.lines.filter(line => !idsAreEqual(line.id, orderLineId));

    // Remove shippingLines not valid for this order
    const orderLineShippingLineIds = order.lines.map(line => line.shippingLineId);

    // Get all the shippingLines that are not included in the order lines
    const shippingLinesToDelete = order.shippingLines.filter(
        shippingLine => !orderLineShippingLineIds.includes(shippingLine.id),
    );

    // In case some shippingLines are founded, remove it
    if (shippingLinesToDelete.length) {
        await this.connection.getRepository(ctx, ShippingLine).remove(shippingLinesToDelete);
    }

    // Update the order before Hydration
    await this.connection.getRepository(ctx, OrderLine).remove(orderLine);
    const updatedOrder = await this.applyPriceAdjustments(ctx, order);
    this.eventBus.publish(new OrderLineEvent(ctx, order, orderLine, 'deleted'));
    return updatedOrder;
}
@geckozr geckozr added the type: bug 🐛 Something isn't working label Nov 25, 2023
@michaelbromley michaelbromley moved this to 📋 Backlog in Vendure OS Roadmap Nov 27, 2023
@giosueDelgado
Copy link
Collaborator

Related: #2540

@michaelbromley michaelbromley moved this from 📋 Backlog to 🏗 In progress in Vendure OS Roadmap Jan 16, 2024
@michaelbromley michaelbromley moved this from 🏗 In progress to 🔖 Ready in Vendure OS Roadmap Jan 16, 2024
@michaelbromley michaelbromley moved this from 🔖 Ready to ✅ Done in Vendure OS Roadmap Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
Status: 🚀 Shipped
Development

No branches or pull requests

3 participants