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:
1 parent
7d6fc5c9e1
commit
329767e279
4 files changed
+43
-29
No files matched your search
+15
-24
@@ -147,17 +147,9 @@ describe("RestockNotificationService", () => {
|
|||||||
eventBusService: EventBusService,
|
eventBusService: EventBusService,
|
||||||
})
|
})
|
||||||
|
|
||||||
restockNotiService.restockExecute_ = jest.fn()
|
restockNotiService.restockExecute = jest.fn()
|
||||||
|
|
||||||
jest.clearAllMocks()
|
|
||||||
jest.useFakeTimers()
|
|
||||||
|
|
||||||
restockNotiService.triggerRestock("variant_test")
|
restockNotiService.triggerRestock("variant_test")
|
||||||
|
expect(restockNotiService.restockExecute).toHaveBeenCalledTimes(1)
|
||||||
jest.runAllTimers()
|
|
||||||
|
|
||||||
expect(setTimeout).toHaveBeenCalledTimes(1)
|
|
||||||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 0)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("trigger delay 10", async () => {
|
it("trigger delay 10", async () => {
|
||||||
@@ -171,21 +163,20 @@ describe("RestockNotificationService", () => {
|
|||||||
{ trigger_delay: 10 }
|
{ trigger_delay: 10 }
|
||||||
)
|
)
|
||||||
|
|
||||||
restockNotiService.restockExecute_ = jest.fn()
|
restockNotiService.restockExecute = jest.fn()
|
||||||
|
|
||||||
jest.clearAllMocks()
|
|
||||||
jest.useFakeTimers()
|
|
||||||
|
|
||||||
restockNotiService.triggerRestock("variant_test")
|
restockNotiService.triggerRestock("variant_test")
|
||||||
|
|
||||||
jest.runAllTimers()
|
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
|
||||||
|
expect(EventBusService.emit).toHaveBeenCalledWith(
|
||||||
expect(setTimeout).toHaveBeenCalledTimes(1)
|
"restock-notification.execute",
|
||||||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 10)
|
{ variant_id: "variant_test" },
|
||||||
|
{ delay: 10 }
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("restockExecute_", () => {
|
describe("restockExecute", () => {
|
||||||
const restockNotiService = new RestockNotificationService({
|
const restockNotiService = new RestockNotificationService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
productVariantService: ProductVariantService,
|
productVariantService: ProductVariantService,
|
||||||
@@ -196,20 +187,20 @@ describe("RestockNotificationService", () => {
|
|||||||
it("non-existing noti does nothing", async () => {
|
it("non-existing noti does nothing", async () => {
|
||||||
jest.clearAllMocks()
|
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 () => {
|
it("existing noti but out of stock does nothing", async () => {
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
|
|
||||||
await expect(restockNotiService.restockExecute_("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.restockExecute_("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(
|
||||||
@@ -238,7 +229,7 @@ describe("RestockNotificationService", () => {
|
|||||||
{ inventory_required: 5 }
|
{ inventory_required: 5 }
|
||||||
)
|
)
|
||||||
|
|
||||||
await service.restockExecute_("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(
|
||||||
@@ -265,7 +256,7 @@ describe("RestockNotificationService", () => {
|
|||||||
{ inventory_required: 5 }
|
{ inventory_required: 5 }
|
||||||
)
|
)
|
||||||
|
|
||||||
await service.restockExecute_("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)
|
||||||
|
|||||||
@@ -110,12 +110,20 @@ class RestockNotificationService extends BaseService {
|
|||||||
* @param {string} variantId - the variant id to trigger restock for
|
* @param {string} variantId - the variant id to trigger restock for
|
||||||
* @return The resulting restock notification
|
* @return The resulting restock notification
|
||||||
*/
|
*/
|
||||||
triggerRestock(variantId) {
|
async triggerRestock(variantId) {
|
||||||
const delay = this.options_?.trigger_delay ?? 0
|
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) => {
|
return await this.atomicPhase_(async (manager) => {
|
||||||
const restockRepo = manager.getRepository(this.restockNotificationModel_)
|
const restockRepo = manager.getRepository(this.restockNotificationModel_)
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,16 @@ class VariantSubscriber {
|
|||||||
"product-variant.updated",
|
"product-variant.updated",
|
||||||
this.handleVariantUpdate
|
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) => {
|
handleVariantUpdate = async (data) => {
|
||||||
|
|||||||
@@ -148,9 +148,10 @@ class EventBusService {
|
|||||||
* Calls all subscribers when an event occurs.
|
* Calls all subscribers when an event occurs.
|
||||||
* @param {string} eventName - the name of the event to be process.
|
* @param {string} eventName - the name of the event to be process.
|
||||||
* @param {?any} data - the data to send to the subscriber.
|
* @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
|
* @return {BullJob} - the job from our queue
|
||||||
*/
|
*/
|
||||||
async emit(eventName, data) {
|
async emit(eventName, data, options = {}) {
|
||||||
if (this.transactionManager_) {
|
if (this.transactionManager_) {
|
||||||
const stagedJobRepository = this.transactionManager_.getCustomRepository(
|
const stagedJobRepository = this.transactionManager_.getCustomRepository(
|
||||||
this.stagedJobRepository_
|
this.stagedJobRepository_
|
||||||
@@ -163,7 +164,11 @@ class EventBusService {
|
|||||||
|
|
||||||
return stagedJobRepository.save(created)
|
return stagedJobRepository.save(created)
|
||||||
} else {
|
} 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