|
|
|
|
@@ -21,7 +21,7 @@ type SqliteRepository struct {
|
|
|
|
|
ctx context.Context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSqliteRepository(queue *sqlc.Queue, ctx context.Context) SqliteRepository {
|
|
|
|
|
func NewSqliteRepository(ctx context.Context, queue *sqlc.Queue) SqliteRepository {
|
|
|
|
|
return SqliteRepository{
|
|
|
|
|
Queue: queue,
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
@@ -29,27 +29,27 @@ func NewSqliteRepository(queue *sqlc.Queue, ctx context.Context) SqliteRepositor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) CreateCustomer(c entities.Customer) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.CreateCustomerParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected CreateCustomerParams")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected CreateCustomerParams")
|
|
|
|
|
}
|
|
|
|
|
return q.CreateCustomer(ctx, params)
|
|
|
|
|
return nil, q.CreateCustomer(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.Queue.EnqueueWriteTx(queryFunc, c.ToSqlcCreateCustomerParams())
|
|
|
|
|
_, err := d.Queue.EnqueueWriteTx(queryFunc, c.ToSqlcCreateCustomerParams())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) WriteNewUser(u entities.User) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.CreateUserParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected CreateUserParams")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected CreateUserParams")
|
|
|
|
|
}
|
|
|
|
|
return q.CreateUser(ctx, params)
|
|
|
|
|
return nil, q.CreateUser(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
// Use the wrapped function in EnqueueWriteTx
|
|
|
|
|
|
|
|
|
|
renew := 0
|
|
|
|
|
if u.Renew {
|
|
|
|
|
renew = 1
|
|
|
|
|
@@ -75,16 +75,17 @@ func (d *SqliteRepository) WriteNewUser(u entities.User) error {
|
|
|
|
|
SvgIDInterface: security.IntArrToByteArr(u.Interface.SvgId),
|
|
|
|
|
CreatedAt: sql.NullString{String: utils.TimeStamp(), Valid: true},
|
|
|
|
|
}
|
|
|
|
|
return d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
_, err := d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) UpdateUserNKode(u entities.User) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.UpdateUserParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected UpdateUserParams")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected UpdateUserParams")
|
|
|
|
|
}
|
|
|
|
|
return q.UpdateUser(ctx, params)
|
|
|
|
|
return nil, q.UpdateUser(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
// Use the wrapped function in EnqueueWriteTx
|
|
|
|
|
renew := 0
|
|
|
|
|
@@ -109,33 +110,34 @@ func (d *SqliteRepository) UpdateUserNKode(u entities.User) error {
|
|
|
|
|
IdxInterface: security.IntArrToByteArr(u.Interface.IdxInterface),
|
|
|
|
|
SvgIDInterface: security.IntArrToByteArr(u.Interface.SvgId),
|
|
|
|
|
}
|
|
|
|
|
return d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
_, err := d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) UpdateUserInterface(id models.UserId, ui entities.UserInterface) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.UpdateUserInterfaceParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected UpdateUserInterfaceParams")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected UpdateUserInterfaceParams")
|
|
|
|
|
}
|
|
|
|
|
return q.UpdateUserInterface(ctx, params)
|
|
|
|
|
return nil, q.UpdateUserInterface(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.UpdateUserInterfaceParams{
|
|
|
|
|
IdxInterface: security.IntArrToByteArr(ui.IdxInterface),
|
|
|
|
|
LastLogin: utils.TimeStamp(),
|
|
|
|
|
ID: uuid.UUID(id).String(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
_, err := d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) UpdateUserRefreshToken(id models.UserId, refreshToken string) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.UpdateUserRefreshTokenParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected UpdateUserRefreshToken")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected UpdateUserRefreshToken")
|
|
|
|
|
}
|
|
|
|
|
return q.UpdateUserRefreshToken(ctx, params)
|
|
|
|
|
return nil, q.UpdateUserRefreshToken(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.UpdateUserRefreshTokenParams{
|
|
|
|
|
RefreshToken: sql.NullString{
|
|
|
|
|
@@ -144,21 +146,23 @@ func (d *SqliteRepository) UpdateUserRefreshToken(id models.UserId, refreshToken
|
|
|
|
|
},
|
|
|
|
|
ID: uuid.UUID(id).String(),
|
|
|
|
|
}
|
|
|
|
|
return d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
_, err := d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) RenewCustomer(renewParams sqlc.RenewCustomerParams) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.RenewCustomerParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected RenewCustomerParams")
|
|
|
|
|
}
|
|
|
|
|
return q.RenewCustomer(ctx, params)
|
|
|
|
|
return nil, q.RenewCustomer(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
return d.Queue.EnqueueWriteTx(queryFunc, renewParams)
|
|
|
|
|
_, err := d.Queue.EnqueueWriteTx(queryFunc, renewParams)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) Renew(id models.CustomerId) error {
|
|
|
|
|
func (d *SqliteRepository) Renew(id models.CustomerID) error {
|
|
|
|
|
setXor, attrXor, err := d.renewCustomer(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
@@ -168,19 +172,17 @@ func (d *SqliteRepository) Renew(id models.CustomerId) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.RenewUserParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected RenewUserParams")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected RenewUserParams")
|
|
|
|
|
}
|
|
|
|
|
return q.RenewUser(ctx, params)
|
|
|
|
|
return nil, q.RenewUser(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, row := range userRenewRows {
|
|
|
|
|
user := entities.User{
|
|
|
|
|
Id: models.UserIdFromString(row.ID),
|
|
|
|
|
CustomerId: models.CustomerId{},
|
|
|
|
|
CustomerId: models.CustomerID{},
|
|
|
|
|
Email: "",
|
|
|
|
|
EncipheredPasscode: models.EncipheredNKode{},
|
|
|
|
|
Kp: entities.KeypadDimension{
|
|
|
|
|
@@ -194,7 +196,6 @@ func (d *SqliteRepository) Renew(id models.CustomerId) error {
|
|
|
|
|
Interface: entities.UserInterface{},
|
|
|
|
|
Renew: false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = user.RenewKeys(setXor, attrXor); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
@@ -204,14 +205,14 @@ func (d *SqliteRepository) Renew(id models.CustomerId) error {
|
|
|
|
|
Renew: 1,
|
|
|
|
|
ID: uuid.UUID(user.Id).String(),
|
|
|
|
|
}
|
|
|
|
|
if err = d.Queue.EnqueueWriteTx(queryFunc, params); err != nil {
|
|
|
|
|
if _, err = d.Queue.EnqueueWriteTx(queryFunc, params); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) renewCustomer(id models.CustomerId) ([]uint64, []uint64, error) {
|
|
|
|
|
func (d *SqliteRepository) renewCustomer(id models.CustomerID) ([]uint64, []uint64, error) {
|
|
|
|
|
customer, err := d.GetCustomer(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
@@ -220,21 +221,19 @@ func (d *SqliteRepository) renewCustomer(id models.CustomerId) ([]uint64, []uint
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.RenewCustomerParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected RenewCustomerParams")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected RenewCustomerParams")
|
|
|
|
|
}
|
|
|
|
|
return q.RenewCustomer(ctx, params)
|
|
|
|
|
return nil, q.RenewCustomer(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.RenewCustomerParams{
|
|
|
|
|
AttributeValues: security.Uint64ArrToByteArr(customer.Attributes.AttrVals),
|
|
|
|
|
SetValues: security.Uint64ArrToByteArr(customer.Attributes.SetVals),
|
|
|
|
|
ID: uuid.UUID(customer.Id).String(),
|
|
|
|
|
ID: uuid.UUID(customer.ID).String(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = d.Queue.EnqueueWriteTx(queryFunc, params); err != nil {
|
|
|
|
|
if _, err = d.Queue.EnqueueWriteTx(queryFunc, params); err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
return setXor, attrXor, nil
|
|
|
|
|
@@ -244,12 +243,12 @@ func (d *SqliteRepository) RefreshUserPasscode(user entities.User, passcodeIdx [
|
|
|
|
|
if err := user.RefreshPasscode(passcodeIdx, customerAttr); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(sqlc.RefreshUserPasscodeParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected RefreshUserPasscodeParams")
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected RefreshUserPasscodeParams")
|
|
|
|
|
}
|
|
|
|
|
return q.RefreshUserPasscode(ctx, params)
|
|
|
|
|
return nil, q.RefreshUserPasscode(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.RefreshUserPasscodeParams{
|
|
|
|
|
Renew: 0,
|
|
|
|
|
@@ -262,17 +261,17 @@ func (d *SqliteRepository) RefreshUserPasscode(user entities.User, passcodeIdx [
|
|
|
|
|
Salt: user.CipherKeys.Salt,
|
|
|
|
|
ID: uuid.UUID(user.Id).String(),
|
|
|
|
|
}
|
|
|
|
|
return d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
_, err := d.Queue.EnqueueWriteTx(queryFunc, params)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) GetCustomer(id models.CustomerId) (*entities.Customer, error) {
|
|
|
|
|
func (d *SqliteRepository) GetCustomer(id models.CustomerID) (*entities.Customer, error) {
|
|
|
|
|
customer, err := d.Queue.Queries.GetCustomer(d.ctx, uuid.UUID(id).String())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &entities.Customer{
|
|
|
|
|
Id: id,
|
|
|
|
|
ID: id,
|
|
|
|
|
NKodePolicy: models.NKodePolicy{
|
|
|
|
|
MaxNkodeLen: int(customer.MaxNkodeLen),
|
|
|
|
|
MinNkodeLen: int(customer.MinNkodeLen),
|
|
|
|
|
@@ -285,7 +284,7 @@ func (d *SqliteRepository) GetCustomer(id models.CustomerId) (*entities.Customer
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) GetUser(email models.UserEmail, customerId models.CustomerId) (*entities.User, error) {
|
|
|
|
|
func (d *SqliteRepository) GetUser(email models.UserEmail, customerId models.CustomerID) (*entities.User, error) {
|
|
|
|
|
userRow, err := d.Queue.Queries.GetUser(d.ctx, sqlc.GetUserParams{
|
|
|
|
|
Email: string(email),
|
|
|
|
|
CustomerID: uuid.UUID(customerId).String(),
|
|
|
|
|
@@ -296,12 +295,10 @@ func (d *SqliteRepository) GetUser(email models.UserEmail, customerId models.Cus
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("failed to get user: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kp := entities.KeypadDimension{
|
|
|
|
|
AttrsPerKey: int(userRow.AttributesPerKey),
|
|
|
|
|
NumbOfKeys: int(userRow.NumberOfKeys),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renew := false
|
|
|
|
|
if userRow.Renew == 1 {
|
|
|
|
|
renew = true
|
|
|
|
|
@@ -351,6 +348,25 @@ func (d *SqliteRepository) GetSvgStringInterface(idxs models.SvgIdInterface) ([]
|
|
|
|
|
return d.getSvgsById(idxs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) AddSVGIcon(svgStr string) (int64, error) {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) (any, error) {
|
|
|
|
|
params, ok := args.(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("invalid argument type: expected string")
|
|
|
|
|
}
|
|
|
|
|
return q.AddSVGIcon(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
svgID, err := d.Queue.EnqueueWriteTx(queryFunc, svgStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return -1, err
|
|
|
|
|
}
|
|
|
|
|
svgIDInt64, ok := svgID.(int64)
|
|
|
|
|
if !ok {
|
|
|
|
|
return -1, errors.New("svgID in DB isn't int64")
|
|
|
|
|
}
|
|
|
|
|
return svgIDInt64, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteRepository) getSvgsById(ids []int) ([]string, error) {
|
|
|
|
|
svgs := make([]string, len(ids))
|
|
|
|
|
for idx, id := range ids {
|
|
|
|
|
|