diff --git a/.changeset/moody-olives-learn.md b/.changeset/moody-olives-learn.md new file mode 100644 index 0000000000..05a01f4ee3 --- /dev/null +++ b/.changeset/moody-olives-learn.md @@ -0,0 +1,5 @@ +--- +"medusa-plugin-restock-notification": patch +--- + +feat(medusa-plugin-restock-notifications): Multi-warehouse support diff --git a/packages/medusa-plugin-restock-notification/src/api/routes/add-email.js b/packages/medusa-plugin-restock-notification/src/api/routes/add-email.js index 1a2e5c9774..db63cac69a 100644 --- a/packages/medusa-plugin-restock-notification/src/api/routes/add-email.js +++ b/packages/medusa-plugin-restock-notification/src/api/routes/add-email.js @@ -4,7 +4,14 @@ export default async (req, res) => { const restockNotificationService = req.scope.resolve( "restockNotificationService" ) - await restockNotificationService.addEmail(variant_id, req.body.email) + + let sales_channel_id = req.body.sales_channel_id + + if (req.publishableApiKeyScopes?.sales_channel_ids.length === 1) { + sales_channel_id = req.publishableApiKeyScopes.sales_channel_ids[0] + } + + await restockNotificationService.addEmail(variant_id, req.body.email, sales_channel_id) res.sendStatus(201) } catch (err) { res.status(400).json({ message: err.message }) diff --git a/packages/medusa-plugin-restock-notification/src/services/restock-notification.js b/packages/medusa-plugin-restock-notification/src/services/restock-notification.js index 137eeb2ecc..e290c20186 100644 --- a/packages/medusa-plugin-restock-notification/src/services/restock-notification.js +++ b/packages/medusa-plugin-restock-notification/src/services/restock-notification.js @@ -12,6 +12,7 @@ class RestockNotificationService extends BaseService { manager, eventBusService, productVariantService, + productVariantInventoryService, restockNotificationModel, }, options @@ -24,6 +25,8 @@ class RestockNotificationService extends BaseService { this.productVariantService_ = productVariantService + this.productVariantInventoryService_ = productVariantInventoryService + this.restockNotificationModel_ = restockNotificationModel this.eventBus_ = eventBusService @@ -68,9 +71,10 @@ class RestockNotificationService extends BaseService { * the variant is not sold out. * @param {string} variantId - the variant id to sign up for notifications for * @param {string} email - the email to signup + * @param {string} salesChannelId - the sales channel id of the variant * @return {Promise} The resulting restock notification */ - async addEmail(variantId, email) { + async addEmail(variantId, email, salesChannelId) { return this.atomicPhase_(async (manager) => { const restockRepo = manager.getRepository(this.restockNotificationModel_) const existing = await this.retrieve(variantId) @@ -84,7 +88,19 @@ class RestockNotificationService extends BaseService { return await restockRepo.save(existing) } else { - const variant = await this.productVariantService_.retrieve(variantId) + let variant = await this.productVariantService_.retrieve(variantId) + + if (salesChannelId) { + const variants = + await this.productVariantInventoryService_.setVariantAvailability( + [variant], + salesChannelId + ) + + if (variants.length) { + variant = variants[0] + } + } if (variant.inventory_quantity > 0) { throw new MedusaError(