fix: Trigger campaigns only during business hours (#3085)

Fixes #2433
This commit is contained in:
Muhsin Keloth
2021-10-12 17:58:33 +05:30
committed by GitHub
parent 7c21cf2255
commit 6bfa551c85
16 changed files with 142 additions and 38 deletions

View File

@@ -14,12 +14,18 @@ const state = {
activeCampaign: {},
};
const resetCampaignTimers = (campaigns, currentURL, websiteToken) => {
const resetCampaignTimers = (
campaigns,
currentURL,
websiteToken,
isInBusinessHours
) => {
const formattedCampaigns = formatCampaigns({ campaigns });
// Find all campaigns that matches the current URL
const filteredCampaigns = filterCampaigns({
campaigns: formattedCampaigns,
currentURL,
isInBusinessHours,
});
campaignTimer.initTimers({ campaigns: filteredCampaigns }, websiteToken);
};
@@ -31,13 +37,21 @@ export const getters = {
};
export const actions = {
fetchCampaigns: async ({ commit }, { websiteToken, currentURL }) => {
fetchCampaigns: async (
{ commit },
{ websiteToken, currentURL, isInBusinessHours }
) => {
try {
const { data: campaigns } = await getCampaigns(websiteToken);
commit('setCampaigns', campaigns);
commit('setError', false);
commit('setHasFetched', true);
resetCampaignTimers(campaigns, currentURL, websiteToken);
resetCampaignTimers(
campaigns,
currentURL,
websiteToken,
isInBusinessHours
);
} catch (error) {
commit('setError', true);
commit('setHasFetched', true);
@@ -45,12 +59,21 @@ export const actions = {
},
initCampaigns: async (
{ getters: { getCampaigns: campaigns }, dispatch },
{ currentURL, websiteToken }
{ currentURL, websiteToken, isInBusinessHours }
) => {
if (!campaigns.length) {
dispatch('fetchCampaigns', { websiteToken, currentURL });
dispatch('fetchCampaigns', {
websiteToken,
currentURL,
isInBusinessHours,
});
} else {
resetCampaignTimers(campaigns, currentURL, websiteToken);
resetCampaignTimers(
campaigns,
currentURL,
websiteToken,
isInBusinessHours
);
}
},
startCampaign: async ({ commit }, { websiteToken, campaignId }) => {

View File

@@ -15,7 +15,11 @@ describe('#actions', () => {
API.get.mockResolvedValue({ data: campaigns });
await actions.fetchCampaigns(
{ commit },
{ websiteToken: 'XDsafmADasd', currentURL: 'https://chatwoot.com' }
{
websiteToken: 'XDsafmADasd',
currentURL: 'https://chatwoot.com',
isInBusinessHours: true,
}
);
expect(commit.mock.calls).toEqual([
['setCampaigns', campaigns],
@@ -25,7 +29,12 @@ describe('#actions', () => {
expect(campaignTimer.initTimers).toHaveBeenCalledWith(
{
campaigns: [
{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' },
{
id: 11,
timeOnPage: '20',
url: 'https://chatwoot.com',
triggerOnlyDuringBusinessHours: false,
},
],
},
'XDsafmADasd'
@@ -35,7 +44,11 @@ describe('#actions', () => {
API.get.mockRejectedValue({ message: 'Authentication required' });
await actions.fetchCampaigns(
{ commit },
{ websiteToken: 'XDsafmADasd', currentURL: 'https://www.chatwoot.com' }
{
websiteToken: 'XDsafmADasd',
currentURL: 'https://www.chatwoot.com',
isInBusinessHours: true,
}
);
expect(commit.mock.calls).toEqual([
['setError', true],
@@ -65,7 +78,12 @@ describe('#actions', () => {
expect(campaignTimer.initTimers).toHaveBeenCalledWith(
{
campaigns: [
{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' },
{
id: 11,
timeOnPage: '20',
url: 'https://chatwoot.com',
triggerOnlyDuringBusinessHours: false,
},
],
},
'XDsafmADasd'