add password reset model ; create entry in database; send email
This commit is contained in:
@@ -20,16 +20,17 @@ var (
|
||||
|
||||
// Conn is the MySQL connection manager
|
||||
type Conn struct {
|
||||
db *sql.DB
|
||||
users *userRepo
|
||||
rides *rideRepo
|
||||
visit *visitRepo
|
||||
provider *providerRepo
|
||||
notification *notificationRepo
|
||||
profile *profileRepo
|
||||
organization *organizationRepo
|
||||
zipcodes *zipcodeRepo
|
||||
plan *planRepo
|
||||
db *sql.DB
|
||||
users *userRepo
|
||||
rides *rideRepo
|
||||
visit *visitRepo
|
||||
provider *providerRepo
|
||||
notification *notificationRepo
|
||||
profile *profileRepo
|
||||
organization *organizationRepo
|
||||
zipcodes *zipcodeRepo
|
||||
plan *planRepo
|
||||
passwordReset *passwordResetRepo
|
||||
}
|
||||
|
||||
// Begin starts a transaction
|
||||
@@ -90,6 +91,10 @@ func (c *Conn) Plans() contract.PlanRepo {
|
||||
return c.plan
|
||||
}
|
||||
|
||||
func (c *Conn) PasswordReset() contract.PasswordResetRepo {
|
||||
return c.passwordReset
|
||||
}
|
||||
|
||||
// Instance returns an instance of a DataManager
|
||||
func Instance(cfg *config.Config) (contract.DataManager, error) {
|
||||
once.Do(func() {
|
||||
@@ -123,6 +128,7 @@ func Instance(cfg *config.Config) (contract.DataManager, error) {
|
||||
instance.organization = newOrganizationRepo(db)
|
||||
instance.zipcodes = newZipcodeRepo(db)
|
||||
instance.plan = newPlanRepo(db)
|
||||
instance.passwordReset = newPasswordResetRepo(db)
|
||||
})
|
||||
|
||||
return instance, connErr
|
||||
|
||||
132
data/datamysql/passwordreset.go
Normal file
132
data/datamysql/passwordreset.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package datamysql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
|
||||
"bitbucket.org/nemt/nemt-portal-api/infra/errors"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
// rideRepo maps methods to database
|
||||
type passwordResetRepo struct {
|
||||
conn executor
|
||||
}
|
||||
|
||||
func newPasswordResetRepo(conn executor) *passwordResetRepo {
|
||||
return &passwordResetRepo{
|
||||
conn: conn,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *passwordResetRepo) getQuery() string {
|
||||
const (
|
||||
query = `SELECT
|
||||
a.password_reset_id,
|
||||
a.password_reset_uuid,
|
||||
a.user_id,
|
||||
b.user_uuid,
|
||||
a.token,
|
||||
a.create_date,
|
||||
a.expire_date,
|
||||
(IFNULL(a.used, b'0') = b'1') used
|
||||
(IFNULL(a.opened, b'0') = b'1') opened
|
||||
FROM
|
||||
tab_password_reset a
|
||||
INNER JOIN tab_user b
|
||||
ON a.user_id = b.user_id`
|
||||
)
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
// parseSet parses a result set result to an entity array
|
||||
func (c *passwordResetRepo) parseSet(rows *sql.Rows, err error) ([]entity.PasswordReset, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
result := make([]entity.PasswordReset, 0)
|
||||
for rows.Next() {
|
||||
entity, err := c.parseEntity(rows)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
result = append(result, entity)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// parseEntity parses a result to an entity
|
||||
func (c *passwordResetRepo) parseEntity(row scanner) (retVal entity.PasswordReset, err error) {
|
||||
err = row.Scan(
|
||||
&retVal.ID, &retVal.UUID, &retVal.User.ID, &retVal.User.UUID, &retVal.Token, &retVal.Created, &retVal.Expires, &retVal.Used, &retVal.Opened)
|
||||
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
func (c *passwordResetRepo) GetAll() ([]entity.PasswordReset, error) {
|
||||
return c.parseSet(c.conn.Query(c.getQuery()))
|
||||
}
|
||||
|
||||
/*
|
||||
func (c *zipcodeRepo) GetByParticipatingZipcode(zipcode string) (entity.Zipcode, error) {
|
||||
return c.parseEntity(c.conn.QueryRow(c.getQuery()+"WHERE a.participating = 1 AND a.zipcode = ?", zipcode))
|
||||
}*/
|
||||
|
||||
func (c *passwordResetRepo) CreatePasswordResetEntry(passwordResetEntry entity.PasswordReset) (entity.PasswordReset, error) {
|
||||
const (
|
||||
query = `INSERT INTO tab_password_reset(password_reset_uuid, user_id, token, expire_date, used, opened) VALUES(?, ?, ?, ?, ?, 0, 0);`
|
||||
)
|
||||
|
||||
retVal := passwordResetEntry
|
||||
guid, _ := uuid.NewV4()
|
||||
|
||||
results, err := c.conn.Exec(query, guid, passwordResetEntry.User.ID, passwordResetEntry.Token, passwordResetEntry.Expires)
|
||||
if err != nil {
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
retVal.ID, err = results.LastInsertId()
|
||||
if err != nil {
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
return c.GetByID(retVal.ID)
|
||||
}
|
||||
|
||||
func (c *passwordResetRepo) GetByID(ID int64) (entity.PasswordReset, error) {
|
||||
return c.parseEntity(c.conn.QueryRow(c.getQuery()+" WHERE a.password_reset_id = ?; ", ID))
|
||||
}
|
||||
|
||||
func (c *passwordResetRepo) GetByToken(token string) (entity.PasswordReset, error) {
|
||||
return c.parseEntity(c.conn.QueryRow(c.getQuery()+" WHERE a.token = ? AND a.used = 0; ", token))
|
||||
}
|
||||
|
||||
func (c *passwordResetRepo) SetTokenOpened(token string) error {
|
||||
const (
|
||||
query = `UPDATE tab_password_reset a
|
||||
SET a.opened = 1,
|
||||
WHERE a.token = ? AND a.used = 0`
|
||||
)
|
||||
|
||||
if _, err := c.conn.Exec(query, token); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *passwordResetRepo) SetTokenUsed(token string) error {
|
||||
const (
|
||||
query = `UPDATE tab_password_reset a
|
||||
SET a.opened = 1,
|
||||
a.used = 1,
|
||||
WHERE a.token = ? AND a.used = 0`
|
||||
)
|
||||
|
||||
if _, err := c.conn.Exec(query, token); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -8,16 +8,17 @@ import (
|
||||
)
|
||||
|
||||
type transaction struct {
|
||||
tx *sql.Tx
|
||||
users *userRepo
|
||||
rides *rideRepo
|
||||
visits *visitRepo
|
||||
provider *providerRepo
|
||||
notification *notificationRepo
|
||||
profile *profileRepo
|
||||
organization *organizationRepo
|
||||
zipcodes *zipcodeRepo
|
||||
plan *planRepo
|
||||
tx *sql.Tx
|
||||
users *userRepo
|
||||
rides *rideRepo
|
||||
visits *visitRepo
|
||||
provider *providerRepo
|
||||
notification *notificationRepo
|
||||
profile *profileRepo
|
||||
organization *organizationRepo
|
||||
zipcodes *zipcodeRepo
|
||||
plan *planRepo
|
||||
passwordReset *passwordResetRepo
|
||||
}
|
||||
|
||||
func newTransaction(tx *sql.Tx) *transaction {
|
||||
@@ -34,6 +35,7 @@ func newTransaction(tx *sql.Tx) *transaction {
|
||||
t.organization = newOrganizationRepo(tx)
|
||||
t.zipcodes = newZipcodeRepo(tx)
|
||||
t.plan = newPlanRepo(tx)
|
||||
t.passwordReset = newPasswordResetRepo(tx)
|
||||
|
||||
return t
|
||||
}
|
||||
@@ -81,6 +83,10 @@ func (t transaction) Plans() contract.PlanRepo {
|
||||
return t.plan
|
||||
}
|
||||
|
||||
func (t transaction) PasswordReset() contract.PasswordResetRepo {
|
||||
return t.passwordReset
|
||||
}
|
||||
|
||||
func (t *transaction) Commit() error {
|
||||
|
||||
err := t.tx.Commit()
|
||||
|
||||
@@ -48,6 +48,32 @@ func (c *userRepo) GetByMemberID(memberID string) (entity.User, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *userRepo) GetByEmail(email string) (entity.User, error) {
|
||||
finalQuery := c.getQuery() + " AND b.email = ?"
|
||||
|
||||
user, err := c.parseSet(c.conn.Query(finalQuery, email))
|
||||
if err != nil {
|
||||
return entity.User{}, err
|
||||
}
|
||||
|
||||
if len(user) > 0 {
|
||||
retVal := user[0]
|
||||
retVal.Contacts, err = c.GetContacts(retVal.ID)
|
||||
if err != nil {
|
||||
return entity.User{}, err
|
||||
}
|
||||
|
||||
retVal.Addresses = nil
|
||||
retVal.Addresses, err = c.getAddressByUserID(retVal.ID)
|
||||
if err != nil {
|
||||
return entity.User{}, err
|
||||
}
|
||||
return retVal, nil
|
||||
} else {
|
||||
return entity.User{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *userRepo) GetByUUID(uuid string, profile string) (entity.User, error) {
|
||||
params := make([]interface{}, 0)
|
||||
params = append(params, uuid)
|
||||
|
||||
Reference in New Issue
Block a user