Skip to content

Commit 3a79578

Browse files
committed
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
1 parent d8f04ad commit 3a79578

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

app/controllers/api/templates_controller.rb

Lines changed: 13 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,13 @@ 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+
return unless audience.in?(VALID_AUDIENCE_VALUES)
212+
213+
template.update!(preferences: template.preferences.merge('submitters_order' => audience))
214+
end
215+
204216
def finalize_template_creation(template, documents)
205217
enqueue_template_created_webhooks(template)
206218
SearchEntries.enqueue_reindex(template)
@@ -252,7 +264,7 @@ def template_params
252264
external_data_fields: {},
253265
submitters: [%i[name uuid is_requester invite_by_uuid optional_invite_by_uuid linked_to_uuid email]],
254266
fields: [[:uuid, :question_id, :submitter_uuid, :name, :type,
255-
:required, :readonly, :default_value,
267+
:required, :readonly, :default_value, :prefill,
256268
:title, :description,
257269
{ preferences: {},
258270
conditions: [%i[field_uuid value action operation]],

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)