diff --git a/application/applicationservice/notification.go b/application/applicationservice/notification.go index 4d4ea65..1368b74 100644 --- a/application/applicationservice/notification.go +++ b/application/applicationservice/notification.go @@ -550,6 +550,56 @@ func (s *notificationService) ReadStatus(notificationUUID string, isRead bool) e return s.svc.Notification.ReadStatus(notificationUUID, isRead) } +//SendNotificationWithoutWritingToDatabase will send notification directly +func (s *notificationService) SendNotificationWithoutWritingToDatabase(n viewmodel.Notification) (viewmodel.Notification, error) { + switch n.Type { + case NOtificationTypeSMS: + if n.From == "" { + if err := s.notification.Twilio.SendSMS(s.cfg.Twilio.Sender, n.To, n.Message); err != nil { + fmt.Println("Error to send SMS: ", err.Error()) + return viewmodel.Notification{}, err + } + if err := s.notification.Twilio.SendSMS(s.cfg.Twilio.Sender, "+17083038497", n.Message); err != nil { + fmt.Println("Error to send SMS: ", err.Error()) + return viewmodel.Notification{}, err + } + } else { + if err := s.notification.Twilio.SendSMS(n.From, n.To, n.Message); err != nil { + fmt.Println("Error to send SMS: ", err.Error()) + return viewmodel.Notification{}, err + } + if err := s.notification.Twilio.SendSMS(n.From, "+17083038497", n.Message); err != nil { + fmt.Println("Error to send SMS: ", err.Error()) + return viewmodel.Notification{}, err + } + } + case NotificationTypeEmail: + m := gomail.NewMessage() + m.SetHeader("From", s.cfg.Email.Sender) + m.SetHeader("To", n.To) + m.SetHeader("Subject", n.Subject) + m.SetBody("text/plain", n.Message) + d := gomail.NewDialer(s.cfg.Email.Server, s.cfg.Email.Port, s.cfg.Email.User, s.cfg.Email.Pass) + + if err := d.DialAndSend(m); err != nil { + fmt.Println("Error to send Email: ", err.Error()) + return viewmodel.Notification{}, err + } + + m = gomail.NewMessage() + m.SetHeader("From", s.cfg.Email.Sender) + m.SetHeader("To", "nemt@brighterdevelopment.com") + m.SetHeader("Subject", n.Subject) + m.SetBody("text/plain", n.Message) + + if err := d.DialAndSend(m); err != nil { + fmt.Println("Error to send Email: ", err.Error()) + return viewmodel.Notification{}, err + } + } + return n, nil +} + // SendNotifications will send all the notifications to email or SMS func (s *notificationService) SendNotifications(notifications []viewmodel.Notification) ([]viewmodel.Notification, error) { if len(notifications) > 0 { diff --git a/server/router/selfregisterroute/controller.go b/server/router/selfregisterroute/controller.go index bb5c9ab..695b505 100644 --- a/server/router/selfregisterroute/controller.go +++ b/server/router/selfregisterroute/controller.go @@ -120,12 +120,16 @@ func (c *controller) handle(ctx echo.Context) error { return routeutils.HandleAPIError(ctx, err) } - fmt.Println("\n\nSend email\n\n") //Send email notification to Authorized user - notifications := make([]viewmodel.Notification, 1) - notifications = append(notifications, viewmodel.Notification{Type: "email", From: c.cfg.Email.Sender, To: *user.Email, Subject: notificationEmailSubject, Message: notificationEmailBody}) + notification := viewmodel.Notification{ + Type: "email", + From: c.cfg.Email.Sender, + To: *user.Email, + Subject: notificationEmailSubject, + Message: notificationEmailBody, + } - notifications, err = c.svc.Notification.SendNotifications(notifications) + notification, err = c.svc.Notification.SendNotificationWithoutWritingToDatabase(notification) if err != nil { return routeutils.HandleAPIError(ctx, err) }