Skip to content

Commit c81b1ea

Browse files
authored
CP-12545 docuseal conversion bugfixes (#60)
* add audience as prefill to template creation * this is primarily for conversion from ATS to Docuseal, since these fields would normally just be updated through the Docuseal iframe itself * update manager first and last name label * use word map to properly display Firstname as First Name, Lastname as Last Name * add warning log if invalid audience is passed in * fix line length
1 parent d8f04ad commit c81b1ea

3 files changed

Lines changed: 102 additions & 3 deletions

File tree

app/controllers/api/templates_controller.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ def destroy
8585
render json: @template.as_json(only: %i[id archived_at])
8686
end
8787

88+
VALID_AUDIENCE_VALUES = %w[single_sided employee_then_manager manager_then_employee simultaneous].freeze
89+
8890
def pdf
8991
template = build_template
92+
audience = params[:audience]
9093
fields_from_request = params[:fields] if params[:fields].present?
9194

9295
template.save!
@@ -99,6 +102,8 @@ def pdf
99102

100103
template.update!(schema: schema)
101104

105+
apply_audience(template, audience)
106+
102107
finalize_template_creation(template, documents)
103108
rescue StandardError => e
104109
template.destroy!
@@ -201,6 +206,17 @@ def map_request_fields_to_documents(fields_from_request, documents)
201206
end
202207
end
203208

209+
def apply_audience(template, audience)
210+
return if audience.blank?
211+
212+
unless audience.in?(VALID_AUDIENCE_VALUES)
213+
Rails.logger.warn("Invalid audience value '#{audience}' for template #{template.id}.")
214+
return
215+
end
216+
217+
template.update!(preferences: template.preferences.merge('submitters_order' => audience))
218+
end
219+
204220
def finalize_template_creation(template, documents)
205221
enqueue_template_created_webhooks(template)
206222
SearchEntries.enqueue_reindex(template)
@@ -252,7 +268,7 @@ def template_params
252268
external_data_fields: {},
253269
submitters: [%i[name uuid is_requester invite_by_uuid optional_invite_by_uuid linked_to_uuid email]],
254270
fields: [[:uuid, :question_id, :submitter_uuid, :name, :type,
255-
:required, :readonly, :default_value,
271+
:required, :readonly, :default_value, :prefill,
256272
:title, :description,
257273
{ preferences: {},
258274
conditions: [%i[field_uuid value action operation]],

app/javascript/template_builder/field_settings.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,14 @@ export default {
595595
},
596596
methods: {
597597
formatPrefillFieldName (fieldName) {
598-
// Convert snake_case to Title Case for display
598+
const wordMap = {
599+
firstname: 'First Name',
600+
lastname: 'Last Name'
601+
}
602+
599603
return fieldName
600604
.split('_')
601-
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
605+
.map(word => wordMap[word.toLowerCase()] || (word.charAt(0).toUpperCase() + word.slice(1)))
602606
.join(' ')
603607
},
604608
onChangeValidation (event) {

spec/requests/templates_spec.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,85 @@
150150
end
151151
end
152152

153+
describe 'POST /api/templates/pdf' do
154+
let(:pdf_base64) { Base64.strict_encode64(Rails.root.join('spec/fixtures/sample-document.pdf').read) }
155+
let(:base_params) do
156+
{
157+
name: 'Test Template',
158+
documents: [{ name: 'sample-document.pdf', file: pdf_base64 }]
159+
}
160+
end
161+
162+
it 'creates a template with audience set as submitters_order' do
163+
post '/api/templates/pdf',
164+
headers: { 'x-auth-token': author.access_token.token },
165+
params: base_params.merge(audience: 'manager_then_employee').to_json,
166+
env: { 'CONTENT_TYPE' => 'application/json' }
167+
168+
expect(response).to have_http_status(:ok)
169+
170+
template = Template.last
171+
expect(template.preferences['submitters_order']).to eq('manager_then_employee')
172+
end
173+
174+
it 'creates a template with simultaneous audience' do
175+
post '/api/templates/pdf',
176+
headers: { 'x-auth-token': author.access_token.token },
177+
params: base_params.merge(audience: 'simultaneous').to_json,
178+
env: { 'CONTENT_TYPE' => 'application/json' }
179+
180+
expect(response).to have_http_status(:ok)
181+
expect(Template.last.preferences['submitters_order']).to eq('simultaneous')
182+
end
183+
184+
it 'ignores audience when not provided' do
185+
post '/api/templates/pdf',
186+
headers: { 'x-auth-token': author.access_token.token },
187+
params: base_params.to_json,
188+
env: { 'CONTENT_TYPE' => 'application/json' }
189+
190+
expect(response).to have_http_status(:ok)
191+
expect(Template.last.preferences['submitters_order']).to eq('single_sided')
192+
end
193+
194+
it 'ignores an invalid audience value' do
195+
post '/api/templates/pdf',
196+
headers: { 'x-auth-token': author.access_token.token },
197+
params: base_params.merge(audience: 'invalid_value').to_json,
198+
env: { 'CONTENT_TYPE' => 'application/json' }
199+
200+
expect(response).to have_http_status(:ok)
201+
expect(Template.last.preferences['submitters_order']).to eq('single_sided')
202+
end
203+
204+
it 'stores prefill attribute on fields' do
205+
employee_uuid = SecureRandom.uuid
206+
fields = [
207+
{
208+
uuid: SecureRandom.uuid,
209+
submitter_uuid: employee_uuid,
210+
name: 'First Name',
211+
type: 'text',
212+
prefill: 'employee_first_name',
213+
areas: [{ x: 0.1, y: 0.1, w: 0.2, h: 0.03, page: 0 }]
214+
}
215+
]
216+
217+
post '/api/templates/pdf',
218+
headers: { 'x-auth-token': author.access_token.token },
219+
params: base_params.merge(
220+
submitters: [{ name: 'Employee', uuid: employee_uuid }],
221+
fields:
222+
).to_json,
223+
env: { 'CONTENT_TYPE' => 'application/json' }
224+
225+
expect(response).to have_http_status(:ok)
226+
227+
stored_field = Template.last.fields.find { |f| f['name'] == 'First Name' }
228+
expect(stored_field['prefill']).to eq('employee_first_name')
229+
end
230+
end
231+
153232
describe 'POST /api/templates/:id/clone' do
154233
it 'clones a template' do
155234
template = create(:template, account:,

0 commit comments

Comments
 (0)