implented action token db operations
This commit is contained in:
parent
59e76a4ec1
commit
7e3d9ec155
@ -1,9 +1,64 @@
|
|||||||
package repo
|
package repo
|
||||||
|
|
||||||
import "backend/src/models"
|
import (
|
||||||
|
"backend/src/models"
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
type ActionTokenRepo interface {
|
type ActionTokenRepo interface {
|
||||||
CreateActionToken(actionToken models.ActionTokenDTO) (*models.ActionTokenDTO, error)
|
CreateActionToken(ctx context.Context, dto models.ActionTokenDTO) (*models.ActionTokenDTO, error)
|
||||||
FindActionToken(userId, val string, target models.ActionTokenTarget) (*models.ActionTokenDTO, error)
|
PopActionToken(ctx context.Context, userId, value string, target models.ActionTokenTarget) (*models.ActionTokenDTO, error)
|
||||||
DeleteActionToken(id string) error
|
}
|
||||||
|
|
||||||
|
func NewActionTokenRepo(db *sql.DB) ActionTokenRepo {
|
||||||
|
return &actionTokenRepo{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type actionTokenRepo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *actionTokenRepo) CreateActionToken(ctx context.Context, dto models.ActionTokenDTO) (*models.ActionTokenDTO, error) {
|
||||||
|
query := `
|
||||||
|
insert into
|
||||||
|
action_tokens (user_id, value, target)
|
||||||
|
values ($1, $2, $3)
|
||||||
|
returning id;`
|
||||||
|
row := a.db.QueryRowContext(ctx, query, dto.UserId, dto.Value, dto.Target)
|
||||||
|
|
||||||
|
id := ""
|
||||||
|
if err := row.Scan(&id); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.ActionTokenDTO{
|
||||||
|
Id: id,
|
||||||
|
UserId: dto.UserId,
|
||||||
|
Value: dto.Value,
|
||||||
|
Target: dto.Target,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *actionTokenRepo) PopActionToken(ctx context.Context, userId, value string, target models.ActionTokenTarget) (*models.ActionTokenDTO, error) {
|
||||||
|
query := `
|
||||||
|
delete
|
||||||
|
from action_tokens
|
||||||
|
where user_id=$1 and value=$2 and target=$3
|
||||||
|
returning id;`
|
||||||
|
row := a.db.QueryRowContext(ctx, query, userId, value, target)
|
||||||
|
|
||||||
|
id := ""
|
||||||
|
if err := row.Scan(&id); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.ActionTokenDTO{
|
||||||
|
Id: id,
|
||||||
|
UserId: userId,
|
||||||
|
Value: value,
|
||||||
|
Target: target,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -112,11 +112,14 @@ func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
actionToken, err := u.deps.ActionTokenRepo.CreateActionToken(models.ActionTokenDTO{
|
actionToken, err := u.deps.ActionTokenRepo.CreateActionToken(
|
||||||
|
ctx,
|
||||||
|
models.ActionTokenDTO{
|
||||||
UserId: user.Id,
|
UserId: user.Id,
|
||||||
Value: uuid.New().String(),
|
Value: uuid.New().String(),
|
||||||
Target: models.ActionTokenTargetForgotPassword,
|
Target: models.ActionTokenTargetForgotPassword,
|
||||||
})
|
},
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -131,7 +134,7 @@ func (u *userService) ChangePasswordForgot(ctx context.Context, userId, newPassw
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
code, err := u.deps.ActionTokenRepo.FindActionToken(userId, accessCode, models.ActionTokenTargetForgotPassword)
|
code, err := u.deps.ActionTokenRepo.PopActionToken(ctx, userId, accessCode, models.ActionTokenTargetForgotPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -139,10 +142,6 @@ func (u *userService) ChangePasswordForgot(ctx context.Context, userId, newPassw
|
|||||||
return fmt.Errorf("wrong user access code")
|
return fmt.Errorf("wrong user access code")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := u.deps.ActionTokenRepo.DeleteActionToken(code.Id); err != nil {
|
|
||||||
return fmt.Errorf("internal error occured: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return u.updatePassword(ctx, *user, newPassword)
|
return u.updatePassword(ctx, *user, newPassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user