osm 1.2.18.dev.5 → 1.2.18.dev.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +8 -8
  2. data/lib/osm/member.rb +170 -112
  3. data/spec/osm/member_spec.rb +436 -273
  4. data/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTVmYTI0ZmYzMWMyNmQ0NDBmY2VhZGQ4MDJjYTZiZTBmYjdiYjU0Yg==
4
+ ODBjYWJhMTRjZDA3NTBmOGJhNjE3YmM2MjRhZmMwMDllM2Y3OGY0ZQ==
5
5
  data.tar.gz: !binary |-
6
- NDRjN2MxMjlkYjc4OWYyYmE4N2VkOThhMDM0N2QyYjI1MzUxZjM0ZA==
6
+ ZWQwN2ZlNGY5ZjkzZGIwYWU0YTM0MGNiNGI4NmM0ZjcxZmJkNDk1Mw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTc0MDVhMjIxZjQ0M2ZhNzIxOWQ2M2NjNTk5MmQyY2JhMmE2NGE3ZWExY2Vm
10
- YzI0ZDkxMTZhOWNhOWQ0MzgwM2UyNTQ3YWU0ZDk2MWFlNjllMzNkZDQwODJi
11
- MDIyZDEwMDM4NjFhODQ1NWNmYjI2OTJkMzY2N2ZjZDFkZGZkOTc=
9
+ NWZkNzgyNDI3ODU5N2UzNzM3YzkzOWI2MTQ0ZDJjODczYWRhY2U3OGVkMTY2
10
+ ODIyODY0MmUxZTVjN2JjNDY1OGI1Y2ExNTE0ZWQ4YmYwYzAxZTZhMDcyYWJi
11
+ YmMzYWYxNmIwMDI1Mjk5M2ZlZjMzODNmMTU2NmVlZGEwNzJmOGE=
12
12
  data.tar.gz: !binary |-
13
- NWZiZDdlYzViMjBhYzc1NjZjZGE2ZjhjMjBjMmYzZDdiNGM3ZmE5NTkzYmE2
14
- OGNjYzhlMWI2YjdlOGIwN2RkODRiZjJlNjliMmVkNWZhMmM4ZGRlYWFiMDFi
15
- ZTYwYTQxYWY0NTM1MmY4YWUzMGZjYWI3MWUwY2M1MjIxNDQ5NTg=
13
+ YmZiOGMxNzMxMGQ3MmIzOWI0NzIyMGVjYzhkY2M5YmQ4ZWU5NGRlZmE4MTkx
14
+ Mjc2YWNkYjc5NDQ3NDRhZTQ5MzlmNTNkMmQ3ODU5YWM3MjY0ZjI3ZjFkNTNl
15
+ ZjgzNWEyNzcyODMyMWVlMWE1ODVkZDA4OWIxZTc3Y2JkNWVlNjE=
data/lib/osm/member.rb CHANGED
@@ -32,6 +32,7 @@ module Osm
32
32
  CID_GENDER = 34
33
33
  CID_SURGERY = 54
34
34
 
35
+
35
36
  # @!attribute [rw] id
36
37
  # @return [Fixnum] the id for the member
37
38
  # @!attribute [rw] section_id
@@ -69,7 +70,7 @@ module Osm
69
70
  # @!attribute [rw] primary_contact
70
71
  # @return [Osm::Member::PrimaryContact, nil] the member's primary contact (primary contact 1 in OSM) (nil if hidden in OSM)
71
72
  # @!attribute [rw] secondary_contact
72
- # @return [Osm::Member::PrimaryContact, nil] the member's secondary contact (primary contact 2 in OSM) (nil if hidden in OSM)
73
+ # @return [Osm::Member::SecondaryContact, nil] the member's secondary contact (primary contact 2 in OSM) (nil if hidden in OSM)
73
74
  # @!attribute [rw] emergency_contact
74
75
  # @return [Osm::Member::EmergencyContact, nil] the member's emergency contact (nil if hidden in OSM)
75
76
  # @!attribute [rw] doctor
@@ -157,22 +158,28 @@ module Osm
157
158
  structure = Hash[ structure.map{ |i| [i['group_id'].to_i, i ] } ] # Make a hash of identifier to group data hash
158
159
 
159
160
  custom_labels = {}
161
+ var_names = {}
160
162
  structure.each do |gid, group|
161
163
  columns = group['columns'] || []
162
- columns.map!{ |c| [c['column_id'].to_i, c['label']] }
163
- columns.select!{ |a| (gid == GID_CUSTOM) || (a[0] > CORE_FIELD_IDS_FINISH_AT) }
164
- labels = DirtyHashy[ columns ]
165
- custom_labels[gid.to_i] = labels
164
+ columns.select!{ |a| (gid == GID_CUSTOM) || (a['column_id'].to_i > CORE_FIELD_IDS_FINISH_AT) }
165
+ custom_labels[gid.to_i] = Hash[ columns.map{ |c| [c['varname'], c['label']] } ]
166
+ var_names[gid.to_i] = DirtyHashy[ columns.map{ |c| [c['column_id'].to_i, c['varname']] } ]
166
167
  end
167
168
 
168
169
  data.each do |item|
169
170
  item_data = Hash[ item['custom_data'].map{ |k,v| [k.to_i, v] } ]
170
- member_contact = item_data[GID_MEMBER_CONTACT].nil? ? nil : Hash[ item_data[GID_MEMBER_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] < CUSTOM_FIELD_IDS_START_AT } ]
171
- primary_contact = item_data[GID_PRIMARY_CONTACT].nil? ? nil : Hash[ item_data[GID_PRIMARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] < CUSTOM_FIELD_IDS_START_AT } ]
172
- secondary_contact = item_data[GID_SECONDARY_CONTACT].nil? ? nil : Hash[ item_data[GID_SECONDARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] < CUSTOM_FIELD_IDS_START_AT } ]
173
- emergency_contact = item_data[GID_EMERGENCY_CONTACT].nil? ? nil : Hash[ item_data[GID_EMERGENCY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] < CUSTOM_FIELD_IDS_START_AT } ]
174
- doctor_contact = item_data[GID_DOCTOR_CONTACT].nil? ? nil : Hash[ item_data[GID_DOCTOR_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] < CUSTOM_FIELD_IDS_START_AT } ]
175
- floating_data = Hash[ item_data[GID_FLOATING].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] < CUSTOM_FIELD_IDS_START_AT } ]
171
+ member_contact = item_data[GID_MEMBER_CONTACT].nil? ? nil : Hash[ item_data[GID_MEMBER_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ]
172
+ member_custom = item_data[GID_MEMBER_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_MEMBER_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_MEMBER_CONTACT][k.to_i], v] } ]
173
+ primary_contact = item_data[GID_PRIMARY_CONTACT].nil? ? nil : Hash[ item_data[GID_PRIMARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ]
174
+ primary_custom = item_data[GID_PRIMARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_PRIMARY_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_PRIMARY_CONTACT][k.to_i], v] } ]
175
+ secondary_contact = item_data[GID_SECONDARY_CONTACT].nil? ? nil : Hash[ item_data[GID_SECONDARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ]
176
+ secondary_custom = item_data[GID_SECONDARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_SECONDARY_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_SECONDARY_CONTACT][k.to_i], v] } ]
177
+ emergency_contact = item_data[GID_EMERGENCY_CONTACT].nil? ? nil : Hash[ item_data[GID_EMERGENCY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ]
178
+ emergency_custom = item_data[GID_EMERGENCY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_EMERGENCY_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_EMERGENCY_CONTACT][k.to_i], v] } ]
179
+ doctor_contact = item_data[GID_DOCTOR_CONTACT].nil? ? nil : Hash[ item_data[GID_DOCTOR_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ]
180
+ doctor_custom = item_data[GID_DOCTOR_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_DOCTOR_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_DOCTOR_CONTACT][k.to_i], v] } ]
181
+ floating_data = item_data[GID_FLOATING].nil? ? {} : Hash[ item_data[GID_FLOATING].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ]
182
+ custom_data = item_data[GID_CUSTOM].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_CUSTOM].map{ |k,v| [var_names[GID_CUSTOM][k.to_i], v] } ]
176
183
 
177
184
  result.push Osm::Member.new(
178
185
  :id => Osm::to_i_or_nil(item['member_id']),
@@ -205,8 +212,8 @@ module Osm
205
212
  receive_phone_2: member_contact[CID_RECIEVE_PHONE_2],
206
213
  receive_email_1: member_contact[CID_RECIEVE_EMAIL_1],
207
214
  receive_email_2: member_contact[CID_RECIEVE_EMAIL_2],
208
- custom: DirtyHashy[ item_data[GID_MEMBER_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] > CORE_FIELD_IDS_FINISH_AT } ],
209
- custom_labels: custom_labels[GID_MEMBER_CONTACT] || DirtyHashy.new,
215
+ custom: member_custom,
216
+ custom_labels: custom_labels[GID_MEMBER_CONTACT],
210
217
  ),
211
218
  :primary_contact => primary_contact.nil? ? nil : PrimaryContact.new(
212
219
  first_name: primary_contact[CID_FIRST_NAME],
@@ -224,10 +231,10 @@ module Osm
224
231
  receive_phone_2: primary_contact[CID_RECIEVE_PHONE_2],
225
232
  receive_email_1: primary_contact[CID_RECIEVE_EMAIL_1],
226
233
  receive_email_2: primary_contact[CID_RECIEVE_EMAIL_2],
227
- custom: DirtyHashy[ item_data[GID_PRIMARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] > CORE_FIELD_IDS_FINISH_AT } ],
228
- custom_labels: custom_labels[GID_PRIMARY_CONTACT] || DirtyHashy.new,
234
+ custom: primary_custom,
235
+ custom_labels: custom_labels[GID_PRIMARY_CONTACT],
229
236
  ),
230
- :secondary_contact => secondary_contact.nil? ? nil : PrimaryContact.new(
237
+ :secondary_contact => secondary_contact.nil? ? nil : SecondaryContact.new(
231
238
  first_name: secondary_contact[CID_FIRST_NAME],
232
239
  last_name: secondary_contact[CID_LAST_NAME],
233
240
  address_1: secondary_contact[CID_ADDRESS_1],
@@ -243,8 +250,8 @@ module Osm
243
250
  receive_phone_2: secondary_contact[CID_RECIEVE_PHONE_2],
244
251
  receive_email_1: secondary_contact[CID_RECIEVE_EMAIL_1],
245
252
  receive_email_2: secondary_contact[CID_RECIEVE_EMAIL_2],
246
- custom: DirtyHashy[ item_data[GID_SECONDARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] > CORE_FIELD_IDS_FINISH_AT } ],
247
- custom_labels: custom_labels[GID_SECONDARY_CONTACT] || DirtyHashy.new,
253
+ custom: secondary_custom,
254
+ custom_labels: custom_labels[GID_SECONDARY_CONTACT],
248
255
  ),
249
256
  :emergency_contact => emergency_contact.nil? ? nil : EmergencyContact.new(
250
257
  first_name: emergency_contact[CID_FIRST_NAME],
@@ -258,8 +265,8 @@ module Osm
258
265
  phone_2: emergency_contact[CID_PHONE_2],
259
266
  email_1: emergency_contact[CID_EMAIL_1],
260
267
  email_2: emergency_contact[CID_EMAIL_2],
261
- custom: DirtyHashy[ item_data[GID_EMERGENCY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] > CORE_FIELD_IDS_FINISH_AT } ],
262
- custom_labels: custom_labels[GID_EMERGENCY_CONTACT] || DirtyHashy.new,
268
+ custom: emergency_custom,
269
+ custom_labels: custom_labels[GID_EMERGENCY_CONTACT],
263
270
  ),
264
271
  :doctor => doctor_contact.nil? ? nil : DoctorContact.new(
265
272
  first_name: doctor_contact[CID_FIRST_NAME],
@@ -272,11 +279,11 @@ module Osm
272
279
  postcode: doctor_contact[CID_POSTCODE],
273
280
  phone_1: doctor_contact[CID_PHONE_1],
274
281
  phone_2: doctor_contact[CID_PHONE_2],
275
- custom: DirtyHashy[ item_data[GID_DOCTOR_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |i| i[0] > CORE_FIELD_IDS_FINISH_AT } ],
276
- custom_labels: custom_labels[GID_DOCTOR_CONTACT] || DirtyHashy.new,
282
+ custom:doctor_custom,
283
+ custom_labels: custom_labels[GID_DOCTOR_CONTACT],
277
284
  ),
278
- custom: DirtyHashy[ item_data[GID_CUSTOM].map{ |k,v| [k.to_i, v] } ],
279
- custom_labels: custom_labels[GID_CUSTOM] || DirtyHashy.new,
285
+ custom: custom_data,
286
+ custom_labels: custom_labels[GID_CUSTOM],
280
287
  )
281
288
  end
282
289
 
@@ -292,49 +299,21 @@ module Osm
292
299
 
293
300
  # Create the user in OSM
294
301
  # @param [Osm::Api] api The api to use to make the request
295
- # @return [Boolan] whether the member was successfully added or not
302
+ # @return [Boolan, nil] whether the member was successfully added or not (nil is returned if the user was created but not with all the data)
296
303
  # @raise [Osm::ObjectIsInvalid] If the Member is invalid
297
304
  # @raise [Osm::Error] If the member already exists in OSM
298
305
  def create(api)
306
+ raise Osm::Error, 'the member already exists in OSM' unless id.nil?
299
307
  raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
300
308
  require_ability_to(api, :write, :member, section_id)
301
- raise Osm::Error, 'the member already exists in OSM' unless id.nil?
302
309
 
303
310
  data = api.perform_query("users.php?action=newMember", {
304
311
  'firstname' => first_name,
305
312
  'lastname' => last_name,
306
313
  'dob' => date_of_birth.strftime(Osm::OSM_DATE_FORMAT),
307
- 'started' => started.strftime(Osm::OSM_DATE_FORMAT),
308
- 'startedsection' => joined.strftime(Osm::OSM_DATE_FORMAT),
309
- 'patrolid' => grouping_id,
310
- 'patrolleader' => grouping_leader,
314
+ 'started' => joined_movement.strftime(Osm::OSM_DATE_FORMAT),
315
+ 'startedsection' => started_section.strftime(Osm::OSM_DATE_FORMAT),
311
316
  'sectionid' => section_id,
312
- 'email1' => email1,
313
- 'email2' => email2,
314
- 'email3' => email3,
315
- 'email4' => email4,
316
- 'phone1' => phone1,
317
- 'phone2' => phone2,
318
- 'phone3' => phone3,
319
- 'phone4' => phone4,
320
- 'address' => address,
321
- 'address2' => address2,
322
- 'parents' => parents,
323
- 'notes' => notes,
324
- 'medical' => medical,
325
- 'religion' => religion,
326
- 'school' => school,
327
- 'ethnicity' => ethnicity,
328
- 'subs' => subs,
329
- 'custom1' => custom1,
330
- 'custom2' => custom2,
331
- 'custom3' => custom3,
332
- 'custom4' => custom4,
333
- 'custom5' => custom5,
334
- 'custom6' => custom6,
335
- 'custom7' => custom7,
336
- 'custom8' => custom8,
337
- 'custom9' => custom9,
338
317
  })
339
318
 
340
319
  if (data.is_a?(Hash) && (data['result'] == 'ok') && (data['scoutid'].to_i > 0))
@@ -343,7 +322,9 @@ module Osm
343
322
  Osm::Term.get_for_section(api, section_id).each do |term|
344
323
  cache_delete(api, ['members', section_id, term.id])
345
324
  end
346
- return true
325
+ # Now it's created we need to give OSM the rest of the data
326
+ updated = update(api, true)
327
+ return updated ? true : nil
347
328
  else
348
329
  return false
349
330
  end
@@ -351,76 +332,80 @@ module Osm
351
332
 
352
333
  # Update the member in OSM
353
334
  # @param [Osm::Api] api The api to use to make the request
354
- # @return [Boolan] whether the member was successfully updated or not
335
+ # @param [Boolean] force Whether to force updates (ie tell OSM every attribute changed even if we don't think it did)
336
+ # @return [Boolean] whether the member was successfully updated or not
355
337
  # @raise [Osm::ObjectIsInvalid] If the Member is invalid
356
- def update(api)
338
+ def update(api, force=false)
357
339
  raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
358
340
  require_ability_to(api, :write, :member, section_id)
359
341
 
360
- to_update = changed_attributes
361
- values = {}
362
- values['firstname'] = first_name if to_update.include?('first_name')
363
- values['lastname'] = last_name if to_update.include?('last_name')
364
- values['dob'] = date_of_birth.strftime(Osm::OSM_DATE_FORMAT) if to_update.include?('date_of_birth')
365
- values['started'] = started.strftime(Osm::OSM_DATE_FORMAT) if to_update.include?('started')
366
- values['startedsection'] = joined.strftime(Osm::OSM_DATE_FORMAT) if to_update.include?('joined')
367
- values['email1'] = email1 if to_update.include?('email1')
368
- values['email2'] = email2 if to_update.include?('email2')
369
- values['email3'] = email3 if to_update.include?('email3')
370
- values['email4'] = email4 if to_update.include?('email4')
371
- values['phone1'] = phone1 if to_update.include?('phone1')
372
- values['phone2'] = phone2 if to_update.include?('phone2')
373
- values['phone3'] = phone3 if to_update.include?('phone3')
374
- values['phone4'] = phone4 if to_update.include?('phone3')
375
- values['address'] = address if to_update.include?('address')
376
- values['address2'] = address2 if to_update.include?('address2')
377
- values['parents'] = parents if to_update.include?('parents')
378
- values['notes'] = notes if to_update.include?('notes')
379
- values['medical'] = medical if to_update.include?('medical')
380
- values['religion'] = religion if to_update.include?('religion')
381
- values['school'] = school if to_update.include?('school')
382
- values['ethnicity'] = ethnicity if to_update.include?('ethnicity')
383
- values['subs'] = subs if to_update.include?('subs')
384
- values['custom1'] = custom1 if to_update.include?('custom1')
385
- values['custom2'] = custom2 if to_update.include?('custom2')
386
- values['custom3'] = custom3 if to_update.include?('custom3')
387
- values['custom4'] = custom4 if to_update.include?('custom4')
388
- values['custom5'] = custom5 if to_update.include?('custom5')
389
- values['custom6'] = custom6 if to_update.include?('custom6')
390
- values['custom7'] = custom7 if to_update.include?('custom7')
391
- values['custom8'] = custom8 if to_update.include?('custom8')
392
- values['custom9'] = custom9 if to_update.include?('custom9')
393
-
394
- result = true
395
- values.each do |column, value|
396
- data = api.perform_query("users.php?action=updateMember&dateFormat=generic", {
342
+ updated = true
343
+
344
+ # Do core attributes
345
+ attribute_map = [
346
+ ['first_name', 'firstname', first_name],
347
+ ['last_name', 'lastname', last_name],
348
+ ['grouping_id', 'patrolid', grouping_id],
349
+ ['grouping_leader', 'patrolleader', grouping_leader],
350
+ ['date_of_birth', 'dob', date_of_birth.strftime(Osm::OSM_DATE_FORMAT)],
351
+ ['started_section', 'startedsection', started_section.strftime(Osm::OSM_DATE_FORMAT)],
352
+ ['joined_movement', 'started', joined_movement.strftime(Osm::OSM_DATE_FORMAT)],
353
+ ] # our name => OSM name
354
+ attribute_map.select{ |attr,col,val| force || changed_attributes.include?(attr) }.each do |attr,col,val|
355
+ data = api.perform_query("ext/members/contact/?action=update", {
397
356
  'scoutid' => self.id,
398
- 'column' => column,
399
- 'value' => value,
357
+ 'column' => col,
358
+ 'value' => val,
400
359
  'sectionid' => section_id,
401
360
  })
402
- result &= (data[column] == value.to_s)
403
- end
404
-
405
- if to_update.include?('grouping_id') || to_update.include?('grouping_leader')
406
- data = api.perform_query("users.php?action=updateMemberPatrol", {
407
- 'scoutid' => self.id,
408
- 'patrolid' => grouping_id,
409
- 'pl' => grouping_leader,
410
- 'sectionid' => section_id,
361
+ updated = updated && data.is_a?(Hash) && data['ok'].eql?(true)
362
+ end # each attr to update
363
+
364
+ # Do 'floating' attributes
365
+ if force || changed_attributes.include?('gender')
366
+ new_value = {male: 'Male', female: 'Female', other: 'Other'}[gender] || 'Unspecified'
367
+ data = api.perform_query("ext/members/contact/?action=update", {
368
+ 'associated_id' => self.id,
369
+ 'associated_type' => 'member',
370
+ 'value' => new_value,
371
+ 'column_id' => CID_GENDER,
372
+ 'group_id' => GID_FLOATING,
373
+ 'context' => 'members',
411
374
  })
412
- result &= ((data['patrolid'].to_i == grouping_id) && (data['patrolleader'].to_i == grouping_leader))
375
+ updated = updated && data.is_a?(Hash) && data['data'].is_a?(Hash) && data['data']['value'].eql?(new_value)
413
376
  end
414
377
 
415
- if result
378
+ # Do custom attributes
379
+ custom.keys.select{ |a| force || custom.changes.keys.include?(a) }.each do |attr|
380
+ new_value = custom[attr]
381
+ data = api.perform_query("ext/customdata/?action=updateColumn&section_id=#{section_id}", {
382
+ 'associated_id' => self.id,
383
+ 'associated_type' => 'member',
384
+ 'value' => new_value,
385
+ 'column_id' => attr,
386
+ 'group_id' => GID_CUSTOM,
387
+ 'context' => 'members',
388
+ })
389
+ updated = updated && data.is_a?(Hash) && data['data'].is_a?(Hash) && data['data']['value'].eql?(new_value)
390
+ end # each attr to update
391
+
392
+ # Do contacts
393
+ updated = contact.update(api, self, force) && updated
394
+ updated = primary_contact.update(api, self, force) && updated
395
+ updated = secondary_contact.update(api, self, force) && updated
396
+ updated = emergency_contact.update(api, self, force) && updated
397
+ updated = doctor.update(api, self, force) && updated
398
+
399
+ # Finish off
400
+ if updated
416
401
  reset_changed_attributes
417
- # The cached columns for the flexi record will be out of date - remove them
402
+ custom.clean_up!
403
+ # The cached columns for the members will be out of date - remove them
418
404
  Osm::Term.get_for_section(api, section_id).each do |term|
419
405
  Osm::Model.cache_delete(api, ['members', section_id, term.id])
420
406
  end
421
407
  end
422
-
423
- return result
408
+ return updated
424
409
  end
425
410
 
426
411
  # Get the years element of this scout's age
@@ -683,12 +668,74 @@ module Osm
683
668
  def all_phones
684
669
  [phone_1, phone_2].select{ |n| !n.blank? }.map{ |n| n.gsub(/[^\d\+]/, '') }
685
670
  end
671
+
672
+ # Update the contact in OSM
673
+ # @param [Osm::Api] api The api to use to make the request
674
+ # @param [Osm::Member] section The member to update the contact for
675
+ # @param [Boolean] force Whether to force updates (ie tell OSM every attribute changed even if we don't think it did)
676
+ # @return [Boolean] whether the member was successfully updated or not
677
+ # @raise [Osm::ObjectIsInvalid] If the Contact is invalid
678
+ def update(api, member, force=false)
679
+ raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
680
+ require_ability_to(api, :write, :member, member.section_id)
681
+
682
+ attribute_map = {
683
+ 'first_name' => 'data[firstname]',
684
+ 'last_name' => 'data[lastname]',
685
+ 'surgery' => 'data[surgery]',
686
+ 'address_1' => 'data[address1]',
687
+ 'address_2' => 'data[address2]',
688
+ 'address_3' => 'data[address3]',
689
+ 'address_4' => 'data[address4]',
690
+ 'postcode' => 'data[postcode]',
691
+ 'phone_1' => 'data[phone1]',
692
+ 'receive_phone_1' => 'data[phone1_sms]',
693
+ 'phone_2' => 'data[phone2]',
694
+ 'receive_phone_2' => 'data[phone2_sms]',
695
+ 'email_1' => 'data[email1]',
696
+ 'receive_email_1' => 'data[email1_leaders]',
697
+ 'email_2' => 'data[email2]',
698
+ 'receive_email_2' => 'data[email2_leaders]',
699
+ } # our name => OSM name
700
+
701
+ data = {}
702
+ attributes.keys.select{ |a| !['custom', 'custom_labels'].include?(a) }.select{ |a| force || changed_attributes.include?(a) }.each do |attr|
703
+ value = send(attr)
704
+ value = 'yes' if value.eql?(true)
705
+ data[attribute_map[attr]] = value
706
+ end
707
+ custom.keys.select{ |a| force || custom.changes.keys.include?(a) }.each do |attr|
708
+ data["data[#{key}]"] = custom[key]
709
+ end
710
+
711
+ updated = false
712
+ unless data.empty?
713
+ result = api.perform_query("ext/customdata/?action=update&section_id=#{member.section_id}", {
714
+ 'associated_id' => member.id,
715
+ 'associated_type' => 'member',
716
+ 'context' => 'members',
717
+ 'group_id' => self.class::GROUP_ID,
718
+ }.merge(data))
719
+ updated = result.is_a?(Hash) && result['status'].eql?(true)
720
+ end
721
+
722
+ # Finish off
723
+ if updated
724
+ reset_changed_attributes
725
+ custom.clean_up!
726
+ end
727
+ return updated
728
+ end
729
+
686
730
  end
687
731
 
688
732
 
689
733
  class MemberContact < Osm::Member::Contact
690
734
  include EnableableEmailableContact
691
735
  include EnableablePhoneableContact
736
+
737
+ GROUP_ID = Osm::Member::GID_MEMBER_CONTACT
738
+
692
739
  # @!attribute [rw] email_1
693
740
  # @return [String] the primary email address for the member
694
741
  # @!attribute [rw] email_2
@@ -724,6 +771,9 @@ module Osm
724
771
  class PrimaryContact < Osm::Member::Contact
725
772
  include EnableableEmailableContact
726
773
  include EnableablePhoneableContact
774
+
775
+ GROUP_ID = Osm::Member::GID_PRIMARY_CONTACT
776
+
727
777
  # @!attribute [rw] email_1
728
778
  # @return [String] the primary email address for the contact
729
779
  # @!attribute [rw] email_2
@@ -755,9 +805,15 @@ module Osm
755
805
  validates_inclusion_of :receive_phone_2, :in => [true, false]
756
806
  end # class PrimaryContact
757
807
 
808
+ class SecondaryContact < Osm::Member::PrimaryContact
809
+ GROUP_ID = Osm::Member::GID_SECONDARY_CONTACT
810
+ end # class SecondaryContact
758
811
 
759
812
  class EmergencyContact < Osm::Member::Contact
760
813
  include EmailableContact
814
+
815
+ GROUP_ID = Osm::Member::GID_EMERGENCY_CONTACT
816
+
761
817
  # @!attribute [rw] email_1
762
818
  # @return [String] the primary email address for the contact
763
819
  # @!attribute [rw] email_2
@@ -780,6 +836,8 @@ module Osm
780
836
 
781
837
 
782
838
  class DoctorContact < Osm::Member::Contact
839
+ GROUP_ID = Osm::Member::GID_DOCTOR_CONTACT
840
+
783
841
  # @!attribute [rw] first_name
784
842
  # @return [String] the contact's first name
785
843
  # @!attribute [rw] last_name
@@ -277,8 +277,8 @@ describe "Member" do
277
277
  member.joined_movement.should == Date.new(2006, 7, 17)
278
278
  member.started_section.should == Date.new(2008, 7, 12)
279
279
  member.finished_section.should == Date.new(2010, 6, 3)
280
- member.custom.should == {4848 => 'Data for 4848'}
281
- member.custom_labels.should == {4848 => 'Label for 4848'}
280
+ member.custom.should == {"label_for_4848" => "Data for 4848"}
281
+ member.custom_labels.should == {"label_for_4848" => 'Label for 4848'}
282
282
  member.contact.first_name.should == 'John'
283
283
  member.contact.last_name.should == 'Smith'
284
284
  member.contact.address_1.should == 'Address 1'
@@ -294,8 +294,8 @@ describe "Member" do
294
294
  member.contact.receive_email_1.should == true
295
295
  member.contact.email_2.should == ''
296
296
  member.contact.receive_email_2.should == false
297
- member.contact.custom.should == {8446=>"Data for 8446"}
298
- member.contact.custom_labels.should == {8446=>"Label for 8446"}
297
+ member.contact.custom.should == {"label_for_8446"=>"Data for 8446"}
298
+ member.contact.custom_labels.should == {"label_for_8446"=>"Label for 8446"}
299
299
  member.primary_contact.first_name.should == 'Primary'
300
300
  member.primary_contact.last_name.should == 'Contact'
301
301
  member.primary_contact.address_1.should == 'Address 1'
@@ -311,8 +311,8 @@ describe "Member" do
311
311
  member.primary_contact.receive_email_1.should == true
312
312
  member.primary_contact.email_2.should == ''
313
313
  member.primary_contact.receive_email_2.should == false
314
- member.primary_contact.custom.should == {8441=>"Data for 8441"}
315
- member.primary_contact.custom_labels.should == {8441=>"Label for 8441"}
314
+ member.primary_contact.custom.should == {"label_for_8441"=>"Data for 8441"}
315
+ member.primary_contact.custom_labels.should == {"label_for_8441"=>"Label for 8441"}
316
316
  member.secondary_contact.first_name.should == 'Secondary'
317
317
  member.secondary_contact.last_name.should == 'Contact'
318
318
  member.secondary_contact.address_1.should == 'Address 1'
@@ -328,8 +328,8 @@ describe "Member" do
328
328
  member.secondary_contact.receive_email_1.should == true
329
329
  member.secondary_contact.email_2.should == ''
330
330
  member.secondary_contact.receive_email_2.should == false
331
- member.secondary_contact.custom.should == {8442=>"Data for 8442"}
332
- member.secondary_contact.custom_labels.should == {8442=>"Label for 8442"}
331
+ member.secondary_contact.custom.should == {"label_for_8442"=>"Data for 8442"}
332
+ member.secondary_contact.custom_labels.should == {"label_for_8442"=>"Label for 8442"}
333
333
  member.emergency_contact.first_name.should == 'Emergency'
334
334
  member.emergency_contact.last_name.should == 'Contact'
335
335
  member.emergency_contact.address_1.should == 'Address 1'
@@ -341,8 +341,8 @@ describe "Member" do
341
341
  member.emergency_contact.phone_2.should == '0987 654321'
342
342
  member.emergency_contact.email_1.should == 'emergency@example.com'
343
343
  member.emergency_contact.email_2.should == ''
344
- member.emergency_contact.custom.should == {8443=>"Data for 8443"}
345
- member.emergency_contact.custom_labels.should == {8443=>"Label for 8443"}
344
+ member.emergency_contact.custom.should == {"label_for_8443"=>"Data for 8443"}
345
+ member.emergency_contact.custom_labels.should == {"label_for_8443"=>"Label for 8443"}
346
346
  member.doctor.first_name.should == 'Doctor'
347
347
  member.doctor.last_name.should == 'Contact'
348
348
  member.doctor.surgery.should == 'Surgery'
@@ -353,8 +353,8 @@ describe "Member" do
353
353
  member.doctor.postcode.should == 'Postcode'
354
354
  member.doctor.phone_1.should == '01234 567890'
355
355
  member.doctor.phone_2.should == '0987 654321'
356
- member.doctor.custom.should == {8444=>"Data for 8444"}
357
- member.doctor.custom_labels.should == {8444=>"Label for 8444"}
356
+ member.doctor.custom.should == {"label_for_8444"=>"Data for 8444"}
357
+ member.doctor.custom_labels.should == {"label_for_8444"=>"Label for 8444"}
358
358
  member.valid?.should == true
359
359
  end
360
360
 
@@ -412,6 +412,105 @@ describe "Member" do
412
412
  member.valid?.should == true
413
413
  end
414
414
 
415
+ it "Get from OSM (handles no custom data)" do
416
+ body = {
417
+ 'status' => true,
418
+ 'error' => nil,
419
+ 'data' => {
420
+ '123' => {
421
+ 'active' => true,
422
+ 'age' => '12 / 00',
423
+ 'date_of_birth' => '2000-03-08',
424
+ 'end_date' => '2010-06-03',
425
+ 'first_name' => 'John',
426
+ 'joined' => '2008-07-12',
427
+ 'last_name' => 'Smith',
428
+ 'member_id' => 123,
429
+ 'patrol' => 'Leaders',
430
+ 'patrol_id' => -2,
431
+ 'patrol_role_level' => 1,
432
+ 'patrol_role_level_label' => 'Assistant leader',
433
+ 'section_id' => 1,
434
+ 'started' => '2006-07-17',
435
+ 'custom_data' => {
436
+ '7' => {'34' => 'Unspecified'},
437
+ },
438
+ }
439
+ },
440
+ 'meta' => {
441
+ 'leader_count' => 20,
442
+ 'member_count' => 30,
443
+ 'status' => true,
444
+ 'structure' => [
445
+ {'group_id' => 5, 'description' => 'This allows you to add extra information for your members.', 'identifier' => 'customisable_data', 'name' => 'Customisable Data', 'columns' => [
446
+ {'column_id' => 4848, 'group_column_id' => '5_4848', 'label' => 'Label for 4848', 'varname' => 'label_for_4848', 'read_only' => 'no', 'required' => 'no', 'type' => 'text', 'width' => 120},
447
+ ]},
448
+ {'group_id' => 7, 'description' => '', 'identifier' => 'floating', 'name' => 'Floating', 'columns' => [
449
+ {'column_id' => 34, 'group_column_id' => '7_34', 'label' => 'Gender', 'varname' => 'gender', 'read_only' => 'no', 'required' => 'no', 'type' => 'text', 'width' => 120},
450
+ ]},
451
+ ],
452
+ },
453
+ }
454
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/ext/members/contact/grid/?action=getMembers", :body => body.to_json, :content_type => 'application/json')
455
+
456
+ members = Osm::Member.get_for_section(@api, 1, 2)
457
+ members.size.should == 1
458
+ member = members[0]
459
+ member.id.should == 123
460
+ member.custom.should == {}
461
+ member.valid?.should == true
462
+ end
463
+
464
+ it "Get from OSM (handles missing floating data)" do
465
+ body = {
466
+ 'status' => true,
467
+ 'error' => nil,
468
+ 'data' => {
469
+ '123' => {
470
+ 'active' => true,
471
+ 'age' => '12 / 00',
472
+ 'date_of_birth' => '2000-03-08',
473
+ 'end_date' => '2010-06-03',
474
+ 'first_name' => 'John',
475
+ 'joined' => '2008-07-12',
476
+ 'last_name' => 'Smith',
477
+ 'member_id' => 123,
478
+ 'patrol' => 'Leaders',
479
+ 'patrol_id' => -2,
480
+ 'patrol_role_level' => 1,
481
+ 'patrol_role_level_label' => 'Assistant leader',
482
+ 'section_id' => 1,
483
+ 'started' => '2006-07-17',
484
+ 'custom_data' => {
485
+ '5' => {'4848' => 'Data for 4848'},
486
+ },
487
+ }
488
+ },
489
+ 'meta' => {
490
+ 'leader_count' => 20,
491
+ 'member_count' => 30,
492
+ 'status' => true,
493
+ 'structure' => [
494
+ {'group_id' => 5, 'description' => 'This allows you to add extra information for your members.', 'identifier' => 'customisable_data', 'name' => 'Customisable Data', 'columns' => [
495
+ {'column_id' => 4848, 'group_column_id' => '5_4848', 'label' => 'Label for 4848', 'varname' => 'label_for_4848', 'read_only' => 'no', 'required' => 'no', 'type' => 'text', 'width' => 120},
496
+ ]},
497
+ {'group_id' => 7, 'description' => '', 'identifier' => 'floating', 'name' => 'Floating', 'columns' => [
498
+ {'column_id' => 34, 'group_column_id' => '7_34', 'label' => 'Gender', 'varname' => 'gender', 'read_only' => 'no', 'required' => 'no', 'type' => 'text', 'width' => 120},
499
+ ]},
500
+ ],
501
+ },
502
+ }
503
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/ext/members/contact/grid/?action=getMembers", :body => body.to_json, :content_type => 'application/json')
504
+
505
+ members = Osm::Member.get_for_section(@api, 1, 2)
506
+ members.size.should == 1
507
+ member = members[0]
508
+ member.id.should == 123
509
+ member.gender.should == nil
510
+ member.valid?.should == true
511
+ end
512
+
513
+
415
514
  it "Get from OSM (handles an empty data array)" do
416
515
  body = {
417
516
  'status' => true,
@@ -425,280 +524,344 @@ describe "Member" do
425
524
  end
426
525
 
427
526
 
428
- it "Create in OSM (succeded)" do
429
- member = Osm::Member.new(
430
- :section_id => 2,
431
- :first_name => 'First',
432
- :last_name => 'Last',
433
- :email1 => 'email1@example.com',
434
- :email2 => 'email2@example.com',
435
- :email3 => 'email3@example.com',
436
- :email4 => 'email4@example.com',
437
- :phone1 => '11111 111111',
438
- :phone2 => '222222',
439
- :phone3 => '+33 3333 333333',
440
- :phone4 => '4444 444 444',
441
- :address => '1 Some Road',
442
- :address2 => 'Address 2',
443
- :date_of_birth => '2000-01-02',
444
- :started => '2006-01-02',
445
- :joined => '2006-01-03',
446
- :parents => 'John and Jane Doe',
447
- :notes => 'None',
448
- :medical => 'Nothing',
449
- :religion => 'Unknown',
450
- :school => 'Some School',
451
- :ethnicity => 'Yes',
452
- :subs => 'Upto end of 2007',
453
- :custom1 => 'Custom Field 1',
454
- :custom2 => 'Custom Field 2',
455
- :custom3 => 'Custom Field 3',
456
- :custom4 => 'Custom Field 4',
457
- :custom5 => 'Custom Field 5',
458
- :custom6 => 'Custom Field 6',
459
- :custom7 => 'Custom Field 7',
460
- :custom8 => 'Custom Field 8',
461
- :custom9 => 'Custom Field 9',
462
- :grouping_id => '3',
463
- :grouping_leader => 0,
464
- )
527
+ describe "Create in OSM" do
465
528
 
466
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=newMember'
467
- post_data = {
468
- 'apiid' => @CONFIGURATION[:api][:osm][:id],
469
- 'token' => @CONFIGURATION[:api][:osm][:token],
470
- 'userid' => 'user_id',
471
- 'secret' => 'secret',
472
- 'sectionid' => 2,
473
- 'firstname' => 'First',
474
- 'lastname' => 'Last',
475
- 'email1' => 'email1@example.com',
476
- 'email2' => 'email2@example.com',
477
- 'email3' => 'email3@example.com',
478
- 'email4' => 'email4@example.com',
479
- 'phone1' => '11111 111111',
480
- 'phone2' => '222222',
481
- 'phone3' => '+33 3333 333333',
482
- 'phone4' => '4444 444 444',
483
- 'address' => '1 Some Road',
484
- 'address2' => 'Address 2',
485
- 'dob' => '2000-01-02',
486
- 'started' => '2006-01-02',
487
- 'startedsection' => '2006-01-03',
488
- 'parents' => 'John and Jane Doe',
489
- 'notes' => 'None',
490
- 'medical' => 'Nothing',
491
- 'religion' => 'Unknown',
492
- 'school' => 'Some School',
493
- 'ethnicity' => 'Yes',
494
- 'subs' => 'Upto end of 2007',
495
- 'custom1' => 'Custom Field 1',
496
- 'custom2' => 'Custom Field 2',
497
- 'custom3' => 'Custom Field 3',
498
- 'custom4' => 'Custom Field 4',
499
- 'custom5' => 'Custom Field 5',
500
- 'custom6' => 'Custom Field 6',
501
- 'custom7' => 'Custom Field 7',
502
- 'custom8' => 'Custom Field 8',
503
- 'custom9' => 'Custom Field 9',
504
- 'patrolid' => 3,
505
- 'patrolleader' => 0,
506
- }
529
+ before :each do
530
+ attributes = {
531
+ :section_id => 2,
532
+ :first_name => 'First',
533
+ :last_name => 'Last',
534
+ :date_of_birth => '2000-01-02',
535
+ :grouping_id => '3',
536
+ :grouping_leader => 0,
537
+ :grouping_label => 'Grouping',
538
+ :grouping_leader_label => '6er',
539
+ :age => '06 / 07',
540
+ :gender => :other,
541
+ :joined_movement => '2006-01-02',
542
+ :started_section => '2006-01-07',
543
+ :finished_section => '2007-12-31',
544
+ :custom => {'12_3' => '123'},
545
+ :custom_labels => {'12_3' => 'Label for 123'},
546
+ :contact => Osm::Member::MemberContact.new(postcode: 'A'),
547
+ :primary_contact => Osm::Member::PrimaryContact.new(postcode: 'B'),
548
+ :secondary_contact => Osm::Member::PrimaryContact.new(postcode: 'C'),
549
+ :emergency_contact => Osm::Member::EmergencyContact.new(postcode: 'D'),
550
+ :doctor => Osm::Member::DoctorContact.new(postcode: 'E'),
551
+ }
552
+ @member = Osm::Member.new(attributes)
553
+ end
507
554
 
508
- Osm::Term.stub(:get_for_section) { [] }
509
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":"ok","scoutid":1}'}) }
510
- member.create(@api).should == true
511
- member.id.should == 1
512
- end
555
+ it "Success" do
556
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/users.php?action=newMember', {:body => {
557
+ "apiid" => "1",
558
+ "token" => "API TOKEN",
559
+ "userid" => "user_id",
560
+ "secret" => "secret",
561
+ "sectionid" => 2,
562
+ "firstname" => "First",
563
+ "lastname" => "Last",
564
+ "dob" => "2000-01-02",
565
+ "started" => "2006-01-02",
566
+ "startedsection" => "2006-01-07",
567
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":"ok","scoutid":577743}'}) }
568
+
569
+ @member.stub(:update) { true }
570
+ Osm::Term.stub(:get_for_section) { [] }
571
+
572
+ @member.create(@api).should == true
573
+ @member.id.should == 577743
574
+ end
513
575
 
514
- it "Create in OSM (failed)" do
515
- member = Osm::Member.new(
516
- :section_id => 2,
517
- :first_name => 'First',
518
- :last_name => 'Last',
519
- :date_of_birth => '2000-01-02',
520
- :started => '2006-01-02',
521
- :joined => '2006-01-03',
522
- :grouping_id => '3',
523
- :grouping_leader => 0,
524
- )
576
+ it "Failed the create stage in OSM" do
577
+ HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
578
+ @member.create(@api).should == false
579
+ end
525
580
 
526
- HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":"ok","scoutid":-1}'}) }
527
- member.create(@api).should == false
528
- end
581
+ it "Failed the update stage in OSM" do
582
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/users.php?action=newMember', {:body => {
583
+ "apiid" => "1",
584
+ "token" => "API TOKEN",
585
+ "userid" => "user_id",
586
+ "secret" => "secret",
587
+ "sectionid" => 2,
588
+ "firstname" => "First",
589
+ "lastname" => "Last",
590
+ "dob" => "2000-01-02",
591
+ "started" => "2006-01-02",
592
+ "startedsection" => "2006-01-07",
593
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":"ok","scoutid":577743}'}) }
594
+
595
+ @member.stub(:update) { false }
596
+ Osm::Term.stub(:get_for_section) { [] }
597
+
598
+ @member.create(@api).should == nil
599
+ @member.id.should == 577743
600
+ end
529
601
 
602
+ it "Raises error if member is invalid" do
603
+ expect{ Osm::Member.new.create(@api) }.to raise_error(Osm::ObjectIsInvalid, 'member is invalid')
604
+ end
530
605
 
531
- it "Update in OSM (succeded)" do
532
- member = Osm::Member.new()
533
- member.id = 1
534
- member.section_id = 2
535
- member.first_name = 'First'
536
- member.last_name = 'Last'
537
- member.email1 = 'email1@example.com'
538
- member.email2 = 'email2@example.com'
539
- member.email3 = 'email3@example.com'
540
- member.email4 = 'email4@example.com'
541
- member.phone1 = '11111 111111'
542
- member.phone2 = '222222'
543
- member.phone3 = '+33 3333 333333'
544
- member.phone4 = '4444 444 444'
545
- member.address = '1 Some Road'
546
- member.address2 = 'Address 2'
547
- member.date_of_birth = '2000-01-02'
548
- member.started = '2006-01-02'
549
- member.joined = '2006-01-03'
550
- member.parents = 'John and Jane Doe'
551
- member.notes = 'None'
552
- member.medical = 'Nothing'
553
- member.religion = 'Unknown'
554
- member.school = 'Some School'
555
- member.ethnicity = 'Yes'
556
- member.subs = 'Upto end of 2007'
557
- member.custom1 = 'Custom Field 1'
558
- member.custom2 = 'Custom Field 2'
559
- member.custom3 = 'Custom Field 3'
560
- member.custom4 = 'Custom Field 4'
561
- member.custom5 = 'Custom Field 5'
562
- member.custom6 = 'Custom Field 6'
563
- member.custom7 = 'Custom Field 7'
564
- member.custom8 = 'Custom Field 8'
565
- member.custom9 = 'Custom Field 9'
566
- member.grouping_id = 3
567
- member.grouping_leader = 0
568
-
569
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=updateMember&dateFormat=generic'
570
- body_data = {
571
- 'firstname' => 'First',
572
- 'lastname' => 'Last',
573
- 'email1' => 'email1@example.com',
574
- 'email2' => 'email2@example.com',
575
- 'email3' => 'email3@example.com',
576
- 'email4' => 'email4@example.com',
577
- 'phone1' => '11111 111111',
578
- 'phone2' => '222222',
579
- 'phone3' => '+33 3333 333333',
580
- 'phone4' => '4444 444 444',
581
- 'address' => '1 Some Road',
582
- 'address2' => 'Address 2',
583
- 'dob' => '2000-01-02',
584
- 'started' => '2006-01-02',
585
- 'startedsection' => '2006-01-03',
586
- 'parents' => 'John and Jane Doe',
587
- 'notes' => 'None',
588
- 'medical' => 'Nothing',
589
- 'religion' => 'Unknown',
590
- 'school' => 'Some School',
591
- 'ethnicity' => 'Yes',
592
- 'subs' => 'Upto end of 2007',
593
- 'custom1' => 'Custom Field 1',
594
- 'custom2' => 'Custom Field 2',
595
- 'custom3' => 'Custom Field 3',
596
- 'custom4' => 'Custom Field 4',
597
- 'custom5' => 'Custom Field 5',
598
- 'custom6' => 'Custom Field 6',
599
- 'custom7' => 'Custom Field 7',
600
- 'custom8' => 'Custom Field 8',
601
- 'custom9' => 'Custom Field 9',
602
- 'patrolid' => 3,
603
- 'patrolleader' => 0,
604
- }
605
- body = (body_data.inject({}) {|h,(k,v)| h[k]=v.to_s; h}).to_json
606
-
607
- body_data.each do |column, value|
608
- unless ['patrolid', 'patrolleader'].include?(column)
609
- HTTParty.should_receive(:post).with(url, {:body => {
610
- 'apiid' => @CONFIGURATION[:api][:osm][:id],
611
- 'token' => @CONFIGURATION[:api][:osm][:token],
612
- 'userid' => 'user_id',
613
- 'secret' => 'secret',
614
- 'scoutid' => member.id,
615
- 'column' => column,
616
- 'value' => value,
617
- 'sectionid' => member.section_id,
618
- }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>body}) }
619
- end
606
+ it "Raises error if member exists in OSM (has an ID)" do
607
+ expect{ Osm::Member.new(id: 12345).create(@api) }.to raise_error(Osm::Error, 'the member already exists in OSM')
620
608
  end
621
- HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/users.php?action=updateMemberPatrol', {:body => {
622
- 'apiid' => @CONFIGURATION[:api][:osm][:id],
623
- 'token' => @CONFIGURATION[:api][:osm][:token],
624
- 'userid' => 'user_id',
625
- 'secret' => 'secret',
626
- 'scoutid' => member.id,
627
- 'patrolid' => member.grouping_id,
628
- 'pl' => member.grouping_leader,
629
- 'sectionid' => member.section_id,
630
- }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>body}) }
631
- Osm::Term.stub(:get_for_section) { [] }
632
-
633
- member.update(@api).should == true
609
+
634
610
  end
635
611
 
636
- it "Update in OSM (only updated fields)" do
637
- member = Osm::Member.new(
638
- :id => 1,
639
- :section_id => 2,
640
- :date_of_birth => '2000-01-02',
641
- :started => '2006-01-02',
642
- :joined => '2006-01-03',
643
- :grouping_leader => 0,
644
- )
645
- member.first_name = 'First'
646
- member.last_name = 'Last'
647
- member.grouping_id = 3
648
-
649
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=updateMember&dateFormat=generic'
650
- body_data = {
651
- 'firstname' => 'First',
652
- 'lastname' => 'Last',
653
- 'patrolid' => 3,
654
- 'patrolleader' => 0,
655
- }
656
- body = (body_data.inject({}) {|h,(k,v)| h[k]=v.to_s; h}).to_json
657
-
658
- body_data.each do |column, value|
659
- unless ['patrolid', 'patrolleader'].include?(column)
660
- HTTParty.should_receive(:post).with(url, {:body => {
661
- 'apiid' => @CONFIGURATION[:api][:osm][:id],
662
- 'token' => @CONFIGURATION[:api][:osm][:token],
663
- 'userid' => 'user_id',
664
- 'secret' => 'secret',
665
- 'scoutid' => member.id,
666
- 'column' => column,
667
- 'value' => value,
668
- 'sectionid' => member.section_id,
669
- }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>body}) }
612
+
613
+ describe "Update in OSM" do
614
+
615
+ before :each do
616
+ attributes = {
617
+ :id => 1,
618
+ :section_id => 2,
619
+ :first_name => 'First',
620
+ :last_name => 'Last',
621
+ :date_of_birth => '2000-01-02',
622
+ :grouping_id => '3',
623
+ :grouping_leader => 0,
624
+ :grouping_label => 'Grouping',
625
+ :grouping_leader_label => '6er',
626
+ :age => '06 / 07',
627
+ :gender => :other,
628
+ :joined_movement => '2006-01-02',
629
+ :started_section => '2006-01-07',
630
+ :finished_section => '2007-12-31',
631
+ :custom => DirtyHashy[ '12_3' => '123' ],
632
+ :custom_labels => {'12_3' => 'Label for 123'},
633
+ :contact => Osm::Member::MemberContact.new(postcode: 'A'),
634
+ :primary_contact => Osm::Member::PrimaryContact.new(postcode: 'B'),
635
+ :secondary_contact => Osm::Member::SecondaryContact.new(postcode: 'C'),
636
+ :emergency_contact => Osm::Member::EmergencyContact.new(postcode: 'D'),
637
+ :doctor => Osm::Member::DoctorContact.new(postcode: 'E'),
638
+ }
639
+ @member = Osm::Member.new(attributes)
640
+ end
641
+
642
+ it "Only updated fields" do
643
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/members/contact/?action=update', {:body => {
644
+ "apiid" => "1",
645
+ "token" => "API TOKEN",
646
+ "userid" => "user_id",
647
+ "secret" => "secret",
648
+ "sectionid" => 2,
649
+ "scoutid" => 1,
650
+ "column" => "firstname",
651
+ "value" => "John",
652
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
653
+
654
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/members/contact/?action=update', {:body => {
655
+ "apiid" => "1",
656
+ "token" => "API TOKEN",
657
+ "userid" => "user_id",
658
+ "secret" => "secret",
659
+ "context" => "members",
660
+ "associated_type" => "member",
661
+ "associated_id" => 1,
662
+ "group_id" => 7,
663
+ "column_id" => 34,
664
+ "value" => "Unspecified",
665
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"data":{"value":"Unspecified"}}'}) }
666
+
667
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
668
+ "apiid" => "1",
669
+ "token" => "API TOKEN",
670
+ "userid" => "user_id",
671
+ "secret" => "secret",
672
+ "context" => "members",
673
+ "associated_type" => "member",
674
+ "associated_id" => 1,
675
+ "group_id" => 6,
676
+ "data[address1]" => "Address 1",
677
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
678
+
679
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
680
+ "apiid" => "1",
681
+ "token" => "API TOKEN",
682
+ "userid" => "user_id",
683
+ "secret" => "secret",
684
+ "context" => "members",
685
+ "associated_type" => "member",
686
+ "associated_id" => 1,
687
+ "group_id" => 1,
688
+ "data[address2]" => "Address 2",
689
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
690
+
691
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
692
+ "apiid" => "1",
693
+ "token" => "API TOKEN",
694
+ "userid" => "user_id",
695
+ "secret" => "secret",
696
+ "context" => "members",
697
+ "associated_type" => "member",
698
+ "associated_id" => 1,
699
+ "group_id" => 2,
700
+ "data[address3]" => "Address 3",
701
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
702
+
703
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
704
+ "apiid" => "1",
705
+ "token" => "API TOKEN",
706
+ "userid" => "user_id",
707
+ "secret" => "secret",
708
+ "context" => "members",
709
+ "associated_type" => "member",
710
+ "associated_id" => 1,
711
+ "group_id" => 3,
712
+ "data[address4]" => "Address 4",
713
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
714
+
715
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
716
+ "apiid" => "1",
717
+ "token" => "API TOKEN",
718
+ "userid" => "user_id",
719
+ "secret" => "secret",
720
+ "context" => "members",
721
+ "associated_type" => "member",
722
+ "associated_id" => 1,
723
+ "group_id" => 4,
724
+ "data[surgery]" => "Surgery",
725
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
726
+
727
+ Osm::Term.stub(:get_for_section) { [] }
728
+
729
+ @member.first_name = 'John'
730
+ @member.gender = :unspecified
731
+ @member.contact.address_1 = 'Address 1'
732
+ @member.primary_contact.address_2 = 'Address 2'
733
+ @member.secondary_contact.address_3 = 'Address 3'
734
+ @member.emergency_contact.address_4 = 'Address 4'
735
+ @member.doctor.surgery = 'Surgery'
736
+ @member.update(@api).should == true
737
+ end
738
+
739
+ it "All fields" do
740
+ {'firstname'=>'First', 'lastname'=>'Last', 'patrolid'=>3, 'patrolleader'=>0, 'dob'=>'2000-01-02', 'startedsection'=>'2006-01-07', 'started'=>'2006-01-02'}.each do |key, value|
741
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/members/contact/?action=update', {:body => {
742
+ "apiid" => "1",
743
+ "token" => "API TOKEN",
744
+ "userid" => "user_id",
745
+ "secret" => "secret",
746
+ "sectionid" => 2,
747
+ "scoutid" => 1,
748
+ "column" => key,
749
+ "value" => value,
750
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
670
751
  end
752
+
753
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/members/contact/?action=update', {:body => {
754
+ "apiid" => "1",
755
+ "token" => "API TOKEN",
756
+ "userid" => "user_id",
757
+ "secret" => "secret",
758
+ "context" => "members",
759
+ "associated_type" => "member",
760
+ "associated_id" => 1,
761
+ "group_id" => 7,
762
+ "column_id" => 34,
763
+ "value" => "Other",
764
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"data":{"value":"Other"}}'}) }
765
+
766
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=updateColumn&section_id=2', {:body => {
767
+ "apiid" => "1",
768
+ "token" => "API TOKEN",
769
+ "userid" => "user_id",
770
+ "secret" => "secret",
771
+ "context" => "members",
772
+ "associated_type" => "member",
773
+ "associated_id" => 1,
774
+ "group_id" => 5,
775
+ "column_id" => "12_3",
776
+ "value" => "123",
777
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"data":{"value":"123"}}'}) }
778
+
779
+ {6=>'A', 1=>'B', 2=>'C'}.each do |group_id, postcode|
780
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
781
+ "apiid" => "1",
782
+ "token" => "API TOKEN",
783
+ "userid" => "user_id",
784
+ "secret" => "secret",
785
+ "context" => "members",
786
+ "associated_type" => "member",
787
+ "associated_id" => 1,
788
+ "group_id" => group_id,
789
+ "data[firstname]" => nil,
790
+ "data[lastname]" => nil,
791
+ "data[address1]" => nil,
792
+ "data[address2]" => nil,
793
+ "data[address3]" => nil,
794
+ "data[address4]" => nil,
795
+ "data[postcode]" => postcode,
796
+ "data[phone1]" => nil,
797
+ "data[phone2]" => nil,
798
+ "data[email1]" => nil,
799
+ "data[email1_leaders]" => false,
800
+ "data[email2]" => nil,
801
+ "data[email2_leaders]" => false,
802
+ "data[phone1_sms]" => false,
803
+ "data[phone2_sms]" => false,
804
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
805
+ end
806
+
807
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
808
+ "apiid" => "1",
809
+ "token" => "API TOKEN",
810
+ "userid" => "user_id",
811
+ "secret" => "secret",
812
+ "context" => "members",
813
+ "associated_type" => "member",
814
+ "associated_id" => 1,
815
+ "group_id" => 3,
816
+ "data[firstname]" => nil,
817
+ "data[lastname]" => nil,
818
+ "data[address1]" => nil,
819
+ "data[address2]" => nil,
820
+ "data[address3]" => nil,
821
+ "data[address4]" => nil,
822
+ "data[postcode]" => "D",
823
+ "data[phone1]" => nil,
824
+ "data[phone2]" => nil,
825
+ "data[email1]" => nil,
826
+ "data[email2]" => nil,
827
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
828
+
829
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/ext/customdata/?action=update&section_id=2', {:body => {
830
+ "apiid" => "1",
831
+ "token" => "API TOKEN",
832
+ "userid" => "user_id",
833
+ "secret" => "secret",
834
+ "context" => "members",
835
+ "associated_type" => "member",
836
+ "associated_id" => 1,
837
+ "group_id" => 4,
838
+ "data[firstname]" => nil,
839
+ "data[lastname]" => nil,
840
+ "data[surgery]" => nil,
841
+ "data[address1]" => nil,
842
+ "data[address2]" => nil,
843
+ "data[address3]" => nil,
844
+ "data[address4]" => nil,
845
+ "data[postcode]" => "E",
846
+ "data[phone1]" => nil,
847
+ "data[phone2]" => nil,
848
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"status":true}'}) }
849
+
850
+ Osm::Term.stub(:get_for_section) { [] }
851
+
852
+ @member.update(@api, true).should == true
671
853
  end
