Skip to content

Commit

Permalink
Add functionality to write and read metadata
Browse files Browse the repository at this point in the history
Signed-off-by: William <[email protected]>
  • Loading branch information
kwesidev committed Dec 15, 2023
1 parent b725c66 commit 8932623
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
35 changes: 35 additions & 0 deletions internal/models/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package models

import (
"database/sql/driver"
"encoding/json"
"errors"
)

// JSONB Interface
type JSONB map[string]interface{}

// Value Marshal
func (j JSONB) Value() (driver.Value, error) {
return json.Marshal(j)
}

// Scan Unmarshal
func (j *JSONB) Scan(value interface{}) error {
source, ok := value.([]byte)
if !ok {
return errors.New("Type assertion .([]byte) failed.")
}

var i interface{}
err := json.Unmarshal(source, &i)
if err != nil {
return err
}

*j, ok = i.(map[string]interface{})
if !ok {
return errors.New("Type assertion .(map[string]interface{}) failed.")
}
return nil
}
2 changes: 2 additions & 0 deletions internal/models/user_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type User struct {
TwoFactorMethod string `json:"twoFactorMethod"`
TOTPSecret string `json:"-"`
TOTPURL string `json:"-"`
Metadata JSONB `json:"metadata"`
}
type EnableTwoFactorRequest struct {
Type string `json:"type"`
Expand All @@ -39,4 +40,5 @@ type UserUpdateRequest struct {
LastName string `json:"lastName"`
CellNumber string `json:"cellNumber"`
AllowTwoFactorAuthentication bool `json:"allowTwoFactorAuthentication"`
Metadata JSONB `json:"metadata"`
}
13 changes: 10 additions & 3 deletions internal/services/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func (usrSrv *UserService) Get(userId int) *models.User {
users.two_factor_enabled,
users.two_factor_method,
users.totp_secret ,
users.totp_url
users.totp_url,
users.meta_data
FROM
users
WHERE
Expand All @@ -87,7 +88,7 @@ func (usrSrv *UserService) Get(userId int) *models.User {
// Inject the data into the struct
err := row.Scan(&userDetails.ID, &userDetails.UUID, &userDetails.Username, &userDetails.FirstName,
&userDetails.LastName, &userDetails.EmailAddress, &userDetails.CellNumber, &userDetails.Active, &userDetails.TwoFactorEnabled,
&userDetails.TwoFactorMethod, &userDetails.TOTPSecret, &userDetails.TOTPURL,
&userDetails.TwoFactorMethod, &userDetails.TOTPSecret, &userDetails.TOTPURL, &userDetails.Metadata,
)
roles, _ := usrSrv.GetRoles(userDetails.ID)
userDetails.Roles = roles
Expand Down Expand Up @@ -203,10 +204,16 @@ func (usrSrv *UserService) Update(userId int, userUpdateRequest models.UserUpdat
}
// Update cell number
if strings.Trim(userUpdateRequest.CellNumber, "") != "" {
query += fmt.Sprintf("cell_number =$%d,", argCount)
query += fmt.Sprintf("cell_number =$%d, ", argCount)
args = append(args, userUpdateRequest.CellNumber)
argCount++
}

if userUpdateRequest.Metadata != nil {
query += fmt.Sprintf("meta_data = $%d, ", argCount)
args = append(args, userUpdateRequest.Metadata)
argCount++
}
// Remove the trailing comma and space
query = query[:len(query)-2]
query += fmt.Sprintf(" WHERE id = $%d", argCount)
Expand Down

0 comments on commit 8932623

Please sign in to comment.