fix: ensures that delayed restock notifications are being sent (#881)
* fix: ensures that delayed restock notifications are being sent * fix: default options to empty object * fix: tests
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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_)
|
||||
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user