Git Product home page Git Product logo

Comments (4)

michaelbromley avatar michaelbromley commented on June 3, 2024 1

Hi,

In this situation, if you don't throw, then the alternative is to return an empty array, in which case no allocations will be made. If that is not what you want to allow, then throwing is the best option as it indicates some exceptional circumstance that needs to be known and handled.

Other than that, is this issue OK to close now?

from vendure.

michaelbromley avatar michaelbromley commented on June 3, 2024

Hi,

In the storefront, when you execute this mutation, what is the result?

mutation Transition {
  transitionOrderToState(state: "ArrangingPayment") {
    ... on Order {
      id
      code
      state
      total
    }
    ... on ErrorResult {
      errorCode
      message
    }
    ...on OrderStateTransitionError {
      transitionError
      fromState
      toState
    }
  }
}

from vendure.

lzzfnc avatar lzzfnc commented on June 3, 2024

Hey there! I returned on this after some time adjusting these two methods as follow:

async getAvailableStock(ctx: RequestContext, productVariantId: ID, stockLevels: StockLevel[]): Promise<AvailableStock> {
	await Promise.all(
		stockLevels.map(async (sl) =>
			this.entityHydrator.hydrate(ctx, sl, { relations: ["stockLocation.channels"] })
		)
	);
	let stockOnHand = 0;
	let stockAllocated = 0;
	for (const stockLevel of stockLevels) {
		let channels = stockLevel.stockLocation.channels.map((ch) => ch.id);
		if (!channels.includes(ctx.channelId)) continue;
		stockOnHand += stockLevel.stockOnHand;
		stockAllocated += stockLevel.stockAllocated;
	}
	return { stockOnHand, stockAllocated };
}
async forAllocation(ctx: RequestContext, stockLocations: StockLocation[], orderLine: OrderLine, quantity: number): Promise<LocationWithQuantity[]> {
	await Promise.all(
		stockLocations.map(async (sl) =>
			this.entityHydrator.hydrate(ctx, sl, { relations: ["channels"] })
		)
	);

	let location = stockLocations.find((sl) =>
		sl.channels.map((ch) => ch.id).includes(ctx.channelId)
	);

	if (!location) {
		throw new InternalServerError("Location not found");
	}

	return [{ location, quantity }];
}

And it seems to work fine! I can complete the order process, and the quantity gets allocated from the correct stock location.

I'm not sure if throwing an error when the location is not found for allocation is the right approach, do you have any suggestion on that?

from vendure.

lzzfnc avatar lzzfnc commented on June 3, 2024

thanks Michael!
yes, we can close the issue

from vendure.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.