-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
248 lines (212 loc) · 6.4 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
io "io/ioutil"
"log"
"net/smtp"
"os"
"strconv"
"strings"
e "github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
em "github.com/jordan-wright/email"
)
//data of the issue policy
type policydata struct {
Name string `json:"name"`
AddressLine1 string `json:"addressLine1"`
AddressLine2 string `json:"addressLine2"`
AddressLine3 string `json:"addressLine3"`
City string `json:"city"`
PinCode int `json:"pinCode"`
MobileNumber int `json:"mobileNumber"`
PolicyNumber int `json:"policyNumber"`
}
//email of the policy holder
type email struct {
From string `json:"from"`
To string `json:"to"`
}
//composite of poilcy issued meta data
type polmetadata struct {
Email email `json:"email"`
Data policydata `json:"data"`
}
func main() {
lambda.Start(handler)
}
func handler(ctx context.Context, event e.S3Event) (string, error) {
for _, record := range event.Records {
log.Println("bucket " + record.S3.Bucket.Name)
log.Println("object " + record.S3.Object.Key)
err := processEvent(record)
if err != nil {
log.Println(err)
return "processing error ", err
}
}
return fmt.Sprintf("object processed "), nil
}
//Sends email to the policy holder with an attachment of the issued policy PDF
func processEvent(record e.S3EventRecord) error {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
pdf, pdferr := policyPDF(sess, record)
if pdferr != nil {
return nil
}
pmd, err := policyMetaData(sess, record)
if err != nil {
return nil
}
serr := sendEmail(pmd, pdf)
if serr != nil {
log.Println("error sending email " + serr.Error())
return nil
}
mverr := moveFiles(sess, record)
if mverr != nil {
return mverr
}
return nil
}
//reterives policy PDF from S3 unprocessed folder
func policyPDF(sess *session.Session, record e.S3EventRecord) ([]byte, error) {
svc := s3.New(sess)
input := &s3.GetObjectInput{
Bucket: aws.String(record.S3.Bucket.Name),
Key: aws.String(record.S3.Object.Key),
}
result, err := svc.GetObject(input)
if err != nil {
log.Println("error getting pdf object " + err.Error())
return nil, err
}
defer result.Body.Close()
bodyBytes, err := io.ReadAll(result.Body)
return bodyBytes, nil
}
//reterives policy meta data from S3 folder.
func policyMetaData(sess *session.Session, record e.S3EventRecord) (*polmetadata, error) {
svc := s3.New(sess)
key := record.S3.Object.Key
//polNumber := key[strings.Index(key, "/")+1 : strings.Index(key, ".")]
polNumber := extactPolNo(key)
input := &s3.GetObjectInput{
Bucket: aws.String(record.S3.Bucket.Name),
Key: aws.String("unprocessed/" + polNumber + ".json"),
}
result, jerr := svc.GetObject(input)
if jerr != nil {
log.Println("error getting json object " + jerr.Error())
return nil, jerr
}
defer result.Body.Close()
bodyBytes, merr := io.ReadAll(result.Body)
pmd, merr := marshallReq(string(bodyBytes))
if merr != nil {
log.Println("json marshall error " + merr.Error())
return nil, merr
}
return pmd, nil
}
//Sends email to the policy holder along with an attached policy document
func sendEmail(pmd *polmetadata, pdf []byte) error {
e := em.NewEmail()
e.From = "Kubesure <" + pmd.Email.From + ">"
e.To = []string{pmd.Email.To}
e.Subject = "Kubesure : EsyHealth - " + strconv.Itoa(pmd.Data.PolicyNumber)
msg := `Hello %s,
Your policy has been issued and the policy document has been attached.
Please use policy number to make any enquiries.
Best Wishes,
Kubesure Customer Service
`
body := fmt.Sprintf(msg, pmd.Data.Name)
ebody := []byte(body)
e.Text = ebody
e.Attach(bytes.NewReader(pdf), strconv.Itoa(pmd.Data.PolicyNumber), "application/pdf")
//log.Printf("User-- %s -- Password %s", os.Getenv("emailuser"), os.Getenv("emailpassword"))
err := e.Send("smtp.gmail.com:587",
smtp.PlainAuth("", os.Getenv("emailuser"), os.Getenv("emailpassword"), "smtp.gmail.com"))
if err != nil {
return err
}
return nil
}
//move json meta data, html email tempalte and PDF policy document to processed folder.
func moveFiles(sess *session.Session, record e.S3EventRecord) error {
svc := s3.New(sess)
polNumber := extactPolNo(record.S3.Object.Key)
listInput := &s3.ListObjectsV2Input{
Bucket: aws.String(record.S3.Bucket.Name),
Prefix: aws.String("unprocessed/" + polNumber),
Delimiter: aws.String("/"),
}
outputList, err := svc.ListObjectsV2(listInput)
if err != nil {
log.Println("error while listing objects " + err.Error())
return nil
}
for _, o := range outputList.Contents {
fileName := extractFileName(*o.Key)
dkey := "processed/" + fileName
copyInput := &s3.CopyObjectInput{
Bucket: aws.String(record.S3.Bucket.Name),
CopySource: aws.String(record.S3.Bucket.Name + "/" + *o.Key),
Key: aws.String("/" + dkey),
}
_, cerr := svc.CopyObject(copyInput)
if cerr != nil {
//movedkeys = append(movedkeys, *o.Key)
log.Println("copy failed due to " + cerr.Error())
//continue
}
deleteInput := &s3.DeleteObjectInput{
Bucket: aws.String(record.S3.Bucket.Name),
Key: o.Key,
}
_, derr := svc.DeleteObject(deleteInput)
if derr != nil {
log.Println("delete failed due to " + cerr.Error())
//movedkeys = append(movedkeys, *o.Key)
//continue
}
}
/*for _, fo : range movedkeys {
deleteInput := &s3.DeleteObjectInput{
Bucket: aws.String(record.S3.Bucket.Name),
Key: aws.String(dkey),
}
_, derr := svc.DeleteObject(deleteInput)
if derr != nil {
movedkeys = append(movedkeys, *o.Key)
continue
}
}*/
return nil
}
func marshallReq(data string) (*polmetadata, error) {
var pd polmetadata
err := json.Unmarshal([]byte(data), &pd)
if err != nil {
return nil, err
}
return &pd, nil
}
func extactPolNo(key string) string {
return key[strings.Index(key, "/")+1 : strings.Index(key, ".")]
}
func extractExt(key string) string {
return key[strings.Index(key, "."):len(key)]
}
func extractFileName(key string) string {
return key[strings.Index(key, "/")+1 : len(key)]
}