Skip to content

Commit 5456c71

Browse files
committed
refactor: remove unnecessary comments and update file reading method in template handling
1 parent 89330bf commit 5456c71

File tree

4 files changed

+5
-40
lines changed

4 files changed

+5
-40
lines changed

backend/src/models/template.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type Template struct {
1111

1212
Name string `json:"name" bson:"name"`
1313
Url string `json:"url" bson:"url"`
14-
// Event (edition) this template is associated with. Optional.
1514
Event int `json:"event,omitempty" bson:"event,omitempty"`
1615
Requirements []Requirement `json:"requirements" bson:"requirements"`
1716
Kind string `json:"kind" bson:"kind"`

backend/src/router/contract.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
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

2423
type 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

3428
func 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.
4236
func 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

backend/src/router/templates.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ func getTemplates(w http.ResponseWriter, r *http.Request) {
175175
json.NewEncoder(w).Encode(templates)
176176
}
177177

178-
// uploadTemplateFile uploads the provided file to Spaces for the given template ID
179-
// and associates the returned CDN URL with the template document in DB. Requires
180-
// query parameter `event` (integer) to determine the event (edition) path.
178+
181179
func uploadTemplateFile(w http.ResponseWriter, r *http.Request) {
182180
params := mux.Vars(r)
183181
templateId, err := primitive.ObjectIDFromHex(params["id"])
@@ -212,7 +210,7 @@ func uploadTemplateFile(w http.ResponseWriter, r *http.Request) {
212210
defer file.Close()
213211

214212
// Read file bytes
215-
b, err := ioutil.ReadAll(file)
213+
b, err := io.ReadAll(file)
216214
if err != nil {
217215
http.Error(w, "unable to read file", http.StatusInternalServerError)
218216
return
@@ -321,7 +319,7 @@ func uploadTemplateFileByName(w http.ResponseWriter, r *http.Request) {
321319
}
322320
defer file.Close()
323321

324-
b, err := ioutil.ReadAll(file)
322+
b, err := io.ReadAll(file)
325323
if err != nil {
326324
http.Error(w, "unable to read file", http.StatusInternalServerError)
327325
return
@@ -348,7 +346,6 @@ func uploadTemplateFileByName(w http.ResponseWriter, r *http.Request) {
348346
json.NewEncoder(w).Encode(updated)
349347
}
350348

351-
// downloadTemplateFile streams the template file stored at template.Url as an attachment.
352349
func downloadTemplateFile(w http.ResponseWriter, r *http.Request) {
353350
params := mux.Vars(r)
354351
templateId, err := primitive.ObjectIDFromHex(params["id"])

frontend/src/views/Dashboard/Companies/CompanyView.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import { getCompanyById, getCompanyRepresentatives } from "@/api/companies";
6161
import CompanyCard from "@/components/cards/CompanyCard.vue";
6262
import CompanyBillingInfo from "@/components/companies/CompanyBillingInfo.vue";
63-
// Button import removed; ContractDownload provides buttons
6463
import CompanyContacts from "@/components/companies/CompanyContacts.vue";
6564
import CompanyCommunications from "@/components/companies/CompanyCommunications.vue";
6665
import ParticipationsCard from "@/components/ParticipationsCard.vue";

0 commit comments

Comments
 (0)