feat: Add cli command to trigger grouped emails

This commit is contained in:
Bastian Beier
2025-01-08 19:31:40 +01:00
parent b5dd1d1ae7
commit db3d49afe5
2 changed files with 58 additions and 0 deletions

View File

@@ -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),

View File

@@ -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
},
}
}