implement contact us

This commit is contained in:
2024-10-24 10:15:45 -05:00
parent 13568d00b5
commit b112c726bb
7 changed files with 99 additions and 2 deletions

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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")

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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
}