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

feat(medusa, inventory): Inventory Management module #2956

Merged
merged 19 commits into from
Jan 10, 2023

Conversation

carlos-r-l-rodrigues
Copy link
Contributor

@carlos-r-l-rodrigues carlos-r-l-rodrigues commented Jan 6, 2023

What:
Module to manage inventory items in multiple locations

FIXES: CORE-921

@carlos-r-l-rodrigues carlos-r-l-rodrigues requested a review from a team as a code owner January 6, 2023 13:52
@changeset-bot
Copy link

changeset-bot bot commented Jan 6, 2023

⚠️ No Changeset found

Latest commit: 5f88a14

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@carlos-r-l-rodrigues carlos-r-l-rodrigues marked this pull request as draft January 6, 2023 13:55
@carlos-r-l-rodrigues carlos-r-l-rodrigues marked this pull request as ready for review January 6, 2023 14:43
Copy link
Collaborator

@srindom srindom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Would be good to get eyes other than mine on this too though :)

@adrien2p, @olivermrbl, @pKorsholm

packages/inventory/src/services/inventory-item.ts Outdated Show resolved Hide resolved
Comment on lines 210 to 243
/**
* Deletes a reservation item by line item id.
* @param {string} lineItemId - the id of the line item to delete.
* @returns {Promise<void>} - an empty promise
*/
async deleteByLineItem(lineItemId: string): Promise<void> {
await this.atomicPhase_(async (manager) => {
const itemRepository = manager.getRepository(ReservationItem)

const items = await this.list({ line_item_id: lineItemId })

const ops: Promise<unknown>[] = []
for (const item of items) {
ops.push(itemRepository.softRemove({ line_item_id: lineItemId }))
ops.push(
this.inventoryLevelService_
.withTransaction(manager)
.adjustReservedQuantity(
item.inventory_item_id,
item.location_id,
item.quantity * -1
)
)
}
await Promise.all(ops)
})

await this.eventBusService_.emit(
ReservationItemService.Events.DELETED_BY_LINE_ITEM,
{
line_item_id: lineItemId,
}
)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought (non-blocking): wonder if the existence of a line_item_id is a bad pattern as it couples the InventoryService with the design of the core.

It could be argued that a cleaner approach would be to have a LineItemReservations table in the core with:

  • line_item_id: id of a cart/order line item
  • reservation_item_id: id of a reservation in the inventory system

It is not a big deal for me, but would like to hear your opinion. (cc: @pKorsholm)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case to me makes more sense the inventory service managing reservations and holding the external relation (line_item_id).
Having a LineItemReservation would transfer the responsibility of managing reservations to the line item, which is somewhat duplicating the business logic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense - I was thinking of a scenario where a user integrates a third party IMS tool through our interfaces. With the current implementation, we assume that the third party tool's representation of a reservation/allocation is able to hold a field to represent the line_item_id. This was the assumption I wanted to challenge, but I think it is fair to go on as is and not delve too much on this right now :)

@olivermrbl olivermrbl changed the title Feat: Inventory module feat(medusa,inventory): Inventory Management module Jan 7, 2023
@olivermrbl olivermrbl changed the title feat(medusa,inventory): Inventory Management module feat(medusa, inventory): Inventory Management module Jan 7, 2023
Copy link
Contributor

@pKorsholm pKorsholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! I only have a couple of nits, but nothing major!

Remember Fixes CORE-921 😄

packages/inventory/src/services/inventory-item.ts Outdated Show resolved Hide resolved
packages/inventory/src/services/inventory-level.ts Outdated Show resolved Hide resolved
packages/inventory/src/services/reservation-item.ts Outdated Show resolved Hide resolved
packages/inventory/src/services/reservation-item.ts Outdated Show resolved Hide resolved
packages/inventory/src/models/inventory-level.ts Outdated Show resolved Hide resolved
Comment on lines +5 to +7
export const service = InventoryService
export const migrations = [SchemaMigration]
export const loaders = [ConnectionLoader]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion(non-blocking): maybe for a different pr, I talked to @adrien2p and he suggested creating a defined shape for module exports.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we could add it in a follow up pr 😄 no need to do anything about it here

Copy link
Contributor Author

@carlos-r-l-rodrigues carlos-r-l-rodrigues Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that, and we also will probably need to expand it a bit to optionaly integrate the module to the core while loading it.

Copy link
Collaborator

@srindom srindom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@carlos-r-l-rodrigues carlos-r-l-rodrigues merged commit 93ee248 into develop Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants