parent
d7cfe6858e
commit
6d378eb206
@ -81,11 +81,6 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
|||||||
def update
|
def update
|
||||||
@contact.assign_attributes(contact_update_params)
|
@contact.assign_attributes(contact_update_params)
|
||||||
@contact.save!
|
@contact.save!
|
||||||
rescue ActiveRecord::RecordInvalid => e
|
|
||||||
render json: {
|
|
||||||
message: e.record.errors.full_messages.join(', '),
|
|
||||||
contact: Current.account.contacts.find_by(email: contact_params[:email])
|
|
||||||
}, status: :unprocessable_entity
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|||||||
@ -37,7 +37,8 @@ module RequestExceptionHandler
|
|||||||
|
|
||||||
def render_record_invalid(exception)
|
def render_record_invalid(exception)
|
||||||
render json: {
|
render json: {
|
||||||
message: exception.record.errors.full_messages.join(', ')
|
message: exception.record.errors.full_messages.join(', '),
|
||||||
|
attributes: exception.record.errors.attribute_names
|
||||||
}, status: :unprocessable_entity
|
}, status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -103,13 +103,15 @@
|
|||||||
},
|
},
|
||||||
"EMAIL_ADDRESS": {
|
"EMAIL_ADDRESS": {
|
||||||
"PLACEHOLDER": "Enter the email address of the contact",
|
"PLACEHOLDER": "Enter the email address of the contact",
|
||||||
"LABEL": "Email Address"
|
"LABEL": "Email Address",
|
||||||
|
"DUPLICATE": "This email address is in use for another contact."
|
||||||
},
|
},
|
||||||
"PHONE_NUMBER": {
|
"PHONE_NUMBER": {
|
||||||
"PLACEHOLDER": "Enter the phone number of the contact",
|
"PLACEHOLDER": "Enter the phone number of the contact",
|
||||||
"LABEL": "Phone Number",
|
"LABEL": "Phone Number",
|
||||||
"HELP": "Phone number should be of E.164 format eg: +1415555555 [+][country code][area code][local phone number]",
|
"HELP": "Phone number should be of E.164 format eg: +1415555555 [+][country code][area code][local phone number]",
|
||||||
"ERROR": "Phone number should be either empty or of E.164 format"
|
"ERROR": "Phone number should be either empty or of E.164 format",
|
||||||
|
"DUPLICATE": "This phone number is in use for another contact."
|
||||||
},
|
},
|
||||||
"LOCATION": {
|
"LOCATION": {
|
||||||
"PLACEHOLDER": "Enter the location of the contact",
|
"PLACEHOLDER": "Enter the location of the contact",
|
||||||
@ -139,7 +141,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"SUCCESS_MESSAGE": "Contact saved successfully",
|
"SUCCESS_MESSAGE": "Contact saved successfully",
|
||||||
"CONTACT_ALREADY_EXIST": "This email address is in use for another contact.",
|
|
||||||
"ERROR_MESSAGE": "There was an error, please try again"
|
"ERROR_MESSAGE": "There was an error, please try again"
|
||||||
},
|
},
|
||||||
"NEW_CONVERSATION": {
|
"NEW_CONVERSATION": {
|
||||||
|
|||||||
@ -121,8 +121,6 @@ export default {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
hasADuplicateContact: false,
|
|
||||||
duplicateContact: {},
|
|
||||||
companyName: '',
|
companyName: '',
|
||||||
description: '',
|
description: '',
|
||||||
email: '',
|
email: '',
|
||||||
@ -201,12 +199,7 @@ export default {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
resetDuplicate() {
|
|
||||||
this.hasADuplicateContact = false;
|
|
||||||
this.duplicateContact = {};
|
|
||||||
},
|
|
||||||
async handleSubmit() {
|
async handleSubmit() {
|
||||||
this.resetDuplicate();
|
|
||||||
this.$v.$touch();
|
this.$v.$touch();
|
||||||
|
|
||||||
if (this.$v.$invalid) {
|
if (this.$v.$invalid) {
|
||||||
@ -218,9 +211,13 @@ export default {
|
|||||||
this.showAlert(this.$t('CONTACT_FORM.SUCCESS_MESSAGE'));
|
this.showAlert(this.$t('CONTACT_FORM.SUCCESS_MESSAGE'));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof DuplicateContactException) {
|
if (error instanceof DuplicateContactException) {
|
||||||
this.hasADuplicateContact = true;
|
if (error.data.includes('email')) {
|
||||||
this.duplicateContact = error.data;
|
this.showAlert(
|
||||||
this.showAlert(this.$t('CONTACT_FORM.CONTACT_ALREADY_EXIST'));
|
this.$t('CONTACT_FORM.FORM.EMAIL_ADDRESS.DUPLICATE')
|
||||||
|
);
|
||||||
|
} else if (error.data.includes('phone_number')) {
|
||||||
|
this.showAlert(this.$t('CONTACT_FORM.FORM.PHONE_NUMBER.DUPLICATE'));
|
||||||
|
}
|
||||||
} else if (error instanceof ExceptionWithMessage) {
|
} else if (error instanceof ExceptionWithMessage) {
|
||||||
this.showAlert(error.data);
|
this.showAlert(error.data);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -60,8 +60,8 @@ export const actions = {
|
|||||||
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
||||||
if (error.response?.data?.contact) {
|
if (error.response?.status === 422) {
|
||||||
throw new DuplicateContactException(error.response.data.contact);
|
throw new DuplicateContactException(error.response.data.attributes);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,9 +94,10 @@ describe('#actions', () => {
|
|||||||
it('sends correct actions if duplicate contact is found', async () => {
|
it('sends correct actions if duplicate contact is found', async () => {
|
||||||
axios.patch.mockRejectedValue({
|
axios.patch.mockRejectedValue({
|
||||||
response: {
|
response: {
|
||||||
|
status: 422,
|
||||||
data: {
|
data: {
|
||||||
message: 'Incorrect header',
|
message: 'Incorrect header',
|
||||||
contact: { id: 1, name: 'contact-name' },
|
attributes: ['email'],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,15 +3,11 @@ const { DuplicateContactException } = require('../CustomErrors');
|
|||||||
describe('DuplicateContactException', () => {
|
describe('DuplicateContactException', () => {
|
||||||
it('returns correct exception', () => {
|
it('returns correct exception', () => {
|
||||||
const exception = new DuplicateContactException({
|
const exception = new DuplicateContactException({
|
||||||
id: 1,
|
attributes: ['email'],
|
||||||
name: 'contact-name',
|
|
||||||
email: 'email@example.com',
|
|
||||||
});
|
});
|
||||||
expect(exception.message).toEqual('DUPLICATE_CONTACT');
|
expect(exception.message).toEqual('DUPLICATE_CONTACT');
|
||||||
expect(exception.data).toEqual({
|
expect(exception.data).toEqual({
|
||||||
id: 1,
|
attributes: ['email'],
|
||||||
name: 'contact-name',
|
|
||||||
email: 'email@example.com',
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -448,7 +448,19 @@ RSpec.describe 'Contacts API', type: :request do
|
|||||||
as: :json
|
as: :json
|
||||||
|
|
||||||
expect(response).to have_http_status(:unprocessable_entity)
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
expect(JSON.parse(response.body)['contact']['id']).to eq(other_contact.id)
|
expect(JSON.parse(response.body)['attributes']).to include('email')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'prevents updating with an existing phone number' do
|
||||||
|
other_contact = create(:contact, account: account, phone_number: '+12000000')
|
||||||
|
|
||||||
|
patch "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
||||||
|
headers: admin.create_new_auth_token,
|
||||||
|
params: valid_params[:contact].merge({ phone_number: other_contact.phone_number }),
|
||||||
|
as: :json
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(JSON.parse(response.body)['attributes']).to include('phone_number')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user