125 lines
2.6 KiB
Go
125 lines
2.6 KiB
Go
package helper
|
|
|
|
import (
|
|
"bytes"
|
|
"email-notification/config"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"net/smtp"
|
|
"reflect"
|
|
"time"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func GenerateSessionID() string {
|
|
return uuid.New().String()
|
|
}
|
|
|
|
func ParseHTML(filePath string, v any) string {
|
|
tmpl, err := template.ParseFiles(filePath)
|
|
if err != nil {
|
|
log.Printf("Error parsing template: %v", err)
|
|
}
|
|
|
|
// type Data struct {
|
|
// Name string
|
|
// }
|
|
|
|
// data := Data{Name: name}
|
|
|
|
var buf bytes.Buffer
|
|
if err := tmpl.Execute(&buf, v); err != nil {
|
|
log.Printf("Error executing template: %v", err)
|
|
}
|
|
|
|
return buf.String()
|
|
}
|
|
|
|
func FormatSince(t time.Time) string {
|
|
now := time.Now()
|
|
dur := now.Sub(t)
|
|
|
|
years := int(dur.Hours() / 24 / 365)
|
|
if years > 0 {
|
|
return fmt.Sprintf("%d", years)
|
|
}
|
|
|
|
// months := int(dur.Hours() / 24 / 30)
|
|
// if months > 0 {
|
|
// return fmt.Sprintf("%d bulan yang lalu", months)
|
|
// }
|
|
|
|
// days := int(dur.Hours() / 24)
|
|
// if days > 0 {
|
|
// return fmt.Sprintf("%d hari yang lalu", days)
|
|
// }
|
|
|
|
// hours := int(dur.Hours())
|
|
// if hours > 0 {
|
|
// return fmt.Sprintf("%d jam yang lalu", hours)
|
|
// }
|
|
|
|
// minutes := int(dur.Minutes())
|
|
// if minutes > 0 {
|
|
// return fmt.Sprintf("%d menit yang lalu", minutes)
|
|
// }
|
|
|
|
return "baru saja"
|
|
}
|
|
|
|
func SendEmail(to []string, cc []string, name, subject, message, htmlString string) error {
|
|
sender := "From: HIS <hats.nutech@gmail.com>\n"
|
|
subjectt := "Subject: " + subject + "\n"
|
|
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
|
|
body := htmlString
|
|
messages := []byte(sender + subjectt + mime + body)
|
|
|
|
for _, v := range cc {
|
|
if v != "" {
|
|
to = append(to, cc...)
|
|
}
|
|
}
|
|
|
|
auth := smtp.PlainAuth("", config.Mail().AuthEmail, config.Mail().AuthPassword, config.Mail().SMTPHost)
|
|
smtpAddr := fmt.Sprintf("%s:%d", config.Mail().SMTPHost, config.Mail().SMTPPort)
|
|
|
|
err := smtp.SendMail(smtpAddr, auth, config.Mail().AuthEmail, to, messages)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func ParseRequest(r *http.Request, v any) error {
|
|
err := json.NewDecoder(r.Body).Decode(&v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateRequest(request any) error {
|
|
c := config.NewValidator()
|
|
if err := c.Struct(request); err != nil {
|
|
var errMessage string
|
|
for _, err := range err.(validator.ValidationErrors) {
|
|
fmt.Println(err)
|
|
fieldName := err.Field()
|
|
field, _ := reflect.TypeOf(request).Elem().FieldByName(fieldName)
|
|
jsonField, _ := field.Tag.Lookup("json")
|
|
errMessage = jsonField + " is " + err.ActualTag()
|
|
}
|
|
fmt.Println(errMessage)
|
|
return errors.New(errMessage)
|
|
}
|
|
|
|
return nil
|
|
}
|