672
- HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/users.php?action=updateMemberPatrol', {:body => {
673
- 'apiid' => @CONFIGURATION[:api][:osm][:id],
674
- 'token' => @CONFIGURATION[:api][:osm][:token],
675
- 'userid' => 'user_id',
676
- 'secret' => 'secret',
677
- 'scoutid' => member.id,
678
- 'patrolid' => member.grouping_id,
679
- 'pl' => member.grouping_leader,
680
- 'sectionid' => member.section_id,
681
- }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>body}) }
682
- Osm::Term.stub(:get_for_section) { [] }
683
-
684
- member.update(@api).should == true
685
- end
686
854
 
687
- it "Update in OSM (failed)" do
688
- member = Osm::Member.new(
689
- :id => 1,
690
- :section_id => 2,
691
- :last_name => 'Last',
692
- :date_of_birth => '2000-01-02',
693
- :started => '2006-01-02',
694
- :joined => '2006-01-03',
695
- :grouping_id => '3',
696
- :grouping_leader => 0,
697
- )
698
- member.first_name = 'First'
855
+ it "Failed to update in OSM" do
856
+ @member.first_name = 'John'
857
+ HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
858
+ @member.update(@api).should == false
859
+ end
860
+
861
+ it "Raises error if member is invalid" do
862
+ expect{ Osm::Member.new.create(@api) }.to raise_error(Osm::ObjectIsInvalid, 'member is invalid')
863
+ end
699
864
 
700
- HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
701
- member.update(@api).should == false
702
865
  end
703
866
 
704
867
  it "Get Photo link" do
data/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Osm
2
- VERSION = "1.2.18.dev.5"
2
+ VERSION = "1.2.18.dev.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.18.dev.5
4
+ version: 1.2.18.dev.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gauld
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-26 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport