zuora_connect 1.4.8 → 1.4.9

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
  SHA1:
3
- metadata.gz: 6dd442f2f79a1b540e36d2247d1e47358d5c7a26
4
- data.tar.gz: 65df716cc903105153b77d6fb86b8a4dc6b79bef
3
+ metadata.gz: d7678bfcc2f33c91cfbab0a0fc7d5166556951b6
4
+ data.tar.gz: 274570d12af9e5060ecd113f7c3653ffb986f0b7
5
5
  SHA512:
6
- metadata.gz: 90d95e90b63b79a04dec028ca016bc6408d4bd51788cf280955344e083f82720d1e9bc49f71f38705244dfe740942f91b564cbf05bba61e0530a32a584239b4f
7
- data.tar.gz: 96eccbdc969fcf86ebadeefe2ead81877a2d6ea477d11ddff4845777c91ddbdac625cd4f316d0fae055aa368d78a6f0f3c31853476e4a422e44371f922bf8a1d
6
+ metadata.gz: 0b1240b7bf2f30b3160de9323a7c19e7d0a7dff6bfc207d6338ad52ea965783140135cc7e61743be7606fa8d01268da3901c4030d366cded48c652306f09feb1
7
+ data.tar.gz: 6892a573c4e855c29871d2113e911d6529d153ca9fe11055279126a32bcbca5222cf607c25d9201d4929a2efdf63e733d52f4b20476711db11e0964fb4f48c23
@@ -1,5 +1,6 @@
1
1
  module ZuoraConnect
2
2
  class AppInstance < ZuoraConnect::AppInstanceBase
3
+ default_scope {select(ZuoraConnect::AppInstance.column_names.delete_if {|x| ["catalog_products", "catalog_rateplans", "catalog_charges", "catalog_mapping", "catalog"].include?(x) }) }
3
4
 
4
5
  end
5
6
  end
@@ -1,6 +1,6 @@
1
1
  module ZuoraConnect
2
2
  class AppInstanceBase < ActiveRecord::Base
3
- default_scope {select(ZuoraConnect::AppInstance.column_names.delete_if {|x| ["catalog_mapping", "catalog"].include?(x) }) }
3
+ default_scope {select(ZuoraConnect::AppInstance.column_names.delete_if {|x| ["catalog_products", "catalog_rateplans", "catalog_charges", "catalog_mapping", "catalog"].include?(x) }) }
4
4
  after_initialize :init
5
5
  self.table_name = "zuora_connect_app_instances"
6
6
  attr_accessor :options, :mode, :logins, :valid, :task_data, :last_refresh, :username, :password, :s3_client, :api_version
@@ -44,20 +44,20 @@ module ZuoraConnect
44
44
  return ActiveSupport::MessageEncryptor.new(secret, sign_secret)
45
45
  end
46
46
 
47
- def catalog_outdated?
48
- return self.catalog_updated_at.blank? || (self.catalog_updated_at < Time.now - 12.hours)
47
+ def catalog_outdated?(time: Time.now - 12.hours)
48
+ return self.catalog_updated_at.blank? || (self.catalog_updated_at < time)
49
49
  end
50
50
 
51
51
  def catalog_lookup(entity_id: nil, object: :product, object_id: nil, map_lower_objects: true)
52
52
  entity_reference = entity_id.blank? ? 'Default' : entity_id
53
- object_hierarchy = ZuoraConnect::AppInstance.unscoped.select("catalog_mapping -> '#{entity_reference}' as item").where(id: self.id).first.item[object_id]
53
+ object_hierarchy = (JSON.parse(ActiveRecord::Base.connection.execute('SELECT catalog_mapping #> \'{%s}\' AS item FROM "public"."zuora_connect_app_instances" WHERE "id" = %s LIMIT 1' % [entity_reference, self.id]).first["item"] || "{}") [object_id] || {"productId" => "SAFTEY", "productRatePlanId" => "SAFTEY", "productRatePlanChargeId" => "SAFTEY"})
54
54
 
55
55
  case object
56
56
  when :product
57
57
  if object_id.blank?
58
- stub_catalog = (ZuoraConnect::AppInstance.unscoped.select("catalog -> '#{entity_reference}' as item").where(id: self.id).first.item || {})
58
+ stub_catalog = JSON.parse(ActiveRecord::Base.connection.execute('SELECT catalog #> \'{%s}\' AS item FROM "public"."zuora_connect_app_instances" WHERE "id" = %s' % [entity_reference, self.id]).first["item"] || "{}")
59
59
  else
60
- stub_catalog = (ZuoraConnect::AppInstance.unscoped.select("catalog #> '{#{entity_reference}, #{object_id}}' as item").where(id: self.id).first.item || {})
60
+ stub_catalog = JSON.parse(ActiveRecord::Base.connection.execute('SELECT catalog #> \'{%s, %s}\' AS item FROM "public"."zuora_connect_app_instances" WHERE "id" = %s' % [entity_reference, object_id, self.id]).first["item"] || "{}")
61
61
  end
62
62
 
63
63
  if !map_lower_objects
@@ -65,9 +65,9 @@ module ZuoraConnect
65
65
  end
66
66
  when :rateplan
67
67
  if object_id.blank?
68
- stub_catalog = (ActiveRecord::Base.connection.execute("SELECT \"value\" #>'{productRatePlans}' as \"rateplan\" from \"public\".\"zuora_connect_app_instances\", json_each((\"public\".\"zuora_connect_app_instances\".\"catalog\"::json -> '#{entity_reference}')) AS e(key, value) WHERE \"id\" = #{self.id}") || {}).map {|rp| JSON.parse(rp["rateplan"])}.reduce({}, :merge)
68
+ stub_catalog = JSON.parse(ActiveRecord::Base.connection.execute('SELECT json_object_agg(rateplan_id, rateplan) as "charges" FROM "public"."zuora_connect_app_instances", jsonb_each(("public"."zuora_connect_app_instances"."catalog" #> \'{%s}\' )) AS e(product_id, product), jsonb_each(product #> \'{productRatePlans}\') AS ee(rateplan_id, rateplan) WHERE "id" = %s' % [entity_reference, self.id]).first["item"] || "{}")
69
69
  else
70
- stub_catalog = (ZuoraConnect::AppInstance.unscoped.select("catalog #> '{#{entity_reference}, #{object_hierarchy['productId']}, productRatePlans, #{object_id}}' as item").where(id: self.id).first.item || {})
70
+ stub_catalog = JSON.parse(ActiveRecord::Base.connection.execute('SELECT catalog #> \'{%s, %s, productRatePlans, %s}\' AS item FROM "public"."zuora_connect_app_instances" WHERE "id" = %s' % [entity_reference, object_hierarchy['productId'], object_id, self.id]).first["item"] || "{}")
71
71
  end
72
72
 
73
73
  if !map_lower_objects
@@ -75,14 +75,12 @@ module ZuoraConnect
75
75
  end
76
76
  when :charge
77
77
  if object_id.blank?
78
- #Still Needs Fixing
79
- stub_catalog = (ActiveRecord::Base.connection.execute("SELECT \"value\" #>'{productRatePlans}' as \"rateplan\" from \"public\".\"zuora_connect_app_instances\", json_each((\"public\".\"zuora_connect_app_instances\".\"catalog\"::json -> '#{entity_reference}')) AS e(key, value) WHERE \"id\" = #{self.id}") || {}).map {|rp| JSON.parse(rp["rateplan"])}.reduce({}, :merge)
80
- stub_catalog = stub_catalog.map{|k,x| x["productRatePlanCharges"] }.reduce({}, :merge)
78
+ stub_catalog = JSON.parse(ActiveRecord::Base.connection.execute('SELECT json_object_agg(charge_id, charge) as item FROM "public"."zuora_connect_app_instances", jsonb_each(("public"."zuora_connect_app_instances"."catalog" #> \'{%s}\' )) AS e(product_id, product), jsonb_each(product #> \'{productRatePlans}\') AS ee(rateplan_id, rateplan), jsonb_each(rateplan #> \'{productRatePlanCharges}\') AS eee(charge_id, charge) WHERE "id" = %s' % [entity_reference, self.id]).first["item"] || "{}")
81
79
  else
82
- stub_catalog = (ZuoraConnect::AppInstance.unscoped.select("catalog #> '{#{entity_reference}, #{object_hierarchy['productId']}, productRatePlans, #{object_hierarchy['productRatePlanId']}, productRatePlanCharges, #{object_id}}' as item").where(id: self.id).first.item || {})
80
+ stub_catalog = JSON.parse(ActiveRecord::Base.connection.execute('SELECT catalog #> \'{%s, %s, productRatePlans, %s, productRatePlanCharges, %s}\' AS item FROM "public"."zuora_connect_app_instances" WHERE "id" = %s' % [entity_reference, object_hierarchy['productId'], object_hierarchy['productRatePlanId'], object_id, self.id]).first["item"] || "{}")
83
81
  end
84
82
  end
85
-
83
+
86
84
  return stub_catalog
87
85
  end
88
86
 
@@ -98,27 +96,56 @@ module ZuoraConnect
98
96
  return results
99
97
  end
100
98
 
101
-
102
- def get_catalog(zuora_login: self.login_lookup.first, force_update: false, entity_id: nil)
99
+ def get_catalog(zuora_login: self.login_lookup(type: "Zuora").first, entity_id: nil)
103
100
  entity_reference = entity_id.blank? ? 'Default' : entity_id
104
101
  Rails.logger.debug("Fetching catalog for default") if entity_id.blank?
105
102
  Rails.logger.debug("Fetching catalog for entity #{entity_id}") if !entity_id.blank?
106
- if catalog_outdated? || force_update
107
- Rails.logger.debug("Fetch Catalog")
108
- self.update_column(:catalog_updated_at, Time.now.utc)
109
- products, catalog_mapping = zuora_login.client(entity_reference).get_catalog
103
+ Rails.logger.debug("Fetch Catalog")
104
+ self.update_column(:catalog_updated_at, Time.now.utc)
105
+
106
+ login = zuora_login.client(entity_reference)
107
+
108
+ old_logger = ActiveRecord::Base.logger
109
+ ActiveRecord::Base.logger = nil
110
+ ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{%{entity}}\', \'{}\'), "catalog_mapping" = jsonb_set("catalog_mapping", \'{%{entity}}\', \'{}\') where "id" = %{id}' % {:entity => entity_reference, :id => self.id})
111
+
112
+ response = {'nextPage' => login.rest_endpoint('catalog/products?pageSize=5')}
113
+ while !response["nextPage"].blank?
114
+ url = login.rest_endpoint(response["nextPage"].split('/v1/').last)
115
+ Rails.logger.debug("Fetch Catalog URL #{url}")
116
+ output_json, response = login.rest_call(:debug => false, :url => url)
117
+
118
+ if !output_json['success'] =~ (/(true|t|yes|y|1)$/i) || output_json['success'].class != TrueClass
119
+ raise ZuoraAPI::Exceptions::ZuoraAPIError.new("Error Getting Catalog: #{output_json}")
120
+ end
121
+
122
+ output_json["products"].each do |product|
123
+ ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{%s, %s}\', \'%s\') where "id" = %s' % [entity_reference, product["id"], {"productId" => product["id"]}.to_json.gsub("'", "''"), self.id])
124
+ rateplans = {}
125
+
126
+ product["productRatePlans"].each do |rateplan|
127
+ ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{%s, %s}\', \'%s\') where "id" = %s' % [entity_reference, rateplan["id"], {"productId" => product["id"], "productRatePlanId" => rateplan["id"]}.to_json.gsub("'", "''"), self.id])
128
+ charges = {}
110
129
 
111
- # Update this entity
112
- ActiveRecord::Base.connection.execute("UPDATE \"public\".\"zuora_connect_app_instances\" SET \"catalog\" = jsonb_set(\"catalog\", '{#{entity_reference}}', '#{products.to_json}') where \"id\" = #{self.id}")
130
+ rateplan["productRatePlanCharges"].each do |charge|
131
+ ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{%s, %s}\', \'%s\') where "id" = %s' % [entity_reference, charge["id"], {"productId" => product["id"], "productRatePlanId" => rateplan["id"], "productRatePlanChargeId" => charge["id"]}.to_json.gsub("'", "''"), self.id])
113
132
 
114
- # Update this entity mapping
115
- ActiveRecord::Base.connection.execute("UPDATE \"public\".\"zuora_connect_app_instances\" SET \"catalog_mapping\" = jsonb_set(\"catalog_mapping\", '{#{entity_reference}}', '#{catalog_mapping.to_json}') where \"id\" = #{self.id}")
133
+ charges[charge["id"]] = charge.merge({"productId" => product["id"], "productName" => product["name"], "productRatePlanId" => rateplan["id"], "productRatePlanName" => rateplan["name"] })
134
+ end
135
+
136
+ rateplan["productRatePlanCharges"] = charges
137
+ rateplans[rateplan["id"]] = rateplan.merge({"productId" => product["id"], "productName" => product["name"]})
138
+ end
139
+ product["productRatePlans"] = rateplans
140
+
141
+ ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{%s, %s}\', \'%s\') where "id" = %s' % [entity_reference, product["id"], product.to_json.gsub("'", "''"), self.id])
142
+ end
143
+ ActiveRecord::Base.logger = old_logger
116
144
 
117
145
  self.touch
118
146
  end
119
147
 
120
- # DO NOT RETURN CATALOG. THIS IS NOT SCALABLE WITH LARGE CATALOGS. USE THE
121
- # return self.catalog[entity_reference]
148
+ # DO NOT RETURN CATALOG. THIS IS NOT SCALABLE WITH LARGE CATALOGS. USE THE CATALOG_LOOKUP method provided
122
149
  return true
123
150
  end
124
151
 
@@ -1,3 +1,3 @@
1
1
  module ZuoraConnect
2
- VERSION = "1.4.8"
2
+ VERSION = "1.4.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zuora_connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.8
4
+ version: 1.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Connect Team