@@ -4,9 +4,12 @@ import (
44 "context"
55 "errors"
66 "fmt"
7+ "html/template"
8+ "os"
79 "time"
810
911 "github.com/hammer-code/lms-be/domain"
12+ "github.com/hammer-code/lms-be/pkg/email"
1013 "github.com/hammer-code/lms-be/pkg/hash"
1114 "github.com/sirupsen/logrus"
1215)
@@ -38,12 +41,86 @@ func (uc usecase) CreateRegisterEvent(ctx context.Context, payload domain.Regist
3841
3942 orderNo := fmt .Sprintf ("TXE-%d-%s%s%s%s" , event .ID , time .Now ().Format ("06" ), time .Now ().Format ("01" ), time .Now ().Format ("02" ), hash [0 :4 ])
4043
44+ // Read HTML template for email
45+ htmlTmpl , err := os .ReadFile ("./assets/event_status_registration_template.html" )
46+ if err != nil {
47+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to read file template: %w" , err )
48+ }
49+
50+ // Parse the HTML template
51+ tmpl , err := template .New ("register_event_status" ).Parse (string (htmlTmpl ))
52+ if err != nil {
53+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to parse template: %w" , err )
54+ }
55+
56+ // Prepare data for the email template
57+ smtpConfig := email.SMTP {
58+ Email : uc .cfg .SMTP_EMAIL ,
59+ Password : uc .cfg .SMTP_PASSWORD ,
60+ Host : uc .cfg .SMTP_HOST ,
61+ Port : uc .cfg .SMTP_PORT ,
62+ }
63+
64+ // Prepare the receiver data
65+ emailPayload := email .NewSendEmail (
66+ ctx ,
67+ smtpConfig ,
68+ "MIME-Version: 1.0\r \n Content-Type: text/html; charset=UTF-8" ,
69+ "Welcome to Our Platform - Event Registration Status" ,
70+ tmpl ,
71+ )
72+ var formattedDate string
73+ if event .Date .Valid {
74+ formattedDate = event .Date .Time .Format ("Monday, 02 January 2006" )
75+ } else {
76+ formattedDate = "Date to be announced"
77+ }
78+
79+ // Add the receiver's email and data to the payload
80+ if err := emailPayload .AddReceiver (
81+ ctx ,
82+ email.Receiver {
83+ Email : payload .Email ,
84+ Data : map [string ]interface {}{
85+ "name" : payload .Name ,
86+ "title" : event .Title ,
87+ "price" : event .Price ,
88+ "email" : payload .Email ,
89+ "order_no" : orderNo ,
90+ "year" : time .Now ().Format ("2006" ),
91+ "date" : formattedDate ,
92+ "duration" : event .Duration ,
93+ "location" : event .Location ,
94+ },
95+ }); err != nil {
96+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to add receiver: %w" , err )
97+ }
98+
4199 // is free event or not
42100 status := "SUCCESS"
43101 upToYou := "registration success"
44102 if event .Price != 0.0 {
45103 status = "PENDING"
46104 upToYou = "new register"
105+ emailPayload .SendEmail (ctx )
106+ } else {
107+ logrus .Info ("free event, send email registration success" )
108+ // Read HTML template for email
109+ htmlTmpl , err := os .ReadFile ("./assets/event_status_registration_sucess_template.html" )
110+ if err != nil {
111+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to read file template: %w" , err )
112+ }
113+
114+ // Parse the HTML template
115+ tmpl , err := template .New ("register_event_status" ).Parse (string (htmlTmpl ))
116+ if err != nil {
117+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to parse template: %w" , err )
118+ }
119+
120+ if err := emailPayload .ChangeTemplate (ctx , tmpl ); err != nil {
121+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to change template: %w" , err )
122+ }
123+ emailPayload .SendEmail (ctx )
47124 }
48125
49126 err = uc .dbTX .StartTransaction (ctx , func (txCtx context.Context ) error {
0 commit comments