Skip to content

Commit df0adb0

Browse files
committed
feat(contract): enhance contract generation to support language-specific templates
1 parent 03e573a commit df0adb0

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

backend/src/router/contract.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,45 @@ func generateCompanyContractDocx(w http.ResponseWriter, r *http.Request) {
131131
writeJSONError(w, http.StatusUnprocessableEntity, fmt.Sprintf("no companyContract template found for event %d", eventID))
132132
return
133133
}
134-
if len(eventTemplates) > 1 {
135-
writeJSONError(w, http.StatusConflict, fmt.Sprintf("multiple companyContract templates found for event %d; expected exactly one", eventID))
134+
135+
// Among eventTemplates, pick the one that matches the requested language.
136+
// Allow both an EN and a PT template to exist for the same event, but require
137+
// exactly one template for the requested language.
138+
var candidates []struct{ Name, Url, Kind string }
139+
// language regexes
140+
reEn := regexp.MustCompile(`(?i)\b(en|english|ingles)\b`)
141+
rePt := regexp.MustCompile(`(?i)\b(pt|pt_pt|ptbr|pt_br|portuguese|portugues)\b`)
142+
for _, t := range eventTemplates {
143+
name := strings.ToLower(t.Name)
144+
// match name tokens against requested language
145+
if lowerLang == "pt" {
146+
if rePt.MatchString(name) {
147+
candidates = append(candidates, t)
148+
}
149+
} else { // en
150+
if reEn.MatchString(name) {
151+
candidates = append(candidates, t)
152+
}
153+
}
154+
}
155+
156+
if len(candidates) == 0 {
157+
// no template for requested language
158+
writeJSONError(w, http.StatusUnprocessableEntity, fmt.Sprintf("no companyContract template found for event %d and language %s", eventID, lowerLang))
159+
return
160+
}
161+
if len(candidates) > 1 {
162+
writeJSONError(w, http.StatusConflict, fmt.Sprintf("multiple companyContract templates found for event %d and language %s; expected exactly one", eventID, lowerLang))
136163
return
137164
}
138-
// use the single template
139-
if eventTemplates[0].Url == "" {
165+
166+
// use the single matching template
167+
if candidates[0].Url == "" {
140168
writeJSONError(w, http.StatusUnprocessableEntity, "template has no URL")
141169
return
142170
}
143-
templateURL = eventTemplates[0].Url
144-
log.Printf("generateCompanyContractDocx: picked event template name=%s url=%s", eventTemplates[0].Name, templateURL)
171+
templateURL = candidates[0].Url
172+
log.Printf("generateCompanyContractDocx: picked event template name=%s url=%s", candidates[0].Name, templateURL)
145173

146174
// No global fallback: templates must be event-scoped and of kind `companyContract`.
147175

0 commit comments

Comments
 (0)