Skip to content

Commit

Permalink
Fix visibility of due dates and auto-year for due date (#106)
Browse files Browse the repository at this point in the history
* Use next year if DD-MM due date is falling before today in current year

* Fix visibility for note with due-date
  • Loading branch information
goyalmunish authored Apr 15, 2022
1 parent 100a5f9 commit 9cb14a4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
12 changes: 6 additions & 6 deletions internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,26 +788,26 @@ func TestNotesApprachingDueDate(t *testing.T) {
notes = append(notes, &model.Note{Text: "RAP02", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 1*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RAP03", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime}) // expected
notes = append(notes, &model.Note{Text: "RAP04", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 1*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RAP05", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 6*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RAP05", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 2*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RAP06", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 9*24*3600})
notes = append(notes, &model.Note{Text: "RAP07", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 9*24*3600 - 2*365})
notes = append(notes, &model.Note{Text: "RAP08", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 1*24*3600 - 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP09", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP10", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 1*24*3600 - 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP11", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 6*24*3600 - 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP11", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 2*24*3600 - 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP12", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 9*24*3600 - 2*365})
notes = append(notes, &model.Note{Text: "RAP13", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 9*24*3600 + 2*365})
notes = append(notes, &model.Note{Text: "RAP14", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 1*24*3600 + 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP15", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP16", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 1*24*3600 + 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP17", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 6*24*3600 + 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP17", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 2*24*3600 + 2*365}) // expected
notes = append(notes, &model.Note{Text: "RAP18", Status: "pending", TagIds: []int{repeatAnnuallyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 9*24*3600 + 2*365})
// repeat-monthly pending notes
notes = append(notes, &model.Note{Text: "RMP01", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 9*24*3600})
notes = append(notes, &model.Note{Text: "RMP02", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime - 1*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RMP03", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime}) // expected
notes = append(notes, &model.Note{Text: "RMP04", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 1*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RMP05", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 2*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RMP04", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 2*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RMP05", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 3*24*3600}) // expected
notes = append(notes, &model.Note{Text: "RMP06", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 7*24*3600})
notes = append(notes, &model.Note{Text: "RMP07", Status: "pending", TagIds: []int{repeatMonthlyTagId}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}, CompleteBy: currentTime + 9*24*3600})
reminderData.Notes = notes
Expand All @@ -820,7 +820,7 @@ func TestNotesApprachingDueDate(t *testing.T) {
utils.AssertEqual(t, urgentNotesText, []string{
"NRP01a", "NRP02a", "NRP02b", "NRP03a", "NRP04a", "NRP04b", "NRP05a", "NRP05b", "NRP06a",
"RAP02", "RAP03", "RAP04", "RAP05", "RAP08", "RAP09", "RAP10", "RAP11", "RAP14", "RAP15", "RAP16", "RAP17",
"RMP02", "RMP03", "RMP04", "RMP05",
"RMP02", "RMP03",
})
}

Expand Down
5 changes: 4 additions & 1 deletion internal/model/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,12 @@ func (note *Note) UpdateCompleteBy(text string) error {
} else {
format := "2-1-2006"
// set current year as year if year part is missing
year := utils.CurrentTime().Year()
timeSplit := strings.Split(text, "-")
if len(timeSplit) == 2 {
year, err := utils.YearForDueDateDDMM(text)
if err != nil {
return err
}
text = fmt.Sprintf("%s-%d", text, year)
}
// parse and set the date
Expand Down
14 changes: 7 additions & 7 deletions internal/model/reminder_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (reminderData *ReminderData) NotesApprachingDueDate() Notes {
// populating currentNotes
for _, note := range pendingNotes {
noteIDsWithRepeat := utils.GetCommonMembersIntSlices(note.TagIds, repeatTagIDs)
// first process notes without tag with group "repeat"
// first process notes WITHOUT tag with group "repeat"
// start showing such notes 7 days in advance from their due date, and until they are marked done
minDay := note.CompleteBy - 7*24*60*60
currentTimestamp := utils.CurrentUnixTimestamp()
Expand All @@ -295,8 +295,8 @@ func (reminderData *ReminderData) NotesApprachingDueDate() Notes {
noteTimestampCurrent := utils.UnixTimestampForCorrespondingCurrentYear(int(noteMonth), noteDay)
noteTimestampPrevious := noteTimestampCurrent - 365*24*60*60
noteTimestampNext := noteTimestampCurrent + 365*24*60*60
daysBefore := int64(7) // days before to start showing the note
daysAfter := int64(2) // days after until to show the note
daysBefore := int64(3) // days before to start showing the note
daysAfter := int64(7) // days after until to show the note
if utils.IsTimeForRepeatNote(noteTimestampCurrent, noteTimestampPrevious, noteTimestampNext, daysBefore, daysAfter) {
currentNotes = append(currentNotes, note)
}
Expand All @@ -307,8 +307,8 @@ func (reminderData *ReminderData) NotesApprachingDueDate() Notes {
noteTimestampCurrent := utils.UnixTimestampForCorrespondingCurrentYearMonth(noteDay)
noteTimestampPrevious := noteTimestampCurrent - 30*24*60*60
noteTimestampNext := noteTimestampCurrent + 30*24*60*60
daysBefore := int64(3) // days beofre to start showing the note
daysAfter := int64(2) // days after until to show the note
daysBefore := int64(1) // days beofre to start showing the note
daysAfter := int64(3) // days after until to show the note
if utils.IsTimeForRepeatNote(noteTimestampCurrent, noteTimestampPrevious, noteTimestampNext, daysBefore, daysAfter) {
currentNotes = append(currentNotes, note)
}
Expand Down Expand Up @@ -616,8 +616,8 @@ func (reminderData *ReminderData) PrintNotesAndAskOptions(notes Notes, tagID int
// fetch notes approaching due date
fmt.Println("Note: Following are the pending notes with due date:")
fmt.Println(" - within a week or already crossed (for non repeat-annually or repeat-monthly)")
fmt.Println(" - within a week for repeat-annually and 2 days post due date (ignoring its year)")
fmt.Println(" - within 3 days for repeat-monthly and 2 days post due date (ignoring its year and month)")
fmt.Println(" - within 3 days for repeat-annually and a week post due date (ignoring its year)")
fmt.Println(" - within 1 day for repeat-monthly and 3 days post due date (ignoring its year and month)")
notes = reminderData.NotesApprachingDueDate()
}
} else {
Expand Down
24 changes: 24 additions & 0 deletions pkg/utils/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ func UnixTimestampForCorrespondingCurrentYearMonth(day int) int64 {
return int64(timeValue.Unix())
}

// YearForDueDateDDMM return the current year if DD-MM is falling after current date, otherwise returns next year
func YearForDueDateDDMM(dateMonth string) (int, error) {
format := "2-1-2006"
currentTime := CurrentTime()
// set current year as year if year part is missing
timeSplit := strings.Split(dateMonth, "-")
if len(timeSplit) != 2 {
return 0, fmt.Errorf("Provided dateMonth string, %s, is not in DD-MM format", dateMonth)
}
// test with current year
year := currentTime.Year()
dateString := fmt.Sprintf("%s-%d", dateMonth, year)
testTimeValue, err := time.Parse(format, dateString)
if err != nil {
return 0, err
}
if testTimeValue.Unix() <= currentTime.Unix() {
// the due date falls before current date in current year
// so, select next year instead
year += 1
}
return year, nil
}

// IntPresentInSlice function performs membership test for integer based array.
func IntPresentInSlice(a int, list []int) bool {
for _, b := range list {
Expand Down

0 comments on commit 9cb14a4

Please sign in to comment.