-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.go
184 lines (153 loc) · 4.5 KB
/
template.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package email
import (
"fmt"
"text/template"
"github.com/opensourceways/app-cla-server/util"
)
const (
TmplCorporationSigning = "corporation signing"
TmplIndividualSigning = "individual signing"
TmplEmployeeSigning = "employee signing"
TmplNotifyingManager = "notifying manager"
TmplVerificationCode = "verificaition code"
TmplAddingCorpAdmin = "adding corp admin"
TmplAddingCorpManager = "adding corp manager"
TmplRemovingCorpManager = "removing corp manager"
TmplActivatingEmployee = "activating employee"
TmplInactivaingEmployee = "inactivating employee"
TmplRemovingingEmployee = "removing employee"
)
var msgTmpl = map[string]*template.Template{}
func initTemplate() error {
items := map[string]string{
TmplCorporationSigning: "./conf/email-template/corporation-signing.tmpl",
TmplIndividualSigning: "./conf/email-template/individual-signing.tmpl",
TmplEmployeeSigning: "./conf/email-template/employee-signing.tmpl",
TmplNotifyingManager: "./conf/email-template/notifying-corp-manager.tmpl",
TmplVerificationCode: "./conf/email-template/verification-code.tmpl",
TmplAddingCorpAdmin: "./conf/email-template/adding-corp-admin.tmpl",
TmplAddingCorpManager: "./conf/email-template/adding-corp-manager.tmpl",
TmplRemovingCorpManager: "./conf/email-template/removing-corp-manager.tmpl",
TmplActivatingEmployee: "./conf/email-template/activating-employee.tmpl",
TmplInactivaingEmployee: "./conf/email-template/inactivating-employee.tmpl",
TmplRemovingingEmployee: "./conf/email-template/removing-employee.tmpl",
}
for name, path := range items {
tmpl, err := util.NewTemplate(name, path)
if err != nil {
return err
}
msgTmpl[name] = tmpl
}
return nil
}
func findTmpl(name string) *template.Template {
v, ok := msgTmpl[name]
if ok {
return v
}
return nil
}
func genEmailMsg(tmplName string, data interface{}) (*EmailMessage, error) {
tmpl := findTmpl(tmplName)
if tmpl == nil {
return nil, fmt.Errorf("Failed to generate email msg: didn't find msg template: %s", tmplName)
}
str, err := util.RenderTemplate(tmpl, data)
if err != nil {
return nil, err
}
return &EmailMessage{Content: str}, nil
}
type IEmailMessageBulder interface {
// msg returned only includes content
GenEmailMsg() (*EmailMessage, error)
}
type CorporationSigning struct {
Org string
Date string
AdminName string
ProjectURL string
SigningInfo string
}
func (this CorporationSigning) GenEmailMsg() (*EmailMessage, error) {
return genEmailMsg(TmplCorporationSigning, this)
}
type IndividualSigning struct {
Name string
}
func (this IndividualSigning) GenEmailMsg() (*EmailMessage, error) {
return genEmailMsg(TmplIndividualSigning, this)
}
type VerificationCode struct {
Email string
Org string
Code string
ProjectURL string
}
func (this VerificationCode) GenEmailMsg() (*EmailMessage, error) {
return genEmailMsg(TmplVerificationCode, this)
}
type AddingCorpManager struct {
Admin bool
ID string
User string
Email string
Password string
Org string
ProjectURL string
URLOfCLAPlatform string
}
func (this AddingCorpManager) GenEmailMsg() (*EmailMessage, error) {
if this.Admin {
return genEmailMsg(TmplAddingCorpAdmin, this)
}
return genEmailMsg(TmplAddingCorpManager, this)
}
type RemovingCorpManager struct {
User string
Org string
ProjectURL string
}
func (this RemovingCorpManager) GenEmailMsg() (*EmailMessage, error) {
return genEmailMsg(TmplRemovingCorpManager, this)
}
type EmployeeSigning struct {
Name string
Org string
ProjectURL string
Managers string
}
func (this EmployeeSigning) GenEmailMsg() (*EmailMessage, error) {
return genEmailMsg(TmplEmployeeSigning, this)
}
type NotifyingManager struct {
EmployeeEmail string
ProjectURL string
URLOfCLAPlatform string
Org string
}
func (this NotifyingManager) GenEmailMsg() (*EmailMessage, error) {
return genEmailMsg(TmplNotifyingManager, this)
}
type EmployeeNotification struct {
Removing bool
Active bool
Inactive bool
Name string
ProjectURL string
Manager string
Org string
}
func (this EmployeeNotification) GenEmailMsg() (*EmailMessage, error) {
if this.Active {
return genEmailMsg(TmplActivatingEmployee, this)
}
if this.Inactive {
return genEmailMsg(TmplInactivaingEmployee, this)
}
if this.Removing {
return genEmailMsg(TmplRemovingingEmployee, this)
}
return nil, fmt.Errorf("do nothing")
}