Skip to content

Commit

Permalink
me dumb me forget callbacks added 1 time
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Jun 13, 2024
1 parent 10b0bda commit 5c908a2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import { callbacks } from '../../../../../lib/callbacks';
import { OmnichannelQueueInactivityMonitor } from '../lib/QueueInactivityMonitor';
import { cbLogger } from '../lib/logger';

export const withTimer = (timer: number) => {
return async (inquiry: ILivechatInquiryRecord) => {
if (!inquiry?._id || !inquiry?._updatedAt) {
return;
}
export const afterInquiryQueued = async (inquiry: ILivechatInquiryRecord) => {
if (!inquiry?._id || !inquiry?._updatedAt) {
return;
}

const timer = settings.get<number>('Livechat_max_queue_wait_time');

if (timer <= 0) {
return;
}

// schedule individual jobs instead of property for close inactivty
const newQueueTime = moment(inquiry._updatedAt).add(timer, 'minutes');
cbLogger.debug(`Scheduling estimated close time at ${newQueueTime} for queued inquiry ${inquiry._id}`);
await OmnichannelQueueInactivityMonitor.scheduleInquiry(inquiry._id, new Date(newQueueTime.format()));
};
// schedule individual jobs instead of property for close inactivty
const newQueueTime = moment(inquiry._updatedAt).add(timer, 'minutes');
cbLogger.debug(`Scheduling estimated close time at ${newQueueTime} for queued inquiry ${inquiry._id}`);
await OmnichannelQueueInactivityMonitor.scheduleInquiry(inquiry._id, new Date(newQueueTime.format()));
};

settings.watch<number>('Livechat_max_queue_wait_time', (value) => {
if (value <= 0) {
callbacks.remove('livechat.afterInquiryQueued', 'livechat-inquiry-queued-set-queue-timer');
return;
}
callbacks.add('livechat.afterInquiryQueued', withTimer(value), callbacks.priority.HIGH, 'livechat-inquiry-queued-set-queue-timer');
callbacks.add('livechat.afterInquiryQueued', afterInquiryQueued, callbacks.priority.HIGH, 'livechat-inquiry-queued-set-queue-timer');
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sinon from 'sinon';

const settingStub = {
watch: sinon.stub(),
get: sinon.stub(),
};

const callbackStub = {
Expand All @@ -18,26 +19,29 @@ const queueMonitorStub = {
scheduleInquiry: sinon.stub(),
};

const { withTimer } = proxyquire.noCallThru().load('../../../../../../app/livechat-enterprise/server/hooks/afterInquiryQueued.ts', {
'../../../../../app/settings/server': {
settings: settingStub,
},
'../../../../../lib/callbacks': {
callbacks: callbackStub,
},
'../lib/QueueInactivityMonitor': {
OmnichannelQueueInactivityMonitor: queueMonitorStub,
},
'../lib/logger': {
cbLogger: { debug: sinon.stub() },
},
});
const { afterInquiryQueued } = proxyquire
.noCallThru()
.load('../../../../../../app/livechat-enterprise/server/hooks/afterInquiryQueued.ts', {
'../../../../../app/settings/server': {
settings: settingStub,
},
'../../../../../lib/callbacks': {
callbacks: callbackStub,
},
'../lib/QueueInactivityMonitor': {
OmnichannelQueueInactivityMonitor: queueMonitorStub,
},
'../lib/logger': {
cbLogger: { debug: sinon.stub() },
},
});

describe('hooks/afterInquiryQueued', () => {
beforeEach(() => {
callbackStub.add.resetHistory();
callbackStub.remove.resetHistory();
queueMonitorStub.scheduleInquiry.resetHistory();
settingStub.get.resetHistory();
});

it('should call settings.watch at first', () => {
Expand All @@ -50,37 +54,67 @@ describe('hooks/afterInquiryQueued', () => {
func(1);
expect(callbackStub.add.callCount).to.be.equal(1);

func(2);
expect(callbackStub.add.callCount).to.be.equal(2);

func(0);
expect(callbackStub.remove.callCount).to.be.equal(1);

func(-1);
expect(callbackStub.remove.callCount).to.be.equal(2);

func(3);
expect(callbackStub.add.callCount).to.be.equal(3);
});

it('withTimer should return a function', () => {
expect(typeof withTimer(1)).to.be.equal('function');
it('should return undefined if no inquiry is passed, or if inquiry doesnt have valid properties', async () => {
expect(await afterInquiryQueued(null)).to.be.equal(undefined);
expect(await afterInquiryQueued({})).to.be.equal(undefined);
expect(await afterInquiryQueued({ _id: 'invalid' })).to.be.equal(undefined);
expect(await afterInquiryQueued({ _updatedAt: new Date() }));
expect(await afterInquiryQueued({ _updatedAt: null, _id: 'afsd34asdX' })).to.be.equal(undefined);
});

it('returned function should fail if no inquiry is passed, or if inquiry doesnt have valid properties', async () => {
const func = withTimer(1);
expect(await func(null)).to.be.equal(undefined);
expect(await func({})).to.be.equal(undefined);
expect(await func({ _id: 'invalid' })).to.be.equal(undefined);
expect(await func({ _updatedAt: null, _id: 'afsd34asdX' })).to.be.equal(undefined);
it('should do nothing if timer is set to 0 or less', async () => {
const inquiry = {
_id: 'afsd34asdX',
_updatedAt: new Date(),
};

settingStub.get.returns(0);
await afterInquiryQueued(inquiry);
expect(queueMonitorStub.scheduleInquiry.callCount).to.be.equal(0);

settingStub.get.returns(-1);
await afterInquiryQueued(inquiry);
expect(queueMonitorStub.scheduleInquiry.callCount).to.be.equal(0);
});

it('should call .scheduleInquiry with proper data', async () => {
const func = withTimer(1);

const inquiry = {
_id: 'afsd34asdX',
_updatedAt: new Date(),
};

await func(inquiry);
settingStub.get.returns(1);
await afterInquiryQueued(inquiry);

const newQueueTime = moment(inquiry._updatedAt).add(1, 'minutes');

expect(queueMonitorStub.scheduleInquiry.calledWith(inquiry._id, new Date(newQueueTime.format()))).to.be.true;
});

it('should call .scheduleInquiry with proper data when more than 1 min is passed as param', async () => {
const inquiry = {
_id: 'afv34avzx',
_updatedAt: new Date(),
};

settingStub.get.returns(3);
await afterInquiryQueued(inquiry);

const newQueueTime = moment(inquiry._updatedAt).add(3, 'minutes');

expect(queueMonitorStub.scheduleInquiry.calledWith(inquiry._id, new Date(newQueueTime.format()))).to.be.true;
});
});

0 comments on commit 5c908a2

Please sign in to comment.