Skip to content

Commit

Permalink
removed the script and the write notification method
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Jun 18, 2024
1 parent 4ad9437 commit 963b358
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 100 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.1.130"
const version = "v0.1.131"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
1 change: 0 additions & 1 deletion interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ type ManageAlerts interface {
AlertSetup(av *entities.AlertVariables) error
SendAlert(category string) error
SendNotification(category string) error
WriteNotificationValues(av *entities.AlertVariables) error
}

type AlertDB interface {
Expand Down
Empty file removed logfile.log
Empty file.
23 changes: 0 additions & 23 deletions script.sh

This file was deleted.

74 changes: 0 additions & 74 deletions usecases/app/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"unicode"

"github.com/ibilalkayy/flow/entities"
"github.com/ibilalkayy/flow/handler"
Expand Down Expand Up @@ -88,24 +86,12 @@ func (h MyAlert) SendAlert(category string) error {
return errors.New("wrong weekday is selected")
}

values := entities.AlertVariables{
Category: category,
Hours: hour,
Minutes: minute,
Days: day,
Weekdays: weekdayStr,
}

switch method {
case "email":
switch frequency {
case "hourly":
h.Deps.SpendAmount.HourlyNotification(category)
case "daily":
err := h.Deps.ManageAlerts.WriteNotificationValues(&values)
if err != nil {
log.Fatal(err)
}
h.Deps.SpendAmount.DailyNotification(hour, minute, category)
case "weekly":
h.Deps.SpendAmount.WeeklyNotification(weekday, hour, minute, category)
Expand All @@ -120,66 +106,6 @@ func (h MyAlert) SendAlert(category string) error {
return nil
}

func (h MyAlert) WriteNotificationValues(av *entities.AlertVariables) error {
hours := av.Hours
minutes := av.Minutes

desiredDay := av.Days
desiredWeekday := av.Weekdays
desiredTime := fmt.Sprintf("%02d:%02d", hours, minutes)

// Convert to upper letter
if len(desiredWeekday) > 0 {
desiredWeekday = string(unicode.ToUpper(rune(desiredWeekday[0]))) + strings.ToLower(desiredWeekday[1:])
}

scriptPath := os.Getenv("SCRIPT_PATH")
if scriptPath == "" {
return errors.New("SCRIPT_PATH environment variable is not set")
}
scriptFile := fmt.Sprintf("%s/script.sh", scriptPath)

input, err := os.ReadFile(scriptFile)
if err != nil {
log.Fatal(err)
}

value, err := h.Deps.AlertDB.ViewAlert(av.Category)
if err != nil {
return err
}

category, ok := value[1].(string)
if !ok {
return errors.New("unable to convert to string")
}

if len(av.Category) != 0 && av.Category == category {
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.Contains(line, "DESIRED_TIME=") {
lines[i] = fmt.Sprintf(`DESIRED_TIME="%s"`, desiredTime)
}
if strings.Contains(line, "DESIRED_DAY=") {
lines[i] = fmt.Sprintf(`DESIRED_DAY="%d"`, desiredDay)
}
if strings.Contains(line, "DESIRED_WEEKDAY=") {
lines[i] = fmt.Sprintf(`DESIRED_WEEKDAY="%s"`, desiredWeekday)
}
}

output := strings.Join(lines, "\n")
err = os.WriteFile(scriptFile, []byte(output), 0644)
if err != nil {
return err
}
} else {
return errors.New("category is not present in the alert")
}

return nil
}

func (h MyAlert) SendNotification(category string) error {
budgetDetails, err := h.Deps.ManageBudget.ViewBudget(category)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion usecases/app/spend/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (h MySpending) HourlyNotification(category string) {
ticker := time.NewTicker(time.Second)
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for {
<-ticker.C
Expand Down

0 comments on commit 963b358

Please sign in to comment.