replace cta with social_media question; add SocialMedia field to landing_session

This commit is contained in:
Lizandro Guarnizo
2026-05-28 20:49:35 -05:00
parent 06d85dc0bf
commit 52ce8638a8
4 changed files with 243 additions and 10 deletions
+8 -7
View File
@@ -12,13 +12,14 @@ import (
type LandingSession struct {
gorm.Model
Token string `json:"token" gorm:"column:token;uniqueIndex;not null"`
UserName string `json:"user_name" gorm:"column:user_name"`
UserEmail string `json:"user_email" gorm:"column:user_email"`
UserPhone string `json:"user_phone" gorm:"column:user_phone"`
UserCompany string `json:"user_company" gorm:"column:user_company"`
UserJob string `json:"user_job" gorm:"column:user_job"`
UserWebsite string `json:"user_website" gorm:"column:user_website"`
UserAddress string `json:"user_address" gorm:"column:user_address"`
UserName string `json:"user_name" gorm:"column:user_name"`
UserEmail string `json:"user_email" gorm:"column:user_email"`
UserPhone string `json:"user_phone" gorm:"column:user_phone"`
UserCompany string `json:"user_company" gorm:"column:user_company"`
UserJob string `json:"user_job" gorm:"column:user_job"`
UserWebsite string `json:"user_website" gorm:"column:user_website"`
UserAddress string `json:"user_address" gorm:"column:user_address"`
SocialMedia string `json:"social_media" gorm:"column:social_media"`
// Historial de mensajes almacenado como JSON
AnswersJSON string `json:"-" gorm:"column:answers_json;type:text"`
HTMLContent string `json:"html_content,omitempty" gorm:"column:html_content;type:text"`
+96
View File
@@ -866,6 +866,100 @@ func mongoStripComments(text string) string {
return strings.TrimSpace(strings.Join(lines, "\n"))
}
// Regexes para convertir sintaxis shell de Mongo a Extended JSON.
var (
reNewDateEmpty = regexp.MustCompile(`\bnew\s+Date\s*\(\s*\)`)
reNewDateStr = regexp.MustCompile(`\bnew\s+Date\s*\(\s*["']([^"']+)["']\s*\)`)
reNewDateMs = regexp.MustCompile(`\bnew\s+Date\s*\(\s*(\d+)\s*\)`)
reISODate = regexp.MustCompile(`\bISODate\s*\(\s*["']([^"']+)["']\s*\)`)
reISODateEmpty = regexp.MustCompile(`\bISODate\s*\(\s*\)`)
reObjectId = regexp.MustCompile(`\bObjectId\s*\(\s*["']([0-9a-fA-F]{24})["']\s*\)`)
reNumberInt = regexp.MustCompile(`\bNumberInt\s*\(\s*(\d+)\s*\)`)
reNumberLong = regexp.MustCompile(`\bNumberLong\s*\(\s*(\d+)\s*\)`)
reNumberDecimal = regexp.MustCompile(`\bNumberDecimal\s*\(\s*["']?([0-9.eE+\-]+)["']?\s*\)`)
reBinData = regexp.MustCompile(`\bBinData\s*\(\s*(\d+)\s*,\s*["']([^"']*)["']\s*\)`)
reTimestamp = regexp.MustCompile(`\bTimestamp\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)`)
reMinKey = regexp.MustCompile(`\bMinKey\b`)
reMaxKey = regexp.MustCompile(`\bMaxKey\b`)
reUndefined = regexp.MustCompile(`\bundefined\b`)
reUnquotedKeys = regexp.MustCompile(`([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)(\s*:)`)
)
// mongoShellToExtJSON convierte sintaxis shell de Mongo a Extended JSON strict
// para que bson.UnmarshalExtJSON pueda parsearlo correctamente.
func mongoShellToExtJSON(s string) string {
now := time.Now().UTC().Format(time.RFC3339)
// new Date() sin argumentos → fecha actual
s = reNewDateEmpty.ReplaceAllStringFunc(s, func(_ string) string {
return `{"$date":"` + now + `"}`
})
// new Date("iso-string")
s = reNewDateStr.ReplaceAllString(s, `{"$$date":"$1"}`)
// new Date(milliseconds)
s = reNewDateMs.ReplaceAllString(s, `{"$$date":{"$$numberLong":"$1"}}`)
// ISODate("iso-string")
s = reISODate.ReplaceAllString(s, `{"$$date":"$1"}`)
// ISODate() sin argumentos → fecha actual
s = reISODateEmpty.ReplaceAllStringFunc(s, func(_ string) string {
return `{"$date":"` + now + `"}`
})
// ObjectId("hex")
s = reObjectId.ReplaceAllString(s, `{"$$oid":"$1"}`)
// NumberInt(n)
s = reNumberInt.ReplaceAllString(s, `{"$$numberInt":"$1"}`)
// NumberLong(n)
s = reNumberLong.ReplaceAllString(s, `{"$$numberLong":"$1"}`)
// NumberDecimal(n)
s = reNumberDecimal.ReplaceAllString(s, `{"$$numberDecimal":"$1"}`)
// BinData(subtype, base64)
s = reBinData.ReplaceAllString(s, `{"$$binary":{"base64":"$2","subType":"$1"}}`)
// Timestamp(t, i)
s = reTimestamp.ReplaceAllString(s, `{"$$timestamp":{"t":$1,"i":$2}}`)
// MinKey / MaxKey
s = reMinKey.ReplaceAllString(s, `{"$$minKey":1}`)
s = reMaxKey.ReplaceAllString(s, `{"$$maxKey":1}`)
// undefined → null
s = reUndefined.ReplaceAllString(s, `null`)
// Comillas simples → dobles (solo en valores string, no dentro de ya-convertidos)
s = mongoSingleToDoubleQuotes(s)
// Claves sin comillas → claves con comillas dobles
s = reUnquotedKeys.ReplaceAllString(s, `$1"$2"$3`)
return s
}
// mongoSingleToDoubleQuotes convierte 'valor' → "valor" respetando escapes.
func mongoSingleToDoubleQuotes(s string) string {
var b strings.Builder
inDouble := false
inSingle := false
runes := []rune(s)
for i := 0; i < len(runes); i++ {
c := runes[i]
switch {
case c == '\\' && (inDouble || inSingle):
b.WriteRune(c)
i++
if i < len(runes) {
b.WriteRune(runes[i])
}
case c == '"' && !inSingle:
inDouble = !inDouble
b.WriteRune(c)
case c == '\'' && !inDouble:
inSingle = !inSingle
if inSingle {
b.WriteRune('"')
} else {
b.WriteRune('"')
}
default:
b.WriteRune(c)
}
}
return b.String()
}
func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult {
start := time.Now()
client, err := mongoConnect(conx)
@@ -891,6 +985,8 @@ func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult
func mongoRunQuery(ctx context.Context, db *mongo.Database, query string) (*QueryResult, error) {
// Eliminar comentarios y líneas vacías
query = mongoStripComments(query)
// Convertir sintaxis shell de Mongo (new Date(), ObjectId, etc.) a Extended JSON
query = mongoShellToExtJSON(query)
m := mongoQueryRe.FindStringSubmatch(query)
if m == nil {