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: add and remove products to/from collection in bulk endpoints #1032

Merged
merged 8 commits into from
Feb 16, 2022

Conversation

kasperkristensen
Copy link
Contributor

@kasperkristensen kasperkristensen commented Feb 7, 2022

What

  • Adds new endpoint POST admin/collections/<id>/products/batch, that allows users to add products associated with the given Product Collection in bulk.
  • Adds new endpoint DELETE admin/collections/<id>/products/batch, that allows users to remove products associated with the given Product Collection in bulk.

Why

  • Currently it is only supported to add/remove products to/from a collection by updating the product directly. This is inefficient as if we were to add 100 products and remove another 50 from a collection we would have to make 150 calls to update product.
  • Using this new approach we can limit this to db updates, one to bulk add products and one to bulk remove products.

How

  • Adds new bulkAddToCollection and bulkRemoveFromCollection functions to the ProductRepository.
  • These methods are used by the new addProducts and removeProducts methods in the ProductCollectionService.

Testing

  • Added new unit and integration tests

…eProducts on collections that uses these bulk operations
): Promise<Product[]> {
await this.createQueryBuilder()
.update(Product)
.set({ collection_id: undefined })
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be more correct to do collection_id: null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TS will complain that collection_id can only be off type string or undefined. Alternativly I could change .update(Product) to .update("product") to avoid using the TS type and thereby avoid the warning.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ahh makes sense - I think we then have to make sure that the type of collection_id becomes string | null.

Generally, if we have @Column({ nullable: true }) in a model we need to allow null as a type on that column; we haven't done this previously as we weren't using TS so completely fine to add that :)

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.

Minor fixes

packages/medusa/src/services/product-collection.js Outdated Show resolved Hide resolved
packages/medusa/src/services/product-collection.js Outdated Show resolved Hide resolved
packages/medusa/src/services/product-collection.js Outdated Show resolved Hide resolved
@kasperkristensen kasperkristensen changed the title feat: add update products in collection endpoint feat: add and remove products to/from collection in bulk endpoints Feb 15, 2022
@srindom srindom merged commit 6629403 into develop Feb 16, 2022
adrien2p pushed a commit to adrien2p/medusa that referenced this pull request Feb 23, 2022
…edusajs#1032)

* adds bulk add/remove products to/from collection. Adds endpoint updateProducts on collections that uses these bulk operations

* fix integration tests and test description

* undo change to swap

* made requested changes

* added removeProducts endpoint

* made requested changes

* fix: set collection_id null

* updated collection_id to type string | undefined
olivermrbl pushed a commit that referenced this pull request Feb 25, 2022
…1032)

* adds bulk add/remove products to/from collection. Adds endpoint updateProducts on collections that uses these bulk operations

* fix integration tests and test description

* undo change to swap

* made requested changes

* added removeProducts endpoint

* made requested changes

* fix: set collection_id null

* updated collection_id to type string | undefined
SGFGOV pushed a commit to SGFGOV/medusa that referenced this pull request Apr 19, 2022
…edusajs#1032)

* adds bulk add/remove products to/from collection. Adds endpoint updateProducts on collections that uses these bulk operations

* fix integration tests and test description

* undo change to swap

* made requested changes

* added removeProducts endpoint

* made requested changes

* fix: set collection_id null

* updated collection_id to type string | undefined
SGFGOV pushed a commit to SGFGOV/medusa that referenced this pull request Apr 19, 2022
…edusajs#1032)

* adds bulk add/remove products to/from collection. Adds endpoint updateProducts on collections that uses these bulk operations

* fix integration tests and test description

* undo change to swap

* made requested changes

* added removeProducts endpoint

* made requested changes

* fix: set collection_id null

* updated collection_id to type string | undefined
@srindom srindom deleted the feat/collection-add-products branch July 3, 2022 13:57
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.

None yet

3 participants