Skip to content

Commit

Permalink
Add filter by a time window
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeGuzmanSierra committed May 13, 2024
1 parent 96b2569 commit 33245a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion lib/v2/bot/format_emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module Bot
#
class FormatEmails < Bot::Base
EMAIL_ATTRIBUTES = %w[subject sender date].freeze
DEFAULT_TIME_ZONE = "+00:00"

# read function to execute the PostgresDB Read component
#
Expand All @@ -62,7 +63,7 @@ def process(read_response)

emails_list = read_response.data["emails"]

notification = emails_list.reduce("") do |payload, email|
notification = process_emails(emails_list).reduce("") do |payload, email|
"#{payload} #{build_template(EMAIL_ATTRIBUTES, email)} \n"
end

Expand All @@ -79,6 +80,32 @@ def write(process_response)

private

def process_emails(emails)
emails.each do |email|
date = DateTime.parse(email["date"]).to_time
email["date"] = at_timezone(date)
end
emails.filter! { |email| email["date"] > time_window } unless process_options[:frequency].nil?

format_timestamp(emails)
end

def format_timestamp(emails)
emails.each { |email| email["date"] = email["date"].strftime("%F %r") }
end

def time_window
date_time = Time.now - (60 * 60 * process_options[:frequency])

at_timezone(date_time)
end

def at_timezone(date)
timezone = process_options[:timezone] || DEFAULT_TIME_ZONE

Time.at(date, in: timezone)
end

def build_template(attributes, instance)
template = process_options[:template]

Expand Down
2 changes: 1 addition & 1 deletion spec/v2/bot/format_emails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
let(:emails) { [{ "date" => "Thu, 09 May", "sender" => "user@mail.com" }] }

let(:formatted_emails) do
" The user@mail.com has requested support the Thu, 09 May \n"
" The user@mail.com has requested support the 2024-05-09 12:00:00 AM \n"
end

it "returns an empty success hash when the birthdays list is empty" do
Expand Down

0 comments on commit 33245a2

Please sign in to comment.