Skip to content

Commit

Permalink
feat(medusa-plugin-restock-notifications): Multi-warehouse support (m…
Browse files Browse the repository at this point in the history
…edusajs#4718)

Co-authored-by: Oli Juhl <[email protected]>
  • Loading branch information
josipmatichr and olivermrbl authored Aug 12, 2023
1 parent d8a908b commit 803aca0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-olives-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"medusa-plugin-restock-notification": patch
---

feat(medusa-plugin-restock-notifications): Multi-warehouse support
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class RestockNotificationService extends BaseService {
manager,
eventBusService,
productVariantService,
productVariantInventoryService,
restockNotificationModel,
},
options
Expand All @@ -24,6 +25,8 @@ class RestockNotificationService extends BaseService {

this.productVariantService_ = productVariantService

this.productVariantInventoryService_ = productVariantInventoryService

this.restockNotificationModel_ = restockNotificationModel

this.eventBus_ = eventBusService
Expand Down Expand Up @@ -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<RestockNotification>} 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)
Expand All @@ -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(
Expand Down

0 comments on commit 803aca0

Please sign in to comment.