Skip to content

Commit

Permalink
fix: adjust behavior from feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jofer86 committed Aug 26, 2024
1 parent 9aabb49 commit 64a1a85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
30 changes: 18 additions & 12 deletions lib/bas/bot/format_do_bill_alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@ class FormatDoBillAlert < Bot::Base
# read function to execute the PostgresDB Read component
#
def read
reader = Read::Postgres.new(read_options.merge(conditions))
reader = Read::Postgres.new(read_options.merge(combined_conditions))

@previous_billing_data = fetch_previous_billing_data
@current_billing_data = reader.execute
@previous_billing_data, @current_billing_data = split_billing_data(reader.execute)
end

# Process function to format the notification using a template
#
def process
return { success: { notification: message } } if balance_alert?

{ success: { notification: "" } }
{ success: { notification: "" } } unless unprocessable_response
end

# Write function to execute the PostgresDB write component
Expand All @@ -72,6 +71,20 @@ def write

private

def combined_conditions
{
where: "archived=$1 AND tag=$2 AND (stage=$3 OR stage=$4) ORDER BY inserted_at ASC",
params: [false, read_options[:tag], "unprocessed", "processed"]
}
end

def split_billing_data(billing_data)
processed = billing_data.select { |record| record["stage"] == "processed" }.last
unprocessed = billing_data.select { |record| record["stage"] == "unprocessed" }.first

[processed, unprocessed]
end

def fetch_previous_billing_data
previous_reader = Read::Postgres.new(read_options.merge(previous_conditions))
previous_reader.execute
Expand All @@ -85,7 +98,7 @@ def previous_conditions
end

def balance_alert?
unprocessable_response || significant_change? || threshold_exceeded
significant_change? || threshold_exceeded
end

def significant_change?
Expand All @@ -95,13 +108,6 @@ def significant_change?
(current_balance - previous_balance) > process_options[:threshold]
end

def conditions
{
where: "archived=$1 AND tag=$2 AND stage=$3 ORDER BY inserted_at ASC",
params: [false, read_options[:tag], "unprocessed"]
}
end

def threshold_exceeded
daily_usage > process_options[:threshold]
end
Expand Down
12 changes: 6 additions & 6 deletions spec/bas/bot/format_do_bill_alert_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "bas/bot/format_do_bill_alert"
require "active_support/core_ext/string"

RSpec.describe Bot::FormatDoBillAlert do
before do
Expand Down Expand Up @@ -68,12 +67,13 @@
end

it "read the notification from the postgres database" do
allow(@bot).to receive(:split_billing_data).and_return([formatted_bill_alert, formatted_bill_alert])
read = @bot.read

expect(read).to be_a Read::Types::Response
expect(read.data).to be_a Hash
expect(read.data).to_not be_nil
expect(read.data).to eq(formatted_bill_alert)
expect(read).to eq([formatted_bill_alert, formatted_bill_alert])
expect(read.first).to be_a Hash
expect(read.first).to_not be_nil
expect(read.first).to eq(formatted_bill_alert)
end
end

Expand Down Expand Up @@ -120,7 +120,7 @@

processed = @bot.process

expect(processed[:success][:notification].squish).to eq(formatted_alert.squish)
expect(processed[:success][:notification].gsub(/\s+/, " ").strip).to eq(formatted_alert.gsub(/\s+/, " ").strip)
end

it "it triggers the alert when unprocessable_response is true" do
Expand Down

0 comments on commit 64a1a85

Please sign in to comment.