Skip to content

Instantly share code, notes, and snippets.

@surendra-wal
Last active November 26, 2020 04:00
Show Gist options
  • Save surendra-wal/0193d6fea9a54d5c8b744b5952d2165d to your computer and use it in GitHub Desktop.
Save surendra-wal/0193d6fea9a54d5c8b744b5952d2165d to your computer and use it in GitHub Desktop.
Inventory item changes for per pay order

Inventoty will be updated right after an newItem/updateItem/deleteItem for pre pay orders

NewItem

inventoryObj() {
    const changeInQuantity = -this.item.count;
    const inventoryItemId = this.item.priceId;
    return { changeInQuantity, inventoryItemId };
}

EditItem

inventoryObj() {
    const changeInQuantity = this.currentItem.quantity - this.item.count;
    const inventoryItemId = this.item.priceId;
    return { changeInQuantity, inventoryItemId };
}

DeleteItem

inventoryObj() {
    return {
        changeInQuantity: this.item.quantity,
        inventoryItemId: this.item.inventoryId,
    };
}

DB update function.

async updateInventory() {
    const updatedItems = this.updatedInventory.map((item) => (
        InventoryItems.query(this.transaction).patch({
            quantity: raw(`quantity + ${item.changeInQuantity}`),
        }).findById(item.inventoryItemId)
    ));
    await Promise.all(updatedItems);
}
@EthanCents
Copy link

what happens if you go through with the order adjustment but the payment hasn't occurred? should we be adjusting the available inventory of a product then or after we complete payment?

@surendra-wal
Copy link
Author

what happens if you go through with the order adjustment but the payment hasn't occurred? should we be adjusting the available inventory of a product then or after we complete payment?

we won't be updating the inventory for POST-PAY orders during order adjustment. will make necessary changes in the payment processor not to update the inventory of products if the order is adjusted and of type post-pay

async updateInventory() {
    if (this.isPostPay()) {
        return;
    }
    const updatedItems = this.updatedInventory.map((item) => (
        InventoryItems.query(this.transaction).patch({
            quantity: raw(`quantity + ${item.changeInQuantity}`),
        }).findById(item.inventoryItemId)
    ));
    await Promise.all(updatedItems);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment