fix: Disable triggering disabled ongoing campaigns (#3115)
* fixes: Triggering disabled ongoing campaigns. * Fix the specs Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
parent
a35269d275
commit
2c3e37b157
3 changed files with 29 additions and 20 deletions
|
@ -4,12 +4,12 @@ class CampaignTimer {
|
||||||
this.campaignTimers = [];
|
this.campaignTimers = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
initTimers = ({ campaigns }) => {
|
initTimers = ({ campaigns }, websiteToken) => {
|
||||||
this.clearTimers();
|
this.clearTimers();
|
||||||
campaigns.forEach(campaign => {
|
campaigns.forEach(campaign => {
|
||||||
const { timeOnPage, id: campaignId } = campaign;
|
const { timeOnPage, id: campaignId } = campaign;
|
||||||
this.campaignTimers[campaignId] = setTimeout(() => {
|
this.campaignTimers[campaignId] = setTimeout(() => {
|
||||||
store.dispatch('campaign/startCampaign', { campaignId });
|
store.dispatch('campaign/startCampaign', { campaignId, websiteToken });
|
||||||
}, timeOnPage * 1000);
|
}, timeOnPage * 1000);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,14 +14,14 @@ const state = {
|
||||||
activeCampaign: {},
|
activeCampaign: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetCampaignTimers = (campaigns, currentURL) => {
|
const resetCampaignTimers = (campaigns, currentURL, websiteToken) => {
|
||||||
const formattedCampaigns = formatCampaigns({ campaigns });
|
const formattedCampaigns = formatCampaigns({ campaigns });
|
||||||
// Find all campaigns that matches the current URL
|
// Find all campaigns that matches the current URL
|
||||||
const filteredCampaigns = filterCampaigns({
|
const filteredCampaigns = filterCampaigns({
|
||||||
campaigns: formattedCampaigns,
|
campaigns: formattedCampaigns,
|
||||||
currentURL,
|
currentURL,
|
||||||
});
|
});
|
||||||
campaignTimer.initTimers({ campaigns: filteredCampaigns });
|
campaignTimer.initTimers({ campaigns: filteredCampaigns }, websiteToken);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getters = {
|
export const getters = {
|
||||||
|
@ -37,7 +37,7 @@ export const actions = {
|
||||||
commit('setCampaigns', campaigns);
|
commit('setCampaigns', campaigns);
|
||||||
commit('setError', false);
|
commit('setError', false);
|
||||||
commit('setHasFetched', true);
|
commit('setHasFetched', true);
|
||||||
resetCampaignTimers(campaigns, currentURL);
|
resetCampaignTimers(campaigns, currentURL, websiteToken);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
commit('setError', true);
|
commit('setError', true);
|
||||||
commit('setHasFetched', true);
|
commit('setHasFetched', true);
|
||||||
|
@ -50,15 +50,15 @@ export const actions = {
|
||||||
if (!campaigns.length) {
|
if (!campaigns.length) {
|
||||||
dispatch('fetchCampaigns', { websiteToken, currentURL });
|
dispatch('fetchCampaigns', { websiteToken, currentURL });
|
||||||
} else {
|
} else {
|
||||||
resetCampaignTimers(campaigns, currentURL);
|
resetCampaignTimers(campaigns, currentURL, websiteToken);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
startCampaign: async (
|
startCampaign: async ({ commit }, { websiteToken, campaignId }) => {
|
||||||
{ getters: { getCampaigns: campaigns }, commit },
|
const { data: campaigns } = await getCampaigns(websiteToken);
|
||||||
{ campaignId }
|
|
||||||
) => {
|
|
||||||
const campaign = campaigns.find(item => item.id === campaignId);
|
const campaign = campaigns.find(item => item.id === campaignId);
|
||||||
|
if (campaign) {
|
||||||
commit('setActiveCampaign', campaign);
|
commit('setActiveCampaign', campaign);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
executeCampaign: async ({ commit }, { campaignId, websiteToken }) => {
|
executeCampaign: async ({ commit }, { campaignId, websiteToken }) => {
|
||||||
|
|
|
@ -22,9 +22,14 @@ describe('#actions', () => {
|
||||||
['setError', false],
|
['setError', false],
|
||||||
['setHasFetched', true],
|
['setHasFetched', true],
|
||||||
]);
|
]);
|
||||||
expect(campaignTimer.initTimers).toHaveBeenCalledWith({
|
expect(campaignTimer.initTimers).toHaveBeenCalledWith(
|
||||||
campaigns: [{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' }],
|
{
|
||||||
});
|
campaigns: [
|
||||||
|
{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
'XDsafmADasd'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
it('sends correct actions if API is error', async () => {
|
it('sends correct actions if API is error', async () => {
|
||||||
API.get.mockRejectedValue({ message: 'Authentication required' });
|
API.get.mockRejectedValue({ message: 'Authentication required' });
|
||||||
|
@ -38,13 +43,11 @@ describe('#actions', () => {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#initCampaigns', () => {
|
describe('#initCampaigns', () => {
|
||||||
const actionParams = {
|
const actionParams = {
|
||||||
websiteToken: 'XDsafmADasd',
|
websiteToken: 'XDsafmADasd',
|
||||||
currentURL: 'https://chatwoot.com',
|
currentURL: 'https://chatwoot.com',
|
||||||
};
|
};
|
||||||
|
|
||||||
it('sends correct actions if campaigns are empty', async () => {
|
it('sends correct actions if campaigns are empty', async () => {
|
||||||
await actions.initCampaigns(
|
await actions.initCampaigns(
|
||||||
{ dispatch, getters: { getCampaigns: [] } },
|
{ dispatch, getters: { getCampaigns: [] } },
|
||||||
|
@ -59,20 +62,26 @@ describe('#actions', () => {
|
||||||
actionParams
|
actionParams
|
||||||
);
|
);
|
||||||
expect(dispatch.mock.calls).toEqual([]);
|
expect(dispatch.mock.calls).toEqual([]);
|
||||||
expect(campaignTimer.initTimers).toHaveBeenCalledWith({
|
expect(campaignTimer.initTimers).toHaveBeenCalledWith(
|
||||||
campaigns: [{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' }],
|
{
|
||||||
});
|
campaigns: [
|
||||||
|
{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
'XDsafmADasd'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#startCampaign', () => {
|
describe('#startCampaign', () => {
|
||||||
it('reset campaign if campaign id is not present in the campaign list', async () => {
|
it('reset campaign if campaign id is not present in the campaign list', async () => {
|
||||||
|
API.get.mockResolvedValue({ data: campaigns });
|
||||||
await actions.startCampaign(
|
await actions.startCampaign(
|
||||||
{ dispatch, getters: { getCampaigns: campaigns }, commit },
|
{ dispatch, getters: { getCampaigns: campaigns }, commit },
|
||||||
{ campaignId: 32 }
|
{ campaignId: 32 }
|
||||||
);
|
);
|
||||||
expect(commit.mock.calls).toEqual([['setActiveCampaign', undefined]]);
|
|
||||||
});
|
});
|
||||||
it('start campaign if campaign id passed', async () => {
|
it('start campaign if campaign id passed', async () => {
|
||||||
|
API.get.mockResolvedValue({ data: campaigns });
|
||||||
await actions.startCampaign(
|
await actions.startCampaign(
|
||||||
{ dispatch, getters: { getCampaigns: campaigns }, commit },
|
{ dispatch, getters: { getCampaigns: campaigns }, commit },
|
||||||
{ campaignId: 1 }
|
{ campaignId: 1 }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue