|
19 | 19 | let(:ratio) { days_passed.to_f / charges_duration } |
20 | 20 |
|
21 | 21 | let(:pricing_unit_usage) { nil } |
| 22 | + let(:presentation_breakdowns) { [] } |
22 | 23 |
|
23 | 24 | let(:usage) do |
24 | 25 | [ |
|
44 | 45 | aggregation_type: billable_metric.aggregation_type, |
45 | 46 | grouped_by: {"card_type" => "visa"}, |
46 | 47 | charge_filter: nil, |
47 | | - pricing_unit_usage: |
| 48 | + pricing_unit_usage:, |
| 49 | + presentation_breakdowns: |
48 | 50 | ) |
49 | 51 | ] |
50 | 52 | end |
|
73 | 75 | "aggregation_type" => billable_metric.aggregation_type |
74 | 76 | }, |
75 | 77 | "filters" => [], |
| 78 | + "presentation_breakdowns" => [], |
76 | 79 | "grouped_usage" => [ |
77 | 80 | { |
78 | 81 | "amount_cents" => 100, |
|
81 | 84 | "units" => "10.0", |
82 | 85 | "total_aggregated_units" => "10.0", |
83 | 86 | "grouped_by" => {"card_type" => "visa"}, |
84 | | - "filters" => [] |
| 87 | + "filters" => [], |
| 88 | + "presentation_breakdowns" => [] |
85 | 89 | } |
86 | 90 | ] |
87 | 91 | ) |
88 | 92 | end |
89 | 93 |
|
| 94 | + context "when contains presentation breakdowns" do |
| 95 | + let(:presentation_breakdowns) do |
| 96 | + [ |
| 97 | + build(:presentation_breakdown, presentation_by: {"card_type" => "visa"}, units: "8"), |
| 98 | + build(:presentation_breakdown, presentation_by: {"card_type" => "mastercard"}, units: "1"), |
| 99 | + build(:presentation_breakdown, presentation_by: {"country" => "pt"}, units: "3") |
| 100 | + ] |
| 101 | + end |
| 102 | + |
| 103 | + it "serializes the breakdowns" do |
| 104 | + expect(result["charges"].first["presentation_breakdowns"]).to eq([]) |
| 105 | + expect(result["charges"].first["grouped_usage"].first["presentation_breakdowns"]).to match_array( |
| 106 | + [ |
| 107 | + {"presentation_by" => {"card_type" => "visa"}, "units" => "8.0"}, |
| 108 | + {"presentation_by" => {"card_type" => "mastercard"}, "units" => "1.0"}, |
| 109 | + {"presentation_by" => {"country" => "pt"}, "units" => "3.0"} |
| 110 | + ] |
| 111 | + ) |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + context "when usage contains two objects, one with grouped_by and other without grouped_by" do |
| 116 | + let(:other_charge) { create(:standard_charge, plan: charge.plan) } |
| 117 | + let(:other_billable_metric) { other_charge.billable_metric } |
| 118 | + let(:empty_group_presentation_breakdowns) { [] } |
| 119 | + let(:visa_group_presentation_breakdowns) { [] } |
| 120 | + |
| 121 | + let(:usage) do |
| 122 | + [ |
| 123 | + OpenStruct.new( |
| 124 | + charge_id: charge.id, |
| 125 | + subscription:, |
| 126 | + billable_metric: billable_metric, |
| 127 | + charge: charge, |
| 128 | + units: "2", |
| 129 | + total_aggregated_units: "2", |
| 130 | + events_count: 2, |
| 131 | + amount_cents: 20, |
| 132 | + amount_currency: "EUR", |
| 133 | + properties: { |
| 134 | + "from_datetime" => from_datetime.to_s, |
| 135 | + "to_datetime" => to_datetime.to_s, |
| 136 | + "charges_duration" => charges_duration |
| 137 | + }, |
| 138 | + invoice_display_name: charge.invoice_display_name, |
| 139 | + lago_id: billable_metric.id, |
| 140 | + name: billable_metric.name, |
| 141 | + code: billable_metric.code, |
| 142 | + aggregation_type: billable_metric.aggregation_type, |
| 143 | + grouped_by: {}, |
| 144 | + charge_filter: nil, |
| 145 | + pricing_unit_usage: pricing_unit_usage, |
| 146 | + presentation_breakdowns: empty_group_presentation_breakdowns |
| 147 | + ), |
| 148 | + OpenStruct.new( |
| 149 | + charge_id: other_charge.id, |
| 150 | + subscription:, |
| 151 | + billable_metric: other_billable_metric, |
| 152 | + charge: other_charge, |
| 153 | + units: "8", |
| 154 | + total_aggregated_units: "8", |
| 155 | + events_count: 10, |
| 156 | + amount_cents: 80, |
| 157 | + amount_currency: "EUR", |
| 158 | + properties: { |
| 159 | + "from_datetime" => from_datetime.to_s, |
| 160 | + "to_datetime" => to_datetime.to_s, |
| 161 | + "charges_duration" => charges_duration |
| 162 | + }, |
| 163 | + invoice_display_name: other_charge.invoice_display_name, |
| 164 | + lago_id: other_billable_metric.id, |
| 165 | + name: other_billable_metric.name, |
| 166 | + code: other_billable_metric.code, |
| 167 | + aggregation_type: other_billable_metric.aggregation_type, |
| 168 | + grouped_by: {"card_type" => "visa"}, |
| 169 | + charge_filter: nil, |
| 170 | + pricing_unit_usage: pricing_unit_usage, |
| 171 | + presentation_breakdowns: visa_group_presentation_breakdowns |
| 172 | + ) |
| 173 | + ] |
| 174 | + end |
| 175 | + |
| 176 | + it "serializes grouped usage including empty group" do |
| 177 | + expect(result["charges"].length).to eq(2) |
| 178 | + |
| 179 | + empty_group_charge = result["charges"].find { |c| c.dig("charge", "lago_id") == charge.id } |
| 180 | + visa_group_charge = result["charges"].find { |c| c.dig("charge", "lago_id") == other_charge.id } |
| 181 | + |
| 182 | + expect(empty_group_charge).to include( |
| 183 | + "units" => "2.0", |
| 184 | + "total_aggregated_units" => "2.0", |
| 185 | + "events_count" => 2, |
| 186 | + "amount_cents" => 20, |
| 187 | + "grouped_usage" => [], |
| 188 | + "presentation_breakdowns" => [] |
| 189 | + ) |
| 190 | + |
| 191 | + expect(visa_group_charge).to include( |
| 192 | + "units" => "8.0", |
| 193 | + "total_aggregated_units" => "8.0", |
| 194 | + "events_count" => 10, |
| 195 | + "amount_cents" => 80, |
| 196 | + "grouped_usage" => [ |
| 197 | + { |
| 198 | + "amount_cents" => 80, |
| 199 | + "pricing_unit_details" => nil, |
| 200 | + "events_count" => 10, |
| 201 | + "units" => "8.0", |
| 202 | + "total_aggregated_units" => "8.0", |
| 203 | + "grouped_by" => {"card_type" => "visa"}, |
| 204 | + "filters" => [], |
| 205 | + "presentation_breakdowns" => [] |
| 206 | + } |
| 207 | + ] |
| 208 | + ) |
| 209 | + end |
| 210 | + |
| 211 | + context "when contains presentation breakdowns" do |
| 212 | + let(:empty_group_presentation_breakdowns) do |
| 213 | + [ |
| 214 | + build(:presentation_breakdown, presentation_by: {"card_type" => "visa"}, units: "8"), |
| 215 | + build(:presentation_breakdown, presentation_by: {"country" => "pt"}, units: "3") |
| 216 | + ] |
| 217 | + end |
| 218 | + let(:visa_group_presentation_breakdowns) do |
| 219 | + [ |
| 220 | + build(:presentation_breakdown, presentation_by: {"card_type" => "mastercard"}, units: "1") |
| 221 | + ] |
| 222 | + end |
| 223 | + |
| 224 | + it "serializes breakdowns for ungrouped and grouped usage" do |
| 225 | + empty_group_charge = result["charges"].find { |c| c.dig("charge", "lago_id") == charge.id } |
| 226 | + visa_group_charge = result["charges"].find { |c| c.dig("charge", "lago_id") == other_charge.id } |
| 227 | + |
| 228 | + expect(empty_group_charge["grouped_usage"]).to eq([]) |
| 229 | + expect(empty_group_charge["presentation_breakdowns"]).to match_array( |
| 230 | + [ |
| 231 | + {"presentation_by" => {"card_type" => "visa"}, "units" => "8.0"}, |
| 232 | + {"presentation_by" => {"country" => "pt"}, "units" => "3.0"} |
| 233 | + ] |
| 234 | + ) |
| 235 | + |
| 236 | + expect(visa_group_charge["presentation_breakdowns"]).to eq([]) |
| 237 | + expect(visa_group_charge["grouped_usage"].first["presentation_breakdowns"]).to match_array( |
| 238 | + [ |
| 239 | + {"presentation_by" => {"card_type" => "mastercard"}, "units" => "1.0"} |
| 240 | + ] |
| 241 | + ) |
| 242 | + end |
| 243 | + end |
| 244 | + end |
| 245 | + |
90 | 246 | context "when charge configured to use pricing units" do |
91 | 247 | let(:pricing_unit_usage) do |
92 | 248 | PricingUnitUsage.new(amount_cents: 200, conversion_rate: 0.5, short_name: "CR") |
|
116 | 272 | "aggregation_type" => billable_metric.aggregation_type |
117 | 273 | }, |
118 | 274 | "filters" => [], |
| 275 | + "presentation_breakdowns" => [], |
119 | 276 | "grouped_usage" => [ |
120 | 277 | { |
121 | 278 | "amount_cents" => 100, |
|
128 | 285 | "units" => "10.0", |
129 | 286 | "total_aggregated_units" => "10.0", |
130 | 287 | "grouped_by" => {"card_type" => "visa"}, |
131 | | - "filters" => [] |
| 288 | + "filters" => [], |
| 289 | + "presentation_breakdowns" => [] |
132 | 290 | } |
133 | 291 | ] |
134 | 292 | ) |
|
157 | 315 | grouped_by: {"card_type" => "visa"}, |
158 | 316 | charge_filter:, |
159 | 317 | charge_filter_id: charge_filter.id, |
160 | | - pricing_unit_usage: |
| 318 | + pricing_unit_usage:, |
| 319 | + presentation_breakdowns: |
161 | 320 | ) |
162 | 321 | end |
163 | 322 | end |
|
0 commit comments