diff --git a/apps/backend/miogest_handler.go b/apps/backend/miogest_handler.go index 818bde0..c5072b2 100644 --- a/apps/backend/miogest_handler.go +++ b/apps/backend/miogest_handler.go @@ -356,15 +356,58 @@ func extractData_update(annuncio *typesdefs.AnnuncioXML, updateImages bool, upda return tmp } +func flatClienteTipologia(t []string) string { + if len(t) == 0 { + return "" + } + if slices.Contains(t, "Proprietario") { + return "proprietario" + } else if slices.Contains(t, "Delegato") { + return "delegato" + } else if slices.Contains(t, "Cliente") { + return "inquilino" + } + return "" +} +func tipologiaPriority(tipologie []string) int { + if slices.Contains(tipologie, "Delegato") { + return 3 + } + if slices.Contains(tipologie, "Cliente") { + return 2 + } + if slices.Contains(tipologie, "Proprietario") { + return 1 + } + return 0 +} +func selectTargetCliente(annuncio *typesdefs.AnnuncioXML) (int, bool) { + if len(annuncio.Clienti) == 0 { + return 0, false + } + best := 0 + bestPriority := tipologiaPriority(annuncio.Clienti[0].Tipologie) + for i := 1; i < len(annuncio.Clienti); i++ { + if p := tipologiaPriority(annuncio.Clienti[i].Tipologie); p > bestPriority { + bestPriority = p + best = i + } + } + return best, true +} + func processLocatore(tmp *typesdefs.AnnuncioParsed, annuncio *typesdefs.AnnuncioXML) { - if annuncio.Clienti == nil || annuncio.Clienti.Cliente == nil { + idx, ok := selectTargetCliente(annuncio) + if !ok { + log.Printf("Annuncio %s has no client information", utils.GetStringValue(annuncio.Codice)) return } + trg := annuncio.Clienti[idx] - tmp.Email = parser.ParseEmail(annuncio.Clienti.Cliente.Email1) + tmp.Email = parser.ParseEmail(trg.Email1) var number string - for _, tel := range []*string{annuncio.Clienti.Cliente.Tel1, annuncio.Clienti.Cliente.Tel2, annuncio.Clienti.Cliente.Tel3} { + for _, tel := range []*string{trg.Tel1, trg.Tel2, trg.Tel3} { if tel != nil { cleanedTel := re.ReplaceAllString(*tel, "") if cleanedTel != "" { @@ -374,10 +417,11 @@ func processLocatore(tmp *typesdefs.AnnuncioParsed, annuncio *typesdefs.Annuncio } } tmp.Numero = &number - if annuncio.Clienti.Cliente.Cognome != nil && annuncio.Clienti.Cliente.Nome != nil { - tmp.Locatore = strUpd(fmt.Sprintf("%s %s", utils.MakeUppercase(*annuncio.Clienti.Cliente.Cognome), utils.MakeUppercase(*annuncio.Clienti.Cliente.Nome))) + if trg.Cognome != nil && trg.Nome != nil { + tmp.Locatore = strUpd(fmt.Sprintf("%s %s", utils.MakeUppercase(*trg.Cognome), utils.MakeUppercase(*trg.Nome))) } - tmp.Idlocatore = annuncio.Clienti.Cliente.Id + tmp.Idlocatore = trg.Id + tmp.Tipo_locatore = strUpd(flatClienteTipologia(trg.Tipologie)) } func processIndirizzo(tmp *typesdefs.AnnuncioParsed, annuncio *typesdefs.AnnuncioXML) { diff --git a/apps/backend/queries/annunci.go b/apps/backend/queries/annunci.go index 1ccdd4e..c1a1468 100644 --- a/apps/backend/queries/annunci.go +++ b/apps/backend/queries/annunci.go @@ -88,7 +88,8 @@ func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool) e disponibile_da = $47, permanenza = $48, persone = $49, - media_updated_at = NOW() + media_updated_at = NOW(), + tipo_locatore = $50 WHERE codice = $46 `, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore, annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il, @@ -101,7 +102,7 @@ func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool) e annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto, annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en, annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, - annuncio.External_videos, annuncio.Codice, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone) + annuncio.External_videos, annuncio.Codice, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone, annuncio.Tipo_locatore) } else { batch.Queue(` @@ -155,9 +156,10 @@ func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool) e disponibile_da, permanenza, persone, - media_updated_at + media_updated_at, + tipo_locatore ) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, NOW()) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, NOW(), $50) `, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore, annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il, annuncio.Indirizzo, annuncio.Civico, annuncio.Comune, annuncio.Cap, @@ -169,7 +171,7 @@ func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool) e annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto, annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en, annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, - annuncio.External_videos, annuncio.Codice, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone) + annuncio.External_videos, annuncio.Codice, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone, annuncio.Tipo_locatore) } } err = Db.Dbpool.SendBatch(Db.Ctx, batch).Close() diff --git a/apps/backend/typesdefs/models.go b/apps/backend/typesdefs/models.go index b6529be..912e13e 100644 --- a/apps/backend/typesdefs/models.go +++ b/apps/backend/typesdefs/models.go @@ -110,17 +110,16 @@ type AnnuncioXML struct { DescrizioneEn *string `xml:"Descrizione_En"` Foto *[]string `xml:"Foto"` Consegna *string `xml:"Consegna"` - Clienti *struct { - Cliente *struct { - Id *string `xml:"Id"` - Cognome *string `xml:"Cognome"` - Nome *string `xml:"Nome"` - Tel1 *string `xml:"Tel1"` - Tel2 *string `xml:"Tel2"` - Tel3 *string `xml:"Tel3"` - Email1 *string `xml:"Email1"` - } `xml:"Cliente"` - } `xml:"Clienti"` + Clienti []struct { + Id *string `xml:"Id"` + Tipologie []string `xml:"Tipologie>Tipologia"` + Cognome *string `xml:"Cognome"` + Nome *string `xml:"Nome"` + Tel1 *string `xml:"Tel1"` + Tel2 *string `xml:"Tel2"` + Tel3 *string `xml:"Tel3"` + Email1 *string `xml:"Email1"` + } `xml:"Clienti>Cliente"` Cap *string `xml:"Cap"` Video *[]string `xml:"Video"` AMMedias *struct { @@ -170,6 +169,7 @@ type AnnuncioParsed struct { Regione *string Caratteristiche *map[string]interface{} Locatore *string + Tipo_locatore *string Numero *string Idlocatore *string Stato *string @@ -190,6 +190,7 @@ type AnnunciDB struct { Id int Codice string Locatore *string + Tipo_locatore *string Numero *string Idlocatore *string Indirizzo *string diff --git a/apps/db/migrations/38_tipo_locatore_usernote.up.sql b/apps/db/migrations/38_tipo_locatore_usernote.up.sql new file mode 100644 index 0000000..5fed1db --- /dev/null +++ b/apps/db/migrations/38_tipo_locatore_usernote.up.sql @@ -0,0 +1,5 @@ +ALTER TABLE IF EXISTS public.annunci +ADD COLUMN IF NOT EXISTS tipo_locatore TEXT; + +ALTER TABLE IF EXISTS public.users +ADD COLUMN IF NOT EXISTS note TEXT; \ No newline at end of file diff --git a/apps/infoalloggi/src/schemas/public/Annunci.ts b/apps/infoalloggi/src/schemas/public/Annunci.ts index 3172dc1..38b9105 100644 --- a/apps/infoalloggi/src/schemas/public/Annunci.ts +++ b/apps/infoalloggi/src/schemas/public/Annunci.ts @@ -110,6 +110,8 @@ export default interface AnnunciTable { persone: ColumnType; media_updated_at: ColumnType; + + tipo_locatore: ColumnType; } export type Annunci = Selectable;