From fba98a9f871c22399dee55d11c06cec6d8cadc8d Mon Sep 17 00:00:00 2001 From: FelipeGuzmanSierra <97761783+FelipeGuzmanSierra@users.noreply.github.com> Date: Fri, 26 Jul 2024 12:50:21 -0500 Subject: [PATCH] Improve format and filter for the PTO fetch bots --- .../bot/fetch_next_week_ptos_from_notion.rb | 47 ++++++++++++++++--- lib/bas/bot/fetch_ptos_from_notion.rb | 19 ++------ .../fetch_next_week_ptos_from_notion_spec.rb | 10 ++-- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/lib/bas/bot/fetch_next_week_ptos_from_notion.rb b/lib/bas/bot/fetch_next_week_ptos_from_notion.rb index f592fd0..f3c38f1 100644 --- a/lib/bas/bot/fetch_next_week_ptos_from_notion.rb +++ b/lib/bas/bot/fetch_next_week_ptos_from_notion.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "date" + require_relative "./base" require_relative "../read/default" require_relative "../utils/notion/request" @@ -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 @@ -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"] } diff --git a/lib/bas/bot/fetch_ptos_from_notion.rb b/lib/bas/bot/fetch_ptos_from_notion.rb index 9ccd5c1..f1c060b 100644 --- a/lib/bas/bot/fetch_ptos_from_notion.rb +++ b/lib/bas/bot/fetch_ptos_from_notion.rb @@ -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 } }, @@ -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) diff --git a/spec/bas/bot/fetch_next_week_ptos_from_notion_spec.rb b/spec/bas/bot/fetch_next_week_ptos_from_notion_spec.rb index 42ab63f..bbe2aeb 100644 --- a/spec/bas/bot/fetch_next_week_ptos_from_notion_spec.rb +++ b/spec/bas/bot/fetch_next_week_ptos_from_notion_spec.rb @@ -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" } }