2022-09-09 20:03:47 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2023-01-12 20:00:16 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-01-12 17:04:56 +01:00
|
|
|
"time"
|
|
|
|
|
2022-09-09 20:03:47 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccountRequest struct {
|
|
|
|
Model
|
|
|
|
Token string
|
|
|
|
Email string
|
|
|
|
SourceAddress string
|
|
|
|
}
|
|
|
|
|
2022-09-19 22:26:54 +02:00
|
|
|
func (self *Store) CreateAccountRequest(ar *AccountRequest, tx *sqlx.Tx) (int, error) {
|
2022-10-21 15:31:12 +02:00
|
|
|
stmt, err := tx.Prepare("insert into account_requests (token, email, source_address) values ($1, $2, $3) returning id")
|
2022-09-09 20:03:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "error preparing account_requests insert statement")
|
|
|
|
}
|
2022-10-21 15:31:12 +02:00
|
|
|
var id int
|
|
|
|
if err := stmt.QueryRow(ar.Token, ar.Email, ar.SourceAddress).Scan(&id); err != nil {
|
2022-09-09 20:03:47 +02:00
|
|
|
return 0, errors.Wrap(err, "error executing account_requests insert statement")
|
|
|
|
}
|
2022-10-21 15:31:12 +02:00
|
|
|
return id, nil
|
2022-09-09 20:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Store) GetAccountRequest(id int, tx *sqlx.Tx) (*AccountRequest, error) {
|
|
|
|
ar := &AccountRequest{}
|
2022-10-21 15:31:12 +02:00
|
|
|
if err := tx.QueryRowx("select * from account_requests where id = $1", id).StructScan(ar); err != nil {
|
2022-09-09 20:03:47 +02:00
|
|
|
return nil, errors.Wrap(err, "error selecting account_request by id")
|
|
|
|
}
|
|
|
|
return ar, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Store) FindAccountRequestWithToken(token string, tx *sqlx.Tx) (*AccountRequest, error) {
|
|
|
|
ar := &AccountRequest{}
|
2022-10-21 15:31:12 +02:00
|
|
|
if err := tx.QueryRowx("select * from account_requests where token = $1", token).StructScan(ar); err != nil {
|
2022-09-09 20:03:47 +02:00
|
|
|
return nil, errors.Wrap(err, "error selecting account_request by token")
|
|
|
|
}
|
|
|
|
return ar, nil
|
|
|
|
}
|
2022-09-20 20:23:01 +02:00
|
|
|
|
2023-01-12 21:09:04 +01:00
|
|
|
func (self *Store) FindExpiredAccountRequests(before time.Time, limit int, tx *sqlx.Tx) ([]*AccountRequest, error) {
|
2023-01-12 22:00:09 +01:00
|
|
|
var sql string
|
|
|
|
switch self.cfg.Type {
|
|
|
|
case "postgres":
|
|
|
|
sql = "select * from account_requests where created_at < $1 limit %d for update"
|
|
|
|
|
|
|
|
case "sqlite3":
|
|
|
|
sql = "select * from account_requests where created_at < $1 limit %d"
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown database type '%v'", self.cfg.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.Queryx(fmt.Sprintf(sql, limit), before)
|
2023-01-12 20:00:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting expired account_requests")
|
|
|
|
}
|
|
|
|
var ars []*AccountRequest
|
|
|
|
for rows.Next() {
|
|
|
|
ar := &AccountRequest{}
|
|
|
|
if err := rows.StructScan(ar); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error scanning account_request")
|
|
|
|
}
|
|
|
|
ars = append(ars, ar)
|
|
|
|
}
|
|
|
|
return ars, nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 20:23:01 +02:00
|
|
|
func (self *Store) FindAccountRequestWithEmail(email string, tx *sqlx.Tx) (*AccountRequest, error) {
|
|
|
|
ar := &AccountRequest{}
|
2022-10-21 15:31:12 +02:00
|
|
|
if err := tx.QueryRowx("select * from account_requests where email = $1", email).StructScan(ar); err != nil {
|
2022-09-20 20:23:01 +02:00
|
|
|
return nil, errors.Wrap(err, "error selecting account_request by email")
|
|
|
|
}
|
|
|
|
return ar, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Store) DeleteAccountRequest(id int, tx *sqlx.Tx) error {
|
2022-10-21 15:31:12 +02:00
|
|
|
stmt, err := tx.Prepare("delete from account_requests where id = $1")
|
2022-09-20 20:23:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error preparing account_requests delete statement")
|
|
|
|
}
|
|
|
|
_, err = stmt.Exec(id)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error executing account_requests delete statement")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-12 17:04:56 +01:00
|
|
|
|
2023-01-12 20:00:16 +01:00
|
|
|
func (self *Store) DeleteMultipleAccountRequests(ids []int, tx *sqlx.Tx) error {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
anyIds := make([]any, len(ids))
|
|
|
|
indexes := make([]string, len(ids))
|
|
|
|
|
|
|
|
for i, id := range ids {
|
|
|
|
anyIds[i] = id
|
|
|
|
indexes[i] = fmt.Sprintf("$%d", i+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(fmt.Sprintf("delete from account_requests where id in (%s)", strings.Join(indexes, ",")))
|
2023-01-12 17:04:56 +01:00
|
|
|
if err != nil {
|
2023-01-12 20:00:16 +01:00
|
|
|
return errors.Wrap(err, "error preparing account_requests delete multiple statement")
|
2023-01-12 17:04:56 +01:00
|
|
|
}
|
2023-01-12 20:00:16 +01:00
|
|
|
_, err = stmt.Exec(anyIds...)
|
2023-01-12 17:04:56 +01:00
|
|
|
if err != nil {
|
2023-01-12 20:00:16 +01:00
|
|
|
return errors.Wrap(err, "error executing account_requests delete multiple statement")
|
2023-01-12 17:04:56 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|