diff --git a/eventEntriesNotifier/main.go b/eventEntriesNotifier/main.go new file mode 100644 index 0000000..124d6dc --- /dev/null +++ b/eventEntriesNotifier/main.go @@ -0,0 +1,87 @@ +package eventEntriesNotifier + +import ( + "git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger" + "github.com/pocketbase/pocketbase" + "github.com/pocketbase/pocketbase/core" + "github.com/pocketbase/pocketbase/tools/mailer" + "github.com/pocketbase/pocketbase/tools/template" + "net/mail" +) + +// sendEmailNotification sends an email notification to the user +func sendEmailNotification(app *pocketbase.PocketBase, recordId string, STATUS string) error { + registry := template.NewRegistry() + + record, err := app.Dao().FindRecordById("eventListSlotEntriesWithUser", recordId) + if err != nil { + return err + } + + user, err := app.Dao().FindRecordById("users", record.GetString("user")) + if err != nil { + return err + } + + // render email template + html, err := registry.LoadFiles( + "html/eventEntryNotification.html", + ).Render(map[string]any{ + "STATUS": STATUS, + "EVENT_NAME": record.GetString("eventName"), + "APP_URL": app.Settings().Meta.AppUrl, + }) + if err != nil { + return err + } + + // send email + email := &mailer.Message{ + From: mail.Address{ + Address: app.Settings().Meta.SenderAddress, + Name: app.Settings().Meta.SenderName, + }, + To: []mail.Address{{Address: user.GetString("email")}}, + Subject: "[StuVe IT] Info zu einer Event Anmeldung", + HTML: html, + } + if err := app.NewMailClient().Send(email); err != nil { + return err + } + return nil +} + +// InitEventEntriesNotifier initializes the eventEntriesNotifier email notifier +func InitEventEntriesNotifier(app *pocketbase.PocketBase, e *core.ServeEvent) error { + + logger.LogInfoF("Adding eventEntriesNotifier email notifier") + + app.OnModelAfterCreate("eventListSlotEntries").Add(func(e *core.ModelEvent) error { + // send email notification + err := sendEmailNotification(app, e.Model.GetId(), "created") + if err != nil { + logger.LogErrorF("Error sending eventEntry email notification: %v", err) + } + return nil + }) + + app.OnModelAfterCreate("eventListSlotEntries").Add(func(e *core.ModelEvent) error { + // send email notification + err := sendEmailNotification(app, e.Model.GetId(), "updated") + if err != nil { + logger.LogErrorF("Error sending eventEntry email notification: %v", err) + } + return nil + }) + + app.OnModelAfterDelete("eventListSlotEntries").Add(func(e *core.ModelEvent) error { + // send email notification + err := sendEmailNotification(app, e.Model.GetId(), "deleted") + if err != nil { + logger.LogErrorF("Error sending eventEntry email notification: %v", err) + } + return nil + }) + + return nil +} diff --git a/html/emailNotification.html b/html/eventEntryNotification.html similarity index 87% rename from html/emailNotification.html rename to html/eventEntryNotification.html index c3ed894..df6b138 100644 --- a/html/emailNotification.html +++ b/html/eventEntryNotification.html @@ -45,18 +45,29 @@ background-color: #228be6; } +
Du hast eine neue {{.MESSAGE_TYPE}}! Schau dir die {{.MESSAGE_TYPE}} an, indem du auf den Button klickst:
+ {{if eq .STATUS "created"}} + Du hast Dich erfolgreich für das Event {{.EVENT_NAME}} angemeldet! + {{else if eq .STATUS "updated"}} + Die Daten Deiner Anmeldung für das Event {{.EVENT_NAME}} wurde aktualisiert. + {{else if eq .STATUS "deleted"}} + Deine Anmeldung für das Event {{.EVENT_NAME}} wurde storniert! + {{else}} + Schau Dir Deine Anmeldungen für das Event {{.EVENT_NAME}} an! + {{end}} +