Skip to content

Commit

Permalink
Merge pull request #100 from hackberrydev/skipped-in-future
Browse files Browse the repository at this point in the history
Don't treat tasks scheduled in future as missed
  • Loading branch information
strika authored Nov 4, 2022
2 parents a45ed00 + 123d18e commit 4e033f5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
22 changes: 14 additions & 8 deletions src/commands/schedule_tasks.janet
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@
(string "every year on " (remove-year formatted-date)) true
(string "on " formatted-date) true))

(defn- missed-on-day [plan task]
(defn- missed-on-day [plan task date]
(find (fn [day] (and (scheduled-for? task (day :date))
(not (day/has-task? day task))))
(plan/all-days plan)))
(plan/all-days-before plan date)))

# Public
(defn missed? [plan task]
(def day (missed-on-day plan task))
(defn missed?
```
Checks if the task was missed in the plan up to the passed date.
Returns true or false.
```
[plan task date]
(def day (missed-on-day plan task date))
(and day (not (plan/has-task-after? plan task (day :date)))))

(defn- mark-tasks-as-missed [plan tasks]
(defn- mark-tasks-as-missed [plan tasks date]
(map (fn [task]
(let [day (missed-on-day plan task)]
(let [day (missed-on-day plan task date)]
(task/mark-as-missed task (day :date))))
tasks))

Expand All @@ -52,8 +58,8 @@
(let [tasks (filter (fn [task] (scheduled-for? task (day :date))) scheduled-tasks)]
(day/add-tasks day tasks)))
(loop [day :in future-days]
(let [tasks (filter (fn [task] (missed? plan task)) scheduled-tasks)
missed-tasks (mark-tasks-as-missed plan tasks)]
(let [tasks (filter (fn [task] (missed? plan task date)) scheduled-tasks)
missed-tasks (mark-tasks-as-missed plan tasks date)]
(day/add-tasks day missed-tasks)))

plan)
Expand Down
12 changes: 11 additions & 1 deletion src/plan.janet
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,22 @@
(reverse
(flatten
(map (fn [day]
(def days (day/generate-days (date/+days date 1) (date/-days (day :date) 1)))
(def days (reverse (day/generate-days (date/+days date 1) (date/-days (day :date) 1))))
(array/push days day)
(set date (day :date))
days)
(reverse (plan :days))))))

(defn all-days-before
```
Return days from the plan without any 'holes' up to the date. For days that are missing, a new day
will be generated, without any tasks.
```
[plan date]
(drop-while (fn [day] (date/after-or-eq? (day :date) date))
(all-days plan)))


## —————————————————————————————————————————————————————————————————————————————————————————————————
## Tasks functions

Expand Down
22 changes: 19 additions & 3 deletions test/commands/schedule_tasks_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,27 @@
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2))
(day/build-day (d/date 2022 8 1))]))
(is (missed? plan scheduled-task)))
(is (missed? plan scheduled-task (d/date 2022 8 3))))

(deftest missed-returns-true-when-day-does-not-exist
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2))
(day/build-day (d/date 2022 7 15))]))
(is (missed? plan scheduled-task)))
(is (missed? plan scheduled-task (d/date 2022 8 3))))

(deftest missed-returns-false-when-task-was-scheduled-for-another-day
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2)
@[]
@[(task/build-task "Weekly meeting" true)])
(day/build-day (d/date 2022 8 1))]))
(is (not (missed? plan scheduled-task))))
(is (not (missed? plan scheduled-task (d/date 2022 8 3)))))

(deftest missed-returns-false-when-task-will-be-scheduled-in-future
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2))
(day/build-day (d/date 2022 7 31))]))
(is (not (missed? plan scheduled-task (d/date 2022 7 31)))))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Test schedule-tasks
Expand Down Expand Up @@ -161,6 +167,16 @@
(is (= false (task :done)))
(is (= "every Monday" (task :schedule)))))))

(deftest does-not-schedule-tasks-today-that-are-not-scheduled-for-future-day-that-is-not-in-plan
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 1 19))
(day/build-day (d/date 2022 1 18))
(day/build-day (d/date 2022 1 16))]))
(schedule-tasks plan scheduled-tasks (d/date 2022 1 16))
(is (= 1 (length (((plan :days) 0) :tasks))))
(is (empty? (((plan :days) 1) :tasks)))
(is (empty? (((plan :days) 2) :tasks))))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Test build-command

Expand Down
27 changes: 21 additions & 6 deletions test/plan_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,31 @@
## Test all-days

(deftest all-days
(def day-1 (day/build-day (d/date 2020 8 5)))
(def day-1 (day/build-day (d/date 2020 8 6)))
(def day-2 (day/build-day (d/date 2020 8 3)))
(def plan (plan/build-plan :days @[day-1 day-2]))
(def days (plan/all-days plan))
(is (= 3 (length days)))
(if (= 3 (length days))
(is (= 4 (length days)))
(if (= 4 (length days))
(do
(is (d/equal? (d/date 2020 8 6) ((days 0) :date)))
(is (d/equal? (d/date 2020 8 5) ((days 1) :date)))
(is (d/equal? (d/date 2020 8 4) ((days 2) :date)))
(is (d/equal? (d/date 2020 8 3) ((days 3) :date))))))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Test all-days-before

(deftest all-days-before
(def day-1 (day/build-day (d/date 2020 8 6)))
(def day-2 (day/build-day (d/date 2020 8 3)))
(def plan (plan/build-plan :days @[day-1 day-2]))
(def days (plan/all-days-before plan (d/date 2020 8 5)))
(is (= 2 (length days)))
(if (= 2 (length days))
(do
(is (d/equal? (d/date 2020 8 5) ((days 0) :date)))
(is (d/equal? (d/date 2020 8 4) ((days 1) :date)))
(is (d/equal? (d/date 2020 8 3) ((days 2) :date))))))
(is (d/equal? (d/date 2020 8 4) ((days 0) :date)))
(is (d/equal? (d/date 2020 8 3) ((days 1) :date))))))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Test all-tasks
Expand Down

0 comments on commit 4e033f5

Please sign in to comment.