fix
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
reLegacyHTMLBlock = regexp.MustCompile(`(?is)<\s*(h[1-4]|p|ul|ol|li|b|strong)\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*(?:h[1-4]|p|ul|ol|li|b|strong)\s*>`)
|
||||
reLegacyBoldUL = regexp.MustCompile(`(?is)<\s*b\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*b\s*>\s*<\s*ul\s*>\s*<\s*li\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*li\s*>\s*<\s*/\s*ul\s*>`)
|
||||
)
|
||||
|
||||
// DescriptionFormulaSection is one ordered block in categories.description_template.
|
||||
type DescriptionFormulaSection struct {
|
||||
Type string `json:"type"`
|
||||
Instructions string `json:"instructions"`
|
||||
}
|
||||
|
||||
// DescriptionFormula is the JSON shape stored in categories.description_template
|
||||
// (sections + optional SEO meta instruction strings).
|
||||
type DescriptionFormula struct {
|
||||
Sections []DescriptionFormulaSection `json:"sections,omitempty"`
|
||||
MetaTitle string `json:"metaTitle,omitempty"`
|
||||
MetaDescription string `json:"metaDescription,omitempty"`
|
||||
}
|
||||
|
||||
// DescriptionTemplateNeedsRepair is true when template is missing or has no sections
|
||||
// (and no usable meta instructions).
|
||||
func DescriptionTemplateNeedsRepair(template any) bool {
|
||||
f, ok := parseDescriptionFormula(template)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
if len(f.Sections) > 0 {
|
||||
return false
|
||||
}
|
||||
return strings.TrimSpace(f.MetaTitle) == "" && strings.TrimSpace(f.MetaDescription) == ""
|
||||
}
|
||||
|
||||
// DeriveDescriptionFormulaFromLegacyParts builds description_template from split
|
||||
// legacy DescriptionRules HTML + MetaRules.
|
||||
func DeriveDescriptionFormulaFromLegacyParts(parts LegacyEnhanceParts) DescriptionFormula {
|
||||
out := DescriptionFormula{
|
||||
MetaDescription: strings.TrimSpace(parts.MetaRules),
|
||||
}
|
||||
body := strings.TrimSpace(parts.DescriptionRules)
|
||||
if body == "" {
|
||||
return out
|
||||
}
|
||||
|
||||
// Walk left-to-right so section order matches the legacy HTML template.
|
||||
remaining := body
|
||||
for remaining != "" {
|
||||
boldUL := reLegacyBoldUL.FindStringSubmatchIndex(remaining)
|
||||
block := reLegacyHTMLBlock.FindStringSubmatchIndex(remaining)
|
||||
switch {
|
||||
case boldUL != nil && (block == nil || boldUL[0] <= block[0]):
|
||||
m := reLegacyBoldUL.FindStringSubmatch(remaining[boldUL[0]:boldUL[1]])
|
||||
heading, instr := "", ""
|
||||
if len(m) >= 3 {
|
||||
heading = cleanLegacyInstruction(m[1])
|
||||
instr = cleanLegacyInstruction(m[2])
|
||||
}
|
||||
if heading != "" && instr != "" && !strings.Contains(strings.ToLower(instr), strings.ToLower(heading)) {
|
||||
instr = heading + ": " + instr
|
||||
} else if instr == "" {
|
||||
instr = heading
|
||||
}
|
||||
if strings.TrimSpace(instr) != "" {
|
||||
out.Sections = append(out.Sections, DescriptionFormulaSection{Type: "ul", Instructions: instr})
|
||||
}
|
||||
remaining = remaining[boldUL[1]:]
|
||||
case block != nil:
|
||||
m := reLegacyHTMLBlock.FindStringSubmatch(remaining[block[0]:block[1]])
|
||||
if len(m) >= 3 {
|
||||
typ := strings.ToLower(strings.TrimSpace(m[1]))
|
||||
instr := cleanLegacyInstruction(m[2])
|
||||
if instr != "" {
|
||||
switch typ {
|
||||
case "b", "strong":
|
||||
typ = "h2"
|
||||
case "li", "ol":
|
||||
typ = "ul"
|
||||
}
|
||||
out.Sections = append(out.Sections, DescriptionFormulaSection{Type: typ, Instructions: instr})
|
||||
}
|
||||
}
|
||||
remaining = remaining[block[1]:]
|
||||
default:
|
||||
return out
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// DeriveDescriptionTemplateJSON encodes DeriveDescriptionFormulaFromLegacyParts for DB write.
|
||||
func DeriveDescriptionTemplateJSON(parts LegacyEnhanceParts) string {
|
||||
f := DeriveDescriptionFormulaFromLegacyParts(parts)
|
||||
if len(f.Sections) == 0 && strings.TrimSpace(f.MetaTitle) == "" && strings.TrimSpace(f.MetaDescription) == "" {
|
||||
return ""
|
||||
}
|
||||
b, err := json.Marshal(f)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func parseDescriptionFormula(template any) (DescriptionFormula, bool) {
|
||||
if template == nil {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
switch t := template.(type) {
|
||||
case DescriptionFormula:
|
||||
return t, true
|
||||
case *DescriptionFormula:
|
||||
if t == nil {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
return *t, true
|
||||
case []byte:
|
||||
if len(t) == 0 {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
var f DescriptionFormula
|
||||
if err := json.Unmarshal(t, &f); err != nil {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
return f, true
|
||||
case string:
|
||||
s := strings.TrimSpace(t)
|
||||
if s == "" || s == "null" || s == "{}" {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
var f DescriptionFormula
|
||||
if err := json.Unmarshal([]byte(s), &f); err != nil {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
return f, true
|
||||
default:
|
||||
b, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
var f DescriptionFormula
|
||||
if err := json.Unmarshal(b, &f); err != nil {
|
||||
return DescriptionFormula{}, false
|
||||
}
|
||||
return f, true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeriveDescriptionFormulaFromLegacyParts(t *testing.T) {
|
||||
t.Parallel()
|
||||
legacy := `Ustvari nov opis
|
||||
|
||||
GPT predloga:
|
||||
<name>{Napiši novo ime izdelka po formuli: "znamka", "tip izdelka lowercase". Ne uporabljaj vejic.}</name>
|
||||
<metaDescription>{Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov}</metaDescription>
|
||||
|
||||
<H2>{Napiši Novo ime izdelka in izpostavi en benefit}</H2>
|
||||
<p>{Napiši odstavek, ki je dolg 100 besed.}</p>
|
||||
<b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>`
|
||||
|
||||
parts := ParseLegacyCombinedEnhancePrompt(legacy)
|
||||
if !parts.WasLegacy {
|
||||
t.Fatal("expected legacy parse")
|
||||
}
|
||||
f := DeriveDescriptionFormulaFromLegacyParts(parts)
|
||||
if strings.TrimSpace(f.MetaDescription) == "" || !strings.Contains(f.MetaDescription, "140") {
|
||||
t.Fatalf("metaDescription=%q", f.MetaDescription)
|
||||
}
|
||||
if len(f.Sections) < 3 {
|
||||
t.Fatalf("want >=3 sections, got %#v", f.Sections)
|
||||
}
|
||||
if f.Sections[0].Type != "h2" {
|
||||
t.Fatalf("first section should be h2 (document order), got %#v", f.Sections[0])
|
||||
}
|
||||
last := f.Sections[len(f.Sections)-1]
|
||||
if last.Type != "ul" {
|
||||
t.Fatalf("last section should be ul specs, got %#v", last)
|
||||
}
|
||||
var sawH2, sawP, sawUL bool
|
||||
for _, s := range f.Sections {
|
||||
switch s.Type {
|
||||
case "h2":
|
||||
sawH2 = true
|
||||
case "p":
|
||||
sawP = true
|
||||
case "ul":
|
||||
sawUL = true
|
||||
}
|
||||
}
|
||||
if !sawH2 || !sawP || !sawUL {
|
||||
t.Fatalf("missing section types: %#v", f.Sections)
|
||||
}
|
||||
raw := DeriveDescriptionTemplateJSON(parts)
|
||||
if raw == "" || DescriptionTemplateNeedsRepair([]byte(raw)) {
|
||||
t.Fatalf("derived template should be OK: %s", raw)
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ type DefaultTemplate struct {
|
||||
const CategoryEnhanceUserTemplate = `Your reply is parsed as JSON {"name":"string","description":"string","meta_title":"string","meta_description":"string","attrs":{}} only (system schema). Write all string fields in {{language}} (do not hardcode a language).
|
||||
|
||||
--- Title ---
|
||||
Role: title. Build JSON "name": short retail title; follow any Title formula constraints that follow; use Attrs.
|
||||
` + TitleRoleInstruction + `
|
||||
Name: {{name}}
|
||||
--- End Title ---
|
||||
|
||||
@@ -84,13 +84,22 @@ Attrs: {{attrs}}
|
||||
--- End Attributes ---`
|
||||
|
||||
// CategoryEnhancePromptNeedsRepair reports whether a stored categories.prompt value
|
||||
// should be replaced by CategoryEnhanceUserTemplate (idempotent equality check).
|
||||
// should be rewritten into role-sectioned enhance overlay form. Empty prompts are
|
||||
// left alone. Canonical CategoryEnhanceUserTemplate and per-category overlays that
|
||||
// already carry Title/Description/Meta/Attributes markers (+ {{attrs}}) are OK —
|
||||
// equality with the shared template is not required (legacy splits keep Slovenian rules).
|
||||
func CategoryEnhancePromptNeedsRepair(prompt string) bool {
|
||||
p := strings.TrimSpace(prompt)
|
||||
if p == "" {
|
||||
return false
|
||||
}
|
||||
return p != strings.TrimSpace(CategoryEnhanceUserTemplate)
|
||||
if IsLegacyCombinedEnhancePrompt(p) {
|
||||
return true
|
||||
}
|
||||
if CategoryEnhanceHasRoleSections(p) && strings.Contains(p, "{{attrs}}") {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// BuiltInDefaults match the previous hardcoded system prompts, with structured user templates.
|
||||
@@ -103,7 +112,7 @@ var BuiltInDefaults = []DefaultTemplate{
|
||||
Rules:
|
||||
- Reply with ONLY JSON (no markdown)
|
||||
- Schema: {"name":"string","description":"string","meta_title":"string","meta_description":"string","attrs":{}}
|
||||
- name: short retail title
|
||||
- name: short retail title — never brand-only; follow any Title formula (type + brand + model); use Attrs
|
||||
- description: product body HTML only — follow any Description formula in the user message (structured HTML sections); otherwise 1-3 factual paragraphs; never copy name as description; never put SEO meta here
|
||||
- meta_title: plain SEO title 50-60 chars (follow any SEO meta formula)
|
||||
- meta_description: plain SEO snippet 120-155 chars (follow any SEO meta formula); never HTML
|
||||
|
||||
@@ -51,6 +51,14 @@ func TestCategoryEnhancePromptNeedsRepair(t *testing.T) {
|
||||
if !CategoryEnhancePromptNeedsRepair(legacy) {
|
||||
t.Fatal("legacy HTML prompt should need repair")
|
||||
}
|
||||
// Per-category sectioned overlay (Slovenian rules) must not force re-repair.
|
||||
split := SplitLegacyCombinedEnhancePrompt(`GPT predloga:
|
||||
<name>{Napiši tip izdelka}</name>
|
||||
<metaDescription>{140 znakov}</metaDescription>
|
||||
<H2>{benefit}</H2>`)
|
||||
if CategoryEnhancePromptNeedsRepair(split) {
|
||||
t.Fatal("sectioned split overlay should be idempotent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuiltInProductEnhanceUsesSharedUserTemplate(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
reLegacyNameTag = regexp.MustCompile(`(?is)<\s*name\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*name\s*>`)
|
||||
reLegacyMetaTag = regexp.MustCompile(`(?is)<\s*metaDescription\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*metaDescription\s*>`)
|
||||
reLegacyDescPH = regexp.MustCompile(`(?i)\{\s*""?\s*OPIS\s+IZDELKA\s*""?\s*\}`)
|
||||
reLegacyNamePH = regexp.MustCompile(`(?i)\{\s*""?\s*STARO\s+IME\s+IZDELKA\s*""?\s*\}`)
|
||||
reDoubledQuotes = regexp.MustCompile(`"{2,}`)
|
||||
)
|
||||
|
||||
// LegacyEnhanceParts holds extracted role content from a combined PHP/A1 category Prompt.
|
||||
type LegacyEnhanceParts struct {
|
||||
TitleRules string
|
||||
DescriptionRules string
|
||||
MetaRules string
|
||||
WasLegacy bool
|
||||
}
|
||||
|
||||
// IsLegacyCombinedEnhancePrompt reports whether prompt looks like the old A1/PHP
|
||||
// combined name+description(+meta) marketing blob (<name>/<metaDescription>/GPT predloga).
|
||||
func IsLegacyCombinedEnhancePrompt(prompt string) bool {
|
||||
p := strings.TrimSpace(prompt)
|
||||
if p == "" {
|
||||
return false
|
||||
}
|
||||
// Require real tag wrappers — not instructional mentions like
|
||||
// `do NOT … <name>/<metaDescription> tags` in CategoryEnhanceUserTemplate.
|
||||
hasNameTag := reLegacyNameTag.MatchString(p)
|
||||
hasMetaTag := reLegacyMetaTag.MatchString(p)
|
||||
if hasNameTag && (hasMetaTag || strings.Contains(strings.ToLower(p), "gpt predloga")) {
|
||||
return true
|
||||
}
|
||||
if hasNameTag && (strings.Contains(p, "STARO IME IZDELKA") || strings.Contains(p, "OPIS IZDELKA")) {
|
||||
return true
|
||||
}
|
||||
if strings.Contains(p, "STARO IME IZDELKA") && strings.Contains(p, "OPIS IZDELKA") && strings.Contains(strings.ToLower(p), "gpt predloga") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ParseLegacyCombinedEnhancePrompt extracts title / description / meta instruction
|
||||
// blobs from a legacy combined prompt. Placeholders are modernized to {{name}} /
|
||||
// {{description}}. WasLegacy is false when no combined markers are found.
|
||||
func ParseLegacyCombinedEnhancePrompt(prompt string) LegacyEnhanceParts {
|
||||
raw := strings.TrimSpace(prompt)
|
||||
out := LegacyEnhanceParts{}
|
||||
if raw == "" || !IsLegacyCombinedEnhancePrompt(raw) {
|
||||
return out
|
||||
}
|
||||
out.WasLegacy = true
|
||||
|
||||
if m := reLegacyNameTag.FindStringSubmatch(raw); len(m) == 2 {
|
||||
out.TitleRules = cleanLegacyInstruction(m[1])
|
||||
}
|
||||
if m := reLegacyMetaTag.FindStringSubmatch(raw); len(m) == 2 {
|
||||
out.MetaRules = cleanLegacyInstruction(m[1])
|
||||
}
|
||||
|
||||
rest := reLegacyNameTag.ReplaceAllString(raw, "")
|
||||
rest = reLegacyMetaTag.ReplaceAllString(rest, "")
|
||||
if idx := strings.Index(strings.ToLower(rest), "gpt predloga:"); idx >= 0 {
|
||||
rest = rest[idx+len("gpt predloga:"):]
|
||||
}
|
||||
// Drop Slovenian boilerplate intro lines that are not body structure.
|
||||
rest = stripLegacyIntroNoise(rest)
|
||||
out.DescriptionRules = cleanLegacyInstruction(rest)
|
||||
return out
|
||||
}
|
||||
|
||||
// SplitLegacyCombinedEnhancePrompt rewrites a legacy combined name+description(+meta)
|
||||
// prompt into CategoryEnhanceUserTemplate role sections, preserving Slovenian
|
||||
// naming / HTML / meta intent as category-specific rules under Title / Description / Meta.
|
||||
// Non-legacy input returns CategoryEnhanceUserTemplate (canonical shared overlay).
|
||||
func SplitLegacyCombinedEnhancePrompt(prompt string) string {
|
||||
parts := ParseLegacyCombinedEnhancePrompt(prompt)
|
||||
if !parts.WasLegacy {
|
||||
return strings.TrimSpace(CategoryEnhanceUserTemplate)
|
||||
}
|
||||
return BuildCategoryEnhanceOverlay(parts)
|
||||
}
|
||||
|
||||
// BuildCategoryEnhanceOverlay composes a role-sectioned enhance USER overlay from
|
||||
// extracted legacy parts (or empty parts → canonical template text with no extra rules).
|
||||
func BuildCategoryEnhanceOverlay(parts LegacyEnhanceParts) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(`Your reply is parsed as JSON {"name":"string","description":"string","meta_title":"string","meta_description":"string","attrs":{}} only (system schema). Write all string fields in {{language}} (do not hardcode a language).`)
|
||||
b.WriteString("\n\n")
|
||||
|
||||
b.WriteString(SectionTitleStart)
|
||||
b.WriteByte('\n')
|
||||
b.WriteString(TitleRoleInstruction)
|
||||
b.WriteByte('\n')
|
||||
if rules := strings.TrimSpace(parts.TitleRules); rules != "" {
|
||||
b.WriteString("Category naming rules (preserve intent): ")
|
||||
b.WriteString(rules)
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
b.WriteString("Name: {{name}}\n")
|
||||
b.WriteString(SectionTitleEnd)
|
||||
b.WriteString("\n\n")
|
||||
|
||||
b.WriteString(SectionDescriptionStart)
|
||||
b.WriteString("\nRole: description. Build JSON \"description\": product body HTML only (not SEO meta). When a Description formula follows, emit ONE HTML string covering each section in order (tags matching type: h1/h2/h3/h4, p, ul); otherwise prefer 1-3 factual paragraphs as ONE string with limited HTML (<h2><p><ul><li>) — do NOT emit a competing full HTML document or wrap the whole reply in <name>/<metaDescription> tags.\n")
|
||||
if rules := strings.TrimSpace(parts.DescriptionRules); rules != "" {
|
||||
b.WriteString("Category HTML structure (preserve intent; emit as ONE description HTML string, not tagged name/meta blocks):\n")
|
||||
b.WriteString(rules)
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
b.WriteString("Description: {{description}}\n")
|
||||
b.WriteString(SectionDescriptionEnd)
|
||||
b.WriteString("\n\n")
|
||||
|
||||
b.WriteString(SectionMetaStart)
|
||||
b.WriteString("\nRole: meta. Build JSON \"meta_title\" and \"meta_description\" as plain SEO text (never HTML). meta_title: 50-60 chars; meta_description: 120-155 chars; follow any SEO meta formula that follows; never copy the full description HTML into meta_description.\n")
|
||||
if rules := strings.TrimSpace(parts.MetaRules); rules != "" {
|
||||
b.WriteString("Category SEO rules (preserve intent; plain text only): ")
|
||||
b.WriteString(rules)
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
b.WriteString(SectionMetaEnd)
|
||||
b.WriteString("\n\n")
|
||||
|
||||
b.WriteString(SectionAttributesStart)
|
||||
b.WriteString("\nRole: attributes. Build JSON \"attrs\" as an object of attribute_key → value strings. Prefer Allowed attribute keys / Title formula attr slots that follow; remap near-miss labels onto those keys; fill missing keys only from Name/Description/Category/Attrs evidence; never invent specs; omit unknown keys; never invent dimensions.\n")
|
||||
b.WriteString("Category: {{category}}\n")
|
||||
b.WriteString("Attrs: {{attrs}}\n")
|
||||
b.WriteString(SectionAttributesEnd)
|
||||
return strings.TrimSpace(b.String())
|
||||
}
|
||||
|
||||
func cleanLegacyInstruction(s string) string {
|
||||
s = modernizeLegacyPlaceholders(s)
|
||||
s = reDoubledQuotes.ReplaceAllString(s, `"`)
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
s = strings.TrimSpace(s)
|
||||
// Collapse excessive blank lines.
|
||||
for strings.Contains(s, "\n\n\n") {
|
||||
s = strings.ReplaceAll(s, "\n\n\n", "\n\n")
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func modernizeLegacyPlaceholders(prompt string) string {
|
||||
prompt = reLegacyDescPH.ReplaceAllString(prompt, "{{description}}")
|
||||
prompt = reLegacyNamePH.ReplaceAllString(prompt, "{{name}}")
|
||||
return prompt
|
||||
}
|
||||
|
||||
func stripLegacyIntroNoise(s string) string {
|
||||
lines := strings.Split(s, "\n")
|
||||
out := make([]string, 0, len(lines))
|
||||
for _, line := range lines {
|
||||
trim := strings.TrimSpace(line)
|
||||
lower := strings.ToLower(trim)
|
||||
switch {
|
||||
case trim == "":
|
||||
if len(out) > 0 {
|
||||
out = append(out, "")
|
||||
}
|
||||
case strings.HasPrefix(lower, "ustvari nov opis"),
|
||||
strings.HasPrefix(lower, "star_opis_izdelka"),
|
||||
strings.HasPrefix(lower, "staro_ime_izdelka"),
|
||||
strings.HasPrefix(lower, "uporabi spodnjo gpt"),
|
||||
strings.HasPrefix(lower, "sledi tej gpt"):
|
||||
continue
|
||||
default:
|
||||
out = append(out, trim)
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(strings.Join(out, "\n"))
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitLegacyCombinedEnhancePrompt(t *testing.T) {
|
||||
t.Parallel()
|
||||
legacy := `Ustvari nov opis izdelka v Slovenščini z naslednjimi spremenljivkami:
|
||||
|
||||
Star_opis_izdelka: {""OPIS IZDELKA""};
|
||||
Staro_ime_izdelka: {""STARO IME IZDELKA""};
|
||||
|
||||
Uporabi spodnjo GPT predlogo.
|
||||
|
||||
Sledi tej GPT predlogi stavek po stavek in sestavi nov opis izdelka:
|
||||
|
||||
GPT predloga:
|
||||
|
||||
|
||||
<name>{Napiši novo ime izdelka po formuli: ""tip izdelka sentence case"", ""znamka s pravilno kapitalizacijo"". Ne uporabljaj vejic.}</name>
|
||||
<metaDescription>{Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov vključno s presledki}</metaDescription>
|
||||
|
||||
<H2>{Napiši Novo ime izdelka in izpostavi en benefit}</H2>
|
||||
<p>{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}</p><b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>`
|
||||
|
||||
got := SplitLegacyCombinedEnhancePrompt(legacy)
|
||||
if !CategoryEnhanceHasRoleSections(got) {
|
||||
t.Fatal("split output must be role-sectioned")
|
||||
}
|
||||
if CategoryEnhancePromptNeedsRepair(got) {
|
||||
t.Fatal("split output should not need further repair")
|
||||
}
|
||||
if reLegacyNameTag.MatchString(got) || reLegacyMetaTag.MatchString(got) {
|
||||
t.Fatal("split must not keep legacy <name>/<metaDescription> wrapper tags")
|
||||
}
|
||||
if !strings.Contains(got, "tip izdelka sentence case") {
|
||||
t.Fatal("title section must preserve Slovenian naming formula intent")
|
||||
}
|
||||
if !strings.Contains(got, "140 znakov") {
|
||||
t.Fatal("meta section must preserve Slovenian SEO intent")
|
||||
}
|
||||
if !strings.Contains(got, "Tehnične specifikacije") && !strings.Contains(got, "tehničnih specifikacij") {
|
||||
t.Fatal("description section must preserve HTML body intent")
|
||||
}
|
||||
if !strings.Contains(got, "{{attrs}}") || !strings.Contains(got, "{{language}}") {
|
||||
t.Fatal("overlay must keep {{attrs}} and {{language}}")
|
||||
}
|
||||
if strings.Contains(got, "OPIS IZDELKA") || strings.Contains(got, "STARO IME IZDELKA") {
|
||||
t.Fatal("legacy placeholders must be modernized")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitLegacyNonLegacyFallsBackToTemplate(t *testing.T) {
|
||||
t.Parallel()
|
||||
got := SplitLegacyCombinedEnhancePrompt("short retail overlay")
|
||||
if strings.TrimSpace(got) != strings.TrimSpace(CategoryEnhanceUserTemplate) {
|
||||
t.Fatalf("non-legacy should fall back to canonical template")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsLegacyCombinedEnhancePrompt(t *testing.T) {
|
||||
t.Parallel()
|
||||
if IsLegacyCombinedEnhancePrompt(CategoryEnhanceUserTemplate) {
|
||||
t.Fatal("canonical template is not legacy combined")
|
||||
}
|
||||
if !IsLegacyCombinedEnhancePrompt("GPT predloga:\n<name>{x}</name><metaDescription>{y}</metaDescription>") {
|
||||
t.Fatal("expected legacy detection")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
reQuotedNameSlot = regexp.MustCompile(`"([^"]+)"`)
|
||||
reNameFormulaTail = regexp.MustCompile(`(?is)formuli\s*:\s*(.*?)(?:\.?\s*Ne uporabljaj|\.?$)`)
|
||||
)
|
||||
|
||||
// TitleFormulaElement is one ordered slot in categories.title_template.
|
||||
type TitleFormulaElement struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"` // "text" | "variable"
|
||||
Label string `json:"label,omitempty"`
|
||||
Value string `json:"value"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Example string `json:"example,omitempty"`
|
||||
}
|
||||
|
||||
// TitleFormula is the JSON shape stored in categories.title_template.
|
||||
type TitleFormula struct {
|
||||
Separator string `json:"separator"`
|
||||
Elements []TitleFormulaElement `json:"elements"`
|
||||
}
|
||||
|
||||
// DefaultRetailTitleFormula matches the dominant old A1/PHP <name> pattern:
|
||||
// product type + brand + full model (never brand alone).
|
||||
func DefaultRetailTitleFormula() TitleFormula {
|
||||
return TitleFormula{
|
||||
Separator: " ",
|
||||
Elements: []TitleFormulaElement{
|
||||
{ID: "0-variable-product_type", Type: "variable", Label: "Product type", Value: "product_type", Description: "Specific product type (sentence case)", Example: "Gaming monitor"},
|
||||
{ID: "1-variable-brand", Type: "variable", Label: "Brand", Value: "brand", Description: "Brand with correct capitalization", Example: "Samsung"},
|
||||
{ID: "2-variable-product_model", Type: "variable", Label: "Model", Value: "product_model", Description: "Full product model / ID", Example: "Odyssey G5"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// TitleRoleInstruction is the shared Title-section body (CategoryEnhanceUserTemplate
|
||||
// and BuildCategoryEnhanceOverlay). Drives enhance JSON "name"; never brand-only.
|
||||
const TitleRoleInstruction = `Role: title. Build JSON "name": short retail title from Title formula + Attrs — never brand-only. Include product type and full model when evidence exists; follow any Title formula constraints that follow; use Attrs.`
|
||||
|
||||
// TitleTemplateIsBrandOnly reports stubs that only encode brand (the migrated A1
|
||||
// default) so enhance collapses to brand-only names.
|
||||
func TitleTemplateIsBrandOnly(template any) bool {
|
||||
els, ok := titleFormulaElements(template)
|
||||
if !ok || len(els) == 0 {
|
||||
return false
|
||||
}
|
||||
vars := 0
|
||||
brandOnly := true
|
||||
for _, el := range els {
|
||||
switch strings.ToLower(strings.TrimSpace(el.Type)) {
|
||||
case "variable":
|
||||
vars++
|
||||
v := strings.ToLower(strings.TrimSpace(el.Value))
|
||||
v = strings.ReplaceAll(v, "-", "_")
|
||||
if v != "brand" && v != "znamka" {
|
||||
brandOnly = false
|
||||
}
|
||||
case "text":
|
||||
// Free-form naming-rule blobs are not brand-only stubs.
|
||||
if strings.TrimSpace(el.Value) != "" {
|
||||
brandOnly = false
|
||||
}
|
||||
}
|
||||
}
|
||||
return vars > 0 && brandOnly
|
||||
}
|
||||
|
||||
// TitleTemplateNeedsRepair is true when template is missing, unparseable, or
|
||||
// brand-only (insufficient vs old name/title rules).
|
||||
func TitleTemplateNeedsRepair(template any) bool {
|
||||
els, ok := titleFormulaElements(template)
|
||||
if !ok || len(els) == 0 {
|
||||
return true
|
||||
}
|
||||
return TitleTemplateIsBrandOnly(template)
|
||||
}
|
||||
|
||||
// DeriveTitleFormulaFromLegacyNameRules turns PHP/A1 <name> formula text
|
||||
// (e.g. "tip izdelka …", "znamka …", "poln model …") into a structured
|
||||
// title_template. Falls back to DefaultRetailTitleFormula when parsing yields
|
||||
// fewer than two variable slots.
|
||||
func DeriveTitleFormulaFromLegacyNameRules(titleRules string) TitleFormula {
|
||||
slots := extractLegacyNameSlots(titleRules)
|
||||
out := TitleFormula{Separator: " ", Elements: make([]TitleFormulaElement, 0, len(slots))}
|
||||
seen := map[string]struct{}{}
|
||||
for _, slot := range slots {
|
||||
el, ok := mapLegacyNameSlot(slot)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key := el.Type + ":" + strings.ToLower(el.Value)
|
||||
if _, dup := seen[key]; dup {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
el.ID = fmt.Sprintf("%d-%s-%s", len(out.Elements), el.Type, sanitizeFormulaID(el.Value))
|
||||
out.Elements = append(out.Elements, el)
|
||||
}
|
||||
if countFormulaVariables(out) < 2 {
|
||||
return ensureRetailTitleCoverage(out)
|
||||
}
|
||||
return ensureRetailTitleCoverage(out)
|
||||
}
|
||||
|
||||
// DeriveTitleTemplateJSON is the JSON encoding used when repairing categories.title_template.
|
||||
func DeriveTitleTemplateJSON(titleRules string) string {
|
||||
f := DeriveTitleFormulaFromLegacyNameRules(titleRules)
|
||||
b, err := json.Marshal(f)
|
||||
if err != nil {
|
||||
def := DefaultRetailTitleFormula()
|
||||
b, _ = json.Marshal(def)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func titleFormulaElements(template any) ([]TitleFormulaElement, bool) {
|
||||
if template == nil {
|
||||
return nil, false
|
||||
}
|
||||
switch t := template.(type) {
|
||||
case TitleFormula:
|
||||
return t.Elements, len(t.Elements) > 0
|
||||
case *TitleFormula:
|
||||
if t == nil {
|
||||
return nil, false
|
||||
}
|
||||
return t.Elements, len(t.Elements) > 0
|
||||
case []byte:
|
||||
if len(t) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
var f TitleFormula
|
||||
if err := json.Unmarshal(t, &f); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return f.Elements, len(f.Elements) > 0
|
||||
case string:
|
||||
s := strings.TrimSpace(t)
|
||||
if s == "" || s == "null" || s == "{}" {
|
||||
return nil, false
|
||||
}
|
||||
var f TitleFormula
|
||||
if err := json.Unmarshal([]byte(s), &f); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return f.Elements, len(f.Elements) > 0
|
||||
case map[string]any:
|
||||
b, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
var f TitleFormula
|
||||
if err := json.Unmarshal(b, &f); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return f.Elements, len(f.Elements) > 0
|
||||
default:
|
||||
b, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
var f TitleFormula
|
||||
if err := json.Unmarshal(b, &f); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return f.Elements, len(f.Elements) > 0
|
||||
}
|
||||
}
|
||||
|
||||
func extractLegacyNameSlots(rules string) []string {
|
||||
rules = strings.TrimSpace(rules)
|
||||
if rules == "" {
|
||||
return nil
|
||||
}
|
||||
body := rules
|
||||
if m := reNameFormulaTail.FindStringSubmatch(rules); len(m) == 2 {
|
||||
body = m[1]
|
||||
}
|
||||
raw := reQuotedNameSlot.FindAllStringSubmatch(body, -1)
|
||||
out := make([]string, 0, len(raw))
|
||||
for _, m := range raw {
|
||||
s := strings.TrimSpace(m[1])
|
||||
s = strings.Trim(s, `",. `)
|
||||
if s == "" || s == "," {
|
||||
continue
|
||||
}
|
||||
out = append(out, s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mapLegacyNameSlot(slot string) (TitleFormulaElement, bool) {
|
||||
s := strings.TrimSpace(slot)
|
||||
if s == "" {
|
||||
return TitleFormulaElement{}, false
|
||||
}
|
||||
low := strings.ToLower(s)
|
||||
switch {
|
||||
case strings.Contains(low, "znamka"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Brand", Value: "brand", Description: s, Example: "Samsung"}, true
|
||||
case strings.Contains(low, "poln model"), strings.Contains(low, "model izdelka"),
|
||||
strings.HasPrefix(low, "model "), low == "model", strings.Contains(low, "model uppercase"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Model", Value: "product_model", Description: s, Example: "EHT6020"}, true
|
||||
case strings.Contains(low, "tip izdelka"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Product type", Value: "product_type", Description: s, Example: "Bluetooth speaker"}, true
|
||||
case strings.HasPrefix(low, "barva"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Color", Value: "barva", Description: s}, true
|
||||
case strings.Contains(low, "dimenzij"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Dimensions", Value: "dimensions", Description: s}, true
|
||||
case strings.Contains(low, "diagonala"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Diagonal", Value: "diagonala_zaslona", Description: s}, true
|
||||
case strings.Contains(low, "pomnilnik"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Memory", Value: "kapaciteta_ram_pomnilnika", Description: s}, true
|
||||
case strings.Contains(low, "procesor"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "CPU", Value: "procesor", Description: s}, true
|
||||
case strings.Contains(low, "grafi"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "GPU", Value: "graficna_kartica", Description: s}, true
|
||||
case strings.Contains(low, "kapaciteta"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Capacity", Value: "kapaciteta", Description: s}, true
|
||||
case strings.Contains(low, "skupina po te") || strings.Contains(low, "teži") || strings.Contains(low, "tezi"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Weight group", Value: "weight_group", Description: s}, true
|
||||
case strings.Contains(low, "priklju"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Connector", Value: "connector", Description: s}, true
|
||||
case strings.Contains(low, "osvež") || strings.Contains(low, "osvez") || strings.Contains(low, "hz"):
|
||||
return TitleFormulaElement{Type: "variable", Label: "Refresh rate", Value: "refresh_rate", Description: s}, true
|
||||
case strings.Contains(low, "lowercase") || strings.Contains(low, "sentence case") || strings.Contains(low, "uppercase"):
|
||||
// Category-specific type word ("cvrtnik lowercase", "monitor lowercase").
|
||||
return TitleFormulaElement{Type: "variable", Label: "Product type", Value: "product_type", Description: s}, true
|
||||
default:
|
||||
// Keep unrecognized instructional slots as text so order/intent survive.
|
||||
if len([]rune(s)) > 80 {
|
||||
s = string([]rune(s)[:80])
|
||||
}
|
||||
return TitleFormulaElement{Type: "text", Label: "Naming detail", Value: s, Description: "Legacy name-formula slot"}, true
|
||||
}
|
||||
}
|
||||
|
||||
func countFormulaVariables(f TitleFormula) int {
|
||||
n := 0
|
||||
for _, el := range f.Elements {
|
||||
if el.Type == "variable" {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func ensureRetailTitleCoverage(f TitleFormula) TitleFormula {
|
||||
has := map[string]bool{}
|
||||
for _, el := range f.Elements {
|
||||
if el.Type != "variable" {
|
||||
continue
|
||||
}
|
||||
k := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(el.Value), "-", "_"))
|
||||
has[k] = true
|
||||
}
|
||||
def := DefaultRetailTitleFormula()
|
||||
for _, el := range def.Elements {
|
||||
k := strings.ToLower(el.Value)
|
||||
if has[k] {
|
||||
continue
|
||||
}
|
||||
// Only inject core retail slots when missing.
|
||||
if k == "brand" || k == "product_model" || (k == "product_type" && countFormulaVariables(f) < 2) {
|
||||
el.ID = fmt.Sprintf("%d-%s-%s", len(f.Elements), el.Type, el.Value)
|
||||
f.Elements = append(f.Elements, el)
|
||||
has[k] = true
|
||||
}
|
||||
}
|
||||
if countFormulaVariables(f) < 2 {
|
||||
return def
|
||||
}
|
||||
if f.Separator == "" {
|
||||
f.Separator = " "
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func sanitizeFormulaID(v string) string {
|
||||
v = strings.ToLower(strings.TrimSpace(v))
|
||||
v = strings.ReplaceAll(v, " ", "_")
|
||||
v = strings.ReplaceAll(v, "-", "_")
|
||||
if v == "" {
|
||||
return "slot"
|
||||
}
|
||||
if len(v) > 40 {
|
||||
v = v[:40]
|
||||
}
|
||||
return v
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeriveTitleFormulaFromLegacyNameRules_Agregati(t *testing.T) {
|
||||
t.Parallel()
|
||||
rules := `Napiši novo ime izdelka po formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka, če lahko z besedo in ID uppercase". Ne uporabljaj vejic.`
|
||||
f := DeriveTitleFormulaFromLegacyNameRules(rules)
|
||||
if len(f.Elements) < 3 {
|
||||
t.Fatalf("want >=3 elements, got %#v", f.Elements)
|
||||
}
|
||||
vals := make([]string, 0, len(f.Elements))
|
||||
for _, el := range f.Elements {
|
||||
if el.Type == "variable" {
|
||||
vals = append(vals, el.Value)
|
||||
}
|
||||
}
|
||||
wantOrder := []string{"product_type", "brand", "product_model"}
|
||||
for i, w := range wantOrder {
|
||||
if i >= len(vals) || vals[i] != w {
|
||||
t.Fatalf("vars=%v want prefix %v", vals, wantOrder)
|
||||
}
|
||||
}
|
||||
if TitleTemplateIsBrandOnly(f) {
|
||||
t.Fatal("derived formula must not be brand-only")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeriveTitleFormulaFromLegacyNameRules_CarSeatOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
rules := `Napiši novo ime izdelka po formuli: "znamka s pravilno kapitalizacijo", "tip izdelka lowercase", "poln model izdelka, če lahko z besedo in ID uppercase", "skupina po teži [kg]". Ne uporabljaj vejic.`
|
||||
f := DeriveTitleFormulaFromLegacyNameRules(rules)
|
||||
vars := []string{}
|
||||
for _, el := range f.Elements {
|
||||
if el.Type == "variable" {
|
||||
vars = append(vars, el.Value)
|
||||
}
|
||||
}
|
||||
if len(vars) < 3 || vars[0] != "brand" || vars[1] != "product_type" || vars[2] != "product_model" {
|
||||
t.Fatalf("unexpected order: %v", vars)
|
||||
}
|
||||
foundWeight := false
|
||||
for _, v := range vars {
|
||||
if v == "weight_group" {
|
||||
foundWeight = true
|
||||
}
|
||||
}
|
||||
if !foundWeight {
|
||||
t.Fatalf("missing weight_group in %v", vars)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTitleTemplateIsBrandOnly(t *testing.T) {
|
||||
t.Parallel()
|
||||
brandOnly := map[string]any{
|
||||
"separator": " ",
|
||||
"elements": []any{
|
||||
map[string]any{"type": "variable", "value": "brand", "label": "Znamka"},
|
||||
},
|
||||
}
|
||||
if !TitleTemplateIsBrandOnly(brandOnly) {
|
||||
t.Fatal("single brand variable should be brand-only")
|
||||
}
|
||||
if !TitleTemplateNeedsRepair(brandOnly) {
|
||||
t.Fatal("brand-only needs repair")
|
||||
}
|
||||
if TitleTemplateIsBrandOnly(DefaultRetailTitleFormula()) {
|
||||
t.Fatal("default retail must not be brand-only")
|
||||
}
|
||||
if TitleTemplateNeedsRepair(DefaultRetailTitleFormula()) {
|
||||
t.Fatal("default retail must not need repair")
|
||||
}
|
||||
if !TitleTemplateNeedsRepair(nil) {
|
||||
t.Fatal("nil needs repair")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTitleRoleInstructionNeverBrandOnly(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !strings.Contains(strings.ToLower(TitleRoleInstruction), "never brand-only") {
|
||||
t.Fatal("TitleRoleInstruction must forbid brand-only names")
|
||||
}
|
||||
if !strings.Contains(CategoryEnhanceUserTemplate, TitleRoleInstruction) {
|
||||
t.Fatal("CategoryEnhanceUserTemplate must embed TitleRoleInstruction")
|
||||
}
|
||||
split := SplitLegacyCombinedEnhancePrompt(`GPT predloga:
|
||||
<name>{Napiši tip izdelka, znamka, poln model}</name>
|
||||
<metaDescription>{140 znakov}</metaDescription>
|
||||
<H2>{benefit}</H2>`)
|
||||
if !strings.Contains(split, "never brand-only") {
|
||||
t.Fatalf("split Title section missing never brand-only: %s", split)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeriveTitleTemplateJSON_roundTrip(t *testing.T) {
|
||||
t.Parallel()
|
||||
raw := DeriveTitleTemplateJSON(`formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka"`)
|
||||
if TitleTemplateNeedsRepair(raw) {
|
||||
t.Fatalf("derived JSON should be OK: %s", raw)
|
||||
}
|
||||
if !strings.Contains(raw, `"product_type"`) || !strings.Contains(raw, `"brand"`) || !strings.Contains(raw, `"product_model"`) {
|
||||
t.Fatalf("missing core vars: %s", raw)
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,11 @@ func ClientError(err error) (msg string, ok bool) {
|
||||
errors.Is(err, ErrEmailRequired),
|
||||
errors.Is(err, ErrSyntheticEmail),
|
||||
errors.Is(err, ErrNotEligibleSetPassword),
|
||||
errors.Is(err, ErrEmailMismatch):
|
||||
errors.Is(err, ErrEmailMismatch),
|
||||
errors.Is(err, ErrNotCompanyOwner),
|
||||
errors.Is(err, ErrCannotRemoveOwner),
|
||||
errors.Is(err, ErrTransferSelf),
|
||||
errors.Is(err, ErrOwnerRequired):
|
||||
return err.Error(), true
|
||||
default:
|
||||
return "", false
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotCompanyOwner = errors.New("company owner required")
|
||||
ErrCannotRemoveOwner = errors.New("transfer ownership before removing the company owner")
|
||||
ErrTransferSelf = errors.New("user is already the company owner")
|
||||
ErrOwnerRequired = errors.New("new owner must be an active company member")
|
||||
)
|
||||
|
||||
// CompanyOwnerID returns the billing representative for the company, if set.
|
||||
func (s *Service) CompanyOwnerID(ctx context.Context, companyID uuid.UUID) (uuid.UUID, bool, error) {
|
||||
var owner *uuid.UUID
|
||||
err := s.Pool.QueryRow(ctx, `SELECT owner_user_id FROM companies WHERE id = $1`, companyID).Scan(&owner)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return uuid.Nil, false, ErrCompanyNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, false, err
|
||||
}
|
||||
if owner == nil || *owner == uuid.Nil {
|
||||
return uuid.Nil, false, nil
|
||||
}
|
||||
return *owner, true, nil
|
||||
}
|
||||
|
||||
// IsCompanyOwner reports whether userID is the company's owner_user_id.
|
||||
func (s *Service) IsCompanyOwner(ctx context.Context, companyID, userID uuid.UUID) (bool, error) {
|
||||
ownerID, ok, err := s.CompanyOwnerID(ctx, companyID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return ok && ownerID == userID, nil
|
||||
}
|
||||
|
||||
// TransferOwnership sets a new company owner. The target must be an active member.
|
||||
// The new owner is promoted to membership admin so they retain team powers.
|
||||
func (s *Service) TransferOwnership(ctx context.Context, companyID, newOwnerID uuid.UUID) error {
|
||||
tx, err := s.Pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var current *uuid.UUID
|
||||
err = tx.QueryRow(ctx, `SELECT owner_user_id FROM companies WHERE id = $1 FOR UPDATE`, companyID).Scan(¤t)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrCompanyNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current != nil && *current == newOwnerID {
|
||||
return ErrTransferSelf
|
||||
}
|
||||
|
||||
var status string
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT status FROM memberships
|
||||
WHERE company_id = $1 AND user_id = $2`, companyID, newOwnerID).Scan(&status)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrOwnerRequired
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if status != "active" {
|
||||
return ErrOwnerRequired
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, `
|
||||
UPDATE memberships
|
||||
SET role = 'admin', status = 'active', updated_at = now()
|
||||
WHERE company_id = $1 AND user_id = $2`, companyID, newOwnerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE companies SET owner_user_id = $2, updated_at = now() WHERE id = $1`, companyID, newOwnerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrCompanyNotFound
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOwnershipErrorClientFacing(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, err := range []error{
|
||||
ErrNotCompanyOwner,
|
||||
ErrCannotRemoveOwner,
|
||||
ErrTransferSelf,
|
||||
ErrOwnerRequired,
|
||||
} {
|
||||
msg, ok := ClientError(err)
|
||||
if !ok || msg == "" {
|
||||
t.Fatalf("expected client error for %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,8 @@ func (s *Service) Register(ctx context.Context, in RegisterInput) (LoginResult,
|
||||
|
||||
var companyID uuid.UUID
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO companies (name) VALUES ($1) RETURNING id`, strings.TrimSpace(in.CompanyName)).Scan(&companyID)
|
||||
INSERT INTO companies (name, owner_user_id) VALUES ($1, $2) RETURNING id`,
|
||||
strings.TrimSpace(in.CompanyName), userID).Scan(&companyID)
|
||||
if err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
|
||||
@@ -41,22 +41,46 @@ func EnsureCategoryAttributeLinks(ctx context.Context, pool *pgxpool.Pool, compa
|
||||
}
|
||||
|
||||
// RepairCompanyCategoryEnhancePrompts is the company-scoped variant of
|
||||
// RepairA1DemoCategoryEnhancePrompts: same repairedCategoryEnhancePromptMap
|
||||
// (sl + "*" with CategoryEnhanceUserTemplate / {{attrs}}), applied to one company.
|
||||
// Idempotent — already-OK categories count as already_ok, not updated.
|
||||
// RepairA1DemoCategoryEnhancePrompts: loads wp_product_categories.sql when
|
||||
// available (upload bytes, SEED_A1_WP_CATEGORIES / Downloads), else
|
||||
// a1-category-prompts.json, splits legacy combined overlays into role sections,
|
||||
// and force-applies WP dump matches. Falls back to CategoryEnhanceUserTemplate.
|
||||
// Idempotent when unchanged.
|
||||
func RepairCompanyCategoryEnhancePrompts(ctx context.Context, pool *pgxpool.Pool, companyID uuid.UUID) (updated, alreadyOK, emptySkipped int, err error) {
|
||||
if pool == nil {
|
||||
return 0, 0, 0, fmt.Errorf("nil pool")
|
||||
}
|
||||
want, err := repairedCategoryEnhancePromptMap()
|
||||
res, err := RepairCompanyCategoryEnhancePromptsWithOptions(ctx, pool, companyID, RepairA1DemoOptions{})
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
summary := &RepairCategoryEnhancePromptsResult{ByCompany: map[string]int{}}
|
||||
if _, err := repairCompanyCategoryEnhancePrompts(ctx, pool, companyID, companyID.String(), want, false, summary); err != nil {
|
||||
return 0, 0, 0, err
|
||||
return res.Updated, res.AlreadyOK, res.EmptySkipped, nil
|
||||
}
|
||||
|
||||
// RepairCompanyCategoryEnhancePromptsWithOptions applies category enhance prompt
|
||||
// repair for one company. Pass WPCategoriesSQL to force-apply an uploaded dump
|
||||
// (Admin Sync A1); empty opts keep auto-detect fallback.
|
||||
func RepairCompanyCategoryEnhancePromptsWithOptions(ctx context.Context, pool *pgxpool.Pool, companyID uuid.UUID, opts RepairA1DemoOptions) (RepairCategoryEnhancePromptsResult, error) {
|
||||
out := RepairCategoryEnhancePromptsResult{
|
||||
ByCompany: map[string]int{},
|
||||
}
|
||||
return summary.Updated, summary.AlreadyOK, summary.EmptySkipped, nil
|
||||
if pool == nil {
|
||||
return out, fmt.Errorf("nil pool")
|
||||
}
|
||||
if len(opts.WPCategoriesSQL) > 0 {
|
||||
if _, _, err := LoadWPCategoryPromptOverlaysFromBytes(opts.WPCategoriesSQL); err != nil {
|
||||
return out, fmt.Errorf("wp_product_categories upload: %w", err)
|
||||
}
|
||||
}
|
||||
seedByNorm, seedByUID, seedMeta := resolveCategoryPromptOverlays(opts)
|
||||
out.SeedPath = seedMeta.Path
|
||||
out.WPCategoriesPath = seedMeta.WPPath
|
||||
out.SeedEntries = seedMeta.Entries
|
||||
forceFromSeed := seedMeta.ForceFromSeed
|
||||
if opts.ForceFromSeed != nil {
|
||||
forceFromSeed = *opts.ForceFromSeed
|
||||
}
|
||||
if _, err := repairCompanyCategoryEnhancePrompts(ctx, pool, companyID, companyID.String(), seedByNorm, seedByUID, false, forceFromSeed, &out); err != nil {
|
||||
return out, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ResolvePlatformDemoCompanyID finds the Platform Demo sandbox (never A1 cohort).
|
||||
|
||||
@@ -46,3 +46,54 @@ func TestRepairedCategoryEnhancePromptMapMatchesA1Demo(t *testing.T) {
|
||||
t.Fatal("template must include {{attrs}}")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCategoryEnhancePromptMapOKAcceptsSplitOverlay(t *testing.T) {
|
||||
t.Parallel()
|
||||
legacy := `GPT predloga:
|
||||
<name>{Napiši tip izdelka sentence case}</name>
|
||||
<metaDescription>{140 znakov}</metaDescription>
|
||||
<H2>{benefit}</H2>`
|
||||
split := aiprompts.SplitLegacyCombinedEnhancePrompt(legacy)
|
||||
m := company.LangPromptMap{"sl": split, company.LangPromptAny: split}
|
||||
if !categoryEnhancePromptMapOK(m) {
|
||||
t.Fatal("split overlay should count as OK")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJsonbTemplatePresent(t *testing.T) {
|
||||
t.Parallel()
|
||||
if jsonbTemplatePresent(nil) || jsonbTemplatePresent([]byte("null")) || jsonbTemplatePresent([]byte("{}")) {
|
||||
t.Fatal("empty templates must be absent")
|
||||
}
|
||||
if !jsonbTemplatePresent([]byte(`{"elements":[{"type":"variable","value":"brand"}]}`)) {
|
||||
t.Fatal("title elements should count")
|
||||
}
|
||||
if !jsonbTemplatePresent([]byte(`{"sections":[{"type":"p","instructions":"x"}]}`)) {
|
||||
t.Fatal("description sections should count")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeriveTitleTemplateJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
got := aiprompts.DeriveTitleTemplateJSON(`Napiši novo ime po formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka"`)
|
||||
if aiprompts.TitleTemplateNeedsRepair(got) {
|
||||
t.Fatalf("derived formula still needs repair: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "product_type") || !strings.Contains(got, "brand") || !strings.Contains(got, "product_model") {
|
||||
t.Fatalf("expected type+brand+model: %s", got)
|
||||
}
|
||||
fallback := aiprompts.DeriveTitleTemplateJSON("")
|
||||
if !strings.Contains(fallback, "brand") || !strings.Contains(fallback, "product_model") {
|
||||
t.Fatalf("fallback title template: %s", fallback)
|
||||
}
|
||||
if aiprompts.TitleTemplateIsBrandOnly([]byte(`{"elements":[{"type":"variable","value":"brand"}]}`)) != true {
|
||||
t.Fatal("single brand stub must be brand-only")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeCategoryPromptName(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := normalizeCategoryPromptName(" Avtosedeži in lupinice "); got != "avtosedezi in lupinice" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,17 @@ package catalog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/billing"
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/security"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
@@ -21,30 +26,63 @@ const platformDemoCompanyName = "Platform Demo"
|
||||
// RepairCategoryEnhancePromptsResult is the dry-run / apply summary for
|
||||
// RepairA1DemoCategoryEnhancePrompts.
|
||||
type RepairCategoryEnhancePromptsResult struct {
|
||||
CompaniesScanned int `json:"companies_scanned"`
|
||||
CategoriesSeen int `json:"categories_seen"`
|
||||
WouldUpdate int `json:"would_update"`
|
||||
Updated int `json:"updated"`
|
||||
AlreadyOK int `json:"already_ok"`
|
||||
EmptySkipped int `json:"empty_skipped"`
|
||||
ByCompany map[string]int `json:"by_company"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
CompaniesScanned int `json:"companies_scanned"`
|
||||
CategoriesSeen int `json:"categories_seen"`
|
||||
WouldUpdate int `json:"would_update"`
|
||||
Updated int `json:"updated"`
|
||||
AlreadyOK int `json:"already_ok"`
|
||||
EmptySkipped int `json:"empty_skipped"`
|
||||
SeedMatched int `json:"seed_matched"`
|
||||
SplitFromLegacy int `json:"split_from_legacy"`
|
||||
FallbackTemplate int `json:"fallback_template"`
|
||||
TitleTemplateBackfill int `json:"title_template_backfill"`
|
||||
DescTemplateBackfill int `json:"description_template_backfill"`
|
||||
ByCompany map[string]int `json:"by_company"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
SeedPath string `json:"seed_path,omitempty"`
|
||||
// WPCategoriesPath is set when overlays came from wp_product_categories.sql.
|
||||
WPCategoriesPath string `json:"wp_categories_path,omitempty"`
|
||||
SeedEntries int `json:"seed_entries,omitempty"`
|
||||
}
|
||||
|
||||
// RepairA1DemoCategoryEnhancePrompts replaces legacy HTML marketing categories.prompt
|
||||
// values for A1 Slovenija + Platform Demo with aiprompts.CategoryEnhanceUserTemplate
|
||||
// (role-sectioned title/description/meta/attributes USER overlay).
|
||||
// RepairA1DemoOptions configures optional seed overlay path for legacy splits.
|
||||
type RepairA1DemoOptions struct {
|
||||
// SeedPromptsPath points at a1-category-prompts.json OR wp_product_categories.sql.
|
||||
// Empty → auto-detect WP SQL (SEED_A1_WP_CATEGORIES / Downloads), else JSON seed.
|
||||
SeedPromptsPath string
|
||||
// WPCategoriesPath forces wp_product_categories.sql (overrides SeedPromptsPath when set).
|
||||
WPCategoriesPath string
|
||||
// WPCategoriesSQL is uploaded dump bytes (primary for Admin Sync A1 in prod).
|
||||
// When non-empty, takes precedence over filesystem auto-detect / path options.
|
||||
WPCategoriesSQL []byte
|
||||
// ForceFromSeed overwrites already-sectioned prompts when a seed match exists
|
||||
// (WP dump / JSON is source of truth). Default true when WP SQL is resolved.
|
||||
ForceFromSeed *bool
|
||||
}
|
||||
|
||||
// RepairA1DemoCategoryEnhancePrompts replaces non-sectioned / legacy combined
|
||||
// categories.prompt values for A1 Slovenija + Platform Demo with role-sectioned
|
||||
// overlays (Title / Description / Meta / Attributes). Prefers wp_product_categories.sql
|
||||
// (SEED_A1_WP_CATEGORIES / Downloads) as source of truth, then a1-category-prompts.json,
|
||||
// splitting combined Name+Description prompts so naming rules land under Title and
|
||||
// HTML body under Description; otherwise fall back to CategoryEnhanceUserTemplate.
|
||||
//
|
||||
// LOCAL repair only (idempotent):
|
||||
// Also repairs empty or brand-only title_template and empty description_template
|
||||
// from legacy <name> / HTML / meta blocks so enhance uses real A1 formulas.
|
||||
//
|
||||
// LOCAL repair only (idempotent when seed unchanged):
|
||||
// - Writes JSON-compatible user overlays under both "sl" (prompt->>'sl') and "*"
|
||||
// (LangPromptAny) so language stays via {{language}}, not hardcoded-only copy.
|
||||
// - Touches ONLY categories.prompt — never title_template / description_template
|
||||
// (unique name/description/meta formulas stay intact; AppendFormulaConstraints
|
||||
// encodes them as plain-text instructions at enhance render time).
|
||||
// (LangPromptAny) so language stays via {{language}}.
|
||||
// - dryRun=true: count WouldUpdate only; dryRun=false: apply and set Updated.
|
||||
//
|
||||
// Entrypoint: go run ./cmd/repair-category-prompts (-dry-run | -apply).
|
||||
func RepairA1DemoCategoryEnhancePrompts(ctx context.Context, pool *pgxpool.Pool, dryRun bool) (RepairCategoryEnhancePromptsResult, error) {
|
||||
return RepairA1DemoCategoryEnhancePromptsWithOptions(ctx, pool, dryRun, RepairA1DemoOptions{})
|
||||
}
|
||||
|
||||
// RepairA1DemoCategoryEnhancePromptsWithOptions is RepairA1DemoCategoryEnhancePrompts
|
||||
// with an explicit seed / WP dump path.
|
||||
func RepairA1DemoCategoryEnhancePromptsWithOptions(ctx context.Context, pool *pgxpool.Pool, dryRun bool, opts RepairA1DemoOptions) (RepairCategoryEnhancePromptsResult, error) {
|
||||
out := RepairCategoryEnhancePromptsResult{
|
||||
DryRun: dryRun,
|
||||
ByCompany: map[string]int{},
|
||||
@@ -53,6 +91,12 @@ func RepairA1DemoCategoryEnhancePrompts(ctx context.Context, pool *pgxpool.Pool,
|
||||
return out, fmt.Errorf("postgres pool is required")
|
||||
}
|
||||
|
||||
if len(opts.WPCategoriesSQL) > 0 {
|
||||
if _, _, err := LoadWPCategoryPromptOverlaysFromBytes(opts.WPCategoriesSQL); err != nil {
|
||||
return out, fmt.Errorf("wp_product_categories upload: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
a1ID, err := uuid.Parse(a1CompanyID)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("a1 company id: %w", err)
|
||||
@@ -90,13 +134,17 @@ func RepairA1DemoCategoryEnhancePrompts(ctx context.Context, pool *pgxpool.Pool,
|
||||
return out, fmt.Errorf("no A1 / Platform Demo companies found")
|
||||
}
|
||||
|
||||
want, err := repairedCategoryEnhancePromptMap()
|
||||
if err != nil {
|
||||
return out, err
|
||||
seedByNorm, seedByUID, seedMeta := resolveCategoryPromptOverlays(opts)
|
||||
out.SeedPath = seedMeta.Path
|
||||
out.WPCategoriesPath = seedMeta.WPPath
|
||||
out.SeedEntries = seedMeta.Entries
|
||||
forceFromSeed := seedMeta.ForceFromSeed
|
||||
if opts.ForceFromSeed != nil {
|
||||
forceFromSeed = *opts.ForceFromSeed
|
||||
}
|
||||
|
||||
for _, c := range companies {
|
||||
n, err := repairCompanyCategoryEnhancePrompts(ctx, pool, c.id, c.name, want, dryRun, &out)
|
||||
n, err := repairCompanyCategoryEnhancePrompts(ctx, pool, c.id, c.name, seedByNorm, seedByUID, dryRun, forceFromSeed, &out)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
@@ -107,9 +155,190 @@ func RepairA1DemoCategoryEnhancePrompts(ctx context.Context, pool *pgxpool.Pool,
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// repairedCategoryEnhancePromptMap is the canonical stored shape: "sl" (prompt->>'sl'
|
||||
// for A1/Demo) plus LangPromptAny ("*") so PromptForLanguage resolves for any content
|
||||
// language. Copy stays language-agnostic via {{language}} — not hardcoded Slovenian.
|
||||
type categoryPromptOverlayMeta struct {
|
||||
Path string
|
||||
WPPath string
|
||||
Entries int
|
||||
ForceFromSeed bool
|
||||
}
|
||||
|
||||
func resolveCategoryPromptOverlays(opts RepairA1DemoOptions) (byNorm, byUID map[string]string, meta categoryPromptOverlayMeta) {
|
||||
byNorm = map[string]string{}
|
||||
byUID = map[string]string{}
|
||||
|
||||
if len(opts.WPCategoriesSQL) > 0 {
|
||||
n, u, err := LoadWPCategoryPromptOverlaysFromBytes(opts.WPCategoriesSQL)
|
||||
if err == nil {
|
||||
meta.Path = "upload:wp_product_categories.sql"
|
||||
meta.WPPath = "upload:wp_product_categories.sql"
|
||||
meta.Entries = len(n)
|
||||
meta.ForceFromSeed = true
|
||||
return n, u, meta
|
||||
}
|
||||
}
|
||||
|
||||
wpPath := strings.TrimSpace(opts.WPCategoriesPath)
|
||||
if wpPath == "" {
|
||||
wpPath = ResolveWPCategoryPromptsPath("")
|
||||
} else {
|
||||
wpPath = ResolveWPCategoryPromptsPath(wpPath)
|
||||
}
|
||||
if wpPath != "" {
|
||||
n, u, err := loadWPCategoryPromptOverlays(wpPath)
|
||||
if err == nil {
|
||||
meta.Path = wpPath
|
||||
meta.WPPath = wpPath
|
||||
meta.Entries = len(n)
|
||||
meta.ForceFromSeed = true
|
||||
return n, u, meta
|
||||
}
|
||||
}
|
||||
|
||||
seedPath := strings.TrimSpace(opts.SeedPromptsPath)
|
||||
if seedPath == "" {
|
||||
seedPath = resolveA1CategoryPromptsPath()
|
||||
}
|
||||
if seedPath != "" && isWPCategoryPromptsSQLPath(seedPath) {
|
||||
n, u, err := loadWPCategoryPromptOverlays(seedPath)
|
||||
if err == nil {
|
||||
meta.Path = seedPath
|
||||
meta.WPPath = seedPath
|
||||
meta.Entries = len(n)
|
||||
meta.ForceFromSeed = true
|
||||
return n, u, meta
|
||||
}
|
||||
}
|
||||
if seedPath != "" {
|
||||
n, u, err := loadA1CategoryPromptOverlays(seedPath)
|
||||
if err == nil {
|
||||
meta.Path = seedPath
|
||||
meta.Entries = len(n)
|
||||
meta.ForceFromSeed = false
|
||||
return n, u, meta
|
||||
}
|
||||
}
|
||||
return byNorm, byUID, meta
|
||||
}
|
||||
|
||||
func resolveA1CategoryPromptsPath() string {
|
||||
candidates := []string{
|
||||
filepath.Join("scripts", "seed", "a1-category-prompts.json"),
|
||||
filepath.Join("..", "..", "scripts", "seed", "a1-category-prompts.json"),
|
||||
filepath.Join("..", "..", "..", "scripts", "seed", "a1-category-prompts.json"),
|
||||
}
|
||||
// Walk up from cwd looking for scripts/seed/a1-category-prompts.json.
|
||||
if wd, err := os.Getwd(); err == nil {
|
||||
dir := wd
|
||||
for i := 0; i < 8; i++ {
|
||||
candidates = append(candidates, filepath.Join(dir, "scripts", "seed", "a1-category-prompts.json"))
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
break
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
for _, p := range candidates {
|
||||
if st, err := os.Stat(p); err == nil && !st.IsDir() {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type a1CategoryPromptFile struct {
|
||||
Entries []a1CategoryPromptEntry `json:"entries"`
|
||||
}
|
||||
|
||||
type a1CategoryPromptEntry struct {
|
||||
Name string `json:"name"`
|
||||
UniqueID string `json:"unique_id,omitempty"`
|
||||
Prompt string `json:"prompt"`
|
||||
}
|
||||
|
||||
func loadA1CategoryPromptOverlays(path string) (byNorm map[string]string, byUID map[string]string, err error) {
|
||||
byNorm = map[string]string{}
|
||||
byUID = map[string]string{}
|
||||
path = filepath.Clean(strings.TrimSpace(path))
|
||||
if path == "" || path == "." {
|
||||
return byNorm, byUID, fmt.Errorf("seed prompts path empty")
|
||||
}
|
||||
if filepath.Base(path) != "a1-category-prompts.json" {
|
||||
return byNorm, byUID, fmt.Errorf("refusing unexpected category prompts file name %q", filepath.Base(path))
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return byNorm, byUID, err
|
||||
}
|
||||
if len(raw) > 8<<20 {
|
||||
return byNorm, byUID, fmt.Errorf("category prompts file too large (%d bytes)", len(raw))
|
||||
}
|
||||
var f a1CategoryPromptFile
|
||||
if err := json.Unmarshal(raw, &f); err != nil {
|
||||
return byNorm, byUID, err
|
||||
}
|
||||
for _, e := range f.Entries {
|
||||
p := strings.TrimSpace(e.Prompt)
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
if uid := strings.ToLower(strings.TrimSpace(e.UniqueID)); uid != "" {
|
||||
byUID[uid] = p
|
||||
}
|
||||
if key := normalizeCategoryPromptName(e.Name); key != "" {
|
||||
byNorm[key] = p
|
||||
}
|
||||
}
|
||||
if len(byNorm) == 0 && len(byUID) == 0 {
|
||||
return byNorm, byUID, fmt.Errorf("no usable seed prompt entries")
|
||||
}
|
||||
return byNorm, byUID, nil
|
||||
}
|
||||
|
||||
func normalizeCategoryPromptName(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
var b strings.Builder
|
||||
b.Grow(len(s))
|
||||
prevSpace := false
|
||||
for _, r := range s {
|
||||
r = foldSlovenePromptRune(r)
|
||||
if unicode.IsSpace(r) {
|
||||
if prevSpace || b.Len() == 0 {
|
||||
continue
|
||||
}
|
||||
b.WriteByte(' ')
|
||||
prevSpace = true
|
||||
continue
|
||||
}
|
||||
prevSpace = false
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '/' || r == '&' || r == '+' {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(b.String())
|
||||
}
|
||||
|
||||
func foldSlovenePromptRune(r rune) rune {
|
||||
switch r {
|
||||
case 'č', 'ć':
|
||||
return 'c'
|
||||
case 'š':
|
||||
return 's'
|
||||
case 'ž':
|
||||
return 'z'
|
||||
case 'đ':
|
||||
return 'd'
|
||||
default:
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
// repairedCategoryEnhancePromptMap is the shared fallback overlay (no per-category
|
||||
// Slovenian rules): "sl" + LangPromptAny ("*").
|
||||
func repairedCategoryEnhancePromptMap() (company.LangPromptMap, error) {
|
||||
tpl := strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)
|
||||
if tpl == "" {
|
||||
@@ -121,33 +350,96 @@ func repairedCategoryEnhancePromptMap() (company.LangPromptMap, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func categoryEnhancePromptMapOK(m company.LangPromptMap, want company.LangPromptMap) bool {
|
||||
if !company.HasAnyPrompt(m) || !company.HasAnyPrompt(want) {
|
||||
func categoryEnhancePromptValueOK(p string) bool {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
return false
|
||||
}
|
||||
tpl := strings.TrimSpace(want[company.LangPromptAny])
|
||||
if tpl == "" {
|
||||
tpl = strings.TrimSpace(want["sl"])
|
||||
}
|
||||
if tpl == "" {
|
||||
return !aiprompts.CategoryEnhancePromptNeedsRepair(p)
|
||||
}
|
||||
|
||||
func categoryEnhancePromptMapOK(m company.LangPromptMap) bool {
|
||||
if !company.HasAnyPrompt(m) {
|
||||
return false
|
||||
}
|
||||
// Accept already-repaired maps: every non-empty value equals the shared template
|
||||
// and LangPromptAny (or legacy sl-only) is present.
|
||||
hasKey := false
|
||||
for lang, p := range m {
|
||||
for _, p := range m {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
if p != tpl {
|
||||
if !categoryEnhancePromptValueOK(p) {
|
||||
return false
|
||||
}
|
||||
if lang == company.LangPromptAny || lang == "sl" {
|
||||
hasKey = true
|
||||
}
|
||||
// Require sl or * present.
|
||||
if strings.TrimSpace(m[company.LangPromptAny]) == "" && strings.TrimSpace(m["sl"]) == "" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func pickSeedPrompt(uniqueID, name string, byNorm, byUID map[string]string) string {
|
||||
if uid := strings.ToLower(strings.TrimSpace(uniqueID)); uid != "" {
|
||||
if p, ok := byUID[uid]; ok {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return hasKey
|
||||
if key := normalizeCategoryPromptName(name); key != "" {
|
||||
if p, ok := byNorm[key]; ok {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func resolveRepairedEnhancePrompt(current company.LangPromptMap, seedLegacy string, preferSeed bool, out *RepairCategoryEnhancePromptsResult) string {
|
||||
prompt, fromSeed, fromLegacy, fallback := computeRepairedEnhancePrompt(current, seedLegacy, preferSeed)
|
||||
if fromSeed {
|
||||
out.SeedMatched++
|
||||
}
|
||||
if fromLegacy {
|
||||
out.SplitFromLegacy++
|
||||
}
|
||||
if fallback {
|
||||
out.FallbackTemplate++
|
||||
}
|
||||
return prompt
|
||||
}
|
||||
|
||||
func computeRepairedEnhancePrompt(current company.LangPromptMap, seedLegacy string, preferSeed bool) (prompt string, fromSeed, fromLegacy, fallback bool) {
|
||||
trySeed := func(raw string) (string, bool, bool) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return "", false, false
|
||||
}
|
||||
if aiprompts.IsLegacyCombinedEnhancePrompt(raw) {
|
||||
return aiprompts.SplitLegacyCombinedEnhancePrompt(raw), true, false
|
||||
}
|
||||
if aiprompts.CategoryEnhanceHasRoleSections(raw) {
|
||||
return security.SanitizePrompt(raw, MaxCategoryPromptRunes), false, false
|
||||
}
|
||||
return strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate), false, true
|
||||
}
|
||||
|
||||
if preferSeed && seedLegacy != "" {
|
||||
p, leg, fb := trySeed(seedLegacy)
|
||||
return p, true, leg, fb
|
||||
}
|
||||
for _, lang := range []string{"sl", company.LangPromptAny} {
|
||||
if cur := strings.TrimSpace(current[lang]); aiprompts.IsLegacyCombinedEnhancePrompt(cur) {
|
||||
return aiprompts.SplitLegacyCombinedEnhancePrompt(cur), false, true, false
|
||||
}
|
||||
}
|
||||
for _, cur := range current {
|
||||
if aiprompts.IsLegacyCombinedEnhancePrompt(cur) {
|
||||
return aiprompts.SplitLegacyCombinedEnhancePrompt(cur), false, true, false
|
||||
}
|
||||
}
|
||||
if seedLegacy != "" {
|
||||
p, leg, fb := trySeed(seedLegacy)
|
||||
return p, true, leg, fb
|
||||
}
|
||||
return strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate), false, false, true
|
||||
}
|
||||
|
||||
func repairCompanyCategoryEnhancePrompts(
|
||||
@@ -155,12 +447,19 @@ func repairCompanyCategoryEnhancePrompts(
|
||||
pool *pgxpool.Pool,
|
||||
companyID uuid.UUID,
|
||||
companyName string,
|
||||
want company.LangPromptMap,
|
||||
seedByNorm map[string]string,
|
||||
seedByUID map[string]string,
|
||||
dryRun bool,
|
||||
forceFromSeed bool,
|
||||
out *RepairCategoryEnhancePromptsResult,
|
||||
) (int, error) {
|
||||
rows, err := pool.Query(ctx, `
|
||||
SELECT id, COALESCE(prompt, '{}'::jsonb)
|
||||
SELECT id,
|
||||
COALESCE(unique_id, ''),
|
||||
name,
|
||||
COALESCE(prompt, '{}'::jsonb),
|
||||
title_template,
|
||||
description_template
|
||||
FROM categories
|
||||
WHERE company_id = $1`, companyID)
|
||||
if err != nil {
|
||||
@@ -171,8 +470,10 @@ func repairCompanyCategoryEnhancePrompts(
|
||||
updatedHere := 0
|
||||
for rows.Next() {
|
||||
var id uuid.UUID
|
||||
var uniqueID, name string
|
||||
var raw []byte
|
||||
if err := rows.Scan(&id, &raw); err != nil {
|
||||
var titleTpl, descTpl []byte
|
||||
if err := rows.Scan(&id, &uniqueID, &name, &raw, &titleTpl, &descTpl); err != nil {
|
||||
return updatedHere, err
|
||||
}
|
||||
out.CategoriesSeen++
|
||||
@@ -181,41 +482,268 @@ func repairCompanyCategoryEnhancePrompts(
|
||||
if err != nil {
|
||||
return updatedHere, fmt.Errorf("decode prompt category=%s company=%s: %w", id, companyName, err)
|
||||
}
|
||||
|
||||
seedLegacy := pickSeedPrompt(uniqueID, name, seedByNorm, seedByUID)
|
||||
needPrompt := company.HasAnyPrompt(m) && !categoryEnhancePromptMapOK(m)
|
||||
// Empty prompt → write sectioned overlay (seed split when available, else shared template).
|
||||
if !company.HasAnyPrompt(m) {
|
||||
out.EmptySkipped++
|
||||
needPrompt = true
|
||||
}
|
||||
|
||||
var wantPrompt string
|
||||
if seedLegacy != "" && (forceFromSeed || needPrompt) {
|
||||
wantPrompt, _, _, _ = computeRepairedEnhancePrompt(m, seedLegacy, forceFromSeed)
|
||||
wantPrompt = security.SanitizePrompt(wantPrompt, MaxCategoryPromptRunes)
|
||||
if cur := currentEnhancePrompt(m); forceFromSeed && cur != "" && cur == wantPrompt && categoryEnhancePromptMapOK(m) {
|
||||
needPrompt = false
|
||||
wantPrompt = ""
|
||||
} else if wantPrompt != "" {
|
||||
needPrompt = true
|
||||
}
|
||||
}
|
||||
|
||||
titleRules := legacyTitleRules(m, seedLegacy)
|
||||
needTitle := aiprompts.TitleTemplateNeedsRepair(titleTpl)
|
||||
legacyParts := legacyEnhanceParts(m, seedLegacy)
|
||||
needDesc := aiprompts.DescriptionTemplateNeedsRepair(descTpl)
|
||||
var wantTitleTpl, wantDescTpl string
|
||||
if forceFromSeed && seedLegacy != "" {
|
||||
if strings.TrimSpace(titleRules) != "" {
|
||||
wantTitleTpl = aiprompts.DeriveTitleTemplateJSON(titleRules)
|
||||
if !jsonbEqual(titleTpl, []byte(wantTitleTpl)) {
|
||||
needTitle = true
|
||||
} else {
|
||||
needTitle = false
|
||||
wantTitleTpl = ""
|
||||
}
|
||||
}
|
||||
if legacyParts.WasLegacy && (strings.TrimSpace(legacyParts.DescriptionRules) != "" || strings.TrimSpace(legacyParts.MetaRules) != "") {
|
||||
wantDescTpl = aiprompts.DeriveDescriptionTemplateJSON(legacyParts)
|
||||
if strings.TrimSpace(wantDescTpl) != "" && !jsonbEqual(descTpl, []byte(wantDescTpl)) {
|
||||
needDesc = true
|
||||
} else if strings.TrimSpace(wantDescTpl) == "" || jsonbEqual(descTpl, []byte(wantDescTpl)) {
|
||||
needDesc = false
|
||||
wantDescTpl = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
if needDesc && strings.TrimSpace(legacyParts.DescriptionRules) == "" && strings.TrimSpace(legacyParts.MetaRules) == "" {
|
||||
needDesc = false
|
||||
}
|
||||
if !needPrompt && !needTitle && !needDesc {
|
||||
if company.HasAnyPrompt(m) {
|
||||
out.AlreadyOK++
|
||||
} else {
|
||||
out.EmptySkipped++
|
||||
}
|
||||
continue
|
||||
}
|
||||
if categoryEnhancePromptMapOK(m, want) {
|
||||
out.AlreadyOK++
|
||||
continue
|
||||
|
||||
if needPrompt && wantPrompt == "" {
|
||||
wantPrompt = resolveRepairedEnhancePrompt(m, seedLegacy, forceFromSeed && seedLegacy != "", out)
|
||||
wantPrompt = security.SanitizePrompt(wantPrompt, MaxCategoryPromptRunes)
|
||||
if wantPrompt == "" {
|
||||
return updatedHere, fmt.Errorf("repaired prompt empty category=%s", id)
|
||||
}
|
||||
} else if needPrompt {
|
||||
// Record seed/split stats for the prompt we already computed.
|
||||
_, fromSeed, fromLegacy, fallback := computeRepairedEnhancePrompt(m, seedLegacy, forceFromSeed && seedLegacy != "")
|
||||
if fromSeed {
|
||||
out.SeedMatched++
|
||||
}
|
||||
if fromLegacy {
|
||||
out.SplitFromLegacy++
|
||||
}
|
||||
if fallback {
|
||||
out.FallbackTemplate++
|
||||
}
|
||||
}
|
||||
|
||||
out.WouldUpdate++
|
||||
if dryRun {
|
||||
if needTitle {
|
||||
out.TitleTemplateBackfill++
|
||||
}
|
||||
if needDesc {
|
||||
out.DescTemplateBackfill++
|
||||
}
|
||||
updatedHere++
|
||||
continue
|
||||
}
|
||||
|
||||
cleaned, err := company.SanitizeLangPromptMap(want, MaxCategoryPromptRunes)
|
||||
if err != nil {
|
||||
return updatedHere, fmt.Errorf("sanitize prompt category=%s: %w", id, err)
|
||||
if needPrompt {
|
||||
want := company.LangPromptMap{
|
||||
"sl": wantPrompt,
|
||||
company.LangPromptAny: wantPrompt,
|
||||
}
|
||||
cleaned, err := company.SanitizeLangPromptMap(want, MaxCategoryPromptRunes)
|
||||
if err != nil {
|
||||
return updatedHere, fmt.Errorf("sanitize prompt category=%s: %w", id, err)
|
||||
}
|
||||
encoded, err := company.EncodeLangPromptMap(cleaned)
|
||||
if err != nil {
|
||||
return updatedHere, err
|
||||
}
|
||||
ct, err := pool.Exec(ctx, `
|
||||
UPDATE categories
|
||||
SET prompt = $3::jsonb, updated_at = now()
|
||||
WHERE id = $1 AND company_id = $2`, id, companyID, string(encoded))
|
||||
if err != nil {
|
||||
return updatedHere, fmt.Errorf("update prompt category=%s: %w", id, err)
|
||||
}
|
||||
if ct.RowsAffected() == 0 {
|
||||
return updatedHere, fmt.Errorf("category %s not updated (company mismatch?)", id)
|
||||
}
|
||||
}
|
||||
encoded, err := company.EncodeLangPromptMap(cleaned)
|
||||
if err != nil {
|
||||
return updatedHere, err
|
||||
|
||||
if needTitle {
|
||||
tpl := wantTitleTpl
|
||||
if tpl == "" {
|
||||
tpl = aiprompts.DeriveTitleTemplateJSON(titleRules)
|
||||
}
|
||||
ct, err := pool.Exec(ctx, `
|
||||
UPDATE categories
|
||||
SET title_template = $3::jsonb, updated_at = now()
|
||||
WHERE id = $1 AND company_id = $2`, id, companyID, tpl)
|
||||
if err != nil {
|
||||
return updatedHere, fmt.Errorf("backfill title_template category=%s: %w", id, err)
|
||||
}
|
||||
if ct.RowsAffected() > 0 {
|
||||
out.TitleTemplateBackfill++
|
||||
}
|
||||
}
|
||||
ct, err := pool.Exec(ctx, `
|
||||
UPDATE categories
|
||||
SET prompt = $3::jsonb, updated_at = now()
|
||||
WHERE id = $1 AND company_id = $2`, id, companyID, string(encoded))
|
||||
if err != nil {
|
||||
return updatedHere, fmt.Errorf("update prompt category=%s: %w", id, err)
|
||||
}
|
||||
if ct.RowsAffected() == 0 {
|
||||
return updatedHere, fmt.Errorf("category %s not updated (company mismatch?)", id)
|
||||
|
||||
if needDesc {
|
||||
tpl := wantDescTpl
|
||||
if tpl == "" {
|
||||
tpl = aiprompts.DeriveDescriptionTemplateJSON(legacyParts)
|
||||
}
|
||||
if strings.TrimSpace(tpl) != "" {
|
||||
ct, err := pool.Exec(ctx, `
|
||||
UPDATE categories
|
||||
SET description_template = $3::jsonb, updated_at = now()
|
||||
WHERE id = $1 AND company_id = $2`, id, companyID, tpl)
|
||||
if err != nil {
|
||||
return updatedHere, fmt.Errorf("backfill description_template category=%s: %w", id, err)
|
||||
}
|
||||
if ct.RowsAffected() > 0 {
|
||||
out.DescTemplateBackfill++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.Updated++
|
||||
updatedHere++
|
||||
}
|
||||
return updatedHere, rows.Err()
|
||||
}
|
||||
|
||||
func currentEnhancePrompt(m company.LangPromptMap) string {
|
||||
for _, lang := range []string{"sl", company.LangPromptAny} {
|
||||
if p := strings.TrimSpace(m[lang]); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
for _, p := range m {
|
||||
if t := strings.TrimSpace(p); t != "" {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func legacyEnhanceParts(current company.LangPromptMap, seedLegacy string) aiprompts.LegacyEnhanceParts {
|
||||
if seedLegacy != "" {
|
||||
parts := aiprompts.ParseLegacyCombinedEnhancePrompt(seedLegacy)
|
||||
if parts.WasLegacy {
|
||||
return parts
|
||||
}
|
||||
}
|
||||
for _, lang := range []string{"sl", company.LangPromptAny} {
|
||||
parts := aiprompts.ParseLegacyCombinedEnhancePrompt(current[lang])
|
||||
if parts.WasLegacy {
|
||||
return parts
|
||||
}
|
||||
}
|
||||
for _, cur := range current {
|
||||
parts := aiprompts.ParseLegacyCombinedEnhancePrompt(cur)
|
||||
if parts.WasLegacy {
|
||||
return parts
|
||||
}
|
||||
}
|
||||
return aiprompts.LegacyEnhanceParts{}
|
||||
}
|
||||
|
||||
func jsonbTemplatePresent(raw []byte) bool {
|
||||
s := strings.TrimSpace(string(raw))
|
||||
if s == "" || s == "null" || s == "{}" || s == "[]" {
|
||||
return false
|
||||
}
|
||||
var obj map[string]any
|
||||
if err := json.Unmarshal(raw, &obj); err != nil {
|
||||
return true // non-empty non-object still counts as present
|
||||
}
|
||||
if elems, ok := obj["elements"].([]any); ok && len(elems) > 0 {
|
||||
return true
|
||||
}
|
||||
if sections, ok := obj["sections"].([]any); ok && len(sections) > 0 {
|
||||
return true
|
||||
}
|
||||
if mt, ok := obj["metaTitle"].(string); ok && strings.TrimSpace(mt) != "" {
|
||||
return true
|
||||
}
|
||||
if md, ok := obj["metaDescription"].(string); ok && strings.TrimSpace(md) != "" {
|
||||
return true
|
||||
}
|
||||
// Any other non-empty keys.
|
||||
return len(obj) > 0
|
||||
}
|
||||
|
||||
func jsonbEqual(a, b []byte) bool {
|
||||
as := strings.TrimSpace(string(a))
|
||||
bs := strings.TrimSpace(string(b))
|
||||
if as == "" || as == "null" {
|
||||
as = ""
|
||||
}
|
||||
if bs == "" || bs == "null" {
|
||||
bs = ""
|
||||
}
|
||||
if as == bs {
|
||||
return true
|
||||
}
|
||||
var ao, bo any
|
||||
if err := json.Unmarshal([]byte(as), &ao); err != nil {
|
||||
return false
|
||||
}
|
||||
if err := json.Unmarshal([]byte(bs), &bo); err != nil {
|
||||
return false
|
||||
}
|
||||
ab, err1 := json.Marshal(ao)
|
||||
bb, err2 := json.Marshal(bo)
|
||||
if err1 != nil || err2 != nil {
|
||||
return false
|
||||
}
|
||||
return string(ab) == string(bb)
|
||||
}
|
||||
|
||||
func legacyTitleRules(current company.LangPromptMap, seedLegacy string) string {
|
||||
if seedLegacy != "" {
|
||||
parts := aiprompts.ParseLegacyCombinedEnhancePrompt(seedLegacy)
|
||||
if parts.WasLegacy && strings.TrimSpace(parts.TitleRules) != "" {
|
||||
return parts.TitleRules
|
||||
}
|
||||
}
|
||||
for _, lang := range []string{"sl", company.LangPromptAny} {
|
||||
parts := aiprompts.ParseLegacyCombinedEnhancePrompt(current[lang])
|
||||
if parts.WasLegacy && strings.TrimSpace(parts.TitleRules) != "" {
|
||||
return parts.TitleRules
|
||||
}
|
||||
}
|
||||
for _, cur := range current {
|
||||
parts := aiprompts.ParseLegacyCombinedEnhancePrompt(cur)
|
||||
if parts.WasLegacy && strings.TrimSpace(parts.TitleRules) != "" {
|
||||
return parts.TitleRules
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -21,19 +21,19 @@ func TestRepairedCategoryEnhancePromptMap(t *testing.T) {
|
||||
if want[company.LangPromptAny] != tpl {
|
||||
t.Fatalf("want * = shared template")
|
||||
}
|
||||
if !categoryEnhancePromptMapOK(want, want) {
|
||||
if !categoryEnhancePromptMapOK(want) {
|
||||
t.Fatal("canonical map should be OK")
|
||||
}
|
||||
legacy := company.LangPromptMap{"sl": "<H2>legacy HTML marketing</H2>"}
|
||||
if categoryEnhancePromptMapOK(legacy, want) {
|
||||
if categoryEnhancePromptMapOK(legacy) {
|
||||
t.Fatal("legacy HTML must need repair")
|
||||
}
|
||||
slOnly := company.LangPromptMap{"sl": tpl}
|
||||
if !categoryEnhancePromptMapOK(slOnly, want) {
|
||||
if !categoryEnhancePromptMapOK(slOnly) {
|
||||
t.Fatal("sl-only repaired map should be OK (idempotent)")
|
||||
}
|
||||
starOnly := company.LangPromptMap{company.LangPromptAny: tpl}
|
||||
if !categoryEnhancePromptMapOK(starOnly, want) {
|
||||
if !categoryEnhancePromptMapOK(starOnly) {
|
||||
t.Fatal("*-only repaired map should be OK (idempotent)")
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,28 @@ func TestRepairTargetsUseSharedTemplate(t *testing.T) {
|
||||
t.Fatalf("shared template missing {{%s}}", v)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(tpl), "never brand-only") {
|
||||
t.Fatal("Title section must forbid brand-only names")
|
||||
}
|
||||
if a1CompanyID == "" || platformDemoCompanyName == "" {
|
||||
t.Fatal("missing company target constants")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyTitleRulesFromSeedShape(t *testing.T) {
|
||||
t.Parallel()
|
||||
seed := `Ustvari nov opis
|
||||
|
||||
GPT predloga:
|
||||
<name>{Napiši novo ime izdelka po formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka". Ne uporabljaj vejic.}</name>
|
||||
<metaDescription>{140 znakov}</metaDescription>
|
||||
<H2>{benefit}</H2>`
|
||||
rules := legacyTitleRules(nil, seed)
|
||||
if !strings.Contains(strings.ToLower(rules), "znamka") {
|
||||
t.Fatalf("expected title rules from seed, got %q", rules)
|
||||
}
|
||||
raw := aiprompts.DeriveTitleTemplateJSON(rules)
|
||||
if aiprompts.TitleTemplateNeedsRepair(raw) {
|
||||
t.Fatalf("derived title_template still needs repair: %s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,313 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
wpProductCategoriesFile = "wp_product_categories.sql"
|
||||
envSeedA1WPCategories = "SEED_A1_WP_CATEGORIES"
|
||||
// MaxWPCategorySQLBytes caps uploaded / on-disk wp_product_categories.sql size (16 MiB).
|
||||
MaxWPCategorySQLBytes = 16 << 20
|
||||
)
|
||||
|
||||
// ResolveWPCategoryPromptsPath picks an explicit path, else the first readable
|
||||
// wp_product_categories.sql candidate (SEED_A1_WP_CATEGORIES + Downloads + scripts/seed).
|
||||
// Used by Sync A1 / RepairCompanyCategoryEnhancePrompts as the A1 prompt source of truth.
|
||||
func ResolveWPCategoryPromptsPath(explicit string) string {
|
||||
if p := strings.TrimSpace(explicit); p != "" {
|
||||
if st, err := os.Stat(p); err == nil && !st.IsDir() {
|
||||
return p
|
||||
}
|
||||
log.Printf("warning: wp category prompts not found at %q — trying auto-detect", p)
|
||||
}
|
||||
if v := strings.TrimSpace(os.Getenv(envSeedA1WPCategories)); v != "" {
|
||||
if st, err := os.Stat(v); err == nil && !st.IsDir() {
|
||||
return v
|
||||
}
|
||||
log.Printf("warning: %s=%q not readable — trying Downloads / scripts/seed", envSeedA1WPCategories, v)
|
||||
}
|
||||
for _, c := range WPCategoryPromptsCandidates() {
|
||||
if st, err := os.Stat(c); err == nil && !st.IsDir() {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// WPCategoryPromptsCandidates lists local paths Sync A1 / repair try when
|
||||
// SEED_A1_WP_CATEGORIES is unset. First readable file wins via ResolveWPCategoryPromptsPath.
|
||||
func WPCategoryPromptsCandidates() []string {
|
||||
var out []string
|
||||
if v := strings.TrimSpace(os.Getenv(envSeedA1WPCategories)); v != "" {
|
||||
out = append(out, v)
|
||||
}
|
||||
names := []string{
|
||||
wpProductCategoriesFile,
|
||||
"wp_product_categories (1).sql",
|
||||
"wp_product_categories(1).sql",
|
||||
}
|
||||
home, _ := os.UserHomeDir()
|
||||
if home != "" {
|
||||
for _, n := range names {
|
||||
out = append(out, filepath.Join(home, "Downloads", n))
|
||||
out = append(out, filepath.Join(home, "downloads", n))
|
||||
}
|
||||
// Windows secondary profile Downloads (e.g. D:\Users\…\Downloads).
|
||||
for _, driveRoot := range []string{`D:\`, `C:\`} {
|
||||
alt := filepath.Join(driveRoot, "Users", filepath.Base(home), "Downloads")
|
||||
for _, n := range names {
|
||||
out = append(out, filepath.Join(alt, n))
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, n := range names {
|
||||
out = append(out,
|
||||
n,
|
||||
filepath.Join("..", "..", n),
|
||||
filepath.Join("scripts", "seed", n),
|
||||
filepath.Join("..", "..", "scripts", "seed", n),
|
||||
)
|
||||
}
|
||||
if root, ok := findMonorepoRootFromCwd(); ok {
|
||||
for _, n := range names {
|
||||
out = append(out, filepath.Join(root, "scripts", "seed", n))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func findMonorepoRootFromCwd() (string, bool) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
dir := cwd
|
||||
for {
|
||||
api := filepath.Join(dir, "apps", "api")
|
||||
web := filepath.Join(dir, "apps", "web")
|
||||
if st, err := os.Stat(api); err == nil && st.IsDir() {
|
||||
if st, err := os.Stat(web); err == nil && st.IsDir() {
|
||||
return dir, true
|
||||
}
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
return "", false
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
|
||||
func isWPCategoryPromptsSQLPath(path string) bool {
|
||||
base := strings.ToLower(filepath.Base(strings.TrimSpace(path)))
|
||||
if base == "" {
|
||||
return false
|
||||
}
|
||||
if base == wpProductCategoriesFile {
|
||||
return true
|
||||
}
|
||||
return strings.HasPrefix(base, "wp_product_categories") && strings.HasSuffix(base, ".sql")
|
||||
}
|
||||
|
||||
// loadWPCategoryPromptOverlays parses Name→Prompt pairs from a wp_product_categories.sql dump.
|
||||
func loadWPCategoryPromptOverlays(path string) (byNorm map[string]string, byUID map[string]string, err error) {
|
||||
byNorm = map[string]string{}
|
||||
byUID = map[string]string{}
|
||||
path = filepath.Clean(strings.TrimSpace(path))
|
||||
if path == "" || path == "." {
|
||||
return byNorm, byUID, fmt.Errorf("wp category prompts path empty")
|
||||
}
|
||||
if !isWPCategoryPromptsSQLPath(path) {
|
||||
return byNorm, byUID, fmt.Errorf("refusing unexpected wp category prompts file name %q", filepath.Base(path))
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return byNorm, byUID, err
|
||||
}
|
||||
return LoadWPCategoryPromptOverlaysFromBytes(raw)
|
||||
}
|
||||
|
||||
// LoadWPCategoryPromptOverlaysFromBytes parses an uploaded or in-memory
|
||||
// wp_product_categories.sql dump. Enforces MaxWPCategorySQLBytes and UTF-8.
|
||||
func LoadWPCategoryPromptOverlaysFromBytes(raw []byte) (byNorm map[string]string, byUID map[string]string, err error) {
|
||||
byNorm = map[string]string{}
|
||||
byUID = map[string]string{}
|
||||
if len(raw) == 0 {
|
||||
return byNorm, byUID, fmt.Errorf("empty wp_product_categories sql")
|
||||
}
|
||||
if len(raw) > MaxWPCategorySQLBytes {
|
||||
return byNorm, byUID, fmt.Errorf("wp category prompts file too large (%d bytes)", len(raw))
|
||||
}
|
||||
if !utf8.Valid(raw) {
|
||||
return byNorm, byUID, fmt.Errorf("wp category prompts file is not valid UTF-8")
|
||||
}
|
||||
entries, err := ParseWPProductCategoriesSQL(string(raw))
|
||||
if err != nil {
|
||||
return byNorm, byUID, err
|
||||
}
|
||||
for _, e := range entries {
|
||||
p := strings.TrimSpace(e.Prompt)
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
if uid := strings.ToLower(strings.TrimSpace(e.UniqueID)); uid != "" {
|
||||
byUID[uid] = p
|
||||
}
|
||||
if key := normalizeCategoryPromptName(e.Name); key != "" {
|
||||
byNorm[key] = p
|
||||
}
|
||||
}
|
||||
if len(byNorm) == 0 && len(byUID) == 0 {
|
||||
return byNorm, byUID, fmt.Errorf("no usable wp_product_categories prompt entries")
|
||||
}
|
||||
return byNorm, byUID, nil
|
||||
}
|
||||
|
||||
// ParseWPProductCategoriesSQL extracts (Name, Prompt) rows from a mysqldump-style
|
||||
// INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES … statement.
|
||||
func ParseWPProductCategoriesSQL(sql string) ([]a1CategoryPromptEntry, error) {
|
||||
sql = strings.TrimSpace(sql)
|
||||
if sql == "" {
|
||||
return nil, fmt.Errorf("empty wp_product_categories sql")
|
||||
}
|
||||
lower := strings.ToLower(sql)
|
||||
if !strings.Contains(lower, "wp_product_categories") {
|
||||
return nil, fmt.Errorf("sql does not reference wp_product_categories")
|
||||
}
|
||||
|
||||
out := make([]a1CategoryPromptEntry, 0, 128)
|
||||
i := 0
|
||||
for i < len(sql) {
|
||||
name, prompt, next, ok := scanWPCategoryRow(sql, i)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
i = next
|
||||
name = strings.TrimSpace(name)
|
||||
prompt = strings.TrimSpace(prompt)
|
||||
if name == "" || prompt == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, a1CategoryPromptEntry{Name: name, Prompt: prompt})
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil, fmt.Errorf("no Name/Prompt rows parsed from wp_product_categories sql")
|
||||
}
|
||||
if len(out) > 5000 {
|
||||
return nil, fmt.Errorf("too many wp_product_categories rows (%d)", len(out))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// scanWPCategoryRow finds the next ('Name', 'Prompt') tuple starting at from.
|
||||
func scanWPCategoryRow(sql string, from int) (name, prompt string, next int, ok bool) {
|
||||
// Locate opening (' after from.
|
||||
i := from
|
||||
for i < len(sql) {
|
||||
if sql[i] == '(' && i+1 < len(sql) && sql[i+1] == '\'' {
|
||||
break
|
||||
}
|
||||
i++
|
||||
}
|
||||
if i >= len(sql) {
|
||||
return "", "", from, false
|
||||
}
|
||||
i += 2 // past ('
|
||||
nameRaw, i2, err := scanMySQLQuotedString(sql, i)
|
||||
if err != nil {
|
||||
return "", "", from, false
|
||||
}
|
||||
i = i2
|
||||
// Expect , then optional whitespace then '
|
||||
for i < len(sql) && (sql[i] == ' ' || sql[i] == '\t' || sql[i] == '\n' || sql[i] == '\r') {
|
||||
i++
|
||||
}
|
||||
if i >= len(sql) || sql[i] != ',' {
|
||||
return "", "", from, false
|
||||
}
|
||||
i++
|
||||
for i < len(sql) && (sql[i] == ' ' || sql[i] == '\t' || sql[i] == '\n' || sql[i] == '\r') {
|
||||
i++
|
||||
}
|
||||
if i >= len(sql) || sql[i] != '\'' {
|
||||
return "", "", from, false
|
||||
}
|
||||
i++
|
||||
promptRaw, i3, err := scanMySQLQuotedString(sql, i)
|
||||
if err != nil {
|
||||
return "", "", from, false
|
||||
}
|
||||
return nameRaw, promptRaw, i3, true
|
||||
}
|
||||
|
||||
// scanMySQLQuotedString reads a mysqldump string whose opening quote was already consumed.
|
||||
// Handles ”, \', \", \\, \n, \r, \t, \0, and leaves the index after the closing quote.
|
||||
func scanMySQLQuotedString(s string, start int) (string, int, error) {
|
||||
var b strings.Builder
|
||||
b.Grow(256)
|
||||
i := start
|
||||
for i < len(s) {
|
||||
c := s[i]
|
||||
if c == '\\' && i+1 < len(s) {
|
||||
n := s[i+1]
|
||||
switch n {
|
||||
case 'n':
|
||||
b.WriteByte('\n')
|
||||
case 'r':
|
||||
b.WriteByte('\r')
|
||||
case 't':
|
||||
b.WriteByte('\t')
|
||||
case '0':
|
||||
b.WriteByte(0)
|
||||
case 'b':
|
||||
b.WriteByte('\b')
|
||||
case 'Z':
|
||||
b.WriteByte(0x1a)
|
||||
case '\'', '"', '\\':
|
||||
b.WriteByte(n)
|
||||
default:
|
||||
// MySQL keeps the second char for unknown escapes.
|
||||
b.WriteByte(n)
|
||||
}
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
if c == '\'' {
|
||||
if i+1 < len(s) && s[i+1] == '\'' {
|
||||
b.WriteByte('\'')
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
return b.String(), i + 1, nil
|
||||
}
|
||||
b.WriteByte(c)
|
||||
i++
|
||||
}
|
||||
return "", start, fmt.Errorf("unterminated mysql string")
|
||||
}
|
||||
|
||||
// ReadWPCategoryPromptOverlaysFromReader is a test helper around ParseWPProductCategoriesSQL.
|
||||
func ReadWPCategoryPromptOverlaysFromReader(r io.Reader) (byNorm map[string]string, err error) {
|
||||
raw, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := ParseWPProductCategoriesSQL(string(raw))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
byNorm = map[string]string{}
|
||||
for _, e := range entries {
|
||||
if key := normalizeCategoryPromptName(e.Name); key != "" && strings.TrimSpace(e.Prompt) != "" {
|
||||
byNorm[key] = e.Prompt
|
||||
}
|
||||
}
|
||||
return byNorm, nil
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
|
||||
)
|
||||
|
||||
func TestParseWPProductCategoriesSQL_SlusalkeSplit(t *testing.T) {
|
||||
t.Parallel()
|
||||
sql := "SET NAMES utf8mb4;\n\n" +
|
||||
"INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n" +
|
||||
"('Slušalke',\t'Ustvari nov opis izdelka v Slovenščini z naslednjimi spremenljivkami:\\n\\n" +
|
||||
"Star_opis_izdelka: {\\\"\\\"OPIS IZDELKA\\\"\\\"};\\n" +
|
||||
"Staro_ime_izdelka: {\\\"\\\"STARO IME IZDELKA\\\"\\\"};\\n\\n" +
|
||||
"Uporabi spodnjo GPT predlogo. \\n\\n" +
|
||||
"Sledi tej GPT predlogi stavek po stavek in sestavi nov opis izdelka:\\n\\n" +
|
||||
"GPT predloga:\\n\\n\\n" +
|
||||
"<name>{Napiši novo ime izdelka po formuli: \\\"\\\"znamka s pravilno kapitalizacijo\\\"\\\", \\\"\\\"tip izdelka lowercase\\\"\\\", \\\"\\\"\\\"poln model izdelka, če lahko z besedo in ID uppercase\\\"\\\"\\\". Ne uporabljaj vejic.}</name>\\n" +
|
||||
"<metaDescription>{Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov vključno s presledki}</metaDescription>\\n\\n" +
|
||||
"<H2>{Napiši Novo ime izdelka in izpostavi en benefit}</H2>\\n" +
|
||||
"<p>{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}</p>\\n" +
|
||||
"<H2>{Izpostavi en benefit, in NE napiši Novo ime izdelka}</H2>\\n" +
|
||||
"<p>{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}</p>" +
|
||||
"<b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>');\n"
|
||||
|
||||
entries, err := ParseWPProductCategoriesSQL(sql)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("want 1 entry, got %d", len(entries))
|
||||
}
|
||||
if entries[0].Name != "Slušalke" {
|
||||
t.Fatalf("name=%q", entries[0].Name)
|
||||
}
|
||||
raw := entries[0].Prompt
|
||||
if strings.Contains(raw, `\"`) {
|
||||
t.Fatalf("mysql escapes should be unescaped, still has backslash-quote: %q", raw[:80])
|
||||
}
|
||||
if !aiprompts.IsLegacyCombinedEnhancePrompt(raw) {
|
||||
t.Fatal("parsed prompt should look like legacy combined enhance")
|
||||
}
|
||||
|
||||
split := aiprompts.SplitLegacyCombinedEnhancePrompt(raw)
|
||||
if !aiprompts.CategoryEnhanceHasRoleSections(split) {
|
||||
t.Fatal("split must produce role sections")
|
||||
}
|
||||
if !strings.Contains(split, aiprompts.SectionTitleStart) || !strings.Contains(split, aiprompts.SectionDescriptionStart) {
|
||||
t.Fatal("split missing Title/Description section markers")
|
||||
}
|
||||
if !strings.Contains(split, "znamka s pravilno kapitalizacijo") {
|
||||
t.Fatal("Title section must keep naming formula")
|
||||
}
|
||||
if !strings.Contains(split, "Tehnične specifikacije") && !strings.Contains(split, "tehničnih specifikacij") {
|
||||
t.Fatal("Description section must keep HTML body intent")
|
||||
}
|
||||
if !strings.Contains(split, "140 znakov") {
|
||||
t.Fatal("Meta section must keep SEO intent")
|
||||
}
|
||||
if aiprompts.IsLegacyCombinedEnhancePrompt(split) {
|
||||
t.Fatal("split output must not still look like legacy combined prompt")
|
||||
}
|
||||
if strings.Contains(split, "<name>{") || strings.Contains(split, "<metaDescription>{") {
|
||||
t.Fatal("split must not keep legacy wrapper tag bodies")
|
||||
}
|
||||
|
||||
parts := aiprompts.ParseLegacyCombinedEnhancePrompt(raw)
|
||||
descJSON := aiprompts.DeriveDescriptionTemplateJSON(parts)
|
||||
if descJSON == "" {
|
||||
t.Fatal("expected description_template JSON from legacy HTML")
|
||||
}
|
||||
if !strings.Contains(descJSON, `"type":"h2"`) && !strings.Contains(descJSON, `"type":"p"`) {
|
||||
t.Fatalf("description_template should include h2/p sections: %s", descJSON)
|
||||
}
|
||||
if !strings.Contains(descJSON, "140 znakov") {
|
||||
t.Fatalf("description_template should carry metaDescription: %s", descJSON)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseWPProductCategoriesSQL_RealDownloadsFile(t *testing.T) {
|
||||
path := ResolveWPCategoryPromptsPath(`d:\Users\Green Eclipse\Downloads\wp_product_categories.sql`)
|
||||
if path == "" {
|
||||
// Also try env / auto-detect without failing CI machines that lack the dump.
|
||||
path = ResolveWPCategoryPromptsPath("")
|
||||
}
|
||||
if path == "" {
|
||||
t.Skip("wp_product_categories.sql not available on this machine")
|
||||
}
|
||||
byNorm, _, err := loadWPCategoryPromptOverlays(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(byNorm) < 50 {
|
||||
t.Fatalf("expected dozens of categories, got %d from %s", len(byNorm), path)
|
||||
}
|
||||
key := normalizeCategoryPromptName("Slušalke")
|
||||
raw, ok := byNorm[key]
|
||||
if !ok {
|
||||
t.Fatalf("Slušalke missing from %s (keys=%d)", path, len(byNorm))
|
||||
}
|
||||
split := aiprompts.SplitLegacyCombinedEnhancePrompt(raw)
|
||||
if aiprompts.CategoryEnhancePromptNeedsRepair(split) {
|
||||
t.Fatal("split of real dump Slušalke should not need repair")
|
||||
}
|
||||
if !strings.Contains(split, "tip izdelka lowercase") && !strings.Contains(split, "znamka") {
|
||||
t.Fatalf("unexpected Title rules in split: %s", split[:min(400, len(split))])
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWPCategoryPromptsPath_Explicit(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
p := filepath.Join(dir, "wp_product_categories.sql")
|
||||
if err := os.WriteFile(p, []byte("INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n('X','GPT predloga:\\n<name>{a}</name><metaDescription>{b}</metaDescription>');\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := ResolveWPCategoryPromptsPath(p)
|
||||
if got != p {
|
||||
t.Fatalf("got %q want %q", got, p)
|
||||
}
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func TestLoadWPCategoryPromptOverlaysFromBytes(t *testing.T) {
|
||||
t.Parallel()
|
||||
sql := "INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n('Slusalke','GPT predloga:\\n<name>{tip}</name><metaDescription>{140}</metaDescription><H2>{benefit}</H2>');\n"
|
||||
byNorm, _, err := LoadWPCategoryPromptOverlaysFromBytes([]byte(sql))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(byNorm) != 1 {
|
||||
t.Fatalf("entries=%d", len(byNorm))
|
||||
}
|
||||
if _, _, err := LoadWPCategoryPromptOverlaysFromBytes(nil); err == nil {
|
||||
t.Fatal("expected empty error")
|
||||
}
|
||||
huge := make([]byte, MaxWPCategorySQLBytes+1)
|
||||
if _, _, err := LoadWPCategoryPromptOverlaysFromBytes(huge); err == nil {
|
||||
t.Fatal("expected too-large error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCategoryPromptOverlaysPrefersUploadBytes(t *testing.T) {
|
||||
t.Parallel()
|
||||
sql := "INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n('UploadCat','GPT predloga:\\n<name>{a}</name><metaDescription>{b}</metaDescription>');\n"
|
||||
byNorm, _, meta := resolveCategoryPromptOverlays(RepairA1DemoOptions{WPCategoriesSQL: []byte(sql)})
|
||||
if meta.WPPath != "upload:wp_product_categories.sql" {
|
||||
t.Fatalf("WPPath=%q", meta.WPPath)
|
||||
}
|
||||
if !meta.ForceFromSeed {
|
||||
t.Fatal("upload must force from seed")
|
||||
}
|
||||
if byNorm[normalizeCategoryPromptName("UploadCat")] == "" {
|
||||
t.Fatal("missing upload category")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/mail"
|
||||
)
|
||||
|
||||
func TestAcceptInviteURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
got := mail.AcceptInviteURL("http://localhost:28472/", "abc123")
|
||||
want := "http://localhost:28472/accept-invite?token=abc123"
|
||||
if got != want {
|
||||
t.Fatalf("AcceptInviteURL = %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/billing"
|
||||
@@ -12,6 +15,10 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// syncA1MaxBodyBytes caps JSON or multipart Sync A1 payloads (dump + form fields).
|
||||
// Slightly above MaxWPCategorySQLBytes for multipart overhead / base64 inflation.
|
||||
const syncA1MaxBodyBytes = catalog.MaxWPCategorySQLBytes + (2 << 20)
|
||||
|
||||
// handleAdminSyncCompanyA1 runs dump category backfill (when dump is on the API
|
||||
// host filesystem) plus FixCompanyCatalog hygiene. Does not wipe/reimport.
|
||||
//
|
||||
@@ -20,9 +27,13 @@ import (
|
||||
// POST /api/admin/companies/{id}/sync-a1
|
||||
// POST /api/admin/companies/{id}/fix-catalog (compat alias)
|
||||
//
|
||||
// Body: confirm=true required; backfill_categories (default true);
|
||||
// Body (JSON): confirm=true required; backfill_categories (default true);
|
||||
// reprocess_sample_limit (default 25, max 200); mysql_dump optional path;
|
||||
// skip_dump_backfill (default false).
|
||||
// skip_dump_backfill (default false); wp_product_categories_sql_b64 optional
|
||||
// base64 of wp_product_categories.sql (primary for category prompts).
|
||||
//
|
||||
// Body (multipart/form-data): same fields as form values; file field
|
||||
// wp_product_categories or wp_categories_sql for the SQL dump upload.
|
||||
//
|
||||
// Flash (UI): result → flash.admin.syncA1Success.
|
||||
func (s *Server) handleAdminSyncCompanyA1(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -37,18 +48,14 @@ func (s *Server) handleAdminSyncCompanyA1(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Confirm bool `json:"confirm"`
|
||||
BackfillCategories *bool `json:"backfill_categories"`
|
||||
ReprocessSampleLimit *int `json:"reprocess_sample_limit"`
|
||||
MySQLDump string `json:"mysql_dump"`
|
||||
SkipDumpBackfill bool `json:"skip_dump_backfill"`
|
||||
}
|
||||
if err := DecodeJSON(r, &body); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid json")
|
||||
r.Body = http.MaxBytesReader(w, r.Body, syncA1MaxBodyBytes)
|
||||
|
||||
parsed, err := parseSyncA1Request(r)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if !body.Confirm {
|
||||
if !parsed.Confirm {
|
||||
Error(w, http.StatusBadRequest, "confirm must be true")
|
||||
return
|
||||
}
|
||||
@@ -67,12 +74,12 @@ func (s *Server) handleAdminSyncCompanyA1(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
|
||||
backfill := true
|
||||
if body.BackfillCategories != nil {
|
||||
backfill = *body.BackfillCategories
|
||||
if parsed.BackfillCategories != nil {
|
||||
backfill = *parsed.BackfillCategories
|
||||
}
|
||||
sampleLimit := 25
|
||||
if body.ReprocessSampleLimit != nil {
|
||||
sampleLimit = *body.ReprocessSampleLimit
|
||||
if parsed.ReprocessSampleLimit != nil {
|
||||
sampleLimit = *parsed.ReprocessSampleLimit
|
||||
}
|
||||
if sampleLimit < 0 {
|
||||
sampleLimit = 0
|
||||
@@ -92,9 +99,10 @@ func (s *Server) handleAdminSyncCompanyA1(w http.ResponseWriter, r *http.Request
|
||||
BackfillCategories: backfill,
|
||||
ReprocessSampleLimit: sampleLimit,
|
||||
AIPrompts: s.AIPrompts,
|
||||
WPCategoriesSQL: parsed.WPCategoriesSQL,
|
||||
},
|
||||
MySQLDumpPath: strings.TrimSpace(body.MySQLDump),
|
||||
SkipDumpBackfill: body.SkipDumpBackfill,
|
||||
MySQLDumpPath: strings.TrimSpace(parsed.MySQLDump),
|
||||
SkipDumpBackfill: parsed.SkipDumpBackfill,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
@@ -104,7 +112,12 @@ func (s *Server) handleAdminSyncCompanyA1(w http.ResponseWriter, r *http.Request
|
||||
|
||||
note := "Synced in place (dump categories when available + hygiene); reprocess recommended products manually (no mass reprocess)."
|
||||
if !result.DumpFound {
|
||||
note = "Hygiene completed without dump backfill. Place descrybe_new.sql on the API host (scripts/seed/ or SEED_A1_MYSQL_DUMP) for mapped category coverage from the legacy dump."
|
||||
note = "Hygiene completed without product-dump category backfill. Place descrybe_new.sql on the API host (scripts/seed/ or SEED_A1_MYSQL_DUMP) for mapped category coverage from the legacy product dump."
|
||||
}
|
||||
if len(parsed.WPCategoriesSQL) > 0 {
|
||||
note = "Applied uploaded wp_product_categories.sql (Title/Description/Meta/Attributes sections) + catalog hygiene. Reprocess recommended products manually (no mass reprocess)."
|
||||
} else if strings.TrimSpace(result.WPCategoriesPath) == "" {
|
||||
note += " Upload wp_product_categories.sql on Sync A1 for force-applied category prompts when the dump is not on the API host."
|
||||
}
|
||||
|
||||
JSON(w, http.StatusOK, map[string]any{
|
||||
@@ -118,3 +131,135 @@ func (s *Server) handleAdminSyncCompanyA1(w http.ResponseWriter, r *http.Request
|
||||
func (s *Server) handleAdminFixCompanyCatalog(w http.ResponseWriter, r *http.Request) {
|
||||
s.handleAdminSyncCompanyA1(w, r)
|
||||
}
|
||||
|
||||
type syncA1Request struct {
|
||||
Confirm bool
|
||||
BackfillCategories *bool
|
||||
ReprocessSampleLimit *int
|
||||
MySQLDump string
|
||||
SkipDumpBackfill bool
|
||||
WPCategoriesSQL []byte
|
||||
}
|
||||
|
||||
func parseSyncA1Request(r *http.Request) (syncA1Request, error) {
|
||||
ct := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type")))
|
||||
if strings.HasPrefix(ct, "multipart/form-data") {
|
||||
return parseSyncA1Multipart(r)
|
||||
}
|
||||
return parseSyncA1JSON(r)
|
||||
}
|
||||
|
||||
func parseSyncA1JSON(r *http.Request) (syncA1Request, error) {
|
||||
var body struct {
|
||||
Confirm bool `json:"confirm"`
|
||||
BackfillCategories *bool `json:"backfill_categories"`
|
||||
ReprocessSampleLimit *int `json:"reprocess_sample_limit"`
|
||||
MySQLDump string `json:"mysql_dump"`
|
||||
SkipDumpBackfill bool `json:"skip_dump_backfill"`
|
||||
WPProductCategoriesB64 string `json:"wp_product_categories_sql_b64"`
|
||||
WPProductCategoriesPath string `json:"wp_product_categories"` // path-only; ignored for apply (upload required in prod)
|
||||
}
|
||||
if err := DecodeJSON(r, &body); err != nil {
|
||||
return syncA1Request{}, errInvalidJSON
|
||||
}
|
||||
out := syncA1Request{
|
||||
Confirm: body.Confirm,
|
||||
BackfillCategories: body.BackfillCategories,
|
||||
ReprocessSampleLimit: body.ReprocessSampleLimit,
|
||||
MySQLDump: body.MySQLDump,
|
||||
SkipDumpBackfill: body.SkipDumpBackfill,
|
||||
}
|
||||
_ = body.WPProductCategoriesPath // path paste is not enough for prod — ignore
|
||||
b64 := strings.TrimSpace(body.WPProductCategoriesB64)
|
||||
if b64 == "" {
|
||||
return out, nil
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(b64)
|
||||
if err != nil {
|
||||
// Allow URL-safe / raw without padding for convenience.
|
||||
raw, err = base64.RawStdEncoding.DecodeString(b64)
|
||||
if err != nil {
|
||||
raw, err = base64.URLEncoding.DecodeString(b64)
|
||||
}
|
||||
if err != nil {
|
||||
return syncA1Request{}, errWPCategoriesB64
|
||||
}
|
||||
}
|
||||
if len(raw) > catalog.MaxWPCategorySQLBytes {
|
||||
return syncA1Request{}, errWPCategoriesTooLarge
|
||||
}
|
||||
out.WPCategoriesSQL = raw
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseSyncA1Multipart(r *http.Request) (syncA1Request, error) {
|
||||
if err := r.ParseMultipartForm(syncA1MaxBodyBytes); err != nil {
|
||||
return syncA1Request{}, errInvalidMultipart
|
||||
}
|
||||
out := syncA1Request{
|
||||
Confirm: parseFormBool(r.FormValue("confirm")),
|
||||
MySQLDump: strings.TrimSpace(r.FormValue("mysql_dump")),
|
||||
SkipDumpBackfill: parseFormBool(r.FormValue("skip_dump_backfill")),
|
||||
}
|
||||
if v := strings.TrimSpace(r.FormValue("backfill_categories")); v != "" {
|
||||
b := parseFormBool(v)
|
||||
out.BackfillCategories = &b
|
||||
}
|
||||
if v := strings.TrimSpace(r.FormValue("reprocess_sample_limit")); v != "" {
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return syncA1Request{}, errInvalidSampleLimit
|
||||
}
|
||||
out.ReprocessSampleLimit = &n
|
||||
}
|
||||
|
||||
file, _, err := r.FormFile("wp_product_categories")
|
||||
if err != nil {
|
||||
file, _, err = r.FormFile("wp_categories_sql")
|
||||
}
|
||||
if err == nil {
|
||||
defer file.Close()
|
||||
raw, readErr := io.ReadAll(io.LimitReader(file, int64(catalog.MaxWPCategorySQLBytes)+1))
|
||||
if readErr != nil {
|
||||
return syncA1Request{}, errWPCategoriesRead
|
||||
}
|
||||
if len(raw) > catalog.MaxWPCategorySQLBytes {
|
||||
return syncA1Request{}, errWPCategoriesTooLarge
|
||||
}
|
||||
out.WPCategoriesSQL = raw
|
||||
}
|
||||
|
||||
if b64 := strings.TrimSpace(r.FormValue("wp_product_categories_sql_b64")); b64 != "" && len(out.WPCategoriesSQL) == 0 {
|
||||
raw, decErr := base64.StdEncoding.DecodeString(b64)
|
||||
if decErr != nil {
|
||||
return syncA1Request{}, errWPCategoriesB64
|
||||
}
|
||||
if len(raw) > catalog.MaxWPCategorySQLBytes {
|
||||
return syncA1Request{}, errWPCategoriesTooLarge
|
||||
}
|
||||
out.WPCategoriesSQL = raw
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseFormBool(v string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(v)) {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type syncA1ParseError string
|
||||
|
||||
func (e syncA1ParseError) Error() string { return string(e) }
|
||||
|
||||
var (
|
||||
errInvalidJSON = syncA1ParseError("invalid json")
|
||||
errInvalidMultipart = syncA1ParseError("invalid multipart form")
|
||||
errInvalidSampleLimit = syncA1ParseError("invalid reprocess_sample_limit")
|
||||
errWPCategoriesB64 = syncA1ParseError("invalid wp_product_categories_sql_b64")
|
||||
errWPCategoriesTooLarge = syncA1ParseError("wp_product_categories.sql too large")
|
||||
errWPCategoriesRead = syncA1ParseError("could not read wp_product_categories upload")
|
||||
)
|
||||
|
||||
@@ -216,6 +216,7 @@ func (s *Server) handleAdminSendSetPasswordEmails(w http.ResponseWriter, r *http
|
||||
skippedRateLimited := 0
|
||||
skippedSend := 0
|
||||
var singleToken string
|
||||
var singleMode string
|
||||
singleUser := body.UserID != nil
|
||||
|
||||
for _, uid := range targets {
|
||||
@@ -251,6 +252,11 @@ func (s *Server) handleAdminSendSetPasswordEmails(w http.ResponseWriter, r *http
|
||||
if err := s.Mail.Send(msg); err != nil {
|
||||
log.Printf("admin set-password send failed user_id=%s", uid)
|
||||
skippedSend++
|
||||
if singleUser {
|
||||
// Still return the one-time link so admins can share it while impersonating / offline SMTP.
|
||||
singleToken = token
|
||||
singleMode = mode
|
||||
}
|
||||
continue
|
||||
}
|
||||
if smtpOn {
|
||||
@@ -258,6 +264,7 @@ func (s *Server) handleAdminSendSetPasswordEmails(w http.ResponseWriter, r *http
|
||||
} else if singleUser {
|
||||
// Share token only for single-user reissue when SMTP is off (no email in response).
|
||||
singleToken = token
|
||||
singleMode = mode
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,6 +282,11 @@ func (s *Server) handleAdminSendSetPasswordEmails(w http.ResponseWriter, r *http
|
||||
}
|
||||
if singleToken != "" {
|
||||
resp["token"] = singleToken
|
||||
if singleMode == "hmac" {
|
||||
resp["accept_url"] = mail.SetPasswordURL(s.Config.WebOrigin, singleToken)
|
||||
} else {
|
||||
resp["accept_url"] = mail.AcceptInviteURL(s.Config.WebOrigin, singleToken)
|
||||
}
|
||||
}
|
||||
JSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/catalog"
|
||||
)
|
||||
|
||||
func TestParseSyncA1JSON_Base64Upload(t *testing.T) {
|
||||
t.Parallel()
|
||||
sql := "INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n('A','GPT predloga:\\n<name>{x}</name><metaDescription>{y}</metaDescription>');\n"
|
||||
b64 := base64.StdEncoding.EncodeToString([]byte(sql))
|
||||
body := `{"confirm":true,"wp_product_categories_sql_b64":"` + b64 + `"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/sync", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
parsed, err := parseSyncA1JSON(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !parsed.Confirm {
|
||||
t.Fatal("confirm")
|
||||
}
|
||||
if string(parsed.WPCategoriesSQL) != sql {
|
||||
t.Fatalf("sql mismatch len=%d", len(parsed.WPCategoriesSQL))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSyncA1JSON_RejectsOversizedBase64(t *testing.T) {
|
||||
t.Parallel()
|
||||
raw := make([]byte, catalog.MaxWPCategorySQLBytes+1)
|
||||
b64 := base64.StdEncoding.EncodeToString(raw)
|
||||
body := `{"confirm":true,"wp_product_categories_sql_b64":"` + b64 + `"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/sync", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
_, err := parseSyncA1JSON(req)
|
||||
if err == nil {
|
||||
t.Fatal("expected too-large error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSyncA1Multipart_FileUpload(t *testing.T) {
|
||||
t.Parallel()
|
||||
sql := "INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n('B','GPT predloga:\\n<name>{x}</name><metaDescription>{y}</metaDescription>');\n"
|
||||
var buf bytes.Buffer
|
||||
w := multipart.NewWriter(&buf)
|
||||
if err := w.WriteField("confirm", "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
part, err := w.CreateFormFile("wp_product_categories", "wp_product_categories.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := part.Write([]byte(sql)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ct := w.FormDataContentType()
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/sync", bytes.NewReader(buf.Bytes()))
|
||||
req.Header.Set("Content-Type", ct)
|
||||
parsed, err := parseSyncA1Multipart(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !parsed.Confirm {
|
||||
t.Fatal("confirm")
|
||||
}
|
||||
if string(parsed.WPCategoriesSQL) != sql {
|
||||
t.Fatalf("sql mismatch len=%d", len(parsed.WPCategoriesSQL))
|
||||
}
|
||||
}
|
||||
@@ -347,9 +347,13 @@ func (s *Server) handleMe(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if m, err := s.Auth.EnsureMembership(r.Context(), uid, cid); err == nil {
|
||||
out["membership"] = map[string]string{"role": m.Role, "status": m.Status}
|
||||
if isOwner, oerr := s.Auth.IsCompanyOwner(r.Context(), cid, uid); oerr == nil {
|
||||
out["is_owner"] = isOwner
|
||||
}
|
||||
} else if staffTenantSwitch && errors.Is(err, auth.ErrNotCompanyMember) {
|
||||
staffOverride = true
|
||||
out["membership"] = map[string]any{"role": "admin", "status": "active", "staff_override": true}
|
||||
out["is_owner"] = false
|
||||
}
|
||||
}
|
||||
if staffTenantSwitch && homeStr != "" {
|
||||
|
||||
@@ -20,20 +20,25 @@ func (s *Server) handleGetCompany(w http.ResponseWriter, r *http.Request) {
|
||||
name, language string
|
||||
merge bool
|
||||
contentLangs []string
|
||||
ownerUserID *uuid.UUID
|
||||
)
|
||||
err := s.Pool.QueryRow(r.Context(), `
|
||||
SELECT id, name, language, merge_products_by_gtin, COALESCE(content_languages, '{}')
|
||||
SELECT id, name, language, merge_products_by_gtin, COALESCE(content_languages, '{}'), owner_user_id
|
||||
FROM companies WHERE id = $1`, cid).
|
||||
Scan(&id, &name, &language, &merge, &contentLangs)
|
||||
Scan(&id, &name, &language, &merge, &contentLangs, &ownerUserID)
|
||||
if err != nil {
|
||||
Error(w, http.StatusNotFound, "company not found")
|
||||
return
|
||||
}
|
||||
parsed, _ := company.ParseContentLanguages(contentLangs, language)
|
||||
JSON(w, http.StatusOK, map[string]any{
|
||||
out := map[string]any{
|
||||
"id": id, "name": name, "language": language,
|
||||
"content_languages": parsed, "merge_products_by_gtin": merge,
|
||||
})
|
||||
}
|
||||
if ownerUserID != nil {
|
||||
out["owner_user_id"] = *ownerUserID
|
||||
}
|
||||
JSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (s *Server) handleUpdateCompany(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -150,6 +155,8 @@ func (s *Server) handlePutCompanySettings(w http.ResponseWriter, r *http.Request
|
||||
func (s *Server) handleListTeam(w http.ResponseWriter, r *http.Request) {
|
||||
cid, _ := CompanyIDFromContext(r.Context())
|
||||
limit, offset := ParseLimitOffset(r)
|
||||
var ownerUserID *uuid.UUID
|
||||
_ = s.Pool.QueryRow(r.Context(), `SELECT owner_user_id FROM companies WHERE id = $1`, cid).Scan(&ownerUserID)
|
||||
var total int64
|
||||
if err := s.Pool.QueryRow(r.Context(), `
|
||||
SELECT count(*) FROM memberships m WHERE m.company_id = $1 AND m.status = 'active'`, cid).Scan(&total); err != nil {
|
||||
@@ -166,12 +173,13 @@ func (s *Server) handleListTeam(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer rows.Close()
|
||||
type member struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
Role string `json:"role"`
|
||||
Status string `json:"status"`
|
||||
Email string `json:"email"`
|
||||
Name *string `json:"name"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
Role string `json:"role"`
|
||||
Status string `json:"status"`
|
||||
Email string `json:"email"`
|
||||
Name *string `json:"name"`
|
||||
IsOwner bool `json:"is_owner"`
|
||||
}
|
||||
out := make([]member, 0)
|
||||
for rows.Next() {
|
||||
@@ -180,9 +188,14 @@ func (s *Server) handleListTeam(w http.ResponseWriter, r *http.Request) {
|
||||
Error(w, http.StatusInternalServerError, "scan failed")
|
||||
return
|
||||
}
|
||||
m.IsOwner = ownerUserID != nil && *ownerUserID == m.UserID
|
||||
out = append(out, m)
|
||||
}
|
||||
JSON(w, http.StatusOK, map[string]any{"members": out, "total": total, "limit": limit, "offset": offset})
|
||||
resp := map[string]any{"members": out, "total": total, "limit": limit, "offset": offset}
|
||||
if ownerUserID != nil {
|
||||
resp["owner_user_id"] = *ownerUserID
|
||||
}
|
||||
JSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (s *Server) handleCreateInvite(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -222,6 +235,7 @@ func (s *Server) handleCreateInvite(w http.ResponseWriter, r *http.Request) {
|
||||
// Token returned when email was not delivered so operators can share the accept link.
|
||||
if includeToken {
|
||||
resp["token"] = token
|
||||
resp["accept_url"] = mail.AcceptInviteURL(s.Config.WebOrigin, token)
|
||||
}
|
||||
JSON(w, http.StatusCreated, resp)
|
||||
}
|
||||
@@ -243,6 +257,13 @@ func (s *Server) handleRemoveMember(w http.ResponseWriter, r *http.Request) {
|
||||
Error(w, http.StatusBadRequest, "invalid user id")
|
||||
return
|
||||
}
|
||||
if ownerID, hasOwner, oerr := s.Auth.CompanyOwnerID(r.Context(), cid); oerr != nil {
|
||||
Error(w, http.StatusInternalServerError, "lookup failed")
|
||||
return
|
||||
} else if hasOwner && ownerID == userID {
|
||||
Error(w, http.StatusConflict, auth.ErrCannotRemoveOwner.Error())
|
||||
return
|
||||
}
|
||||
var currentRole, status string
|
||||
err = s.Pool.QueryRow(r.Context(), `
|
||||
SELECT role, status FROM memberships
|
||||
@@ -361,3 +382,49 @@ func (s *Server) handleUpdateMemberRole(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
JSON(w, http.StatusOK, map[string]any{"status": "ok", "role": newRole, "user_id": userID})
|
||||
}
|
||||
|
||||
func (s *Server) handleTransferOwnership(w http.ResponseWriter, r *http.Request) {
|
||||
cid, _ := CompanyIDFromContext(r.Context())
|
||||
uid, ok := UserIDFromContext(r.Context())
|
||||
if !ok {
|
||||
Error(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
platformAdmin, err := s.checkPlatformAdmin(r.Context(), uid)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "authorization check failed")
|
||||
return
|
||||
}
|
||||
if !platformAdmin {
|
||||
isOwner, oerr := s.Auth.IsCompanyOwner(r.Context(), cid, uid)
|
||||
if oerr != nil {
|
||||
Error(w, http.StatusInternalServerError, "authorization check failed")
|
||||
return
|
||||
}
|
||||
if !isOwner {
|
||||
Error(w, http.StatusForbidden, auth.ErrNotCompanyOwner.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
var body struct {
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
}
|
||||
if err := DecodeJSON(r, &body); err != nil || body.UserID == uuid.Nil {
|
||||
Error(w, http.StatusBadRequest, "invalid json")
|
||||
return
|
||||
}
|
||||
if err := s.Auth.TransferOwnership(r.Context(), cid, body.UserID); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, auth.ErrTransferSelf):
|
||||
JSON(w, http.StatusOK, map[string]any{"status": "ok", "owner_user_id": body.UserID})
|
||||
case errors.Is(err, auth.ErrOwnerRequired), errors.Is(err, auth.ErrNotCompanyMember):
|
||||
Error(w, http.StatusBadRequest, err.Error())
|
||||
case errors.Is(err, auth.ErrCompanyNotFound):
|
||||
Error(w, http.StatusNotFound, err.Error())
|
||||
default:
|
||||
ClientOrLog(w, http.StatusBadRequest, "could not transfer ownership", err, auth.ClientError)
|
||||
}
|
||||
return
|
||||
}
|
||||
JSON(w, http.StatusOK, map[string]any{"status": "ok", "owner_user_id": body.UserID})
|
||||
}
|
||||
|
||||
@@ -89,6 +89,49 @@ func (s *Server) allowCompanyAdminOrPlatform(w http.ResponseWriter, r *http.Requ
|
||||
return false
|
||||
}
|
||||
|
||||
// allowCompanyOwnerOrPlatform allows the company billing owner or a platform admin.
|
||||
// When owner_user_id is unset (pre-backfill), falls back to company admin so billing is not locked out.
|
||||
func (s *Server) allowCompanyOwnerOrPlatform(w http.ResponseWriter, r *http.Request) bool {
|
||||
uid, ok := UserIDFromContext(r.Context())
|
||||
if !ok {
|
||||
Error(w, http.StatusUnauthorized, "unauthorized")
|
||||
return false
|
||||
}
|
||||
isAdmin, err := s.checkPlatformAdmin(r.Context(), uid)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "authorization check failed")
|
||||
return false
|
||||
}
|
||||
if isAdmin {
|
||||
return true
|
||||
}
|
||||
cid, ok := CompanyIDFromContext(r.Context())
|
||||
if !ok {
|
||||
Error(w, http.StatusForbidden, "company required")
|
||||
return false
|
||||
}
|
||||
if s.Auth != nil {
|
||||
ownerID, hasOwner, err := s.Auth.CompanyOwnerID(r.Context(), cid)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "authorization check failed")
|
||||
return false
|
||||
}
|
||||
if hasOwner {
|
||||
if ownerID == uid {
|
||||
return true
|
||||
}
|
||||
Error(w, http.StatusForbidden, "company owner required")
|
||||
return false
|
||||
}
|
||||
}
|
||||
// Legacy fallback before owner backfill, or unit tests without Auth wired.
|
||||
if CompanyAdminAllowed(r.Context()) {
|
||||
return true
|
||||
}
|
||||
Error(w, http.StatusForbidden, "company owner required")
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) RequireSession(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
uidStr := s.Sessions.GetString(r.Context(), auth.SessionUserIDKey)
|
||||
|
||||
@@ -447,6 +447,7 @@ func (s *Server) Router() http.Handler {
|
||||
r.Post("/team/invites", s.handleCreateInvite)
|
||||
r.Get("/team/invites", s.handleListInvites)
|
||||
r.Delete("/team/invites/{inviteID}", s.handleRevokeInvite)
|
||||
r.Post("/team/transfer-ownership", s.handleTransferOwnership)
|
||||
r.Patch("/team/{userID}", s.handleUpdateMemberRole)
|
||||
r.Delete("/team/{userID}", s.handleRemoveMember)
|
||||
|
||||
|
||||
@@ -40,9 +40,7 @@ func (s *Server) handleStripeStatus(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleStripeCheckout(w http.ResponseWriter, r *http.Request) {
|
||||
cid, _ := CompanyIDFromContext(r.Context())
|
||||
uid, _ := UserIDFromContext(r.Context())
|
||||
role, _ := RoleFromContext(r.Context())
|
||||
if role != "admin" {
|
||||
Error(w, http.StatusForbidden, "admin required")
|
||||
if !s.allowCompanyOwnerOrPlatform(w, r) {
|
||||
return
|
||||
}
|
||||
platformAdmin, err := s.checkPlatformAdmin(r.Context(), uid)
|
||||
@@ -73,9 +71,7 @@ func (s *Server) handleStripeCheckout(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) handleStripePortal(w http.ResponseWriter, r *http.Request) {
|
||||
cid, _ := CompanyIDFromContext(r.Context())
|
||||
role, _ := RoleFromContext(r.Context())
|
||||
if role != "admin" {
|
||||
Error(w, http.StatusForbidden, "admin required")
|
||||
if !s.allowCompanyOwnerOrPlatform(w, r) {
|
||||
return
|
||||
}
|
||||
res, err := s.stripeSvc().CreatePortalSession(r.Context(), cid)
|
||||
|
||||
@@ -107,7 +107,7 @@ func hasHeaderBreak(v string) bool {
|
||||
}
|
||||
|
||||
func InviteMessage(webOrigin, email, token, companyName string) Message {
|
||||
link := strings.TrimRight(webOrigin, "/") + "/accept-invite?token=" + token
|
||||
link := AcceptInviteURL(webOrigin, token)
|
||||
text := fmt.Sprintf("You have been invited to %s on Descrybe.\n\nAccept: %s\n", companyName, link)
|
||||
html := fmt.Sprintf(
|
||||
`<p>You have been invited to <strong>%s</strong> on Descrybe.</p><p><a href="%s">Accept invite</a></p>`,
|
||||
@@ -116,6 +116,11 @@ func InviteMessage(webOrigin, email, token, companyName string) Message {
|
||||
return Message{To: email, Subject: "You are invited to Descrybe", Text: text, HTML: html}
|
||||
}
|
||||
|
||||
// AcceptInviteURL builds the durable invite / set-password accept link (hashed invite tokens).
|
||||
func AcceptInviteURL(webOrigin, token string) string {
|
||||
return strings.TrimRight(webOrigin, "/") + "/accept-invite?token=" + token
|
||||
}
|
||||
|
||||
// SetPasswordURL builds the HMAC set-password accept-invite link.
|
||||
func SetPasswordURL(webOrigin, token string) string {
|
||||
return strings.TrimRight(webOrigin, "/") + "/accept-invite?token=" + token + "&mode=set-password"
|
||||
@@ -133,7 +138,7 @@ func SetPasswordMessage(webOrigin, email, token string) Message {
|
||||
|
||||
// MigratedSetPasswordMessage uses migrator invite tokens (accept-invite flow).
|
||||
func MigratedSetPasswordMessage(webOrigin, email, token string) Message {
|
||||
link := strings.TrimRight(webOrigin, "/") + "/accept-invite?token=" + token
|
||||
link := AcceptInviteURL(webOrigin, token)
|
||||
text := fmt.Sprintf(
|
||||
"Your Descrybe account was migrated. Set your password here:\n\n%s\n\nIf you did not expect this email, ignore it.\n",
|
||||
link,
|
||||
|
||||
@@ -20,6 +20,10 @@ type FixCompanyCatalogOpts struct {
|
||||
ReprocessSampleLimit int
|
||||
// AIPrompts optionally re-applies BuiltInDefaults product_enhance per content language.
|
||||
AIPrompts *aiprompts.Service
|
||||
// WPCategoriesSQL is an uploaded wp_product_categories.sql dump. When set,
|
||||
// RepairCompanyCategoryEnhancePrompts force-applies overlays from these bytes
|
||||
// (Admin Sync A1 primary path). Empty keeps filesystem auto-detect fallback.
|
||||
WPCategoriesSQL []byte
|
||||
}
|
||||
|
||||
// FixCompanyCatalogResult is the idempotent admin Fix A1 report.
|
||||
@@ -36,6 +40,10 @@ type FixCompanyCatalogResult struct {
|
||||
CategoryPromptsEmpty int `json:"category_prompts_empty_skipped"`
|
||||
// Prompts aliases category_prompts_updated for flash.admin.fixA1Success {prompts}.
|
||||
Prompts int `json:"prompts"`
|
||||
// WPCategoriesPath is set when prompts came from wp_product_categories.sql
|
||||
// (upload:… or a resolved filesystem path).
|
||||
WPCategoriesPath string `json:"wp_categories_path,omitempty"`
|
||||
WPCategoriesEntries int `json:"wp_categories_entries,omitempty"`
|
||||
|
||||
ProductEnhanceLanguages int `json:"product_enhance_languages"`
|
||||
|
||||
@@ -88,13 +96,17 @@ func FixCompanyCatalog(ctx context.Context, pool *pgxpool.Pool, companyID uuid.U
|
||||
out.CategoryAttributeOrphansRemoved = orphans
|
||||
out.CategoryAttributeLinks = links
|
||||
|
||||
updated, alreadyOK, emptySkipped, err := catalog.RepairCompanyCategoryEnhancePrompts(ctx, pool, companyID)
|
||||
updatedRes, err := catalog.RepairCompanyCategoryEnhancePromptsWithOptions(ctx, pool, companyID, catalog.RepairA1DemoOptions{
|
||||
WPCategoriesSQL: opts.WPCategoriesSQL,
|
||||
})
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.CategoryPromptsUpdated = updated
|
||||
out.CategoryPromptsAlreadyOK = alreadyOK
|
||||
out.CategoryPromptsEmpty = emptySkipped
|
||||
out.CategoryPromptsUpdated = updatedRes.Updated
|
||||
out.CategoryPromptsAlreadyOK = updatedRes.AlreadyOK
|
||||
out.CategoryPromptsEmpty = updatedRes.EmptySkipped
|
||||
out.WPCategoriesPath = updatedRes.WPCategoriesPath
|
||||
out.WPCategoriesEntries = updatedRes.SeedEntries
|
||||
|
||||
if opts.AIPrompts != nil {
|
||||
n, err := opts.AIPrompts.ApplyBuiltInProductEnhance(ctx, companyID)
|
||||
|
||||
@@ -174,7 +174,7 @@ func FormatTitleFormulaConstraint(template any) string {
|
||||
continue
|
||||
}
|
||||
}
|
||||
b.WriteString("Prefer Attrs values for [attr] slots; keep literal text as written; write name in {{language}}.")
|
||||
b.WriteString("Prefer Attrs values for [attr] slots; keep literal text as written; write name in {{language}}; never emit brand-only when product_type or product_model slots exist.")
|
||||
return strings.TrimSpace(b.String())
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ func AppendDescriptionFormulaSystemOverride(systemTpl string, descriptionTemplat
|
||||
// titleFormulaSystemOverride is appended to the enhance system template when a
|
||||
// category title_template is present so company/built-in "short retail title"
|
||||
// rules cannot ignore the Title formula (mirrors description formula override).
|
||||
const titleFormulaSystemOverride = `When the user message includes a "Title formula", obey that structure for "name" over any shorter "short retail title" guidance: build name from Attrs in the given element order, keep literal text as written, write name in {{language}}. Never ignore the Title formula.`
|
||||
const titleFormulaSystemOverride = `When the user message includes a "Title formula", obey that structure for "name" over any shorter "short retail title" guidance: build name from Attrs in the given element order, keep literal text as written, write name in {{language}}. Never ignore the Title formula. Never emit brand-only when the formula includes product_type or product_model.`
|
||||
|
||||
// AppendTitleFormulaSystemOverride strengthens the system prompt when a title
|
||||
// formula is active. No-op when template is empty/unparseable.
|
||||
|
||||
Reference in New Issue
Block a user