From 46cf098dfabf6faf156c4ca33cc9566b4c1cdd8a Mon Sep 17 00:00:00 2001 From: Finley Ghosh Date: Fri, 5 Dec 2025 22:17:38 +1100 Subject: [PATCH] Updating pdf attachment logic --- go/internal/cmc/handlers/quotes/quotes.go | 57 ++++++++++++++++------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/go/internal/cmc/handlers/quotes/quotes.go b/go/internal/cmc/handlers/quotes/quotes.go index 9fb3d62a..849e0ed2 100644 --- a/go/internal/cmc/handlers/quotes/quotes.go +++ b/go/internal/cmc/handlers/quotes/quotes.go @@ -4,6 +4,8 @@ import ( "context" "database/sql" "fmt" + "io" + "log" "net/http" "os" "strconv" @@ -502,25 +504,46 @@ func (h *QuotesHandler) SendQuoteReminderEmailWithAttachment(ctx context.Context return fmt.Errorf("invalid reminder type: %v", reminderType) } - // Check if PDF exists and prepare attachment if found + // Download PDF from URL and prepare attachment if available var attachments []interface{} - if _, err := os.Stat(pdfPath); err == nil { - // PDF exists, attach it - type Attachment struct { - Filename string - FilePath string - } - attachments = []interface{}{ - Attachment{ - Filename: pdfFilename, - FilePath: pdfPath, - }, + resp, err := http.Get(pdfPath) + if err == nil && resp.StatusCode == 200 { + defer resp.Body.Close() + + // Create temporary file for the PDF + tmpFile, err := os.CreateTemp("", "quote_*.pdf") + if err == nil { + defer os.Remove(tmpFile.Name()) + defer tmpFile.Close() + + // Copy PDF content to temp file + if _, err := io.Copy(tmpFile, resp.Body); err == nil { + type Attachment struct { + Filename string + FilePath string + } + attachments = []interface{}{ + Attachment{ + Filename: pdfFilename, + FilePath: tmpFile.Name(), + }, + } + } else { + log.Printf("Failed to copy PDF content for quote %d: %v", quoteID, err) + } + } else { + log.Printf("Failed to create temporary file for quote %d PDF: %v", quoteID, err) } + } else if err != nil { + log.Printf("Failed to download PDF from %s for quote %d: %v", pdfPath, quoteID, err) + } else if resp != nil { + defer resp.Body.Close() + log.Printf("PDF download returned status %d for quote %d at %s", resp.StatusCode, quoteID, pdfPath) } - // If PDF doesn't exist, just send email without attachment + // If PDF download fails, just send email without attachment // Send the email (with or without attachment) - err := h.emailService.SendTemplateEmailWithAttachments( + err = h.emailService.SendTemplateEmailWithAttachments( recipient, subject, templateName, @@ -617,8 +640,8 @@ func (h *QuotesHandler) SendManualReminder(w http.ResponseWriter, r *http.Reques ccs = append(ccs, userEmail) } - // Attach PDF quote - pdfPath := fmt.Sprintf("webroot/pdf/%d.pdf", quoteID) + // Attach PDF quote from URL + pdfURL := fmt.Sprintf("https://stg.cmctechnologies.com.au/pdf/%s.pdf", enquiryRef) pdfFilename := fmt.Sprintf("%s.pdf", enquiryRef) // Get username from request @@ -636,7 +659,7 @@ func (h *QuotesHandler) SendManualReminder(w http.ResponseWriter, r *http.Reques templateData, ccs, usernamePtr, - pdfPath, + pdfURL, pdfFilename, )