55 "encoding/json"
66 "fmt"
77 "io/ioutil"
8- "log"
98 "net/http"
109 "os"
1110 "path/filepath"
@@ -22,13 +21,8 @@ import (
2221)
2322
2423type contractRequest struct {
25- Language string `json:"language"`
26- EventID int `json:"eventId"`
27- CompanyNif string `json:"companyNif"`
28- CompanyAddress string `json:"companyAddress"`
29- CompanyName string `json:"companyName"`
30- PackageName string `json:"packageName"`
31- PackagePrice string `json:"packagePrice"`
24+ Language string `json:"language"`
25+ EventID int `json:"eventId"`
3226}
3327
3428func writeJSONError (w http.ResponseWriter , status int , msg string ) {
@@ -40,18 +34,14 @@ func writeJSONError(w http.ResponseWriter, status int, msg string) {
4034// generateCompanyContractDocx downloads a DOCX template, replaces variables and
4135// returns the filled DOCX as a download.
4236func generateCompanyContractDocx (w http.ResponseWriter , r * http.Request ) {
43- // Disable verbose logging from gooxml (library prints warnings about
44- // unsupported Office XML elements which are harmless for our use).
4537 gooxml .DisableLogging ()
4638 // Recover from panics to ensure we log the error and return 500.
4739 defer func () {
4840 if rec := recover (); rec != nil {
49- log .Printf ("panic in generateCompanyContractDocx: %v" , rec )
5041 http .Error (w , "internal server error" , http .StatusInternalServerError )
5142 }
5243 }()
5344
54- log .Printf ("generateCompanyContractDocx: %s %s" , r .Method , r .URL .Path )
5545 // Read company ID from path and fetch server-side data.
5646 params := mux .Vars (r )
5747 companyHex , ok := params ["id" ]
@@ -62,14 +52,12 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
6252
6353 companyID , err := primitive .ObjectIDFromHex (companyHex )
6454 if err != nil {
65- log .Printf ("invalid company id '%s': %v" , companyHex , err )
6655 writeJSONError (w , http .StatusBadRequest , "invalid company id" )
6756 return
6857 }
6958
7059 company , err := mongodb .Companies .GetCompany (companyID )
7160 if err != nil {
72- log .Printf ("unable to find company %s: %v" , companyID .Hex (), err )
7361 writeJSONError (w , http .StatusNotFound , "unable to find company: " + err .Error ())
7462 return
7563 }
@@ -78,32 +66,27 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
7866
7967 var req contractRequest
8068 _ = json .NewDecoder (r .Body ).Decode (& req )
81- log .Printf ("generateCompanyContractDocx: request body: %+v" , req )
8269
8370 // Prefer templates stored in DB (which should point to Spaces CDN).
8471 templateURL := ""
8572
8673 // Require explicit eventId in the request. There must be exactly one
8774 // `companyContract` template for the given event.
8875 if req .EventID == 0 {
89- log .Printf ("generateCompanyContractDocx: missing required eventId in request" )
9076 writeJSONError (w , http .StatusBadRequest , "eventId is required" )
9177 return
9278 }
9379 eventID := req .EventID
94- log .Printf ("generateCompanyContractDocx: using eventId from request: %d" , eventID )
9580
9681 // Require language in the request (e.g. "en" or "pt").
9782 if strings .TrimSpace (req .Language ) == "" {
98- log .Printf ("generateCompanyContractDocx: missing required language in request" )
9983 writeJSONError (w , http .StatusBadRequest , "language is required" )
10084 return
10185 }
10286
10387 // Validate allowed language values (only 'en' and 'pt' supported).
10488 lowerLang := strings .ToLower (strings .TrimSpace (req .Language ))
10589 if lowerLang != "en" && lowerLang != "pt" {
106- log .Printf ("generateCompanyContractDocx: unsupported language '%s'" , req .Language )
10790 writeJSONError (w , http .StatusUnprocessableEntity , "unsupported language; allowed values are: en, pt" )
10891 return
10992 }
@@ -112,11 +95,9 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
11295 opts .EventID = & eventID
11396 templates , err := mongodb .Templates .GetTemplates (opts )
11497 if err != nil {
115- log .Printf ("unable to retrieve templates for event %d: %v" , eventID , err )
11698 writeJSONError (w , http .StatusInternalServerError , "unable to retrieve templates for event" )
11799 return
118100 }
119- log .Printf ("generateCompanyContractDocx: found %d templates for event %d" , len (templates ), eventID )
120101
121102 // filter to templates of kind `companyContract` (case-insensitive)
122103 var eventTemplates []struct { Name , Url , Kind string }
@@ -125,7 +106,6 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
125106 eventTemplates = append (eventTemplates , struct { Name , Url , Kind string }{t .Name , t .Url , t .Kind })
126107 }
127108 }
128- log .Printf ("generateCompanyContractDocx: found %d event templates of kind=companyContract" , len (eventTemplates ))
129109
130110 if len (eventTemplates ) == 0 {
131111 writeJSONError (w , http .StatusUnprocessableEntity , fmt .Sprintf ("no companyContract template found for event %d" , eventID ))
@@ -169,7 +149,6 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
169149 return
170150 }
171151 templateURL = candidates [0 ].Url
172- log .Printf ("generateCompanyContractDocx: picked event template name=%s url=%s" , candidates [0 ].Name , templateURL )
173152
174153 // No global fallback: templates must be event-scoped and of kind `companyContract`.
175154
@@ -179,22 +158,15 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
179158 return
180159 }
181160
182- log .Printf ("generateCompanyContractDocx: using template URL %s" , templateURL )
183161 resp , err := http .Get (templateURL )
184162 if err != nil || resp .StatusCode >= 400 {
185- if err != nil {
186- log .Printf ("error downloading template %s: %v" , templateURL , err )
187- } else {
188- log .Printf ("error downloading template %s: status=%d" , templateURL , resp .StatusCode )
189- }
190163 writeJSONError (w , http .StatusBadGateway , "unable to download template" )
191164 return
192165 }
193166 defer resp .Body .Close ()
194167
195168 b , err := ioutil .ReadAll (resp .Body )
196169 if err != nil {
197- log .Printf ("unable to read template body from %s: %v" , templateURL , err )
198170 writeJSONError (w , http .StatusInternalServerError , "unable to read template" )
199171 return
200172 }
@@ -243,7 +215,6 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
243215
244216 modifiedDocx , err := replaceDocxPlaceholders (b , replacements , replacementsPlain )
245217 if err != nil {
246- log .Printf ("replaceDocxPlaceholders error: %v" , err )
247218 writeJSONError (w , http .StatusInternalServerError , "error processing template" )
248219 return
249220 }
@@ -253,7 +224,6 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
253224 w .Header ().Set ("Content-Type" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document" )
254225 w .Header ().Set ("Content-Disposition" , fmt .Sprintf ("attachment; filename=\" %s\" " , filename ))
255226 if _ , err := w .Write (modifiedDocx ); err != nil {
256- log .Printf ("error writing modified docx response: %v" , err )
257227 }
258228}
259229
0 commit comments