diff --git a/services/notifications/pkg/command/root.go b/services/notifications/pkg/command/root.go index 2833e622e..d61b299dc 100644 --- a/services/notifications/pkg/command/root.go +++ b/services/notifications/pkg/command/root.go @@ -15,6 +15,7 @@ func GetCommands(cfg *config.Config) cli.Commands { Server(cfg), // interaction with this service + SendEmail(cfg), // infos about this service Health(cfg), diff --git a/services/notifications/pkg/command/send_email.go b/services/notifications/pkg/command/send_email.go new file mode 100644 index 000000000..8da57ce84 --- /dev/null +++ b/services/notifications/pkg/command/send_email.go @@ -0,0 +1,57 @@ +package command + +import ( + "github.com/cs3org/reva/v2/pkg/events" + "github.com/cs3org/reva/v2/pkg/events/stream" + "github.com/owncloud/ocis/v2/services/notifications/pkg/config" + "github.com/pkg/errors" + "github.com/urfave/cli/v2" +) + +// SendEmail triggers the sending of grouped email notifications for daily or weekly emails. +func SendEmail(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "send-email", + Usage: "Send grouped email notifications with daily or weekly interval. Specify at least one of the flags '--daily' or '--weekly'.", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "daily", + Aliases: []string{"d"}, + Usage: "Sends grouped daily email notifications.", + }, + &cli.BoolFlag{ + Name: "weekly", + Aliases: []string{"w"}, + Usage: "Sends grouped weekly email notifications.", + }, + }, + Action: func(c *cli.Context) error { + daily := c.Bool("daily") + weekly := c.Bool("weekly") + if !daily && !weekly { + return errors.New("at least one of '--daily' or '--weekly' must be set") + } + s, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Notifications.Events)) + if err != nil { + return err + } + if daily { + err = events.Publish(c.Context, s, events.SendEmailsEvent{ + Interval: "daily", + }) + if err != nil { + return err + } + } + if weekly { + err = events.Publish(c.Context, s, events.SendEmailsEvent{ + Interval: "weekly", + }) + if err != nil { + return err + } + } + return nil + }, + } +}