This repository was archived by the owner on Jul 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinvoice.go
More file actions
160 lines (139 loc) · 3.72 KB
/
Copy pathinvoice.go
File metadata and controls
160 lines (139 loc) · 3.72 KB
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
package main
import (
"context"
"crypto/tls"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/Shopify/gomail"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
func sendInvoice(id int) error {
dog, err := getDog(id)
if err != nil {
return err
}
_, err = generatePdf(dog)
if err != nil {
return err
}
err = sendEmail(dog)
if err != nil {
return err
}
return nil
}
func sendInvoices() (status string, err error) {
emails, err := getEmailQueue()
if err != nil {
return "", err
}
err = markEmailsInProcess(emails)
if err != nil {
return "", err
}
for _, email := range emails {
if err != nil {
return "", err
}
err = sendInvoice(email.DogID)
if err != nil {
return "", err
}
err = markEmailSent(email.ID)
if err != nil {
return "", err
}
}
return "Processed " + strconv.Itoa(len(emails)) + " emails", nil
}
func generatePdf(dog Dog) (string, error) {
// Create a headless Chrome context with no sandbox - required for running in non-root environments
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.NoSandbox,
chromedp.Flag("disable-setuid-sandbox", true),
)
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var pdfBuf []byte
err := chromedp.Run(ctx,
chromedp.Navigate(os.Getenv("BASE_URL")+"/invoice/"+strconv.Itoa(dog.ID)),
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
pdfBuf, _, err = page.PrintToPDF().
WithPrintBackground(true).
WithScale(0.8).
WithPaperHeight(12).
Do(ctx)
return err
}),
)
if err != nil {
return "", fmt.Errorf("error generating PDF: %w", err)
}
invoiceFile := fmt.Sprintf("./public/%s.pdf", getInvoiceNumber(dog))
// Save to file
if err := os.WriteFile(invoiceFile, pdfBuf, 0644); err != nil {
return "", fmt.Errorf("error writing PDF to file: %w", err)
}
return invoiceFile, nil
}
func sendEmail(dog Dog) error {
invoiceFile := fmt.Sprintf("./public/%s.pdf", getInvoiceNumber(dog))
smtpPort, err := strconv.Atoi(os.Getenv("SMTP_PORT"))
if err != nil {
return fmt.Errorf("error converting SMTP_PORT to int: %s", err)
}
d := gomail.NewDialer(
os.Getenv("SMTP_HOST"),
smtpPort,
os.Getenv("SMTP_USER"),
os.Getenv("SMTP_PASS"),
)
d.Timeout = 30 * time.Second
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
ownerFirstName := strings.Split(dog.OwnerName, " ")[0]
fromFirstName := strings.Split(os.Getenv("FROM_NAME"), " ")[0]
m := gomail.NewMessage()
m.SetHeader("From", fmt.Sprintf("Canine Club<%s>", os.Getenv("SMTP_USER")))
m.SetHeader("To", fmt.Sprintf("%s <%s>", dog.OwnerName, dog.Email))
m.SetHeader("Subject", "Canine Club - Invoice for "+dog.Name)
m.SetBody(
"text/html",
"Hi "+ownerFirstName+",<br><br>Please find attached the invoice for "+dog.Name+"'s walks this week.<p style='font-weight:lighter;'>Please use '<b>"+dog.Name+"</b>' as the reference when making payment. Also note that payment is due by "+nextMonday(
time.Now(),
).Format("Monday, 2 January 2006")+
".</p><br>Any questions let me know,<br>Thank you!<br><br>"+fromFirstName+"<br>Canine Club",
)
m.Attach(invoiceFile)
err = d.DialAndSend(m)
if err != nil {
return err
} else {
return nil
}
}
func getInvoiceNumber(dog Dog) string {
name := dog.Name
if len(name) < 3 {
name = name + strings.Repeat("0", 3-len(name))
}
prefix := strings.ToUpper(name[0:3])
return prefix + time.Now().Format("20060102")
}
func nextMonday(t time.Time) time.Time {
if t.Weekday() == time.Monday {
return t.AddDate(0, 0, 7)
}
for t.Weekday() != time.Monday {
t = t.AddDate(0, 0, 1)
}
return t
}