-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(medusa): Add ProductVariantInventoryService #2883
feat(medusa): Add ProductVariantInventoryService #2883
Conversation
🦋 Changeset detectedLatest commit: a18936c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
c791cca
to
cf5063d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Mostly looks good had a few nits and thoughts that are non blocking - otherwise just a bit of docs.
@@ -14,8 +14,14 @@ import { | |||
AbstractCartCompletionStrategy, | |||
CartCompletionResponse, | |||
} from "../interfaces" | |||
import { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought (this-file): this flow is great for @carlos-r-l-rodrigues' transaction orchestrator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great piece of work! Have added a couple of suggestions and minor todos 💪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - great work! Pending @srindom's approval :)
} else { | ||
await lineItemServiceTx.delete(item.id) | ||
return | ||
return item |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion to make this block easier to read
cart.items = (
await Promise.all(
cart.items.map(async (item) => {
if (!item.variant_id) {
return item
}
const availablePrice = await this.priceSelectionStrategy_
.withTransaction(transactionManager)
.calculateVariantPrice(item.variant_id, {
region_id: region.id,
currency_code: region.currency_code,
quantity: item.quantity,
customer_id: customer_id || cart.customer_id,
include_discount_prices: true,
})
.catch(() => undefined)
if (
!isDefined(availablePrice) ||
availablePrice.calculatedPrice === null
) {
return await lineItemServiceTx.delete(item.id)
}
return await lineItemServiceTx.update(item.id, {
has_shipping: false,
unit_price: availablePrice.calculatedPrice,
})
})
)
)
.withTransaction(manager) | ||
.update(variantId, { | ||
inventory_quantity: variant.inventory_quantity + quantity, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion:
return
here and remove the else block
4c8245b
to
b296651
Compare
d5f5a24
to
b296651
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM w. comments - also think @carlos-r-l-rodrigues' suggestions are worth considering including
await Promise.all( | ||
cart.items.map(async (item) => { | ||
if (item.variant_id) { | ||
const inventoryConfirmed = | ||
await productVariantInventoryServiceTx.confirmInventory( | ||
item.variant_id, | ||
item.quantity, | ||
{ salesChannelId: cart.sales_channel_id } | ||
) | ||
|
||
if (!inventoryConfirmed) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_ALLOWED, | ||
`Variant with id: ${item.variant_id} does not have the required inventory`, | ||
MedusaError.Codes.INSUFFICIENT_INVENTORY | ||
) | ||
} | ||
|
||
await productVariantInventoryServiceTx.reserveQuantity( | ||
item.variant_id, | ||
item.quantity, | ||
{ | ||
lineItemId: item.id, | ||
salesChannelId: cart.sales_channel_id, | ||
} | ||
) | ||
} | ||
}) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: this should probably go into its own step -- but let's not do anything, as we will refactor to the transaction orchestrator in any case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Ready to merge? 💪 |
What
sales-channel-inventory
sales-channel-location
product-variant-inventory
inventoryService
picks out the core parts of #2459
Fixes CORE-914, CORE-912