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:
Josip Matić
2023-08-12 14:02:39 +02:00
committed by GitHub
parent d8a908bd8e
commit 803aca0689
3 changed files with 31 additions and 3 deletions

View File

@@ -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 })

View File

@@ -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<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)
@@ -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(