From b112c726bbeeedfb966f86351053e1f42be7a48d Mon Sep 17 00:00:00 2001 From: Donovan Date: Thu, 24 Oct 2024 10:15:45 -0500 Subject: [PATCH] implement contact us --- core/in_memory_db.go | 4 ++++ core/nkode_api.go | 15 ++++++++++-- core/nkode_handler.go | 42 +++++++++++++++++++++++++++++++++ core/sqlite-init/sqlite_init.go | 11 +++++++++ core/sqlite_db.go | 20 ++++++++++++++++ core/type.go | 8 +++++++ main.go | 1 + 7 files changed, 99 insertions(+), 2 deletions(-) diff --git a/core/in_memory_db.go b/core/in_memory_db.go index 6d1c8f9..486c7de 100644 --- a/core/in_memory_db.go +++ b/core/in_memory_db.go @@ -131,6 +131,10 @@ func (db *InMemoryDb) GetSvgStringInterface(idxs SvgIdInterface) ([]string, erro return make([]string, len(idxs)), nil } +func (db *InMemoryDb) WriteNewContactUsForm(userId UserId, form ContactFormPost) error { + return nil +} + func userIdKey(customerId CustomerId, username UserEmail) string { key := fmt.Sprintf("%s:%s", customerId, username) return key diff --git a/core/nkode_api.go b/core/nkode_api.go index faa1363..81fdfac 100644 --- a/core/nkode_api.go +++ b/core/nkode_api.go @@ -122,8 +122,7 @@ func (n *NKodeAPI) ConfirmNKode(customerId CustomerId, sessionId SessionId, keyS if err != nil { return err } - err = customer.IsValidNKode(userSession.Kp, passcode) - if err != nil { + if err = customer.IsValidNKode(userSession.Kp, passcode); err != nil { return err } user, err := NewUser(*customer, string(userSession.UserEmail), passcode, userSession.LoginUserInterface, userSession.Kp) @@ -264,3 +263,15 @@ func (n *NKodeAPI) ResetNKode(userEmail UserEmail, customerId CustomerId) error n.EmailQueue.AddEmail(email) return nil } + +func (n *NKodeAPI) ContactUs(userEmail UserEmail, customerId CustomerId, form ContactFormPost) error { + user, err := n.Db.GetUser(userEmail, customerId) + if err != nil { + return err + } + + if user == nil { + return ErrUserForCustomerDNE + } + return n.Db.WriteNewContactUsForm(user.Id, form) +} diff --git a/core/nkode_handler.go b/core/nkode_handler.go index 8119d9c..1f01330 100644 --- a/core/nkode_handler.go +++ b/core/nkode_handler.go @@ -24,6 +24,7 @@ const ( RandomSvgInterface = "/random-svg-interface" RefreshToken = "/refresh-token" ResetNKode = "/reset-nkode" + ContactUs = "/contact-us" ) const ( @@ -55,6 +56,9 @@ func (h *NKodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.RefreshTokenHandler(w, r) case ResetNKode: h.ResetNKode(w, r) + + case ContactUs: + h.ContactUs(w, r) default: w.WriteHeader(http.StatusNotFound) _, err := w.Write([]byte("404 not found")) @@ -237,6 +241,7 @@ func (h *NKodeHandler) LoginHandler(w http.ResponseWriter, r *http.Request) { } func (h *NKodeHandler) RenewAttributesHandler(w http.ResponseWriter, r *http.Request) { + println("renew attributes") if r.Method != http.MethodPost { methodNotAllowed(w) return @@ -278,6 +283,7 @@ func (h *NKodeHandler) RandomSvgInterfaceHandler(w http.ResponseWriter, r *http. } func (h *NKodeHandler) RefreshTokenHandler(w http.ResponseWriter, r *http.Request) { + println("refresh tokens") if r.Method != http.MethodGet { methodNotAllowed(w) } @@ -309,6 +315,7 @@ func (h *NKodeHandler) RefreshTokenHandler(w http.ResponseWriter, r *http.Reques } func (h *NKodeHandler) ResetNKode(w http.ResponseWriter, r *http.Request) { + println("reset nkode") if r.Method != http.MethodPost { methodNotAllowed(w) } @@ -337,6 +344,41 @@ func (h *NKodeHandler) ResetNKode(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } +func (h *NKodeHandler) ContactUs(w http.ResponseWriter, r *http.Request) { + println("contact us") + if r.Method != http.MethodPost { + methodNotAllowed(w) + } + accessToken, err := getBearerToken(r) + if err != nil { + forbidden(w) + return + } + refreshClaims, err := ParseRegisteredClaimToken(accessToken) + customerId, err := uuid.Parse(refreshClaims.Issuer) + if err != nil { + badRequest(w, malformedCustomerId) + return + } + userEmail, err := ParseEmail(refreshClaims.Subject) + if err != nil { + badRequest(w, malformedUserEmail) + log.Println(err) + return + } + + var contactFormPost ContactFormPost + if err := decodeJson(w, r, &contactFormPost); err != nil { + return + } + + if err := h.Api.ContactUs(userEmail, CustomerId(customerId), contactFormPost); err != nil { + handleError(w, err) + log.Println(err) + return + } +} + func decodeJson(w http.ResponseWriter, r *http.Request, post any) error { if r.Body == nil { badRequest(w, "unable to parse body") diff --git a/core/sqlite-init/sqlite_init.go b/core/sqlite-init/sqlite_init.go index ebd4803..216471b 100644 --- a/core/sqlite-init/sqlite_init.go +++ b/core/sqlite-init/sqlite_init.go @@ -249,6 +249,17 @@ CREATE TABLE IF NOT EXISTS svg_icon ( id INTEGER PRIMARY KEY AUTOINCREMENT ,svg TEXT NOT NULL ); + +CREATE TABLE IF NOT EXISTS contact_us ( + id INTEGER PRIMARY KEY AUTOINCREMENT + ,name TEXT NOT NULL + ,message TEXT NOT NULL + ,company_name TEXT NOT NULL + ,alternate_email TEXT NOT NULL + ,user_id TEXT NOT NULL + ,created_at TEXT NOT NULL + ,FOREIGN KEY (user_id) REFERENCES user(id) +) ` _, err = db.Exec(createTable) if err != nil { diff --git a/core/sqlite_db.go b/core/sqlite_db.go index 969911a..7723fc9 100644 --- a/core/sqlite_db.go +++ b/core/sqlite_db.go @@ -452,6 +452,26 @@ func (d *SqliteDB) GetSvgStringInterface(idxs SvgIdInterface) ([]string, error) return d.getSvgsById(idxs) } +func (d *SqliteDB) WriteNewContactUsForm(userId UserId, form ContactFormPost) error { + query := ` +INSERT INTO contact_us ( + name + ,message + ,company_name + ,alternate_email + ,user_id + ,created_at +) +VALUES (?,?,?,?,?,?) +` + args := []any{ + form.Name, form.Message, form.CompanyName, form.AlternateEmail, uuid.UUID(userId), timeStamp(), + } + + return d.addWriteTx(query, args) + return nil +} + func (d *SqliteDB) getSvgsById(ids []int) ([]string, error) { tx, err := d.db.Begin() if err != nil { diff --git a/core/type.go b/core/type.go index 5f20d22..05d1b62 100644 --- a/core/type.go +++ b/core/type.go @@ -68,6 +68,13 @@ type ResetNKodePost struct { CustomerId string `json:"customer_id"` } +type ContactFormPost struct { + Name string `json:"name"` + AlternateEmail string `json:"alternate_email"` + CompanyName string `json:"company_name"` + Message string `json:"message"` +} + type CreateNewCustomerResp struct { CustomerId string `json:"customer_id"` } @@ -151,4 +158,5 @@ type DbAccessor interface { RandomSvgInterface(KeypadDimension) ([]string, error) RandomSvgIdxInterface(KeypadDimension) (SvgIdInterface, error) GetSvgStringInterface(SvgIdInterface) ([]string, error) + WriteNewContactUsForm(userId UserId, form ContactFormPost) error } diff --git a/main.go b/main.go index 3cd15cf..0f0d878 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func main() { mux.Handle(core.RandomSvgInterface, &handler) mux.Handle(core.RefreshToken, &handler) mux.Handle(core.ResetNKode, &handler) + mux.Handle(core.ContactUs, &handler) fmt.Println("Running on localhost:8080...") log.Fatal(http.ListenAndServe("localhost:8080", corsMiddleware(mux))) }