zuora_connect 2.0.60p → 2.0.60u

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49954ce1a10057c773fa3dbc859cccd19e756297192ee85d276cada97c085446
4
- data.tar.gz: bd0ce83a9631479cb2402383c5a53e104742d19fb83b951d24cdf5fe66b48bfb
3
+ metadata.gz: 92a983cff8e7ff831929da9496cbb34e728f2033e79135c8bb76fa604bd33e71
4
+ data.tar.gz: 67170a20afb4fee284cc402484e5770b47422ebbeb7652dca39ceab862ca4d20
5
5
  SHA512:
6
- metadata.gz: d1e341cea3b729c967af29ecfaa9bd143ef656a415487654ae59870ef75e3b719d4873975aa12fe6b845b3916d8ef1de2f63faca22c42c89280058d404c8d3b7
7
- data.tar.gz: 22804e4a0db2285d91ef7c1da8fab9d67261ff13302b48db31f872c820d3b037425ece070a41aa3a119c1bfc7be039a78a6298ff99066b0a2fb3bbfeb6559a40
6
+ metadata.gz: cb814342311b45d90903a72fff8d7bb9d313b91fb89346e99039276af4aae03ed538841215c9737ebc237d80a3584f3955c6e30e48a8e0c322ba7c7fd145ad78
7
+ data.tar.gz: 8f3d002f881cc6403baaae4f00bb88c8fff0f063c4a6290f4ad2c02c49361afe037717c6a55823a1834a9b29e3f16f11b2339211b992f83c42042133ebe33c55
@@ -22,6 +22,11 @@ module ZuoraConnect
22
22
  CONNECT_APPLICATION_ID = 0
23
23
  CONNECT_COMMUNICATION_SLEEP = Rails.env.test? ? 0.seconds : 5.seconds
24
24
  CATALOG_LOOKUP_PAGE_SIZE = 10_000
25
+ CATALOG_LOOKUP_CACHE_TIME_KEY = 'CatalogCachedAt'
26
+ CATALOG_LOOKUP_TTL = 60.seconds
27
+ CATALOG_LOOKUP_CACHE_RESULT_KEY = 'CatalogCache'
28
+ TIMEZONE_LOG_RATE_LIMIT_KEY = 'TimezoneLoggedAt'
29
+ TIMEZONE_LOG_PERIOD = 4.hours
25
30
  IGNORED_LOCALS = ['fr', 'ja', 'es', 'zh', 'de']
26
31
  INTERNAL_HOSTS = []
27
32
  LOGIN_TENANT_DESTINATION = 'target_login'
@@ -242,48 +247,7 @@ module ZuoraConnect
242
247
  ZuoraConnect.logger.error(ex) if !IGNORED_LOCALS.include?(ex.locale.to_s.downcase)
243
248
  end
244
249
 
245
- begin
246
- sql = <<-eos
247
- SELECT zuora_users.zuora_identity_response
248
- FROM "#{self.id}".zuora_users
249
- ORDER BY zuora_users.updated_at DESC
250
- LIMIT 1;
251
- eos
252
- user = ActiveRecord::Base.connection.execute(sql).to_a.first
253
-
254
- if user.present?
255
- zuora_identity_response = JSON.parse(user.fetch('zuora_identity_response', '{}'))
256
- self.user_timezone = zuora_identity_response.values.first&.dig('timeZone')
257
- end
258
- rescue => ex
259
- Rails.logger.error('Failed to get users while setting app instance timezone', ex)
260
- end
261
-
262
- if self.user_timezone.present?
263
- # connect instance which has a custom timezone
264
- if !self.auto_deployed? && (
265
- ActiveSupport::TimeZone[self.task_data.dig('user_settings', 'timezone') || '']&.utc_offset !=
266
- ActiveSupport::TimeZone[self.user_timezone]&.utc_offset
267
- )
268
- if self.environment == 'Production'
269
- ZuoraConnect.logger.error(
270
- "Instance and user timezones are different. User has '#{self.user_timezone}' and " \
271
- "instance has '#{self.task_data.dig('user_settings', 'timezone')}'",
272
- app_instance_id: self.id
273
- )
274
- end
275
- self.user_timezone = nil
276
- Time.zone = self.timezone
277
- else
278
- begin
279
- Time.zone = self.user_timezone
280
- rescue ArgumentError
281
- Time.zone = self.timezone
282
- end
283
- end
284
- else
285
- Time.zone = self.timezone
286
- end
250
+ self.set_timezone
287
251
 
