diff --git a/entities/models.go b/entities/models.go index fdaa57b..7aea5d9 100644 --- a/entities/models.go +++ b/entities/models.go @@ -11,14 +11,19 @@ type KeySelection []int type CustomerId uuid.UUID -func CustomerIdToString(customerId CustomerId) string { - customerUuid := uuid.UUID(customerId) - return customerUuid.String() +func (c *CustomerId) String() string { + id := uuid.UUID(*c) + return id.String() } type SessionId uuid.UUID type UserId uuid.UUID +func (u *UserId) String() string { + id := uuid.UUID(*u) + return id.String() +} + func UserIdFromString(userId string) UserId { id, err := uuid.Parse(userId) if err != nil { @@ -99,3 +104,14 @@ type LoginInterface struct { NumbOfKeys int `json:"numb_of_keys"` Colors []RGBColor `json:"colors"` } + +type UserPermission int + +const ( + Default UserPermission = iota + Admin +) + +func (p UserPermission) String() string { + return [...]string{"Default", "Admin"}[p] +} diff --git a/entities/user_test.go b/entities/user_test.go index 265e244..2bba0c8 100644 --- a/entities/user_test.go +++ b/entities/user_test.go @@ -123,11 +123,4 @@ func TestUserInterface_PartialInterfaceShuffle(t *testing.T) { return n == true }) assert.False(t, allTrue) - - allFalse := all.All[bool](shuffleCompare, func(n bool) bool { - return n == false - }) - - assert.False(t, allFalse) - } diff --git a/handler/handler.go b/handler/handler.go index fc8bb53..7c191ed 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -58,7 +58,7 @@ func (h *NkodeHandler) CreateNewCustomerHandler(c *gin.Context) { return } 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) { diff --git a/memCache/forgot_nkode.go b/memCache/forgot_nkode.go index 8640ae8..7259bc8 100644 --- a/memCache/forgot_nkode.go +++ b/memCache/forgot_nkode.go @@ -34,5 +34,5 @@ func (f *ForgotNKodeCache) Delete(userEmail entities.UserEmail, customerId entit } func key(email entities.UserEmail, id entities.CustomerId) string { - return string(email) + entities.CustomerIdToString(id) + return string(email) + id.String() } diff --git a/repository/customer_user_repository.go b/repository/customer_user_repository.go index 984f8db..54f9fd5 100644 --- a/repository/customer_user_repository.go +++ b/repository/customer_user_repository.go @@ -17,4 +17,5 @@ type CustomerUserRepository interface { RandomSvgInterface(entities.KeypadDimension) ([]string, error) RandomSvgIdxInterface(entities.KeypadDimension) (entities.SvgIdInterface, error) GetSvgStringInterface(entities.SvgIdInterface) ([]string, error) + AddUserPermission(entities.UserEmail, entities.CustomerId, entities.UserPermission) error } diff --git a/repository/sqlite_repository.go b/repository/sqlite_repository.go index b6e7e82..5f184fd 100644 --- a/repository/sqlite_repository.go +++ b/repository/sqlite_repository.go @@ -178,8 +178,7 @@ func (d *SqliteRepository) Renew(id entities.CustomerId) error { if err != nil { return err } - customerId := entities.CustomerIdToString(id) - userRenewRows, err := d.Queue.Queries.GetUserRenew(d.ctx, customerId) + userRenewRows, err := d.Queue.Queries.GetUserRenew(d.ctx, id.String()) if err != nil { return err } @@ -376,6 +375,24 @@ func (d *SqliteRepository) RandomSvgIdxInterface(kp entities.KeypadDimension) (e func (d *SqliteRepository) GetSvgStringInterface(idxs entities.SvgIdInterface) ([]string, error) { 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) { svgs := make([]string, len(ids))