module Constituents def get_constituent_snapshot(id, options = {}) options.merge!(basic_auth: @auth, headers: @headers) response = self.class.get(base_api_endpoint("CRM/Constituents/#{id}/Snapshot"), options) JSON.parse(response.body) end def create_constituent_by_snapshot(first_name, last_name, email, phone, constituent_type, source, street1, street2, city, state, zip, country, options = {}) parameters = { 'ConstituentType': { 'Id': constituent_type, 'Inactive': false, }, 'FirstName': first_name, 'LastName': last_name, 'ElectronicAddress': { 'Address': email, 'ElectronicAddressType': { 'Id': 1, }, 'AllowHtmlFormat': true, 'Inactive': false, 'AllowMarketing': false, 'Months': 'YYYYYYYYYYYY', 'PrimaryIndicator': true, }, 'PrimaryPhoneNumbers': [ { 'PhoneNumber': phone, 'PhoneType': { 'Id': 1, }, }, ], 'OriginalSource': { 'Id': source, }, 'Address': { 'AddressType': { 'Id': 3, }, 'City': city, 'PostalCode': zip, 'State': { 'Id': state, }, 'Street1': street1, 'Street2': street2, 'Country': { 'Id': country, }, }, } parameters.delete(:PrimaryPhoneNumbers) unless phone.present? options.merge!(basic_auth: @auth, headers: @headers) options.merge!(:body => parameters.to_json, :headers => { 'Content-Type' => 'application/json' }) self.class.post(base_api_endpoint('CRM/Constituents/Snapshot'), options) end def update_constituent(constituent_id, params, options = {}) current = get_constituent_snapshot(constituent_id) if is_household?(current) update_household(params, current) else parameters = { 'ConstituentType': { 'Id': current['ConstituentType']['Id'], 'Inactive': false, }, 'Id': constituent_id, 'UpdatedDateTime': current['UpdatedDateTime'], 'FirstName': params[:first_name], 'LastName': params[:last_name], 'MiddleName': params[:middle_name], 'SortName': current['SortName'], 'Prefix': { 'Id': params[:prefix], 'Inactive': false, }, 'Suffix': { 'Id': params[:suffix], 'Inactive': false, }, 'OriginalSource': { 'Id': current['OriginalSource']['Id'], 'Inactive': false, }, } parameters.delete(:Prefix) unless params[:prefix].present? parameters.delete(:Suffix) unless params[:suffix].present? options.merge!(basic_auth: @auth, headers: @headers) options.merge!(:body => parameters.to_json, :headers => { 'Content-Type' => 'application/json' }) self.class.put(base_api_endpoint("CRM/Constituents/#{constituent_id}"), options) end end def is_household?(snapshot) snapshot['ConstituentType']['Id'] == 9 end def update_household(params, current, options = {}) constituents = current['Affiliates'].map { |c| c['RelatedConstituentId'] } update = [] constituents.each_with_index do |constituent, i| snapshot = get_constituent_snapshot(constituent) parameters = { 'ConstituentType': { 'Id': snapshot['ConstituentType']['Id'], 'Inactive': false, }, 'Id': constituent, 'FirstName': i == 0 ? params[:first_name] : params[:first_name_2], 'UpdatedDateTime': snapshot['UpdatedDateTime'], 'LastName': i == 0 ? params[:last_name] : params[:last_name_2], 'MiddleName': i == 0 ? params[:middle_name] : params[:middle_name_2], 'Prefix': { 'Id': i == 0 ? params[:prefix] : params[:prefix_2], 'Inactive': false, }, 'Suffix': { 'Id': i == 0 ? params[:suffix] : params[:suffix_2], 'Inactive': false, }, 'OriginalSource': { 'Id': snapshot['OriginalSource']['Id'], 'Inactive': false, }, } options.merge!(basic_auth: @auth, headers: @headers) options.merge!(:body => parameters) update << self.class.put(base_api_endpoint("CRM/Constituents/#{constituent}"), options) end update.last if update.present? end end