288
252
  if self.task_data.present?
289
253
  tenants = self.task_data.fetch('tenant_ids', [])
@@ -316,6 +280,67 @@ module ZuoraConnect
316
280
  end
317
281
  end
318
282
 
283
+ def set_timezone(timezone: self.timezone, type: :default)
284
+ if timezone.blank?
285
+ timezone = self.timezone
286
+ end
287
+
288
+ if type == :default
289
+ Time.zone = timezone
290
+ elsif type == :user
291
+ begin
292
+ sql = <<-eos
293
+ SELECT zuora_users.zuora_identity_response
294
+ FROM "#{self.id}".zuora_users
295
+ ORDER BY zuora_users.updated_at DESC
296
+ LIMIT 1;
297
+ eos
298
+ user = ActiveRecord::Base.connection.execute(sql).to_a.first
299
+
300
+ if user.present?
301
+ zuora_identity_response = JSON.parse(user.fetch('zuora_identity_response', '{}'))
302
+ self.user_timezone = zuora_identity_response.values.first&.dig('timeZone')
303
+ else
304
+ Rails.logger.error('Cannot find any user to set the timezone', app_instance_id: self.id)
305
+ end
306
+ rescue => ex
307
+ Rails.logger.error('There is an error while getting timezone users', ex)
308
+ end
309
+
310
+ if self.user_timezone.present?
311
+ # connect instance which has a custom timezone
312
+ if !self.auto_deployed? && (
313
+ ActiveSupport::TimeZone[self.task_data.dig('user_settings', 'timezone') || '']&.utc_offset !=
314
+ ActiveSupport::TimeZone[self.user_timezone]&.utc_offset
315
+ )
316
+ if self.environment == 'Production' &&
317
+ (Redis.current.hget(TIMEZONE_LOG_RATE_LIMIT_KEY, self.id).to_i + TIMEZONE_LOG_PERIOD.to_i) <= Time.now.to_i
318
+ ZuoraConnect.logger.error(
319
+ "Instance and user timezones are different. User has '#{self.user_timezone}' and " \
320
+ "instance has '#{self.task_data.dig('user_settings', 'timezone')}'",
321
+ app_instance_id: self.id
322
+ )
323
+ Redis.current.hset(TIMEZONE_LOG_RATE_LIMIT_KEY, self.id, Time.now.to_i)
324
+ end
325
+ self.user_timezone = nil
326
+ Time.zone = timezone
327
+ else
328
+ begin
329
+ Time.zone = self.user_timezone
330
+ rescue ArgumentError
331
+ Rails.logger.error('Malformed user timezone', app_instance_id: self.id)
332
+ Time.zone = timezone
333
+ end
334
+ end
335
+ else
336
+ Time.zone = timezone
337
+ end
338
+ end
339
+ rescue => e
340
+ Rails.logger.error('Malformed timezone used', e, app_instance_id: self.id)
341
+ Time.zone = self.timezone
342
+ end
343
+
319
344
  def auto_deployed?
320
345
  self.id >= 25000000
321
346
  end
@@ -1015,25 +1040,29 @@ module ZuoraConnect
1015
1040
  end
1016
1041
 
1017
1042
  if source == 'API'
1018
- zuora_login = self.login_lookup(type: 'Zuora').first
1019
- login = zuora_login.client(entity_reference)
1020
-
1021
1043
  catalog_container = {}
1022
- response = {
1023
- 'nextPage' => login.rest_endpoint("catalog/products?pageSize=#{CATALOG_LOOKUP_PAGE_SIZE}")
1024
- }
1025
- while response['nextPage'].present?
1026
- url = login.rest_endpoint(response['nextPage'].split('/v1/').last)
1027
- output_json, response = login.rest_call(debug: false, url: url, timeout_retry: true)
1028
-
1029
- case object
1030
- when :product
1031
- output_json.fetch('products', []).each do |product|
1032
- if object_id.nil?
1033
- catalog_container[product['id']] = product.except('productRatePlans')
1034
- elsif object_id.is_a?(String)
1035
- next unless product['id'] == object_id
1036
1044
 
