|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class ConnectedFields::Eg001SetConnectedFieldsService |
| 4 | + attr_reader :args |
| 5 | + |
| 6 | + include ApiCreator |
| 7 | + |
| 8 | + def initialize(args) |
| 9 | + @args = args |
| 10 | + end |
| 11 | + |
| 12 | + def get_tab_groups |
| 13 | + #ds-snippet-start:ConnectedFields1Step2 |
| 14 | + headers = { |
| 15 | + 'Authorization' => "Bearer #{args[:access_token]}", |
| 16 | + 'Accept' => 'application/json', |
| 17 | + 'Content-Type' => 'application/json' |
| 18 | + } |
| 19 | + #ds-snippet-end:ConnectedFields1Step2 |
| 20 | + |
| 21 | + #ds-snippet-start:ConnectedFields1Step3 |
| 22 | + url = URI("#{args[:extensions_base_path]}/v1/accounts/#{args[:account_id]}/connected-fields/tab-groups") |
| 23 | + response = Net::HTTP.get_response(url, headers) |
| 24 | + response_data = JSON.parse(response.body) |
| 25 | + |
| 26 | + filtered_apps = response_data.select do |app| |
| 27 | + app['tabs']&.any? do |tab| |
| 28 | + (tab['extensionData']&.dig('actionContract')&.include? 'Verify') || |
| 29 | + (tab['tabLabel']&.include? 'connecteddata') |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # return unique apps |
| 34 | + filtered_apps.uniq { |app| app['appId'] } |
| 35 | + #ds-snippet-end:ConnectedFields1Step3 |
| 36 | + end |
| 37 | + |
| 38 | + #ds-snippet-start:ConnectedFields1Step4 |
| 39 | + def extract_verification_data(selected_app_id, tab) |
| 40 | + extension_data = tab['extensionData'] |
| 41 | + |
| 42 | + { |
| 43 | + app_id: selected_app_id, |
| 44 | + extension_group_id: extension_data['extensionGroupId'], |
| 45 | + publisher_name: extension_data['publisherName'], |
| 46 | + application_name: extension_data['applicationName'], |
| 47 | + action_name: extension_data['actionName'], |
| 48 | + action_input_key: extension_data['actionInputKey'], |
| 49 | + action_contract: extension_data['actionContract'], |
| 50 | + extension_name: extension_data['extensionName'], |
| 51 | + extension_contract: extension_data['extensionContract'], |
| 52 | + required_for_extension: extension_data['requiredForExtension'], |
| 53 | + tab_label: tab['tabLabel'], |
| 54 | + connection_key: extension_data['connectionInstances']&.dig(0, 'connectionKey') || '', |
| 55 | + connection_value: extension_data['connectionInstances']&.dig(0, 'connectionValue') || '' |
| 56 | + } |
| 57 | + end |
| 58 | + #ds-snippet-end:ConnectedFields1Step4 |
| 59 | + |
| 60 | + def send_envelope(app) |
| 61 | + # Create the envelope definition |
| 62 | + #ds-snippet-start:ConnectedFields1Step6 |
| 63 | + envelope = make_envelope args[:envelope_args], app |
| 64 | + |
| 65 | + # Call Docusign to create the envelope |
| 66 | + envelope_api = create_envelope_api(args) |
| 67 | + |
| 68 | + results = envelope_api.create_envelope args[:account_id], envelope |
| 69 | + |
| 70 | + { 'envelope_id' => results.envelope_id } |
| 71 | + #ds-snippet-end:ConnectedFields1Step6 |
| 72 | + end |
| 73 | + |
| 74 | + private |
| 75 | + |
| 76 | + #ds-snippet-start:ConnectedFields1Step5 |
| 77 | + def make_envelope(envelope_args, app) |
| 78 | + doc = DocuSign_eSign::Document.new |
| 79 | + doc.document_base64 = Base64.encode64(File.binread(envelope_args[:doc_pdf])) |
| 80 | + doc.name = 'Lorem Ipsum' |
| 81 | + doc.file_extension = 'pdf' |
| 82 | + doc.document_id = '1' |
| 83 | + |
| 84 | + # The order in the docs array determines the order in the envelope |
| 85 | + # Create a signer recipient to sign the document, identified by name and email |
| 86 | + # We're setting the parameters via the object creation |
| 87 | + signer = DocuSign_eSign::Signer.new |
| 88 | + signer.email = envelope_args[:signer_email] |
| 89 | + signer.name = envelope_args[:signer_name] |
| 90 | + signer.recipient_id = 1 |
| 91 | + |
| 92 | + # The Docusign platform searches throughout your envelope's documents for matching |
| 93 | + # anchor strings. So the sign_here_2 tab will be used in both document 2 and 3 |
| 94 | + # since they use the same anchor string for their "signer 1" tabs. |
| 95 | + sign_here = DocuSign_eSign::SignHere.new |
| 96 | + sign_here.anchor_string = '/sn1/' |
| 97 | + sign_here.anchor_units = 'pixels' |
| 98 | + sign_here.anchor_x_offset = '20' |
| 99 | + sign_here.anchor_y_offset = '10' |
| 100 | + |
| 101 | + text_tabs = [] |
| 102 | + app['tabs'].each do |tab| |
| 103 | + next if tab['tabLabel'].include?('SuggestionInput') |
| 104 | + |
| 105 | + verification_data = extract_verification_data(app['appId'], tab) |
| 106 | + text_tabs.push(text_tab(verification_data, text_tabs.length)) |
| 107 | + end |
| 108 | + |
| 109 | + tabs = DocuSign_eSign::Tabs.new |
| 110 | + tabs.sign_here_tabs = [sign_here] |
| 111 | + tabs.text_tabs = text_tabs |
| 112 | + signer.tabs = tabs |
| 113 | + |
| 114 | + recipients = DocuSign_eSign::Recipients.new |
| 115 | + recipients.signers = [signer] |
| 116 | + |
| 117 | + envelope_definition = DocuSign_eSign::EnvelopeDefinition.new |
| 118 | + envelope_definition.email_subject = 'Please sign this document' |
| 119 | + envelope_definition.documents = [doc] |
| 120 | + envelope_definition.recipients = recipients |
| 121 | + envelope_definition.status = 'sent' |
| 122 | + envelope_definition |
| 123 | + end |
| 124 | + |
| 125 | + def extension_data(verification_data) |
| 126 | + { |
| 127 | + extensionGroupId: verification_data[:extension_group_id], |
| 128 | + publisherName: verification_data[:publisher_name], |
| 129 | + applicationId: verification_data[:app_id], |
| 130 | + applicationName: verification_data[:application_name], |
| 131 | + actionName: verification_data[:action_name], |
| 132 | + actionContract: verification_data[:action_contract], |
| 133 | + extensionName: verification_data[:extension_name], |
| 134 | + extensionContract: verification_data[:extension_contract], |
| 135 | + requiredForExtension: verification_data[:required_for_extension], |
| 136 | + actionInputKey: verification_data[:action_input_key], |
| 137 | + extensionPolicy: 'MustVerifyToSign', |
| 138 | + connectionInstances: [ |
| 139 | + { |
| 140 | + connectionKey: verification_data[:connection_key], |
| 141 | + connectionValue: verification_data[:connection_value] |
| 142 | + } |
| 143 | + ] |
| 144 | + } |
| 145 | + end |
| 146 | + |
| 147 | + def text_tab(verification_data, text_tabs_count) |
| 148 | + { |
| 149 | + requireInitialOnSharedChange: false, |
| 150 | + requireAll: false, |
| 151 | + name: verification_data[:application_name], |
| 152 | + required: true, |
| 153 | + locked: false, |
| 154 | + disableAutoSize: false, |
| 155 | + maxLength: 4000, |
| 156 | + tabLabel: verification_data[:tab_label], |
| 157 | + font: 'lucidaconsole', |
| 158 | + fontColor: 'black', |
| 159 | + fontSize: 'size9', |
| 160 | + documentId: '1', |
| 161 | + recipientId: '1', |
| 162 | + pageNumber: '1', |
| 163 | + xPosition: (70 + 100 * (text_tabs_count / 10)).to_s, |
| 164 | + yPosition: (560 + 20 * (text_tabs_count % 10)).to_s, |
| 165 | + width: '84', |
| 166 | + height: '22', |
| 167 | + templateRequired: false, |
| 168 | + tabType: 'text', |
| 169 | + tooltip: verification_data[:action_input_key], |
| 170 | + extensionData: extension_data(verification_data) |
| 171 | + } |
| 172 | + end |
| 173 | + #ds-snippet-end:ConnectedFields1Step5 |
| 174 | +end |
0 commit comments