Commit b8aa2037 by Daniel Lee

signup: fix email sent logic for tempuser

Fixes #8656 and properly sets the email_sent and email_sent_on fields
for a tempuser (signup user).
parent fb99ddf2
......@@ -78,6 +78,11 @@ func AddOrgInvite(c *middleware.Context, inviteDto dtos.AddInviteForm) Response
return ApiError(500, "Failed to send email invite", err)
}
emailSentCmd := m.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code}
if err := bus.Dispatch(&emailSentCmd); err != nil {
return ApiError(500, "Failed to update invite with email sent info", err)
}
return ApiSuccess(fmt.Sprintf("Sent invite to %s", inviteDto.LoginOrEmail))
}
......
......@@ -60,6 +60,10 @@ type UpdateTempUserStatusCommand struct {
Status TempUserStatus
}
type UpdateTempUserWithEmailSentCommand struct {
Code string
}
type GetTempUsersQuery struct {
OrgId int64
Email string
......
......@@ -146,7 +146,7 @@ func signUpStartedHandler(evt *events.SignUpStarted) error {
return nil
}
return sendEmailCommandHandler(&m.SendEmailCommand{
err := sendEmailCommandHandler(&m.SendEmailCommand{
To: []string{evt.Email},
Template: tmplSignUpStarted,
Data: map[string]interface{}{
......@@ -155,6 +155,12 @@ func signUpStartedHandler(evt *events.SignUpStarted) error {
"SignUpUrl": setting.ToAbsUrl(fmt.Sprintf("signup/?email=%s&code=%s", url.QueryEscape(evt.Email), url.QueryEscape(evt.Code))),
},
})
if err != nil {
return err
}
emailSentCmd := m.UpdateTempUserWithEmailSentCommand{Code: evt.Code}
return bus.Dispatch(&emailSentCmd)
}
func signUpCompletedHandler(evt *events.SignUpCompleted) error {
......
......@@ -16,6 +16,8 @@ func InitTestDB(t *testing.T) {
//x, err := xorm.NewEngine(sqlutil.TestDB_Mysql.DriverName, sqlutil.TestDB_Mysql.ConnStr)
//x, err := xorm.NewEngine(sqlutil.TestDB_Postgres.DriverName, sqlutil.TestDB_Postgres.ConnStr)
// x.ShowSQL()
if err != nil {
t.Fatalf("Failed to init in memory sqllite3 db %v", err)
}
......
......@@ -12,6 +12,7 @@ func init() {
bus.AddHandler("sql", GetTempUsersQuery)
bus.AddHandler("sql", UpdateTempUserStatus)
bus.AddHandler("sql", GetTempUserByCode)
bus.AddHandler("sql", UpdateTempUserWithEmailSent)
}
func UpdateTempUserStatus(cmd *m.UpdateTempUserStatusCommand) error {
......@@ -35,6 +36,7 @@ func CreateTempUser(cmd *m.CreateTempUserCommand) error {
Status: cmd.Status,
RemoteAddr: cmd.RemoteAddr,
InvitedByUserId: cmd.InvitedByUserId,
EmailSentOn: time.Now(),
Created: time.Now(),
Updated: time.Now(),
}
......@@ -48,6 +50,19 @@ func CreateTempUser(cmd *m.CreateTempUserCommand) error {
})
}
func UpdateTempUserWithEmailSent(cmd *m.UpdateTempUserWithEmailSentCommand) error {
return inTransaction(func(sess *DBSession) error {
user := &m.TempUser{
EmailSent: true,
EmailSentOn: time.Now(),
}
_, err := sess.Where("code = ?", cmd.Code).Cols("email_sent", "email_sent_on").Update(user)
return err
})
}
func GetTempUsersQuery(query *m.GetTempUsersQuery) error {
rawSql := `SELECT
tu.id as id,
......
......@@ -54,6 +54,19 @@ func TestTempUserCommandsAndQueries(t *testing.T) {
So(err, ShouldBeNil)
})
Convey("Should be able update email sent and email sent on", func() {
cmd3 := m.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code}
err := UpdateTempUserWithEmailSent(&cmd3)
So(err, ShouldBeNil)
query := m.GetTempUsersQuery{OrgId: 2256, Status: m.TmpUserInvitePending}
err = GetTempUsersQuery(&query)
So(err, ShouldBeNil)
So(query.Result[0].EmailSent, ShouldBeTrue)
So(query.Result[0].EmailSentOn, ShouldHappenOnOrAfter, (query.Result[0].Created))
})
})
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment