2019-12-28 16:26:42 +00:00
|
|
|
import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
|
|
|
|
import * as types from '../mutation-types';
|
2021-04-16 15:01:07 +00:00
|
|
|
import { INBOX_TYPES } from 'shared/mixins/inboxMixin';
|
2019-12-28 16:26:42 +00:00
|
|
|
import InboxesAPI from '../../api/inboxes';
|
|
|
|
import WebChannel from '../../api/channel/webChannel';
|
|
|
|
import FBChannel from '../../api/channel/fbChannel';
|
2020-04-05 16:41:27 +00:00
|
|
|
import TwilioChannel from '../../api/channel/twilioChannel';
|
2019-12-28 16:26:42 +00:00
|
|
|
|
2020-05-31 10:46:22 +00:00
|
|
|
const buildInboxData = inboxParams => {
|
|
|
|
const formData = new FormData();
|
|
|
|
const { channel = {}, ...inboxProperties } = inboxParams;
|
|
|
|
Object.keys(inboxProperties).forEach(key => {
|
|
|
|
formData.append(key, inboxProperties[key]);
|
|
|
|
});
|
2022-01-20 06:20:04 +00:00
|
|
|
const { selectedFeatureFlags, ...channelParams } = channel;
|
|
|
|
// selectedFeatureFlags needs to be empty when creating a website channel
|
|
|
|
if (selectedFeatureFlags) {
|
|
|
|
if (selectedFeatureFlags.length) {
|
|
|
|
selectedFeatureFlags.forEach(featureFlag => {
|
|
|
|
formData.append(`channel[selected_feature_flags][]`, featureFlag);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
formData.append('channel[selected_feature_flags][]', '');
|
|
|
|
}
|
2020-08-05 12:16:17 +00:00
|
|
|
}
|
|
|
|
Object.keys(channelParams).forEach(key => {
|
2020-05-31 10:46:22 +00:00
|
|
|
formData.append(`channel[${key}]`, channel[key]);
|
|
|
|
});
|
|
|
|
return formData;
|
|
|
|
};
|
|
|
|
|
2019-12-28 16:26:42 +00:00
|
|
|
export const state = {
|
|
|
|
records: [],
|
|
|
|
uiFlags: {
|
|
|
|
isFetching: false,
|
|
|
|
isFetchingItem: false,
|
|
|
|
isCreating: false,
|
|
|
|
isUpdating: false,
|
2020-02-19 09:10:03 +00:00
|
|
|
isUpdatingAutoAssignment: false,
|
2019-12-28 16:26:42 +00:00
|
|
|
isDeleting: false,
|
2021-11-19 06:22:27 +00:00
|
|
|
isUpdatingIMAP: false,
|
|
|
|
isUpdatingSMTP: false,
|
2019-12-28 16:26:42 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getInboxes($state) {
|
|
|
|
return $state.records;
|
|
|
|
},
|
2021-04-16 15:01:07 +00:00
|
|
|
getNewConversationInboxes($state) {
|
|
|
|
return $state.records.filter(inbox => {
|
|
|
|
const {
|
|
|
|
channel_type: channelType,
|
|
|
|
phone_number: phoneNumber = '',
|
|
|
|
} = inbox;
|
|
|
|
|
|
|
|
const isEmailChannel = channelType === INBOX_TYPES.EMAIL;
|
|
|
|
const isSmsChannel =
|
|
|
|
channelType === INBOX_TYPES.TWILIO &&
|
|
|
|
phoneNumber.startsWith('whatsapp');
|
|
|
|
return isEmailChannel || isSmsChannel;
|
|
|
|
});
|
|
|
|
},
|
2019-12-28 16:26:42 +00:00
|
|
|
getInbox: $state => inboxId => {
|
|
|
|
const [inbox] = $state.records.filter(
|
|
|
|
record => record.id === Number(inboxId)
|
|
|
|
);
|
|
|
|
return inbox || {};
|
|
|
|
},
|
|
|
|
getUIFlags($state) {
|
|
|
|
return $state.uiFlags;
|
|
|
|
},
|
2021-06-06 11:29:05 +00:00
|
|
|
getWebsiteInboxes($state) {
|
2021-08-11 14:59:33 +00:00
|
|
|
return $state.records.filter(item => item.channel_type === INBOX_TYPES.WEB);
|
|
|
|
},
|
|
|
|
getTwilioInboxes($state) {
|
2021-06-06 11:29:05 +00:00
|
|
|
return $state.records.filter(
|
2021-08-11 14:59:33 +00:00
|
|
|
item => item.channel_type === INBOX_TYPES.TWILIO
|
2021-06-06 11:29:05 +00:00
|
|
|
);
|
|
|
|
},
|
2022-02-03 23:22:13 +00:00
|
|
|
getSMSInboxes($state) {
|
2021-09-24 15:07:30 +00:00
|
|
|
return $state.records.filter(
|
2022-02-03 23:22:13 +00:00
|
|
|
item =>
|
|
|
|
item.channel_type === INBOX_TYPES.SMS ||
|
|
|
|
(item.channel_type === INBOX_TYPES.TWILIO && item.medium === 'sms')
|
2021-09-24 15:07:30 +00:00
|
|
|
);
|
|
|
|
},
|
2021-11-12 12:23:03 +00:00
|
|
|
dialogFlowEnabledInboxes($state) {
|
|
|
|
return $state.records.filter(
|
|
|
|
item => item.channel_type !== INBOX_TYPES.EMAIL
|
|
|
|
);
|
|
|
|
},
|
2019-12-28 16:26:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
get: async ({ commit }) => {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
|
|
|
const response = await InboxesAPI.get();
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isFetching: false });
|
|
|
|
commit(types.default.SET_INBOXES, response.data.payload);
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
2020-07-21 06:45:24 +00:00
|
|
|
createChannel: async ({ commit }, params) => {
|
|
|
|
try {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: true });
|
|
|
|
const response = await WebChannel.create(params);
|
|
|
|
commit(types.default.ADD_INBOXES, response.data);
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
return response.data;
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2019-12-28 16:26:42 +00:00
|
|
|
createWebsiteChannel: async ({ commit }, params) => {
|
|
|
|
try {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: true });
|
2020-05-31 10:46:22 +00:00
|
|
|
const response = await WebChannel.create(buildInboxData(params));
|
2019-12-28 16:26:42 +00:00
|
|
|
commit(types.default.ADD_INBOXES, response.data);
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
return response.data;
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2020-04-05 16:41:27 +00:00
|
|
|
createTwilioChannel: async ({ commit }, params) => {
|
|
|
|
try {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: true });
|
|
|
|
const response = await TwilioChannel.create(params);
|
|
|
|
commit(types.default.ADD_INBOXES, response.data);
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
return response.data;
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2019-12-28 16:26:42 +00:00
|
|
|
createFBChannel: async ({ commit }, params) => {
|
|
|
|
try {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: true });
|
|
|
|
const response = await FBChannel.create(params);
|
|
|
|
commit(types.default.ADD_INBOXES, response.data);
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
return response.data;
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isCreating: false });
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2021-02-15 19:28:57 +00:00
|
|
|
updateInbox: async ({ commit }, { id, formData = true, ...inboxParams }) => {
|
2020-02-22 14:46:56 +00:00
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingAutoAssignment: true,
|
|
|
|
});
|
2020-02-19 09:10:03 +00:00
|
|
|
try {
|
2021-02-15 19:28:57 +00:00
|
|
|
const response = await InboxesAPI.update(
|
|
|
|
id,
|
|
|
|
formData ? buildInboxData(inboxParams) : inboxParams
|
|
|
|
);
|
2020-02-19 09:10:03 +00:00
|
|
|
commit(types.default.EDIT_INBOXES, response.data);
|
2020-02-22 14:46:56 +00:00
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingAutoAssignment: false,
|
|
|
|
});
|
2020-02-19 09:10:03 +00:00
|
|
|
} catch (error) {
|
2020-02-22 14:46:56 +00:00
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingAutoAssignment: false,
|
|
|
|
});
|
2020-02-19 09:10:03 +00:00
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2021-11-19 06:22:27 +00:00
|
|
|
updateInboxIMAP: async (
|
|
|
|
{ commit },
|
|
|
|
{ id, formData = true, ...inboxParams }
|
|
|
|
) => {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingIMAP: true,
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const response = await InboxesAPI.update(
|
|
|
|
id,
|
|
|
|
formData ? buildInboxData(inboxParams) : inboxParams
|
|
|
|
);
|
|
|
|
commit(types.default.EDIT_INBOXES, response.data);
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingIMAP: false,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingIMAP: false,
|
|
|
|
});
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateInboxSMTP: async (
|
|
|
|
{ commit },
|
|
|
|
{ id, formData = true, ...inboxParams }
|
|
|
|
) => {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingSMTP: true,
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const response = await InboxesAPI.update(
|
|
|
|
id,
|
|
|
|
formData ? buildInboxData(inboxParams) : inboxParams
|
|
|
|
);
|
|
|
|
commit(types.default.EDIT_INBOXES, response.data);
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingSMTP: false,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, {
|
|
|
|
isUpdatingSMTP: false,
|
|
|
|
});
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2019-12-28 16:26:42 +00:00
|
|
|
delete: async ({ commit }, inboxId) => {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isDeleting: true });
|
|
|
|
try {
|
|
|
|
await InboxesAPI.delete(inboxId);
|
|
|
|
commit(types.default.DELETE_INBOXES, inboxId);
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isDeleting: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_INBOXES_UI_FLAG, { isDeleting: false });
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2020-09-29 19:42:32 +00:00
|
|
|
reauthorizeFacebookPage: async ({ commit }, params) => {
|
|
|
|
try {
|
|
|
|
const response = await FBChannel.reauthorizeFacebookPage(params);
|
|
|
|
commit(types.default.EDIT_INBOXES, response.data);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error.message);
|
|
|
|
}
|
|
|
|
},
|
2021-08-31 09:42:05 +00:00
|
|
|
deleteInboxAvatar: async (_, inboxId) => {
|
|
|
|
try {
|
|
|
|
await InboxesAPI.deleteInboxAvatar(inboxId);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2019-12-28 16:26:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
[types.default.SET_INBOXES_UI_FLAG]($state, uiFlag) {
|
|
|
|
$state.uiFlags = { ...$state.uiFlags, ...uiFlag };
|
|
|
|
},
|
|
|
|
[types.default.SET_INBOXES]: MutationHelpers.set,
|
|
|
|
[types.default.SET_INBOXES_ITEM]: MutationHelpers.setSingleRecord,
|
|
|
|
[types.default.ADD_INBOXES]: MutationHelpers.create,
|
|
|
|
[types.default.EDIT_INBOXES]: MutationHelpers.update,
|
|
|
|
[types.default.DELETE_INBOXES]: MutationHelpers.destroy,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|