fixes
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// productEnhanceUpsertLanguages returns ISO 639-1 codes to store product_enhance
|
||||
// under. Never returns LangPromptAny ("*") — ai_prompt_templates.language has
|
||||
// CHECK (language ~ '^[a-z]{2}$'). Categories.prompt JSON may still use "*".
|
||||
func productEnhanceUpsertLanguages(contentLangs []string, primary string) []string {
|
||||
primary = company.NormalizeLanguage(primary)
|
||||
if primary == "" || primary == company.LangPromptAny || !company.IsAllowedLanguage(primary) {
|
||||
primary = company.DefaultLanguage
|
||||
}
|
||||
out := make([]string, 0, len(contentLangs)+1)
|
||||
seen := map[string]struct{}{}
|
||||
add := func(code string) {
|
||||
code = company.NormalizeLanguage(code)
|
||||
if code == "" || code == company.LangPromptAny || !company.IsAllowedLanguage(code) {
|
||||
return
|
||||
}
|
||||
if _, ok := seen[code]; ok {
|
||||
return
|
||||
}
|
||||
seen[code] = struct{}{}
|
||||
out = append(out, code)
|
||||
}
|
||||
add(primary)
|
||||
for _, lang := range contentLangs {
|
||||
add(lang)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return []string{company.DefaultLanguage}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ApplyBuiltInProductEnhance upserts BuiltInDefaults for product_enhance under
|
||||
// the company primary language and each content language. Templates still use
|
||||
// {{language}} at render time; Resolve falls back requested → primary → built-in.
|
||||
// Do not store LangPromptAny ("*") in ai_prompt_templates (DB language CHECK).
|
||||
func (s *Service) ApplyBuiltInProductEnhance(ctx context.Context, companyID uuid.UUID) (languagesApplied int, err error) {
|
||||
def, ok := DefaultFor(KeyProductEnhance)
|
||||
if !ok {
|
||||
return 0, ErrInvalidKey
|
||||
}
|
||||
primary := company.LoadLanguage(ctx, s.Pool, companyID)
|
||||
langs := productEnhanceUpsertLanguages(
|
||||
company.LoadContentLanguages(ctx, s.Pool, companyID),
|
||||
primary,
|
||||
)
|
||||
enabled := true
|
||||
items := make([]UpdateItem, 0, len(langs))
|
||||
for _, lang := range langs {
|
||||
items = append(items, UpdateItem{
|
||||
Key: KeyProductEnhance,
|
||||
Language: lang,
|
||||
SystemTemplate: def.SystemTemplate,
|
||||
UserTemplate: def.UserTemplate,
|
||||
IsEnabled: &enabled,
|
||||
})
|
||||
}
|
||||
_, err = s.Update(ctx, companyID, UpdateInput{
|
||||
Language: langs[0],
|
||||
Prompts: items,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(langs), nil
|
||||
}
|
||||
Reference in New Issue
Block a user