Skip to content

Commit

Permalink
Fix: Update contact validation messages (#3500)
Browse files Browse the repository at this point in the history
Fixes #3476
  • Loading branch information
thedev105 committed Dec 3, 2021
1 parent d7cfe68 commit 6d378eb
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
5 changes: 0 additions & 5 deletions app/controllers/api/v1/accounts/contacts_controller.rb
Expand Up @@ -81,11 +81,6 @@ def create
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
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/concerns/request_exception_handler.rb
Expand Up @@ -37,7 +37,8 @@ def render_internal_server_error(message)

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

Expand Down
7 changes: 4 additions & 3 deletions app/javascript/dashboard/i18n/locale/en/contact.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
Expand Up @@ -121,8 +121,6 @@ export default {
},
data() {
return {
hasADuplicateContact: false,
duplicateContact: {},
companyName: '',
description: '',
email: '',
Expand Down Expand Up @@ -201,12 +199,7 @@ export default {
},
};
},
resetDuplicate() {
this.hasADuplicateContact = false;
this.duplicateContact = {};
},
async handleSubmit() {
this.resetDuplicate();
this.$v.$touch();
if (this.$v.$invalid) {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/dashboard/store/modules/contacts/actions.js
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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'],
},
},
});
Expand Down
8 changes: 2 additions & 6 deletions app/javascript/shared/helpers/specs/CustomErrors.spec.js
Expand Up @@ -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'],
});
});
});
14 changes: 13 additions & 1 deletion spec/controllers/api/v1/accounts/contacts_controller_spec.rb
Expand Up @@ -448,7 +448,19 @@
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
Expand Down

0 comments on commit 6d378eb

Please sign in to comment.