silvermobi/handler/notifications.go

71 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-12-08 06:35:04 +00:00
package handler
import (
"context"
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
"git.coopgo.io/coopgo-apps/silvermobi/services"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
gomail "gopkg.in/mail.v2"
)
func (h *SilverMobiHandler) PutFirebase(ctx context.Context, id string, token string, devicePlatform string) (err error) {
err = h.Storage.CreateFirebaseToken(id, token, devicePlatform)
2023-12-08 06:35:04 +00:00
return err
}
2024-10-31 19:32:54 +00:00
func (h *SilverMobiHandler) SendNotification(id, title, message string) error {
firebaseToken, platform, err := h.Storage.GetFirebaseToken(id)
2023-12-08 06:35:04 +00:00
if err != nil {
return err
}
2024-10-31 19:32:54 +00:00
notify := services.Notification{
Recipients: []string{firebaseToken},
Title: title,
Message: message,
}
if platform == "android" {
notify.Platform = services.PushToAndroid
err = h.Services.Push.Send(notify)
2023-12-08 06:35:04 +00:00
} else {
2024-10-31 19:32:54 +00:00
notify.Platform = services.PushToIos
err = h.Services.Push.Send(notify)
2023-12-08 06:35:04 +00:00
}
2024-10-31 19:32:54 +00:00
return err
2023-12-08 06:35:04 +00:00
}
2024-10-31 19:32:54 +00:00
func (h *SilverMobiHandler) SendEmail(id, title, message string) (err error) {
var resp *grpcapi.GetAccountResponse
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(context.Background(),
&grpcapi.GetAccountRequest{Id: id}); err != nil {
2023-12-08 06:35:04 +00:00
return err
}
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
m := gomail.NewMessage()
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
m.SetHeader("From", h.Config.GetString("emailing.smtp.username"))
m.SetHeader("To", account.Email)
m.SetHeader("Subject", title)
m.SetBody("text/plain", message)
2024-10-31 19:32:54 +00:00
d := gomail.NewDialer(
h.Config.GetString("emailing.smtp.host"),
h.Config.GetInt("emailing.smtp.port"),
h.Config.GetString("emailing.smtp.username"),
2023-12-08 06:35:04 +00:00
h.Config.GetString("emailing.smtp.password"))
2024-10-31 19:32:54 +00:00
d.StartTLSPolicy = gomail.MandatoryStartTLS
if err = d.DialAndSend(m); err != nil {
2023-12-08 06:35:04 +00:00
return err
}
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
return nil
}