feat(medusa-plugin-restock-notifications): Multi-warehouse support (#4718)
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"medusa-plugin-restock-notification": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa-plugin-restock-notifications): Multi-warehouse support
|
||||||
@@ -4,7 +4,14 @@ export default async (req, res) => {
|
|||||||
const restockNotificationService = req.scope.resolve(
|
const restockNotificationService = req.scope.resolve(
|
||||||
"restockNotificationService"
|
"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)
|
res.sendStatus(201)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res.status(400).json({ message: err.message })
|
res.status(400).json({ message: err.message })
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class RestockNotificationService extends BaseService {
|
|||||||
manager,
|
manager,
|
||||||
eventBusService,
|
eventBusService,
|
||||||
productVariantService,
|
productVariantService,
|
||||||
|
productVariantInventoryService,
|
||||||
restockNotificationModel,
|
restockNotificationModel,
|
||||||
},
|
},
|
||||||
options
|
options
|
||||||
@@ -24,6 +25,8 @@ class RestockNotificationService extends BaseService {
|
|||||||
|
|
||||||
this.productVariantService_ = productVariantService
|
this.productVariantService_ = productVariantService
|
||||||
|
|
||||||
|
this.productVariantInventoryService_ = productVariantInventoryService
|
||||||
|
|
||||||
this.restockNotificationModel_ = restockNotificationModel
|
this.restockNotificationModel_ = restockNotificationModel
|
||||||
|
|
||||||
this.eventBus_ = eventBusService
|
this.eventBus_ = eventBusService
|
||||||
@@ -68,9 +71,10 @@ class RestockNotificationService extends BaseService {
|
|||||||
* the variant is not sold out.
|
* the variant is not sold out.
|
||||||
* @param {string} variantId - the variant id to sign up for notifications for
|
* @param {string} variantId - the variant id to sign up for notifications for
|
||||||
* @param {string} email - the email to signup
|
* @param {string} email - the email to signup
|
||||||
|
* @param {string} salesChannelId - the sales channel id of the variant
|
||||||
* @return {Promise<RestockNotification>} The resulting restock notification
|
* @return {Promise<RestockNotification>} The resulting restock notification
|
||||||
*/
|
*/
|
||||||
async addEmail(variantId, email) {
|
async addEmail(variantId, email, salesChannelId) {
|
||||||
return this.atomicPhase_(async (manager) => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const restockRepo = manager.getRepository(this.restockNotificationModel_)
|
const restockRepo = manager.getRepository(this.restockNotificationModel_)
|
||||||
const existing = await this.retrieve(variantId)
|
const existing = await this.retrieve(variantId)
|
||||||
@@ -84,7 +88,19 @@ class RestockNotificationService extends BaseService {
|
|||||||
|
|
||||||
return await restockRepo.save(existing)
|
return await restockRepo.save(existing)
|
||||||
} else {
|
} 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) {
|
if (variant.inventory_quantity > 0) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
|
|||||||
Reference in New Issue
Block a user