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

Regenerate allocations #661

Open
chladog opened this issue Jan 19, 2021 · 5 comments
Open

Regenerate allocations #661

chladog opened this issue Jan 19, 2021 · 5 comments
Labels
P2: important Critical issue which does not affect majority of users status: reproduction needed 🔁 Reproduction of described behaviour needed by core team @vendure/core

Comments

@chladog
Copy link
Contributor

chladog commented Jan 19, 2021

Is your feature request related to a problem? Please describe.
After few weeks of implementation of stock movement feature we have quite a few product variants that because of an transition error, sometimes for unknown reason have unexpected stock allocation values - e.g. we have product variants that have negative allocations, or had some orders allocated multiple times.
The main issue to address here is ofc refinement of our project-specific order process and error handling to keep track of right allocations as much as possible, though I believe there will be edge cases/unpredictable errors that can result in wrong allocations that don't match to open orders. Currently when this happens there is no easy way how to get or update the current allocations.

Describe the solution you'd like
I propose a new function + button in AdminUI regenerate allocations, that when run would cycle through open orders and adjust allocations of all product variants that have invalid values. (and reset all allocations for product variants that are not in any of open order to 0).

@michaelbromley michaelbromley added this to the v1.0.0 milestone Jan 19, 2021
@chladog
Copy link
Contributor Author

chladog commented May 31, 2021

This was quick and dirty solution by me, but might be helpful in the implementation:

async recalculateAllocations(ctx: RequestContext): Promise<boolean> {
        const candidateAdjustments = new Map();
        const rawResults: { productVariantId: number, orderLineId: number | null, quantity: number, count: string }[] = await this.connection.getRepository(StockMovement).query(
            `select *
            from "order_item"
            LEFT JOIN "order_line" 
            ON order_line.id = order_item."lineId"
			LEFT JOIN "order" 
            ON "order".id = order_line."orderId"
            LEFT JOIN "product_variant" 
            ON "product_variant".id = order_line."productVariantId"
            where "lineId" in (select "id"
            from "order_line"
            where "orderId" in (
            select "id"
            from "order"
            where "id" in (select "orderId"
            from order_line
            where "id" IN (
                select "lineId" from "order_item"
            LEFT JOIN "order_item_fulfillments_fulfillment" 
                ON order_item_fulfillments_fulfillment."orderItemId" = order_item.id
            where "fulfillmentId" IS NULL and "cancelled" = false
            )) AND "state" not in ('AddingItems', 'ArrangingPayment', 'Cancelled')
                )
            )`
        );
        rawResults.forEach(element => {
            let candidateValue = 0;
            if (candidateAdjustments.has(element.productVariantId)) {
                candidateValue = candidateAdjustments.get(element.productVariantId);
            }
            candidateValue += 1;
            candidateAdjustments.set(element.productVariantId, candidateValue);
        });
        await this.connection.getRepository(ProductVariant).update({}, {stockAllocated: 0});
        const array = Array.from(candidateAdjustments, ([variantId, stockAllocated]) => ({ variantId, stockAllocated }));
        await Promise.all(array.map(async({variantId, stockAllocated}) => {
            try {
                await this.connection.getRepository(ProductVariant).update({id: variantId, trackInventory: GlobalFlag.INHERIT}, {stockAllocated});
                await this.connection.getRepository(ProductVariant).update({id: variantId, trackInventory: GlobalFlag.TRUE}, {stockAllocated});
            } catch (error) {
              console.log('error'+ error);
            }
          }));
        return true;
    }

@michaelbromley
Copy link
Member

After giving this some thought, I have decided that this is best handled in conjunction with #1425. It makes sense to have a job like this run periodically and that also solves the problem of where the UI parts would go that controls this.

So I'm deferring this to the next minor.

@michaelbromley michaelbromley modified the milestones: v1.7, v1.8 Aug 24, 2022
@michaelbromley michaelbromley modified the milestones: v1.8, v1.9 Sep 8, 2022
@michaelbromley
Copy link
Member

Moving to v2.1 milestone because it goes together with the scheduled tasks feature

@michaelbromley michaelbromley modified the milestones: v1.9, v2.1 Dec 1, 2022
@nyousefzai
Copy link

Is there any update on this?

@martijnvdbrug martijnvdbrug added the P2: important Critical issue which does not affect majority of users label Jul 18, 2024
@dlhck dlhck added the status: reproduction needed 🔁 Reproduction of described behaviour needed by core team label Sep 24, 2024
@dlhck dlhck moved this from 📅 Planned to 👀 Under consideration in Vendure OS Roadmap Sep 24, 2024
@dlhck dlhck removed this from the v2.1 milestone Sep 24, 2024
@martijnvdbrug
Copy link
Collaborator

@michaelbromley will this exact issue be included in core? As in, will we have periodically regenerate allocations as functionality in core, or will the core functionality only included the Scheduled Tasks mechanism?

If core will not include the actual periodically regenerate allocation functionality, can we then close this issue, and asks people to follow #1425 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2: important Critical issue which does not affect majority of users status: reproduction needed 🔁 Reproduction of described behaviour needed by core team @vendure/core
Projects
Status: 👀 Under consideration
Development

No branches or pull requests

5 participants