hotfix(medusa-plugin-restock-notification): add delay for event trigger (#544)
* fix: add delay before triggering email * fix: non-blocking delay
This commit is contained in:
1 parent
a175560efa
commit
7c0700e03f
2 files changed
+62
-12
No files matched your search
+56
-5
@@ -135,6 +135,57 @@ describe("RestockNotificationService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("triggerRestock", () => {
|
describe("triggerRestock", () => {
|
||||||
|
afterEach(() => {
|
||||||
|
jest.useRealTimers()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("trigger delay default to 0", async () => {
|
||||||
|
const restockNotiService = new RestockNotificationService({
|
||||||
|
manager: MockManager,
|
||||||
|
productVariantService: ProductVariantService,
|
||||||
|
restockNotificationModel: RestockNotificationModel,
|
||||||
|
eventBusService: EventBusService,
|
||||||
|
})
|
||||||
|
|
||||||
|
restockNotiService.restockExecute_ = jest.fn()
|
||||||
|
|
||||||
|
jest.clearAllMocks()
|
||||||
|
jest.useFakeTimers()
|
||||||
|
|
||||||
|
restockNotiService.triggerRestock("variant_test")
|
||||||
|
|
||||||
|
jest.runAllTimers()
|
||||||
|
|
||||||
|
expect(setTimeout).toHaveBeenCalledTimes(1)
|
||||||
|
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("trigger delay 10", async () => {
|
||||||
|
const restockNotiService = new RestockNotificationService(
|
||||||
|
{
|
||||||
|
manager: MockManager,
|
||||||
|
productVariantService: ProductVariantService,
|
||||||
|
restockNotificationModel: RestockNotificationModel,
|
||||||
|
eventBusService: EventBusService,
|
||||||
|
},
|
||||||
|
{ trigger_delay: 10 }
|
||||||
|
)
|
||||||
|
|
||||||
|
restockNotiService.restockExecute_ = jest.fn()
|
||||||
|
|
||||||
|
jest.clearAllMocks()
|
||||||
|
jest.useFakeTimers()
|
||||||
|
|
||||||
|
restockNotiService.triggerRestock("variant_test")
|
||||||
|
|
||||||
|
jest.runAllTimers()
|
||||||
|
|
||||||
|
expect(setTimeout).toHaveBeenCalledTimes(1)
|
||||||
|
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 10)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("restockExecute_", () => {
|
||||||
const restockNotiService = new RestockNotificationService({
|
const restockNotiService = new RestockNotificationService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
productVariantService: ProductVariantService,
|
productVariantService: ProductVariantService,
|
||||||
@@ -145,20 +196,20 @@ describe("RestockNotificationService", () => {
|
|||||||
it("non-existing noti does nothing", async () => {
|
it("non-existing noti does nothing", async () => {
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
|
|
||||||
await expect(restockNotiService.triggerRestock("variant_test")).resolves
|
await expect(restockNotiService.restockExecute_("variant_test")).resolves
|
||||||
})
|
})
|
||||||
|
|
||||||
it("existing noti but out of stock does nothing", async () => {
|
it("existing noti but out of stock does nothing", async () => {
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
|
|
||||||
await expect(restockNotiService.triggerRestock("variant_outofstock"))
|
await expect(restockNotiService.restockExecute_("variant_outofstock"))
|
||||||
.resolves
|
.resolves
|
||||||
})
|
})
|
||||||
|
|
||||||
it("existing noti emits and deletes", async () => {
|
it("existing noti emits and deletes", async () => {
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
|
|
||||||
await restockNotiService.triggerRestock("variant_1234")
|
await restockNotiService.restockExecute_("variant_1234")
|
||||||
|
|
||||||
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
|
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
|
||||||
expect(EventBusService.emit).toHaveBeenCalledWith(
|
expect(EventBusService.emit).toHaveBeenCalledWith(
|
||||||
@@ -187,7 +238,7 @@ describe("RestockNotificationService", () => {
|
|||||||
{ inventory_required: 5 }
|
{ inventory_required: 5 }
|
||||||
)
|
)
|
||||||
|
|
||||||
await service.triggerRestock("variant_1234")
|
await service.restockExecute_("variant_1234")
|
||||||
|
|
||||||
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
|
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
|
||||||
expect(EventBusService.emit).toHaveBeenCalledWith(
|
expect(EventBusService.emit).toHaveBeenCalledWith(
|
||||||
@@ -214,7 +265,7 @@ describe("RestockNotificationService", () => {
|
|||||||
{ inventory_required: 5 }
|
{ inventory_required: 5 }
|
||||||
)
|
)
|
||||||
|
|
||||||
await service.triggerRestock("variant_low_inventory")
|
await service.restockExecute_("variant_low_inventory")
|
||||||
|
|
||||||
expect(EventBusService.emit).toHaveBeenCalledTimes(0)
|
expect(EventBusService.emit).toHaveBeenCalledTimes(0)
|
||||||
expect(RestockNotificationModel.delete).toHaveBeenCalledTimes(0)
|
expect(RestockNotificationModel.delete).toHaveBeenCalledTimes(0)
|
||||||
|
|||||||
@@ -108,16 +108,15 @@ class RestockNotificationService extends BaseService {
|
|||||||
* and emits a restocked event to the event bus. After successful emission the
|
* and emits a restocked event to the event bus. After successful emission the
|
||||||
* restock notification is deleted.
|
* restock notification is deleted.
|
||||||
* @param {string} variantId - the variant id to trigger restock for
|
* @param {string} variantId - the variant id to trigger restock for
|
||||||
* @return {Promise<RestockNotification>} The resulting restock notification
|
* @return The resulting restock notification
|
||||||
*/
|
*/
|
||||||
async triggerRestock(variantId) {
|
triggerRestock(variantId) {
|
||||||
if (this.options_?.trigger_delay) {
|
const delay = this.options_?.trigger_delay ?? 0
|
||||||
await new Promise((resolve) =>
|
setTimeout(() => this.restockExecute_(variantId), delay)
|
||||||
setTimeout(resolve, this.options_.trigger_delay)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.atomicPhase_(async (manager) => {
|
async restockExecute_(variantId) {
|
||||||
|
return await 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)
|
||||||
|
|||||||
Reference in new issue
Block a user