From d8792751ea2b41c347a66bde17438bce8cd94318 Mon Sep 17 00:00:00 2001 From: seanwelch Date: Sun, 16 Jun 2024 21:53:14 +0100 Subject: [PATCH] include email for registration --- server/services/registrationService.go | 51 ++++++++++++++++++++++++-- server/services/warrantyService.go | 5 +-- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/server/services/registrationService.go b/server/services/registrationService.go index 369b1ad..d3a0e84 100644 --- a/server/services/registrationService.go +++ b/server/services/registrationService.go @@ -1,8 +1,10 @@ package services import ( + "github.com/sean-david-welch/farmec-v2/server/lib" "github.com/sean-david-welch/farmec-v2/server/stores" "github.com/sean-david-welch/farmec-v2/server/types" + "net/smtp" ) type RegistrationService interface { @@ -14,11 +16,12 @@ type RegistrationService interface { } type RegistrationServiceImpl struct { - store stores.RegistrationStore + smtpClient lib.SMTPClient + store stores.RegistrationStore } -func NewRegistrationService(store stores.RegistrationStore) *RegistrationServiceImpl { - return &RegistrationServiceImpl{store: store} +func NewRegistrationService(store stores.RegistrationStore, smtpClient lib.SMTPClient) *RegistrationServiceImpl { + return &RegistrationServiceImpl{store: store, smtpClient: smtpClient} } func (service *RegistrationServiceImpl) GetRegistrations() ([]types.MachineRegistration, error) { @@ -44,6 +47,27 @@ func (service *RegistrationServiceImpl) CreateRegistration(registration *types.M return err } + client, err := service.smtpClient.SetupSMTPClient() + if err != nil { + return err + } + defer func(client *smtp.Client) { + err := client.Close() + if err != nil { + return + } + }(client) + + data := &types.EmailData{ + Name: registration.OwnerName, + Email: registration.DealerName, + Message: registration.MachineModel, + } + + if err := service.smtpClient.SendFormNotification(client, data, "Machine Registration"); err != nil { + return err + } + return nil } @@ -52,6 +76,27 @@ func (service *RegistrationServiceImpl) UpdateRegistration(id string, registrati return err } + client, err := service.smtpClient.SetupSMTPClient() + if err != nil { + return err + } + defer func(client *smtp.Client) { + err := client.Close() + if err != nil { + return + } + }(client) + + data := &types.EmailData{ + Name: registration.OwnerName, + Email: registration.DealerName, + Message: registration.MachineModel, + } + + if err := service.smtpClient.SendFormNotification(client, data, "Machine Registration"); err != nil { + return err + } + return nil } diff --git a/server/services/warrantyService.go b/server/services/warrantyService.go index 7309f68..bce9595 100644 --- a/server/services/warrantyService.go +++ b/server/services/warrantyService.go @@ -16,13 +16,12 @@ type WarrantyService interface { } type WarrantyServiceImpl struct { - secrets *lib.Secrets smtpClient lib.SMTPClient store stores.WarrantyStore } -func NewWarrantyService(store stores.WarrantyStore, smtpClient lib.SMTPClient, secrets *lib.Secrets) *WarrantyServiceImpl { - return &WarrantyServiceImpl{store: store, smtpClient: smtpClient, secrets: secrets} +func NewWarrantyService(store stores.WarrantyStore, smtpClient lib.SMTPClient) *WarrantyServiceImpl { + return &WarrantyServiceImpl{store: store, smtpClient: smtpClient} } func (service *WarrantyServiceImpl) GetWarranties() ([]types.DealerOwnerInfo, error) {