Module: Constituents

Included in:
TessituraRest
Defined in:
lib/tessitura_rest/crm/constituents.rb

Instance Method Summary collapse

Instance Method Details

#create_constituent_by_snapshot(first_name, last_name, email, phone, constituent_type, source, street1, street2, city, state, zip, country, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tessitura_rest/crm/constituents.rb', line 14

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': 5,
              },
          },
        ],
      '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)
  self.class.post(base_api_endpoint('CRM/Constituents/Snapshot'), options)
end

#create_constituent_by_snapshot_v16(first_name, last_name, email, phone, constituent_type, source, street1, street2, city, state, zip, country, options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tessitura_rest/crm/constituents.rb', line 69

def create_constituent_by_snapshot_v16(first_name, last_name, email, phone, constituent_type, source, street1, street2, city, state, zip, country, options = {})
  parameters =
    {
      'ConstituentType': {
        'Id': constituent_type,
      },
      'FirstName': first_name,
      'LastName': last_name,
      'OriginalSource': {
        'Id': source,
      },
      'PrimaryAddress': {
        'AddressType': {
          'Id': 3,
        },
        'City': city,
        'PostalCode': zip,
        'State': {
          'Id': state,
        },
        'Street1': street1,
        'Street2': street2,
        'Country': {
          'Id': country,
        },
      },
      'PrimaryElectronicAddress': {
        'Address': email,
        'ElectronicAddressType': {
          'Id': 1,
        },
        'AllowHtmlFormat': true,
        'Inactive': false,
        'AllowMarketing': false,
        'Months': 'YYYYYYYYYYYY',
        'PrimaryIndicator': true,
        'PrimaryPhone': {
          'PhoneNumber': phone,
          'PhoneType': {
            'Id': 5,
          },
        },
      },
    }
  options.merge!(basic_auth: @auth, headers: @headers)
  options.merge!(:body => parameters.to_json)
  self.class.post(base_api_endpoint('CRM/Constituents/Snapshot'), options)
end

#get_constituent_snapshot(id, options = {}) ⇒ Object



2
3
4
5
6
# File 'lib/tessitura_rest/crm/constituents.rb', line 2

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

#is_household?(snapshot) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/tessitura_rest/crm/constituents.rb', line 156

def is_household?(snapshot)
  snapshot['ConstituentType']['Id'] == 9
end

#search_constituents(query, type = 'fluent', include_affiliates = false, constituent_groups = 'individuals', options = {}) ⇒ Object



8
9
10
11
12
# File 'lib/tessitura_rest/crm/constituents.rb', line 8

def search_constituents(query, type = 'fluent', include_affiliates = false, constituent_groups = 'individuals', options = {})
  options.merge!(basic_auth: @auth, headers: @headers)
  response = self.class.get(base_api_endpoint("CRM/Constituents/Search?type=#{type}&q=#{query}&includeAffiliates=#{include_affiliates}&constituentGroups=#{constituent_groups}"), options)
  JSON.parse(response.body)
end

#update_constituent(constituent_id, params, options = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tessitura_rest/crm/constituents.rb', line 118

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)
    self.class.put(base_api_endpoint("CRM/Constituents/#{constituent_id}"), options)
  end
end

#update_household(params, current, options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/tessitura_rest/crm/constituents.rb', line 160

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.to_json)
    update << self.class.put(base_api_endpoint("CRM/Constituents/#{constituent}"), options)
  end
  update.last if update.present?
end