implement user_permission

This commit is contained in:
2025-02-01 03:04:52 -06:00
parent 72414bc8fb
commit d58c69cb08
6 changed files with 41 additions and 14 deletions

View File

@@ -11,14 +11,19 @@ type KeySelection []int
type CustomerId uuid.UUID type CustomerId uuid.UUID
func CustomerIdToString(customerId CustomerId) string { func (c *CustomerId) String() string {
customerUuid := uuid.UUID(customerId) id := uuid.UUID(*c)
return customerUuid.String() return id.String()
} }
type SessionId uuid.UUID type SessionId uuid.UUID
type UserId uuid.UUID type UserId uuid.UUID
func (u *UserId) String() string {
id := uuid.UUID(*u)
return id.String()
}
func UserIdFromString(userId string) UserId { func UserIdFromString(userId string) UserId {
id, err := uuid.Parse(userId) id, err := uuid.Parse(userId)
if err != nil { if err != nil {
@@ -99,3 +104,14 @@ type LoginInterface struct {
NumbOfKeys int `json:"numb_of_keys"` NumbOfKeys int `json:"numb_of_keys"`
Colors []RGBColor `json:"colors"` Colors []RGBColor `json:"colors"`
} }
type UserPermission int
const (
Default UserPermission = iota
Admin
)
func (p UserPermission) String() string {
return [...]string{"Default", "Admin"}[p]
}

View File

@@ -123,11 +123,4 @@ func TestUserInterface_PartialInterfaceShuffle(t *testing.T) {
return n == true return n == true
}) })
assert.False(t, allTrue) assert.False(t, allTrue)
allFalse := all.All[bool](shuffleCompare, func(n bool) bool {
return n == false
})
assert.False(t, allFalse)
} }

View File

@@ -58,7 +58,7 @@ func (h *NkodeHandler) CreateNewCustomerHandler(c *gin.Context) {
return return
} }
h.Logger.Println("create new customer") h.Logger.Println("create new customer")
c.JSON(200, gin.H{"customer_id": entities.CustomerIdToString(*customerId)}) c.JSON(200, gin.H{"customer_id": customerId.String()})
} }
func (h *NkodeHandler) SignupHandler(c *gin.Context) { func (h *NkodeHandler) SignupHandler(c *gin.Context) {

View File

@@ -34,5 +34,5 @@ func (f *ForgotNKodeCache) Delete(userEmail entities.UserEmail, customerId entit
} }
func key(email entities.UserEmail, id entities.CustomerId) string { func key(email entities.UserEmail, id entities.CustomerId) string {
return string(email) + entities.CustomerIdToString(id) return string(email) + id.String()
} }

View File

@@ -17,4 +17,5 @@ type CustomerUserRepository interface {
RandomSvgInterface(entities.KeypadDimension) ([]string, error) RandomSvgInterface(entities.KeypadDimension) ([]string, error)
RandomSvgIdxInterface(entities.KeypadDimension) (entities.SvgIdInterface, error) RandomSvgIdxInterface(entities.KeypadDimension) (entities.SvgIdInterface, error)
GetSvgStringInterface(entities.SvgIdInterface) ([]string, error) GetSvgStringInterface(entities.SvgIdInterface) ([]string, error)
AddUserPermission(entities.UserEmail, entities.CustomerId, entities.UserPermission) error
} }

View File

@@ -178,8 +178,7 @@ func (d *SqliteRepository) Renew(id entities.CustomerId) error {
if err != nil { if err != nil {
return err return err
} }
customerId := entities.CustomerIdToString(id) userRenewRows, err := d.Queue.Queries.GetUserRenew(d.ctx, id.String())
userRenewRows, err := d.Queue.Queries.GetUserRenew(d.ctx, customerId)
if err != nil { if err != nil {
return err return err
} }
@@ -376,6 +375,24 @@ func (d *SqliteRepository) RandomSvgIdxInterface(kp entities.KeypadDimension) (e
func (d *SqliteRepository) GetSvgStringInterface(idxs entities.SvgIdInterface) ([]string, error) { func (d *SqliteRepository) GetSvgStringInterface(idxs entities.SvgIdInterface) ([]string, error) {
return d.getSvgsById(idxs) return d.getSvgsById(idxs)
} }
func (d *SqliteRepository) AddUserPermission(userEmail entities.UserEmail, customerId entities.CustomerId, permission entities.UserPermission) error {
user, err := d.GetUser(userEmail, customerId)
if err != nil {
return err
}
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
params, ok := args.(sqlc.AddUserPermissionParams)
if !ok {
return fmt.Errorf("invalid argument type: expected AddUserPermissionParams")
}
return q.AddUserPermission(ctx, params)
}
params := sqlc.AddUserPermissionParams{
UserID: user.Id.String(),
Permission: permission.String(),
}
return d.Queue.EnqueueWriteTx(queryFunc, params)
}
func (d *SqliteRepository) getSvgsById(ids []int) ([]string, error) { func (d *SqliteRepository) getSvgsById(ids []int) ([]string, error) {
svgs := make([]string, len(ids)) svgs := make([]string, len(ids))