Fixes: #3628 - FreshDesk import fails on certain custom field types.

This commit is contained in:
Martin Gruner 2021-07-12 07:51:07 +00:00
parent a8f028164d
commit ce5fa9def9
2 changed files with 71 additions and 13 deletions

View file

@ -34,17 +34,22 @@ class Sequencer
private
DATA_TYPE_MAP = {
'custom_date' => 'date',
'custom_checkbox' => 'boolean',
'custom_dropdown' => 'select',
'custom_text' => 'input',
'custom_number' => 'integer',
'custom_paragraph' => 'input',
'custom_decimal' => 'input', # Don't use 'integer' as it would cut off the fractional part.
'custom_date' => 'date',
'custom_checkbox' => 'boolean',
'custom_dropdown' => 'select',
'custom_text' => 'input',
'custom_number' => 'integer',
'custom_paragraph' => 'input',
'custom_decimal' => 'input', # Don't use 'integer' as it would cut off the fractional part.
'custom_url' => 'input',
'custom_phone_number' => 'input',
}.freeze
def data_type
@data_type ||= DATA_TYPE_MAP[resource['type']]
raise "The custom field type '#{resource['type']}' cannot be mapped to an internal field, aborting." if !@data_type
@data_type
end
def data_option
@ -54,7 +59,7 @@ class Sequencer
}.merge(data_type_options)
end
def data_type_options
def data_type_options # rubocop:disable Metrics/CyclomaticComplexity
case data_type
when 'date'
@ -77,10 +82,23 @@ class Sequencer
options: options,
}
when 'input'
{
type: 'text',
maxlength: 255,
}
case resource['type']
when 'custom_phone_number'
{
type: 'tel',
maxlength: 100,
}
when 'custom_url'
{
type: 'url',
maxlength: 250,
}
else
{
type: 'text',
maxlength: 255,
}
end
when 'integer'
{
min: 0,

View file

@ -141,6 +141,46 @@ RSpec.describe ::Sequencer::Sequence::Import::Freshdesk::TicketField, sequencer:
end
end
context 'when field is a phone number' do
let(:resource) do
base_resource.merge(
{
'name' => 'cf_custom_phone_number',
'type' => 'custom_phone_number',
}
)
end
it 'adds a custom field' do
expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_phone_number'])
end
it 'the custom field has type "tel"' do
process(process_payload)
expect( ObjectManager::Attribute.find_by(name: 'cf_custom_phone_number').data_option ).to include( 'type' => 'tel' )
end
end
context 'when field is an URL' do
let(:resource) do
base_resource.merge(
{
'name' => 'cf_custom_url',
'type' => 'custom_url',
}
)
end
it 'adds a custom field' do
expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_url'])
end
it 'the custom field has type "url"' do
process(process_payload)
expect( ObjectManager::Attribute.find_by(name: 'cf_custom_url').data_option ).to include( 'type' => 'url' )
end
end
context 'when field is invalid' do
let(:resource) do
base_resource.merge(
@ -152,7 +192,7 @@ RSpec.describe ::Sequencer::Sequence::Import::Freshdesk::TicketField, sequencer:
end
it 'raises an error' do
expect { process(process_payload) }.to raise_error(ActiveRecord::RecordInvalid)
expect { process(process_payload) }.to raise_error(RuntimeError, "The custom field type 'custom_unknown' cannot be mapped to an internal field, aborting.")
end
end
end