Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve #75 - Update fetch pto from notion bot #90

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions lib/bas/bot/fetch_next_week_ptos_from_notion.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "date"

require_relative "./base"
require_relative "../read/default"
require_relative "../utils/notion/request"
Expand Down Expand Up @@ -34,7 +36,7 @@ module Bot
# bot = Bot::FetchNextWeekPtosFromNotion.new(options)
# bot.execute
#
class FetchNextWeekPtosFromNotion < Bot::Base
class FetchNextWeekPtosFromNotion < Bot::Base # rubocop:disable Metrics/ClassLength
# Read function to execute the default Read component
#
def read
Expand Down Expand Up @@ -130,14 +132,47 @@ def normalize_response(results)
results.map do |pto|
pto_fields = pto["properties"]

{
"Name" => extract_description_field_value(pto_fields["Description"]),
"StartDateTime" => extract_date_field_value(pto_fields["StartDateTime"]),
"EndDateTime" => extract_date_field_value(pto_fields["EndDateTime"])
}
name = extract_description_field_value(pto_fields["Description"])
start_date = extract_date_field_value(pto_fields["StartDateTime"])
end_date = extract_date_field_value(pto_fields["EndDateTime"])

description(name, start_date, end_date)
end
end

def description(name, start_date, end_date)
start = start_description(start_date)
finish = end_description(end_date)

"#{name} will not be working between #{start} and #{finish}. And returns the #{returns(finish)}"
end

def start_description(date)
date[:from]
end

def end_description(date)
return date[:from] if date[:to].nil?

date[:to]
end

def returns(date)
date.include?("T12") ? "#{date} in the afternoon" : next_work_day(date)
end

def next_work_day(date)
datetime = DateTime.parse(date)

return_day = case datetime.wday
when 5 then datetime + 3
when 6 then datetime + 2
else datetime + 1
end

return_day.strftime("%A %B %d of %Y").to_s
end

def extract_description_field_value(data)
names = data["title"].map { |name| name["plain_text"] }

Expand Down
19 changes: 3 additions & 16 deletions lib/bas/bot/fetch_ptos_from_notion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,13 @@ def params
endpoint: "databases/#{process_options[:database_id]}/query",
secret: process_options[:secret],
method: "post",
body:
body: { filter: today_condition }
}
end

def body
{ filter: { "or": conditions } }
end

def conditions
[
today_condition,
{ property: "StartDateTime", date: { this_week: {} } },
{ property: "EndDateTime", date: { this_week: {} } },
{ property: "StartDateTime", date: { next_week: {} } },
{ property: "EndDateTime", date: { next_week: {} } }
]
end

def today_condition
today = Time.now.utc.strftime("%F").to_s

{
"and": [
{ property: "StartDateTime", date: { on_or_before: today } },
Expand Down Expand Up @@ -134,7 +121,7 @@ def end_description(date)
end

def returns(date)
date.include?("T") ? "#{date} in the afternoon" : next_work_day(date)
date.include?("T12") ? "#{date} in the afternoon" : next_work_day(date)
end

def next_work_day(date)
Expand Down
10 changes: 3 additions & 7 deletions spec/bas/bot/fetch_next_week_ptos_from_notion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@
{
"properties" => {
"Description" => { "title" => [{ "plain_text" => "John Doe" }] },
"StartDateTime" => { "date" => { "start" => "2024-05-01", "end" => "" } },
"EndDateTime" => { "date" => { "start" => "2024-05-02", "end" => "" } }
"StartDateTime" => { "date" => { "start" => "2024-05-01", "end" => nil } },
"EndDateTime" => { "date" => { "start" => "2024-05-02", "end" => nil } }
}
}
end

let(:formatted_pto) do
{
"Name" => "John Doe",
"StartDateTime" => { from: "2024-05-01", to: "" },
"EndDateTime" => { from: "2024-05-02", to: "" }
}
"John Doe will not be working between 2024-05-01 and 2024-05-02. And returns the Friday May 03 of 2024"
end

let(:error_response) { { "object" => "error", "status" => 404, "message" => "not found" } }
Expand Down
Loading