From f8292c300f8401b1e08e4365ce36e052a7cf9a0f Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Wed, 14 Feb 2024 17:42:21 -0800 Subject: [PATCH] working signup wizard. --- backend/pkg/models/user.go | 5 +++ backend/pkg/utils/newsletter.go | 45 +++++++++++++++++++ backend/pkg/web/handler/auth.go | 15 +++++-- .../auth-signup-wizard.component.html | 14 +++--- .../auth-signup-wizard.component.ts | 7 +-- 5 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 backend/pkg/utils/newsletter.go diff --git a/backend/pkg/models/user.go b/backend/pkg/models/user.go index b1493957..f967c704 100644 --- a/backend/pkg/models/user.go +++ b/backend/pkg/models/user.go @@ -6,6 +6,11 @@ import ( "strings" ) +type UserWizard struct { + *User `json:",inline"` + JoinMailingList bool `json:"join_mailing_list"` +} + type User struct { ModelBase FullName string `json:"full_name"` diff --git a/backend/pkg/utils/newsletter.go b/backend/pkg/utils/newsletter.go new file mode 100644 index 00000000..9d0de743 --- /dev/null +++ b/backend/pkg/utils/newsletter.go @@ -0,0 +1,45 @@ +package utils + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "log" + "net/http" +) + +func JoinNewsletter(name string, email string, message string, wantsToChat string) error { + mailingListRequestData := map[string]string{ + "email": email, + "name": name, + "message": message, + "11_chat": wantsToChat, + } + + payloadBytes, err := json.Marshal(mailingListRequestData) + if err != nil { + return fmt.Errorf("an error occurred while marshalling join newsletter request: %w", err) + } + + req, err := http.NewRequest(http.MethodPost, "https://api.platform.fastenhealth.com/v1/newsletter/join", bytes.NewBuffer(payloadBytes)) + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + + newsletterResp, err := http.DefaultClient.Do(req) + + if err != nil { + return fmt.Errorf("an error occurred while sending newsletter join request: %w", err) + } + defer newsletterResp.Body.Close() + if newsletterResp.StatusCode >= 300 || newsletterResp.StatusCode < 200 { + b, err := io.ReadAll(newsletterResp.Body) + if err == nil { + log.Printf("Error Response body: %s", string(b)) + } + return fmt.Errorf("an error occurred while attempting to join newsletter: %d", newsletterResp.StatusCode) + + } + return nil +} diff --git a/backend/pkg/web/handler/auth.go b/backend/pkg/web/handler/auth.go index 74880d16..982b4f11 100644 --- a/backend/pkg/web/handler/auth.go +++ b/backend/pkg/web/handler/auth.go @@ -7,6 +7,7 @@ import ( "github.com/fastenhealth/fasten-onprem/backend/pkg/config" "github.com/fastenhealth/fasten-onprem/backend/pkg/database" "github.com/fastenhealth/fasten-onprem/backend/pkg/models" + "github.com/fastenhealth/fasten-onprem/backend/pkg/utils" "github.com/gin-gonic/gin" "net/http" ) @@ -15,19 +16,25 @@ func AuthSignup(c *gin.Context) { databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository) appConfig := c.MustGet(pkg.ContextKeyTypeConfig).(config.Interface) - var user models.User - if err := c.ShouldBindJSON(&user); err != nil { + var userWizard models.UserWizard + if err := c.ShouldBindJSON(&userWizard); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()}) return } - err := databaseRepo.CreateUser(c, &user) + err := databaseRepo.CreateUser(c, userWizard.User) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()}) return } //TODO: we can derive the encryption key and the hash'ed user from the responseData sub. For now the Sub will be the user id prepended with hello. - userFastenToken, err := auth.JwtGenerateFastenTokenFromUser(user, appConfig.GetString("jwt.issuer.key")) + userFastenToken, err := auth.JwtGenerateFastenTokenFromUser(*userWizard.User, appConfig.GetString("jwt.issuer.key")) + + //check if the user wants to join the mailing list + if userWizard.JoinMailingList { + //ignore error messages, we don't want to block the user from signing up + utils.JoinNewsletter(userWizard.FullName, userWizard.Email, "", "") + } c.JSON(http.StatusOK, gin.H{"success": true, "data": userFastenToken}) } diff --git a/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.html b/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.html index eb11034c..3e80ab3b 100644 --- a/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.html +++ b/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.html @@ -69,14 +69,14 @@
- + -
-
+
+
You must agree to the Privacy Policy and Terms of Use.
@@ -84,22 +84,20 @@
- +
+ +
-
diff --git a/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.ts b/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.ts index ee705aef..ea5eebde 100644 --- a/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.ts +++ b/frontend/src/app/pages/auth-signup-wizard/auth-signup-wizard.component.ts @@ -7,9 +7,9 @@ import {ToastService} from '../../services/toast.service'; import {ToastNotification, ToastType} from '../../models/fasten/toast'; class UserWizard extends User { - passwordConfirm: string = "" - agreeTerms: boolean = false - joinMailingList: boolean = true + password_confirm: string = "" + agree_terms: boolean = false + join_mailing_list: boolean = true } @@ -126,6 +126,7 @@ export class AuthSignupWizardComponent implements OnInit { this.loading = true this.submitted = true; + console.log("starting signup process...",this.newUser) this.authService.Signup(this.newUser).then((tokenResp: any) => { this.loading = false console.log(tokenResp);