1045
+ if (Redis.current.hget(CATALOG_LOOKUP_CACHE_TIME_KEY, self.id).to_i + CATALOG_LOOKUP_TTL.to_i) > Time.now.to_i
1046
+ begin
1047
+ catalog_container = JSON.parse(Redis.current.hget(CATALOG_LOOKUP_CACHE_RESULT_KEY, self.id))
1048
+ rescue JSON::ParserError => ex
1049
+ Rails.logger.warn('Failed to parse catalog cache', ex)
1050
+ end
1051
+ else
1052
+ zuora_login = self.login_lookup(type: 'Zuora').first
1053
+ login = zuora_login.client(entity_reference)
1054
+
1055
+ response = {
1056
+ 'nextPage' => login.rest_endpoint("catalog/products?pageSize=#{CATALOG_LOOKUP_PAGE_SIZE}")
1057
+ }
1058
+
1059
+ while response['nextPage'].present?
1060
+ url = login.rest_endpoint(response['nextPage'].split('/v1/').last)
1061
+ output_json, response = login.rest_call(debug: false, url: url, timeout_retry: true)
1062
+
1063
+ case object
1064
+ when :product
1065
+ output_json.fetch('products', []).each do |product|
1037
1066
  rate_plans = {}
1038
1067
  product['productRatePlans'].each do |rate_plan|
1039
1068
  charges = {}
@@ -1058,13 +1087,23 @@ module ZuoraConnect
1058
1087
  end
1059
1088
 
1060
1089
  product['productRatePlans'] = rate_plans
1061
- catalog_container = product
1090
+ catalog_container[product['id']] = product
1062
1091
  end
1092
+ else
1093
+ raise "Available objects include [:product]"
1063
1094
  end
1064
- else
1065
- raise "Available objects include [:product]"
1066
1095
  end
1096
+
1097
+ Redis.current.hset(CATALOG_LOOKUP_CACHE_RESULT_KEY, self.id, catalog_container.to_json)
1098
+ Redis.current.hset(CATALOG_LOOKUP_CACHE_TIME_KEY, self.id, Time.now.to_i)
1067
1099
  end
1100
+
1101
+ if object_id.nil?
1102
+ catalog_container.transform_values! { |v| v.except('productRatePlans') }
1103
+ elsif object_id.is_a?(String)
1104
+ catalog_container = catalog_container[object_id]
1105
+ end
1106
+
1068
1107
  return catalog_container
1069
1108
  end
1070
1109
 
@@ -33,6 +33,8 @@ if defined? Prometheus
33
33
  file.write(Prometheus::Client::Formats::Text.marshal(@registry))
34
34
  end
35
35
  end
36
+ rescue
37
+ # Ignored
36
38
  end
37
39
  end
38
40
 
@@ -138,12 +138,8 @@ module ZuoraConnect
138
138
  end
139
139
  ZuoraConnect.logger.error(ex) if !ZuoraConnect::AppInstance::IGNORED_LOCALS.include?(ex.locale.to_s.downcase)
140
140
  end
141
- begin
142
- if @appinstance.user_timezone.blank?
143
- Time.zone = session["#{@appinstance.id}::user::timezone"] ? session["#{@appinstance.id}::user::timezone"] : @appinstance.timezone
144
- end
145
- rescue
146
- ZuoraConnect.logger.error(ex)
141
+ if @appinstance.user_timezone.blank?
142
+ @appinstance.set_timezone(timezone: session["#{@appinstance.id}::user::timezone"], type: :default)
147
143
  end
148
144
  end
149
145
  rescue ZuoraConnect::Exceptions::InvalidCredentialSet => ex
@@ -1,3 +1,3 @@
1
1
  module ZuoraConnect
2
- VERSION = "2.0.60p"
2
+ VERSION = "2.0.60u"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zuora_connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.60p
4
+ version: 2.0.60u
5
5
  platform: ruby
6
6
  authors:
7
7
  - Connect Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-12 00:00:00.000000000 Z
11
+ date: 2020-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: apartment