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:
Sebastian Rindom
2021-10-13 17:56:10 +02:00
committed by GitHub
parent a175560efa
commit 7c0700e03f
2 changed files with 63 additions and 13 deletions

View File

@@ -135,6 +135,57 @@ describe("RestockNotificationService", () => {
})
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({
manager: MockManager,
productVariantService: ProductVariantService,
@@ -145,20 +196,20 @@ describe("RestockNotificationService", () => {
it("non-existing noti does nothing", async () => {
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 () => {
jest.clearAllMocks()
await expect(restockNotiService.triggerRestock("variant_outofstock"))
await expect(restockNotiService.restockExecute_("variant_outofstock"))
.resolves
})
it("existing noti emits and deletes", async () => {
jest.clearAllMocks()
await restockNotiService.triggerRestock("variant_1234")
await restockNotiService.restockExecute_("variant_1234")
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
expect(EventBusService.emit).toHaveBeenCalledWith(
@@ -187,7 +238,7 @@ describe("RestockNotificationService", () => {
{ inventory_required: 5 }
)
await service.triggerRestock("variant_1234")
await service.restockExecute_("variant_1234")
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
expect(EventBusService.emit).toHaveBeenCalledWith(
@@ -214,7 +265,7 @@ describe("RestockNotificationService", () => {
{ inventory_required: 5 }
)
await service.triggerRestock("variant_low_inventory")
await service.restockExecute_("variant_low_inventory")
expect(EventBusService.emit).toHaveBeenCalledTimes(0)
expect(RestockNotificationModel.delete).toHaveBeenCalledTimes(0)

View File

@@ -108,16 +108,15 @@ class RestockNotificationService extends BaseService {
* and emits a restocked event to the event bus. After successful emission the
* restock notification is deleted.
* @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) {
if (this.options_?.trigger_delay) {
await new Promise((resolve) =>
setTimeout(resolve, this.options_.trigger_delay)
)
}
triggerRestock(variantId) {
const delay = this.options_?.trigger_delay ?? 0
setTimeout(() => this.restockExecute_(variantId), delay)
}
return this.atomicPhase_(async (manager) => {
async restockExecute_(variantId) {
return await this.atomicPhase_(async (manager) => {
const restockRepo = manager.getRepository(this.restockNotificationModel_)
const existing = await this.retrieve(variantId)