Fix: Update contact validation messages (#3500)

Fixes #3476
This commit is contained in:
thedev105 2021-12-03 10:49:11 +03:00 committed by GitHub
parent d7cfe6858e
commit 6d378eb206
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 29 deletions

View file

@ -81,11 +81,6 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
def update
@contact.assign_attributes(contact_update_params)
@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
def destroy

View file

@ -37,7 +37,8 @@ module RequestExceptionHandler
def render_record_invalid(exception)
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
end

View file

@ -103,13 +103,15 @@
},
"EMAIL_ADDRESS": {
"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": {
"PLACEHOLDER": "Enter the phone number of the contact",
"LABEL": "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": {
"PLACEHOLDER": "Enter the location of the contact",
@ -139,7 +141,6 @@
}
},
"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"
},
"NEW_CONVERSATION": {

View file

@ -121,8 +121,6 @@ export default {
},
data() {
return {
hasADuplicateContact: false,
duplicateContact: {},
companyName: '',
description: '',
email: '',
@ -201,12 +199,7 @@ export default {
},
};
},
resetDuplicate() {
this.hasADuplicateContact = false;
this.duplicateContact = {};
},
async handleSubmit() {
this.resetDuplicate();
this.$v.$touch();
if (this.$v.$invalid) {
@ -218,9 +211,13 @@ export default {
this.showAlert(this.$t('CONTACT_FORM.SUCCESS_MESSAGE'));
} catch (error) {
if (error instanceof DuplicateContactException) {
this.hasADuplicateContact = true;
this.duplicateContact = error.data;
this.showAlert(this.$t('CONTACT_FORM.CONTACT_ALREADY_EXIST'));
if (error.data.includes('email')) {
this.showAlert(
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) {
this.showAlert(error.data);
} else {

View file

@ -60,8 +60,8 @@ export const actions = {
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
} catch (error) {
commit(types.SET_CONTACT_UI_FLAG, { isUpdating: false });
if (error.response?.data?.contact) {
throw new DuplicateContactException(error.response.data.contact);
if (error.response?.status === 422) {
throw new DuplicateContactException(error.response.data.attributes);
} else {
throw new Error(error);
}

View file

@ -94,9 +94,10 @@ describe('#actions', () => {
it('sends correct actions if duplicate contact is found', async () => {
axios.patch.mockRejectedValue({
response: {
status: 422,
data: {
message: 'Incorrect header',
contact: { id: 1, name: 'contact-name' },
attributes: ['email'],
},
},
});

View file

@ -3,15 +3,11 @@ const { DuplicateContactException } = require('../CustomErrors');
describe('DuplicateContactException', () => {
it('returns correct exception', () => {
const exception = new DuplicateContactException({
id: 1,
name: 'contact-name',
email: 'email@example.com',
attributes: ['email'],
});
expect(exception.message).toEqual('DUPLICATE_CONTACT');
expect(exception.data).toEqual({
id: 1,
name: 'contact-name',
email: 'email@example.com',
attributes: ['email'],
});
});
});

View file

@ -448,7 +448,19 @@ RSpec.describe 'Contacts API', type: :request do
as: :json
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