-
Notifications
You must be signed in to change notification settings - Fork 2
/
confirm_email.go
69 lines (56 loc) · 1.62 KB
/
confirm_email.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package character
import (
"errors"
"text/template"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
templateLib "github.com/kaikourok/lunchtote-backend/library/template"
usecaseErrors "github.com/kaikourok/lunchtote-backend/usecase/errors"
)
var confirmedMailTitleTemplate, confirmedMailBodyTemplate *template.Template
func init() {
confirmedMailTitleTemplate = template.Must(template.ParseFiles("template/mail/confirmed/title.gotmpl"))
confirmedMailBodyTemplate = template.Must(template.ParseFiles("template/mail/confirmed/body.gotmpl"))
}
func (s *CharacterUsecase) ConfirmEmail(characterId int, code string) error {
repository := s.registry.GetRepository()
logger := s.registry.GetLogger()
mailSender := s.registry.GetEmail()
err := validation.Validate(code, is.Hexadecimal)
if err != nil {
return usecaseErrors.ErrValidate
}
savedCode, email, err := repository.RetrieveConfirmCode(characterId)
if err != nil {
logger.Error(err)
return err
}
if code != savedCode {
err := errors.New("コードが一致しません")
logger.Error(err)
return err
}
err = repository.UpdateEmail(characterId, email)
if err != nil {
logger.Error(err)
return err
}
defer func() {
body, err := templateLib.TemplateExecute(confirmedMailBodyTemplate, map[string]any{})
if err != nil {
logger.Error(err)
return
}
title, err := templateLib.TemplateExecute(confirmedMailTitleTemplate, map[string]any{})
if err != nil {
logger.Error(err)
return
}
err = mailSender.SendEmail(email, title, body)
if err != nil {
logger.Error(err)
return
}
}()
return nil
}