From 796a48fd13f4dad6236be8ecf53e25b2940ad16b Mon Sep 17 00:00:00 2001 From: Ebite God'sgift Uloamaka Date: Fri, 9 Aug 2024 23:09:14 +0100 Subject: [PATCH 1/7] fix: removed struct on the help center service --- internal/models/help_center.go | 6 ++++++ services/helpcenter/helpcenter.go | 21 +++++++-------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/internal/models/help_center.go b/internal/models/help_center.go index 05152b0d..35d1e825 100644 --- a/internal/models/help_center.go +++ b/internal/models/help_center.go @@ -8,6 +8,12 @@ import ( "gorm.io/gorm" ) +type HelpCntSummary struct { + ID string `json:"id"` + Title string `json:"title"` + Content string `json:"content"` + Author string `json:"author"` +} type HelpCenter struct { ID string `gorm:"type:uuid; primaryKey" json:"id"` diff --git a/services/helpcenter/helpcenter.go b/services/helpcenter/helpcenter.go index 62e6fa6d..faff4905 100644 --- a/services/helpcenter/helpcenter.go +++ b/services/helpcenter/helpcenter.go @@ -9,13 +9,6 @@ import ( "gorm.io/gorm" ) -type HelpCntSummary struct { - ID string `json:"id"` - Title string `json:"title"` - Content string `json:"content"` - Author string `json:"author"` -} - func CreateHelpCenterTopic(req models.CreateHelpCenter, db *gorm.DB) (models.HelpCenter, error) { helpCnt := models.HelpCenter{ ID: utility.GenerateUUID(), @@ -33,7 +26,7 @@ func CreateHelpCenterTopic(req models.CreateHelpCenter, db *gorm.DB) (models.Hel return helpCnt, nil } -func GetPaginatedTopics(c *gin.Context, db *gorm.DB) ([]HelpCntSummary, postgresql.PaginationResponse, error) { +func GetPaginatedTopics(c *gin.Context, db *gorm.DB) ([]models.HelpCntSummary, postgresql.PaginationResponse, error) { helpCnt := models.HelpCenter{} helpCnts, paginationResponse, err := helpCnt.FetchAllTopics(db, c) @@ -42,12 +35,12 @@ func GetPaginatedTopics(c *gin.Context, db *gorm.DB) ([]HelpCntSummary, postgres } if len(helpCnts) == 0 { - return nil, paginationResponse, gorm.ErrRecordNotFound + return []models.HelpCntSummary{}, paginationResponse, nil } - var topicSummaries []HelpCntSummary + var topicSummaries []models.HelpCntSummary for _, Hlp := range helpCnts { - summary := HelpCntSummary{ + summary := models.HelpCntSummary{ ID: Hlp.ID, Title: Hlp.Title, Content: Hlp.Content, @@ -69,7 +62,7 @@ func FetchTopicByID(db *gorm.DB, id string) (models.HelpCenter, error) { return helpCnt, nil } -func SearchHelpCenterTopics(c *gin.Context, db *gorm.DB, query string) ([]HelpCntSummary, postgresql.PaginationResponse, error) { +func SearchHelpCenterTopics(c *gin.Context, db *gorm.DB, query string) ([]models.HelpCntSummary, postgresql.PaginationResponse, error) { var helpCnt models.HelpCenter topics, paginationResponse, err := helpCnt.SearchHelpCenterTopics(db, c, query) @@ -81,9 +74,9 @@ func SearchHelpCenterTopics(c *gin.Context, db *gorm.DB, query string) ([]HelpCn return nil, paginationResponse, gorm.ErrRecordNotFound } - var topicSummaries []HelpCntSummary + var topicSummaries []models.HelpCntSummary for _, topic := range topics { - summary := HelpCntSummary{ + summary := models.HelpCntSummary{ ID: topic.ID, Title: topic.Title, Content: topic.Content, From 480d66b40b54bc452a8010731e246d6d58e0d9c6 Mon Sep 17 00:00:00 2001 From: Ayotola Ajisola <49092282+Ajinx1@users.noreply.github.com> Date: Sat, 10 Aug 2024 10:32:29 +0100 Subject: [PATCH 2/7] fix: standardized reponse parameter --- internal/models/contact.go | 1 - internal/models/notification.go | 1 - services/contact/contact.go | 2 -- tests/test_contact/create_test.go | 2 -- tests/test_contact/delete_test.go | 1 - tests/test_contact/get_test.go | 4 ---- utility/response.go | 1 - 7 files changed, 12 deletions(-) diff --git a/internal/models/contact.go b/internal/models/contact.go index e6608f11..2fb7bece 100644 --- a/internal/models/contact.go +++ b/internal/models/contact.go @@ -13,7 +13,6 @@ type ContactUs struct { ID string `gorm:"type:uuid;primary_key;" json:"id"` Name string `gorm:"type:varchar(100);not null" json:"name" binding:"required" validate:"required"` Email string `gorm:"type:varchar(100);not null;index" json:"email" binding:"required" validate:"required,email"` - Subject string `gorm:"type:varchar(255);not null" json:"subject" binding:"required" validate:"required"` Message string `gorm:"type:text;not null" json:"message" binding:"required" validate:"required"` CreatedAt time.Time `gorm:"type:timestamp;default:current_timestamp" json:"created_at"` UpdatedAt time.Time `gorm:"type:timestamp;default:current_timestamp" json:"updated_at"` diff --git a/internal/models/notification.go b/internal/models/notification.go index fe137b7c..2917d7ad 100644 --- a/internal/models/notification.go +++ b/internal/models/notification.go @@ -48,7 +48,6 @@ type SendSqueeze struct { type SendContactUsMail struct { Name string `json:"name" validate:"required"` Email string `json:"email" ` - Subject string `son:"subject" validate:"required"` Message string `json:"message" validate:"required"` } diff --git a/services/contact/contact.go b/services/contact/contact.go index 295188a5..93ebb883 100644 --- a/services/contact/contact.go +++ b/services/contact/contact.go @@ -34,7 +34,6 @@ func GetAllContactUs(c *gin.Context, db *gorm.DB) ([]models.ContactUs, *postgres func AddToContactUs(contact *models.ContactUs, db *gorm.DB) error { - contact.Subject = utility.CleanStringInput(contact.Subject) contact.Message = utility.CleanStringInput(contact.Message) if err := contact.CreateContactUs(db); err != nil { @@ -44,7 +43,6 @@ func AddToContactUs(contact *models.ContactUs, db *gorm.DB) error { msgReq := models.ContactUs{ Email: contact.Email, Name: contact.Name, - Subject: contact.Subject, Message: contact.Message, } diff --git a/tests/test_contact/create_test.go b/tests/test_contact/create_test.go index 3b72d1e1..a6b31434 100644 --- a/tests/test_contact/create_test.go +++ b/tests/test_contact/create_test.go @@ -54,7 +54,6 @@ func TestAddToContactUs(t *testing.T) { contactData := models.ContactUs{ Name: "John Doe", Email: "johndoe@example.com", - Subject: "
Inquiry about services3 with html", Message: "I would like to know more about your services3.", } payload, _ := json.Marshal(contactData) @@ -115,7 +114,6 @@ func TestAddToContactUs(t *testing.T) { contactData := models.ContactUs{ Name: "John Doe", Email: "invalid_email", - Subject: "test subject", Message: "message test", } payload, _ := json.Marshal(contactData) diff --git a/tests/test_contact/delete_test.go b/tests/test_contact/delete_test.go index 689d754c..bf7f0c0f 100644 --- a/tests/test_contact/delete_test.go +++ b/tests/test_contact/delete_test.go @@ -40,7 +40,6 @@ func TestDeleteContactUs(t *testing.T) { contact := models.ContactUs{ ID: utility.GenerateUUID(), Email: fmt.Sprintf("contact%v@qa.team", currUUID), - Subject: fmt.Sprintf("Test subject - %v ", currUUID), Message: fmt.Sprintf("Test message - %v ", currUUID), } diff --git a/tests/test_contact/get_test.go b/tests/test_contact/get_test.go index 32a25042..03a4f953 100644 --- a/tests/test_contact/get_test.go +++ b/tests/test_contact/get_test.go @@ -40,13 +40,11 @@ func TestGetAllContactUs(t *testing.T) { contact1 := models.ContactUs{ ID: utility.GenerateUUID(), Email: fmt.Sprintf("contact%v@qa.team", currUUID), - Subject: fmt.Sprintf("First subject - %v ", currUUID), Message: fmt.Sprintf("First test message - %v ", currUUID), } contact2 := models.ContactUs{ ID: utility.GenerateUUID(), Email: fmt.Sprintf("contact2%v@qa.team", currUUID), - Subject: fmt.Sprintf("Second subject - %v ", currUUID), Message: fmt.Sprintf("Second test message - %v ", currUUID), } @@ -149,7 +147,6 @@ func TestGetContactUsById(t *testing.T) { contact := models.ContactUs{ ID: utility.GenerateUUID(), Email: fmt.Sprintf("contact%v@qa.team", currUUID), - Subject: fmt.Sprintf("Test subject - %v ", currUUID), Message: fmt.Sprintf("Test message - %v ", currUUID), } @@ -251,7 +248,6 @@ func TestGetContactUsByEmail(t *testing.T) { contact := models.ContactUs{ ID: utility.GenerateUUID(), Email: fmt.Sprintf("contact%v@qa.team", currUUID), - Subject: fmt.Sprintf("Test subject - %v ", currUUID), Message: fmt.Sprintf("Test message - %v ", currUUID), } diff --git a/utility/response.go b/utility/response.go index 9e24e617..1ee5a5a9 100644 --- a/utility/response.go +++ b/utility/response.go @@ -49,7 +49,6 @@ func ResponseMessage(code int, status string, name string, message string, err i res := Response{ StatusCode: code, Name: name, - Status: status, Message: message, Error: err, Data: data, From c434ee35b4ef10176ebee912b44304ba51648e93 Mon Sep 17 00:00:00 2001 From: Ayotola Ajisola <49092282+Ajinx1@users.noreply.github.com> Date: Sat, 10 Aug 2024 10:47:13 +0100 Subject: [PATCH 3/7] bug-fix:updated test cases to pass --- tests/test_categories/category_test.go | 1 - tests/test_users/get_user_privacy_test.go | 2 -- tests/test_users/get_user_region_test.go | 2 -- tests/test_users/update_user_privacy_test.go | 3 --- 4 files changed, 8 deletions(-) diff --git a/tests/test_categories/category_test.go b/tests/test_categories/category_test.go index c9b68a71..ac503663 100644 --- a/tests/test_categories/category_test.go +++ b/tests/test_categories/category_test.go @@ -73,7 +73,6 @@ func TestGetCategoryNames(t *testing.T) { t.Fatalf("Could not parse response: %v", err) } - assert.Equal(t, "success", response["status"]) assert.Equal(t, float64(200), response["status_code"]) assert.Equal(t, "Categories fetched successfully", response["message"]) diff --git a/tests/test_users/get_user_privacy_test.go b/tests/test_users/get_user_privacy_test.go index 2ebf4188..8c656ac1 100644 --- a/tests/test_users/get_user_privacy_test.go +++ b/tests/test_users/get_user_privacy_test.go @@ -79,7 +79,6 @@ func TestGetUserDataPrivacy(t *testing.T) { tests.AssertStatusCode(t, resp.Code, http.StatusOK) response := tests.ParseResponse(resp) - tests.AssertResponseMessage(t, response["status"].(string), "success") tests.AssertResponseMessage(t, response["message"].(string), "User data privacy settings retrieved successfully") }) @@ -134,7 +133,6 @@ func TestGetUserDataPrivacy(t *testing.T) { tests.AssertStatusCode(t, resp.Code, http.StatusNotFound) response := tests.ParseResponse(resp) - tests.AssertResponseMessage(t, response["status"].(string), "error") tests.AssertResponseMessage(t, response["message"].(string), "user not found") }) } diff --git a/tests/test_users/get_user_region_test.go b/tests/test_users/get_user_region_test.go index 9145a5d0..894f4212 100644 --- a/tests/test_users/get_user_region_test.go +++ b/tests/test_users/get_user_region_test.go @@ -75,7 +75,6 @@ func TestGetUserRegion(t *testing.T) { tests.AssertStatusCode(t, resp.Code, http.StatusOK) response := tests.ParseResponse(resp) - tests.AssertResponseMessage(t, response["status"].(string), "success") tests.AssertResponseMessage(t, response["message"].(string), "User region retrieved successfully") }) @@ -131,7 +130,6 @@ func TestGetUserRegion(t *testing.T) { tests.AssertStatusCode(t, resp.Code, http.StatusNotFound) response := tests.ParseResponse(resp) - tests.AssertResponseMessage(t, response["status"].(string), "error") tests.AssertResponseMessage(t, response["message"].(string), "user not found") }) } diff --git a/tests/test_users/update_user_privacy_test.go b/tests/test_users/update_user_privacy_test.go index 6c864283..f25b7d53 100644 --- a/tests/test_users/update_user_privacy_test.go +++ b/tests/test_users/update_user_privacy_test.go @@ -92,7 +92,6 @@ func TestUpdateUserDataPrivacy(t *testing.T) { tests.AssertStatusCode(t, resp.Code, http.StatusOK) response := tests.ParseResponse(resp) - tests.AssertResponseMessage(t, response["status"].(string), "success") tests.AssertResponseMessage(t, response["message"].(string), "User data privacy settings updated successfully") }) @@ -124,7 +123,6 @@ func TestUpdateUserDataPrivacy(t *testing.T) { tests.AssertStatusCode(t, resp.Code, http.StatusOK) response := tests.ParseResponse(resp) - tests.AssertResponseMessage(t, response["status"].(string), "success") tests.AssertResponseMessage(t, response["message"].(string), "User data privacy settings updated successfully") }) @@ -181,7 +179,6 @@ func TestUpdateUserDataPrivacy(t *testing.T) { tests.AssertStatusCode(t, resp.Code, http.StatusNotFound) response := tests.ParseResponse(resp) - tests.AssertResponseMessage(t, response["status"].(string), "error") tests.AssertResponseMessage(t, response["message"].(string), "user not found") }) } From b63fecd97396ade00a4a62e88191f1afb185d05b Mon Sep 17 00:00:00 2001 From: Cyberguru1 Date: Sat, 10 Aug 2024 05:40:05 -0500 Subject: [PATCH 4/7] chore: email-service-revamp --- services/send/email.go | 69 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/services/send/email.go b/services/send/email.go index 298f53ea..578c8e5f 100644 --- a/services/send/email.go +++ b/services/send/email.go @@ -1,6 +1,7 @@ package send import ( + "crypto/tls" "fmt" "net/smtp" "strings" @@ -113,18 +114,70 @@ func (e *EmailRequest) sendEmailViaSMTP() error { subject := e.Subject recipients := e.To mime := "\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" - body := []byte(subject + mime + e.Body) + body := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\n%s%s%s", sender, recipients[0], subject, mime, e.Body)) - err := smtp.SendMail( + conn, err := tls.Dial( + "tcp", mailConfig.Server+":"+mailConfig.Port, - auth, - sender, - recipients, - body, - ) + &tls.Config{ + InsecureSkipVerify: true, + ServerName: mailConfig.Server, + }) + + if err != nil { + + return fmt.Errorf("failed to connect to the server: %v", err) + + } + + defer conn.Close() + + client, err := smtp.NewClient(conn, mailConfig.Server) if err != nil { - return fmt.Errorf("error connecting to SMTP server: %w", err) + + return fmt.Errorf("failed to create SMTP client: %v", err) + } + + defer client.Quit() + + if err = client.Auth(auth); err != nil { + return fmt.Errorf("failed to authenticate: %v", err) + + } + + if err = client.Mail(sender); err != nil { + return fmt.Errorf("failed to set the sender: %v", err) + + } + + if err = client.Rcpt(recipients[0]); err != nil { + return fmt.Errorf("failed to set the recipient: %v", err) + + } + + writer, err := client.Data() + if err != nil { + + return fmt.Errorf("failed to write the message: %v", err) + + } + + _, err = writer.Write(body) + if err != nil { + + return fmt.Errorf("failed to send the message: %v", err) + + } + + err = writer.Close() + if err != nil { + + return fmt.Errorf("failed to close the writer: %v", err) + + } + return nil + } From 99d83e3f9e5966c3a7c39e5f58e9153f5f9ce7c7 Mon Sep 17 00:00:00 2001 From: Ebite God'sgift Uloamaka Date: Sat, 10 Aug 2024 12:02:24 +0100 Subject: [PATCH 5/7] fix: removed help center struct --- services/helpcenter/helpcenter.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/helpcenter/helpcenter.go b/services/helpcenter/helpcenter.go index faff4905..0d890cc0 100644 --- a/services/helpcenter/helpcenter.go +++ b/services/helpcenter/helpcenter.go @@ -1,4 +1,4 @@ -package service +package helpcenter import ( @@ -28,18 +28,18 @@ func CreateHelpCenterTopic(req models.CreateHelpCenter, db *gorm.DB) (models.Hel func GetPaginatedTopics(c *gin.Context, db *gorm.DB) ([]models.HelpCntSummary, postgresql.PaginationResponse, error) { helpCnt := models.HelpCenter{} - helpCnts, paginationResponse, err := helpCnt.FetchAllTopics(db, c) + topics, paginationResponse, err := helpCnt.FetchAllTopics(db, c) if err != nil { return nil, paginationResponse, err } - if len(helpCnts) == 0 { + if len(topics) == 0 { return []models.HelpCntSummary{}, paginationResponse, nil } var topicSummaries []models.HelpCntSummary - for _, Hlp := range helpCnts { + for _, Hlp := range topics { summary := models.HelpCntSummary{ ID: Hlp.ID, Title: Hlp.Title, @@ -71,7 +71,7 @@ func SearchHelpCenterTopics(c *gin.Context, db *gorm.DB, query string) ([]models } if len(topics) == 0 { - return nil, paginationResponse, gorm.ErrRecordNotFound + return []models.HelpCntSummary{}, paginationResponse, nil } var topicSummaries []models.HelpCntSummary From a881b1695542ce2ee69b17f6cd11dc18d0669c86 Mon Sep 17 00:00:00 2001 From: Cyberguru1 Date: Sat, 10 Aug 2024 06:29:43 -0500 Subject: [PATCH 6/7] chore: fix insecure check --- services/send/email.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/send/email.go b/services/send/email.go index 578c8e5f..c9f3bc9b 100644 --- a/services/send/email.go +++ b/services/send/email.go @@ -120,7 +120,7 @@ func (e *EmailRequest) sendEmailViaSMTP() error { "tcp", mailConfig.Server+":"+mailConfig.Port, &tls.Config{ - InsecureSkipVerify: true, + InsecureSkipVerify: false, ServerName: mailConfig.Server, }) From db9a201f6cdad3c0388ae1cf743bc0d086d19e70 Mon Sep 17 00:00:00 2001 From: Cyberguru1 Date: Sat, 10 Aug 2024 06:44:53 -0500 Subject: [PATCH 7/7] chore: change sender email address --- services/send/email.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/send/email.go b/services/send/email.go index c9f3bc9b..2da3519b 100644 --- a/services/send/email.go +++ b/services/send/email.go @@ -112,9 +112,10 @@ func (e *EmailRequest) sendEmailViaSMTP() error { sender := mailConfig.Username subject := e.Subject + From := "telex@hng.email" recipients := e.To mime := "\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" - body := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\n%s%s%s", sender, recipients[0], subject, mime, e.Body)) + body := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\n%s%s%s", From, recipients[0], subject, mime, e.Body)) conn, err := tls.Dial( "tcp",