diff --git a/packages/medusa-plugin-restock-notification/src/services/__tests__/restock-notification.js b/packages/medusa-plugin-restock-notification/src/services/__tests__/restock-notification.js index caa4b924bc..9baaf9608d 100644 --- a/packages/medusa-plugin-restock-notification/src/services/__tests__/restock-notification.js +++ b/packages/medusa-plugin-restock-notification/src/services/__tests__/restock-notification.js @@ -147,17 +147,9 @@ describe("RestockNotificationService", () => { eventBusService: EventBusService, }) - restockNotiService.restockExecute_ = jest.fn() - - jest.clearAllMocks() - jest.useFakeTimers() - + restockNotiService.restockExecute = jest.fn() restockNotiService.triggerRestock("variant_test") - - jest.runAllTimers() - - expect(setTimeout).toHaveBeenCalledTimes(1) - expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 0) + expect(restockNotiService.restockExecute).toHaveBeenCalledTimes(1) }) it("trigger delay 10", async () => { @@ -171,21 +163,20 @@ describe("RestockNotificationService", () => { { trigger_delay: 10 } ) - restockNotiService.restockExecute_ = jest.fn() - - jest.clearAllMocks() - jest.useFakeTimers() + restockNotiService.restockExecute = jest.fn() restockNotiService.triggerRestock("variant_test") - jest.runAllTimers() - - expect(setTimeout).toHaveBeenCalledTimes(1) - expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 10) + expect(EventBusService.emit).toHaveBeenCalledTimes(1) + expect(EventBusService.emit).toHaveBeenCalledWith( + "restock-notification.execute", + { variant_id: "variant_test" }, + { delay: 10 } + ) }) }) - describe("restockExecute_", () => { + describe("restockExecute", () => { const restockNotiService = new RestockNotificationService({ manager: MockManager, productVariantService: ProductVariantService, @@ -196,20 +187,20 @@ describe("RestockNotificationService", () => { it("non-existing noti does nothing", async () => { jest.clearAllMocks() - await expect(restockNotiService.restockExecute_("variant_test")).resolves + await expect(restockNotiService.restockExecute("variant_test")).resolves }) it("existing noti but out of stock does nothing", async () => { jest.clearAllMocks() - await expect(restockNotiService.restockExecute_("variant_outofstock")) + await expect(restockNotiService.restockExecute("variant_outofstock")) .resolves }) it("existing noti emits and deletes", async () => { jest.clearAllMocks() - await restockNotiService.restockExecute_("variant_1234") + await restockNotiService.restockExecute("variant_1234") expect(EventBusService.emit).toHaveBeenCalledTimes(1) expect(EventBusService.emit).toHaveBeenCalledWith( @@ -238,7 +229,7 @@ describe("RestockNotificationService", () => { { inventory_required: 5 } ) - await service.restockExecute_("variant_1234") + await service.restockExecute("variant_1234") expect(EventBusService.emit).toHaveBeenCalledTimes(1) expect(EventBusService.emit).toHaveBeenCalledWith( @@ -265,7 +256,7 @@ describe("RestockNotificationService", () => { { inventory_required: 5 } ) - await service.restockExecute_("variant_low_inventory") + await service.restockExecute("variant_low_inventory") expect(EventBusService.emit).toHaveBeenCalledTimes(0) expect(RestockNotificationModel.delete).toHaveBeenCalledTimes(0) 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 b0de97286a..137eeb2ecc 100644 --- a/packages/medusa-plugin-restock-notification/src/services/restock-notification.js +++ b/packages/medusa-plugin-restock-notification/src/services/restock-notification.js @@ -110,12 +110,20 @@ class RestockNotificationService extends BaseService { * @param {string} variantId - the variant id to trigger restock for * @return The resulting restock notification */ - triggerRestock(variantId) { + async triggerRestock(variantId) { const delay = this.options_?.trigger_delay ?? 0 - setTimeout(() => this.restockExecute_(variantId), delay) + if (delay) { + return await this.eventBus_.emit( + "restock-notification.execute", + { variant_id: variantId }, + { delay } + ) + } + + return await this.restockExecute(variantId) } - async restockExecute_(variantId) { + async restockExecute(variantId) { return await this.atomicPhase_(async (manager) => { const restockRepo = manager.getRepository(this.restockNotificationModel_) diff --git a/packages/medusa-plugin-restock-notification/src/subscribers/index.js b/packages/medusa-plugin-restock-notification/src/subscribers/index.js index e97e5293c6..b610aca1b5 100644 --- a/packages/medusa-plugin-restock-notification/src/subscribers/index.js +++ b/packages/medusa-plugin-restock-notification/src/subscribers/index.js @@ -7,6 +7,16 @@ class VariantSubscriber { "product-variant.updated", this.handleVariantUpdate ) + + eventBusService.subscribe( + "restock-notification.execute", + this.handleDelayedExecute + ) + } + + handleDelayedExecute = async (data) => { + const { variant_id } = data + return await this.restockNotificationService_.restockExecute(variant_id) } handleVariantUpdate = async (data) => { diff --git a/packages/medusa/src/services/event-bus.js b/packages/medusa/src/services/event-bus.js index d49de20ecc..10b4e306df 100644 --- a/packages/medusa/src/services/event-bus.js +++ b/packages/medusa/src/services/event-bus.js @@ -148,9 +148,10 @@ class EventBusService { * Calls all subscribers when an event occurs. * @param {string} eventName - the name of the event to be process. * @param {?any} data - the data to send to the subscriber. + * @param {?any} options - options to add the job with * @return {BullJob} - the job from our queue */ - async emit(eventName, data) { + async emit(eventName, data, options = {}) { if (this.transactionManager_) { const stagedJobRepository = this.transactionManager_.getCustomRepository( this.stagedJobRepository_ @@ -163,7 +164,11 @@ class EventBusService { return stagedJobRepository.save(created) } else { - this.queue_.add({ eventName, data }, { removeOnComplete: true }) + const opts = { removeOnComplete: true } + if (typeof options.delay === "number") { + opts.delay = options.delay + } + this.queue_.add({ eventName, data }, opts) } }