feat: Add an option to delete campaigns (#2402)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Muhsin Keloth 2021-06-15 14:14:59 +05:30 committed by GitHub
parent a56aa5656d
commit 0f377da109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 334 additions and 244 deletions

View file

@ -10,6 +10,11 @@ class Api::V1::Accounts::CampaignsController < Api::V1::Accounts::BaseController
@campaign = Current.account.campaigns.create!(campaign_params) @campaign = Current.account.campaigns.create!(campaign_params)
end end
def destroy
@campaign.destroy
head :ok
end
def show; end def show; end
def update def update

View file

@ -42,6 +42,19 @@
"ERROR_MESSAGE": "There was an error. Please try again." "ERROR_MESSAGE": "There was an error. Please try again."
} }
}, },
"DELETE": {
"BUTTON_TEXT": "Delete",
"CONFIRM": {
"TITLE": "Confirm Deletion",
"MESSAGE": "Are you sure to delete?",
"YES": "Yes, Delete ",
"NO": "No, Keep "
},
"API": {
"SUCCESS_MESSAGE": "Campaign deleted successfully",
"ERROR_MESSAGE": "Could not delete the campaign. Please try again later."
}
},
"EDIT": { "EDIT": {
"TITLE": "Edit campaign", "TITLE": "Edit campaign",
"UPDATE_BUTTON_TEXT": "Update", "UPDATE_BUTTON_TEXT": "Update",

View file

@ -1,132 +1,112 @@
<template> <template>
<modal :show.sync="show" :on-close="onClose"> <div class="column content-box">
<div class="column content-box"> <woot-modal-header
<woot-modal-header :header-title="$t('CAMPAIGN.ADD.TITLE')"
:header-title="$t('CAMPAIGN.ADD.TITLE')" :header-content="$t('CAMPAIGN.ADD.DESC')"
:header-content="$t('CAMPAIGN.ADD.DESC')" />
/> <form class="row" @submit.prevent="addCampaign">
<form class="row" @submit.prevent="addCampaign"> <div class="medium-12 columns">
<div class="medium-12 columns"> <woot-input
<label :class="{ error: $v.title.$error }"> v-model="title"
{{ $t('CAMPAIGN.ADD.FORM.TITLE.LABEL') }} :label="$t('CAMPAIGN.ADD.FORM.TITLE.LABEL')"
<input type="text"
v-model.trim="title" :class="{ error: $v.title.$error }"
type="text" :error="$v.title.$error ? $t('CAMPAIGN.ADD.FORM.TITLE.ERROR') : ''"
:placeholder="$t('CAMPAIGN.ADD.FORM.TITLE.PLACEHOLDER')" :placeholder="$t('CAMPAIGN.ADD.FORM.TITLE.PLACEHOLDER')"
@input="$v.title.$touch" @blur="$v.title.$touch"
/> />
</label>
</div>
<div class="medium-12 columns"> <label :class="{ error: $v.message.$error }">
<label :class="{ error: $v.message.$error }"> {{ $t('CAMPAIGN.ADD.FORM.MESSAGE.LABEL') }}
{{ $t('CAMPAIGN.ADD.FORM.MESSAGE.LABEL') }} <textarea
<textarea v-model="message"
v-model.trim="message" rows="5"
rows="5" type="text"
type="text" :placeholder="$t('CAMPAIGN.ADD.FORM.MESSAGE.PLACEHOLDER')"
:placeholder="$t('CAMPAIGN.ADD.FORM.MESSAGE.PLACEHOLDER')" @blur="$v.message.$touch"
@input="$v.message.$touch" />
/> <span v-if="$v.message.$error" class="message">
</label> {{ $t('CAMPAIGN.ADD.FORM.MESSAGE.ERROR') }}
</div> </span>
</label>
<div class="medium-12 columns"> <label :class="{ error: $v.selectedSender.$error }">
<label :class="{ error: $v.selectedSender.$error }"> {{ $t('CAMPAIGN.ADD.FORM.SENT_BY.LABEL') }}
{{ $t('CAMPAIGN.ADD.FORM.SENT_BY.LABEL') }} <select v-model="selectedSender">
<select v-model="selectedSender"> <option
<option v-for="sender in sendersAndBotList"
v-for="sender in sendersAndBotList" :key="sender.name"
:key="sender.name" :value="sender.id"
:value="sender.id" >
> {{ sender.name }}
{{ sender.name }} </option>
</option> </select>
</select> <span v-if="$v.selectedSender.$error" class="message">
<span v-if="$v.selectedSender.$error" class="message"> {{ $t('CAMPAIGN.ADD.FORM.SENT_BY.ERROR') }}
{{ $t('CAMPAIGN.ADD.FORM.SENT_BY.ERROR') }} </span>
</span> </label>
</label>
</div>
<div class="medium-12 columns"> <woot-input
<label :class="{ error: $v.endPoint.$error }"> v-model="endPoint"
{{ $t('CAMPAIGN.ADD.FORM.END_POINT.LABEL') }} :label="$t('CAMPAIGN.ADD.FORM.END_POINT.LABEL')"
<input type="text"
v-model.trim="endPoint" :class="{ error: $v.endPoint.$error }"
type="text" :error="
:placeholder="$t('CAMPAIGN.ADD.FORM.END_POINT.PLACEHOLDER')" $v.endPoint.$error ? $t('CAMPAIGN.ADD.FORM.END_POINT.ERROR') : ''
@input="$v.endPoint.$touch" "
/> :placeholder="$t('CAMPAIGN.ADD.FORM.END_POINT.PLACEHOLDER')"
<span v-if="$v.endPoint.$error" class="message"> @blur="$v.endPoint.$touch"
{{ $t('CAMPAIGN.ADD.FORM.END_POINT.ERROR') }} />
</span> <woot-input
</label> v-model="timeOnPage"
</div> :label="$t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.LABEL')"
<div class="medium-12 columns"> type="text"
<label :class="{ error: $v.timeOnPage.$error }"> :class="{ error: $v.timeOnPage.$error }"
{{ $t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.LABEL') }} :error="
<input $v.timeOnPage.$error
v-model.trim="timeOnPage" ? $t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.ERROR')
type="number" : ''
:placeholder="$t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.PLACEHOLDER')" "
@input="$v.timeOnPage.$touch" :placeholder="$t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.PLACEHOLDER')"
/> @blur="$v.timeOnPage.$touch"
<span v-if="$v.timeOnPage.$error" class="message"> />
{{ $t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.ERROR') }} <label>
</span> <input
</label> v-model="enabled"
</div> type="checkbox"
value="enabled"
name="enabled"
/>
{{ $t('CAMPAIGN.ADD.FORM.ENABLED') }}
</label>
</div>
<div class="medium-12 columns"> <div class="modal-footer">
<label> <woot-button
<input :is-disabled="buttonDisabled"
v-model="enabled" :is-loading="uiFlags.isCreating"
type="checkbox" >
value="enabled" {{ $t('CAMPAIGN.ADD.CREATE_BUTTON_TEXT') }}
name="enabled" </woot-button>
/> <woot-button variant="clear" @click.prevent="onClose">
{{ $t('CAMPAIGN.ADD.FORM.ENABLED') }} {{ $t('CAMPAIGN.ADD.CANCEL_BUTTON_TEXT') }}
</label> </woot-button>
</div> </div>
</form>
<div class="modal-footer"> </div>
<div class="medium-12 columns">
<woot-submit-button
:disabled="buttonDisabled"
:loading="uiFlags.isCreating"
:button-text="$t('CAMPAIGN.ADD.CREATE_BUTTON_TEXT')"
/>
<button class="button clear" @click.prevent="onClose">
{{ $t('CAMPAIGN.ADD.CANCEL_BUTTON_TEXT') }}
</button>
</div>
</div>
</form>
</div>
</modal>
</template> </template>
<script> <script>
import { mapGetters } from 'vuex'; import { mapGetters } from 'vuex';
import { required, url, minLength } from 'vuelidate/lib/validators'; import { required, url, minLength } from 'vuelidate/lib/validators';
import Modal from 'dashboard/components/Modal';
import alertMixin from 'shared/mixins/alertMixin'; import alertMixin from 'shared/mixins/alertMixin';
export default { export default {
components: {
Modal,
},
mixins: [alertMixin], mixins: [alertMixin],
props: { props: {
senderList: { senderList: {
type: Array, type: Array,
default: () => [], default: () => [],
}, },
onClose: {
type: Function,
default: () => {},
},
}, },
data() { data() {
return { return {
@ -158,7 +138,6 @@ export default {
required, required,
}, },
}, },
computed: { computed: {
...mapGetters({ ...mapGetters({
uiFlags: 'campaigns/getUIFlags', uiFlags: 'campaigns/getUIFlags',
@ -183,8 +162,10 @@ export default {
]; ];
}, },
}, },
methods: { methods: {
onClose() {
this.$emit('on-close');
},
async addCampaign() { async addCampaign() {
try { try {
await this.$store.dispatch('campaigns/create', { await this.$store.dispatch('campaigns/create', {

View file

@ -9,33 +9,44 @@
:campaigns="records" :campaigns="records"
:show-empty-result="showEmptyResult" :show-empty-result="showEmptyResult"
:is-loading="uiFlags.isFetching" :is-loading="uiFlags.isFetching"
:on-edit-click="openEditPopup" @on-edit-click="openEditPopup"
@on-delete-click="openDeletePopup"
/> />
<woot-modal :show.sync="showAddPopup" :on-close="hideAddPopup"> <woot-modal :show.sync="showAddPopup" :on-close="hideAddPopup">
<add-campaign :on-close="hideAddPopup" :sender-list="selectedAgents" /> <add-campaign :sender-list="selectedAgents" @on-close="hideAddPopup" />
</woot-modal> </woot-modal>
<woot-modal :show.sync="showEditPopup" :on-close="hideEditPopup"> <woot-modal :show.sync="showEditPopup" :on-close="hideEditPopup">
<edit-campaign <edit-campaign
:on-close="hideEditPopup"
:selected-campaign="selectedCampaign" :selected-campaign="selectedCampaign"
:sender-list="selectedAgents" :sender-list="selectedAgents"
@on-close="hideEditPopup"
/> />
</woot-modal> </woot-modal>
<woot-delete-modal
:show.sync="showDeleteConfirmationPopup"
:on-close="closeDeletePopup"
:on-confirm="confirmDeletion"
:title="$t('CAMPAIGN.DELETE.CONFIRM.TITLE')"
:message="$t('CAMPAIGN.DELETE.CONFIRM.MESSAGE')"
:confirm-text="$t('CAMPAIGN.DELETE.CONFIRM.YES')"
:reject-text="$t('CAMPAIGN.DELETE.CONFIRM.NO')"
/>
</div> </div>
</template> </template>
<script> <script>
import { mapGetters } from 'vuex'; import { mapGetters } from 'vuex';
import alertMixin from 'shared/mixins/alertMixin';
import AddCampaign from './AddCampaign'; import AddCampaign from './AddCampaign';
import CampaignsTable from './CampaignsTable'; import CampaignsTable from './CampaignsTable';
import EditCampaign from './EditCampaign'; import EditCampaign from './EditCampaign';
export default { export default {
components: { components: {
AddCampaign, AddCampaign,
CampaignsTable, CampaignsTable,
EditCampaign, EditCampaign,
}, },
mixins: [alertMixin],
props: { props: {
selectedAgents: { selectedAgents: {
type: Array, type: Array,
@ -48,6 +59,7 @@ export default {
showAddPopup: false, showAddPopup: false,
showEditPopup: false, showEditPopup: false,
selectedCampaign: {}, selectedCampaign: {},
showDeleteConfirmationPopup: false,
}; };
}, },
computed: { computed: {
@ -81,6 +93,28 @@ export default {
hideEditPopup() { hideEditPopup() {
this.showEditPopup = false; this.showEditPopup = false;
}, },
openDeletePopup(response) {
this.showDeleteConfirmationPopup = true;
this.selectedCampaign = response;
},
closeDeletePopup() {
this.showDeleteConfirmationPopup = false;
},
confirmDeletion() {
this.closeDeletePopup();
const {
row: { id },
} = this.selectedCampaign;
this.deleteCampaign(id);
},
async deleteCampaign(id) {
try {
await this.$store.dispatch('campaigns/delete', id);
this.showAlert(this.$t('CAMPAIGN.DELETE.API.SUCCESS_MESSAGE'));
} catch (error) {
this.showAlert(this.$t('CAMPAIGN.DELETE.API.ERROR_MESSAGE'));
}
},
}, },
}; };
</script> </script>
@ -91,4 +125,8 @@ export default {
justify-content: flex-end; justify-content: flex-end;
padding-bottom: var(--space-one); padding-bottom: var(--space-one);
} }
.content-box .page-top-bar::v-deep {
padding: var(--space-large) var(--space-large) var(--space-zero);
}
</style> </style>

View file

@ -44,10 +44,6 @@ export default {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
onEditClick: {
type: Function,
default: () => {},
},
}, },
data() { data() {
@ -140,10 +136,18 @@ export default {
icon="ion-edit" icon="ion-edit"
color-scheme="secondary" color-scheme="secondary"
classNames="grey-btn" classNames="grey-btn"
onClick={() => this.onEditClick(row)} onClick={() => this.$emit('on-edit-click', row)}
> >
{this.$t('CAMPAIGN.LIST.BUTTONS.EDIT')} {this.$t('CAMPAIGN.LIST.BUTTONS.EDIT')}
</WootButton> </WootButton>
<WootButton
variant="link"
icon="ion-close-circled"
color-scheme="secondary"
onClick={() => this.$emit('on-delete-click', row)}
>
{this.$t('CAMPAIGN.LIST.BUTTONS.DELETE')}
</WootButton>
</div> </div>
), ),
}, },

View file

@ -1,132 +1,101 @@
<template> <template>
<modal :show.sync="show" :on-close="onClose"> <div class="column content-box">
<div class="column content-box"> <woot-modal-header :header-title="pageTitle" />
<woot-modal-header :header-title="pageTitle" /> <form class="row" @submit.prevent="editCampaign">
<form class="row" @submit.prevent="editCampaign"> <div class="medium-12 columns">
<div class="medium-12 columns"> <woot-input
<label :class="{ error: $v.title.$error }"> v-model="title"
{{ $t('CAMPAIGN.ADD.FORM.TITLE.LABEL') }} :label="$t('CAMPAIGN.ADD.FORM.TITLE.LABEL')"
<input type="text"
v-model.trim="title" :class="{ error: $v.title.$error }"
type="text" :error="$v.title.$error ? $t('CAMPAIGN.ADD.FORM.TITLE.ERROR') : ''"
:placeholder="$t('CAMPAIGN.ADD.FORM.TITLE.PLACEHOLDER')" :placeholder="$t('CAMPAIGN.ADD.FORM.TITLE.PLACEHOLDER')"
@input="$v.title.$touch" @blur="$v.title.$touch"
/> />
<span v-if="$v.title.$error" class="message"> <label :class="{ error: $v.message.$error }">
{{ $t('CAMPAIGN.ADD.FORM.TITLE.ERROR') }} {{ $t('CAMPAIGN.ADD.FORM.MESSAGE.LABEL') }}
</span> <textarea
</label> v-model.trim="message"
</div> rows="5"
type="text"
<div class="medium-12 columns"> :placeholder="$t('CAMPAIGN.ADD.FORM.MESSAGE.PLACEHOLDER')"
<label :class="{ error: $v.message.$error }"> @input="$v.message.$touch"
{{ $t('CAMPAIGN.ADD.FORM.MESSAGE.LABEL') }} />
<textarea <span v-if="$v.message.$error" class="message">
v-model.trim="message" {{ $t('CAMPAIGN.ADD.FORM.MESSAGE.ERROR') }}
rows="5" </span>
type="text" </label>
:placeholder="$t('CAMPAIGN.ADD.FORM.MESSAGE.PLACEHOLDER')" <label :class="{ error: $v.selectedSender.$error }">
@input="$v.message.$touch" {{ $t('CAMPAIGN.ADD.FORM.SENT_BY.LABEL') }}
/> <select v-model="selectedSender">
<span v-if="$v.message.$error" class="message"> <option
{{ $t('CAMPAIGN.ADD.FORM.MESSAGE.ERROR') }} v-for="sender in sendersAndBotList"
</span> :key="sender.name"
</label> :value="sender.id"
</div> >
{{ sender.name }}
<div class="medium-12 columns"> </option>
<label :class="{ error: $v.selectedSender.$error }"> </select>
{{ $t('CAMPAIGN.ADD.FORM.SENT_BY.LABEL') }} <span v-if="$v.selectedSender.$error" class="message">
<select v-model="selectedSender"> {{ $t('CAMPAIGN.ADD.FORM.SENT_BY.ERROR') }}
<option </span>
v-for="sender in sendersAndBotList" </label>
:key="sender.name" <woot-input
:value="sender.id" v-model="endPoint"
> :label="$t('CAMPAIGN.ADD.FORM.END_POINT.LABEL')"
{{ sender.name }} type="text"
</option> :class="{ error: $v.endPoint.$error }"
</select> :error="
<span v-if="$v.selectedSender.$error" class="message"> $v.endPoint.$error ? $t('CAMPAIGN.ADD.FORM.END_POINT.ERROR') : ''
{{ $t('CAMPAIGN.ADD.FORM.SENT_BY.ERROR') }} "
</span> :placeholder="$t('CAMPAIGN.ADD.FORM.END_POINT.PLACEHOLDER')"
</label> @blur="$v.endPoint.$touch"
</div> />
<woot-input
<div class="medium-12 columns"> v-model="timeOnPage"
<label :class="{ error: $v.endPoint.$error }"> :label="$t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.LABEL')"
{{ $t('CAMPAIGN.ADD.FORM.END_POINT.LABEL') }} type="text"
<input :class="{ error: $v.timeOnPage.$error }"
v-model.trim="endPoint" :error="
type="text" $v.timeOnPage.$error
:placeholder="$t('CAMPAIGN.ADD.FORM.END_POINT.PLACEHOLDER')" ? $t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.ERROR')
@input="$v.endPoint.$touch" : ''
/> "
<span v-if="$v.endPoint.$error" class="message"> :placeholder="$t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.PLACEHOLDER')"
{{ $t('CAMPAIGN.ADD.FORM.END_POINT.ERROR') }} @blur="$v.timeOnPage.$touch"
</span> />
</label> <label>
</div> <input
<div class="medium-12 columns"> v-model="enabled"
<label :class="{ error: $v.timeOnPage.$error }"> type="checkbox"
{{ $t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.LABEL') }} value="enabled"
<input name="enabled"
v-model.trim="timeOnPage" />
type="number" {{ $t('CAMPAIGN.ADD.FORM.ENABLED') }}
:placeholder="$t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.PLACEHOLDER')" </label>
@input="$v.timeOnPage.$touch" </div>
/> <div class="modal-footer">
<span v-if="$v.timeOnPage.$error" class="message"> <woot-button
{{ $t('CAMPAIGN.ADD.FORM.TIME_ON_PAGE.ERROR') }} :is-disabled="buttonDisabled"
</span> :is-loading="uiFlags.isCreating"
</label> >
</div> {{ $t('CAMPAIGN.EDIT.UPDATE_BUTTON_TEXT') }}
</woot-button>
<div class="medium-12 columns"> <woot-button variant="clear" @click.prevent="onClose">
<label> {{ $t('CAMPAIGN.ADD.CANCEL_BUTTON_TEXT') }}
<input </woot-button>
v-model="enabled" </div>
type="checkbox" </form>
value="enabled" </div>
name="enabled"
/>
{{ $t('CAMPAIGN.ADD.FORM.ENABLED') }}
</label>
</div>
<div class="modal-footer">
<woot-button :disabled="buttonDisabled" :loading="uiFlags.isCreating">
{{ $t('CAMPAIGN.EDIT.UPDATE_BUTTON_TEXT') }}
</woot-button>
<woot-button
class="button clear"
:disabled="buttonDisabled"
:loading="uiFlags.isCreating"
@click.prevent="onClose"
>
{{ $t('CAMPAIGN.ADD.CANCEL_BUTTON_TEXT') }}
</woot-button>
</div>
</form>
</div>
</modal>
</template> </template>
<script> <script>
import { mapGetters } from 'vuex'; import { mapGetters } from 'vuex';
import { required, url, minLength } from 'vuelidate/lib/validators'; import { required, url, minLength } from 'vuelidate/lib/validators';
import Modal from 'dashboard/components/Modal';
import alertMixin from 'shared/mixins/alertMixin'; import alertMixin from 'shared/mixins/alertMixin';
export default { export default {
components: {
Modal,
},
mixins: [alertMixin], mixins: [alertMixin],
props: { props: {
onClose: {
type: Function,
default: () => {},
},
selectedCampaign: { selectedCampaign: {
type: Object, type: Object,
default: () => {}, default: () => {},
@ -166,7 +135,6 @@ export default {
required, required,
}, },
}, },
computed: { computed: {
...mapGetters({ ...mapGetters({
uiFlags: 'campaigns/getUIFlags', uiFlags: 'campaigns/getUIFlags',
@ -200,6 +168,9 @@ export default {
this.setFormValues(); this.setFormValues();
}, },
methods: { methods: {
onClose() {
this.$emit('on-close');
},
setFormValues() { setFormValues() {
const { const {
title, title,
@ -215,7 +186,6 @@ export default {
this.selectedSender = (sender && sender.id) || 0; this.selectedSender = (sender && sender.id) || 0;
this.enabled = enabled; this.enabled = enabled;
}, },
async editCampaign() { async editCampaign() {
try { try {
await this.$store.dispatch('campaigns/update', { await this.$store.dispatch('campaigns/update', {

View file

@ -54,6 +54,17 @@ export const actions = {
commit(types.SET_CAMPAIGN_UI_FLAG, { isUpdating: false }); commit(types.SET_CAMPAIGN_UI_FLAG, { isUpdating: false });
} }
}, },
delete: async ({ commit }, id) => {
commit(types.SET_CAMPAIGN_UI_FLAG, { isDeleting: true });
try {
await CampaignsAPI.delete(id);
commit(types.DELETE_CAMPAIGN, id);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_CAMPAIGN_UI_FLAG, { isDeleting: false });
}
},
}; };
export const mutations = { export const mutations = {
@ -67,6 +78,7 @@ export const mutations = {
[types.ADD_CAMPAIGN]: MutationHelpers.create, [types.ADD_CAMPAIGN]: MutationHelpers.create,
[types.SET_CAMPAIGNS]: MutationHelpers.set, [types.SET_CAMPAIGNS]: MutationHelpers.set,
[types.EDIT_CAMPAIGN]: MutationHelpers.update, [types.EDIT_CAMPAIGN]: MutationHelpers.update,
[types.DELETE_CAMPAIGN]: MutationHelpers.destroy,
}; };
export default { export default {

View file

@ -68,4 +68,26 @@ describe('#actions', () => {
]); ]);
}); });
}); });
describe('#delete', () => {
it('sends correct actions if API is success', async () => {
axios.delete.mockResolvedValue({ data: campaignList[0] });
await actions.delete({ commit }, campaignList[0].id);
expect(commit.mock.calls).toEqual([
[types.default.SET_CAMPAIGN_UI_FLAG, { isDeleting: true }],
[types.default.DELETE_CAMPAIGN, campaignList[0].id],
[types.default.SET_CAMPAIGN_UI_FLAG, { isDeleting: false }],
]);
});
it('sends correct actions if API is error', async () => {
axios.delete.mockRejectedValue({ message: 'Incorrect header' });
await expect(
actions.delete({ commit }, campaignList[0].id)
).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([
[types.default.SET_CAMPAIGN_UI_FLAG, { isDeleting: true }],
[types.default.SET_CAMPAIGN_UI_FLAG, { isDeleting: false }],
]);
});
});
}); });

View file

@ -30,4 +30,12 @@ describe('#mutations', () => {
expect(state.records[0].message).toEqual('Hey, What brings you today'); expect(state.records[0].message).toEqual('Hey, What brings you today');
}); });
}); });
describe('#DELETE_LABEL', () => {
it('delete campaign record', () => {
const state = { records: [campaigns[0]] };
mutations[types.DELETE_CAMPAIGN](state, 1);
expect(state.records).toEqual([]);
});
});
}); });

View file

@ -158,6 +158,7 @@ export default {
SET_CAMPAIGNS: 'SET_CAMPAIGNS', SET_CAMPAIGNS: 'SET_CAMPAIGNS',
ADD_CAMPAIGN: 'ADD_CAMPAIGN', ADD_CAMPAIGN: 'ADD_CAMPAIGN',
EDIT_CAMPAIGN: 'EDIT_CAMPAIGN', EDIT_CAMPAIGN: 'EDIT_CAMPAIGN',
DELETE_CAMPAIGN: 'DELETE_CAMPAIGN',
// Contact notes // Contact notes
SET_CONTACT_NOTES_UI_FLAG: 'SET_CONTACT_NOTES_UI_FLAG', SET_CONTACT_NOTES_UI_FLAG: 'SET_CONTACT_NOTES_UI_FLAG',

View file

@ -49,7 +49,7 @@ Rails.application.routes.draw do
end end
end end
resources :canned_responses, except: [:show, :edit, :new] resources :canned_responses, except: [:show, :edit, :new]
resources :campaigns, only: [:index, :create, :show, :update] resources :campaigns, only: [:index, :create, :show, :update, :destroy]
namespace :channels do namespace :channels do
resource :twilio_channel, only: [:create] resource :twilio_channel, only: [:create]

View file

@ -145,4 +145,40 @@ RSpec.describe 'Campaigns API', type: :request do
end end
end end
end end
describe 'DELETE /api/v1/accounts/{account.id}/campaigns/:id' do
let(:inbox) { create(:inbox, account: account) }
let!(:campaign) { create(:campaign, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:administrator) { create(:user, account: account, role: :administrator) }
it 'return unauthorized if agent' do
delete "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
it 'delete campaign if admin' do
delete "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(::Campaign.exists?(campaign.display_id)).to eq false
end
end
end
end end