@@ -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/hammer-code/lms-be/utils"
1215)
@@ -41,12 +44,86 @@ func (uc usecase) CreateRegistrationEvent(ctx context.Context, payload domain.Re
4144
4245 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 ])
4346
47+ // Read HTML template for email
48+ htmlTmpl , err := os .ReadFile ("./assets/event_status_registration_template.html" )
49+ if err != nil {
50+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to read file template: %w" , err )
51+ }
52+
53+ // Parse the HTML template
54+ tmpl , err := template .New ("register_event_status" ).Parse (string (htmlTmpl ))
55+ if err != nil {
56+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to parse template: %w" , err )
57+ }
58+
59+ // Prepare data for the email template
60+ smtpConfig := email.SMTP {
61+ Email : uc .cfg .SMTP_EMAIL ,
62+ Password : uc .cfg .SMTP_PASSWORD ,
63+ Host : uc .cfg .SMTP_HOST ,
64+ Port : uc .cfg .SMTP_PORT ,
65+ }
66+
67+ // Prepare the receiver data
68+ emailPayload := email .NewSendEmail (
69+ ctx ,
70+ smtpConfig ,
71+ "MIME-Version: 1.0\r \n Content-Type: text/html; charset=UTF-8" ,
72+ "Welcome to Our Platform - Event Registration Status" ,
73+ tmpl ,
74+ )
75+ var formattedDate string
76+ if event .Date .Valid {
77+ formattedDate = event .Date .Time .Format ("Monday, 02 January 2006" )
78+ } else {
79+ formattedDate = "Date to be announced"
80+ }
81+
82+ // Add the receiver's email and data to the payload
83+ if err := emailPayload .AddReceiver (
84+ ctx ,
85+ email.Receiver {
86+ Email : payload .Email ,
87+ Data : map [string ]interface {}{
88+ "name" : payload .Name ,
89+ "title" : event .Title ,
90+ "price" : event .Price ,
91+ "email" : payload .Email ,
92+ "order_no" : orderNo ,
93+ "year" : time .Now ().Format ("2006" ),
94+ "date" : formattedDate ,
95+ "duration" : event .Duration ,
96+ "location" : event .Location ,
97+ },
98+ }); err != nil {
99+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to add receiver: %w" , err )
100+ }
101+
44102 // is free event or not
45103 status := "SUCCESS"
46104 upToYou := "registration success"
47105 if event .Price != 0.0 {
48106 status = "PENDING"
49107 upToYou = "new register"
108+ emailPayload .SendEmail (ctx )
109+ } else {
110+ logrus .Info ("free event, send email registration success" )
111+ // Read HTML template for email
112+ htmlTmpl , err := os .ReadFile ("./assets/event_status_registration_sucess_template.html" )
113+ if err != nil {
114+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to read file template: %w" , err )
115+ }
116+
117+ // Parse the HTML template
118+ tmpl , err := template .New ("register_event_status" ).Parse (string (htmlTmpl ))
119+ if err != nil {
120+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to parse template: %w" , err )
121+ }
122+
123+ if err := emailPayload .ChangeTemplate (ctx , tmpl ); err != nil {
124+ return domain.RegisterEventResponse {}, fmt .Errorf ("failed to change template: %w" , err )
125+ }
126+ emailPayload .SendEmail (ctx )
50127 }
51128
52129 err = uc .dbTX .StartTransaction (ctx , func (txCtx context.Context ) error {
0 commit comments