# BUY-2479 - Authenticated buyers are able to save favorites on the listing platform
- https://809.preview.lisa.staging.int.casavo.tech/it/case-in-vendita/milano/
- https://casavo.atlassian.net/browse/BUY-2479
- https://github.com/casavo/lisa/tree/buy-2479-save-favourites-authenticated-users
- https://github.com/casavo/auth-system/pull/18
- https://github.com/casavo/auth-system/pull/20
- https://github.com/casavo/spike-sso-kratos/blob/example-kratos-oathkeeper/services/express-simple/src/verifyJwt.ts
- https://ktor.io/docs/jwt.html#authenticate-route
## # TODO
### Auth System
- [x] complete PR to disable cors and allow OPTIONS gluglu proxy https://github.com/casavo/auth-system/pull/14
### Gluglu
- [x] implement usecases
- [x] check favorite
- [x] add favorite
- [x] remove favorite
- [x] get all favorite by user
- [x] rename /listing/{id}/star into /listing/{id}/favorite
- [x] remove route /listing/{id}/favorite, because no more used / needed
- [x] handle unexpected scenarios
- [x] unreachable DB
- [x] malformed UUID
- [x] listing id
- [x] user from jwt (?)
- [x] implement DatabaseFavoriteListingRepository and replace inmemory usage
- [x] remember to implement soft delete via "removed_at" timestamp
- [x] handle double add on same listing id for same user (do not duplicate)
- [x] e2e test
- [x] unauthorized
- [x] malformed listing uuid
- [x] add to favorite + check added favorites + delete added favorite
- [x] add already-added favorite -> ok response
- [x] delete a never-added favorite -> ok response
- [x] // FIXME this is a minimal validation for payload from kratos/oathkeeper
- [ ] set `AUTH_JWK_PROVIDER_DOMAIN` value
- [x] local
- [x] e2e
- [x] staging
- [ ] production (probably `oathk.casavo.com`)
- [x] update web/src/main/resources/api/gluglu-openapi.yaml with new routes
- [x] /listing/{id}/favorite route
- [x] /user/favorite-listings route
- [x] authentication infos in both routes
- [ ] QUALCUNO HA DETTO SILVER ZONE :question:
### Lisa
- [x] implement api-gluglu-private
- [x] check favorite
- [x] add favorite
- [x] remove favorite
- [x] get all favorite by user
- [x] remove check favorite (will be used only get all favorite by user to avoid make a request for every listing in the list - too many requests)
- [x] dettaglio listing
- [x] mostro cuore rosso se è già preferito
- [x] gestisco click sul cuore
- [x] se non sono loggato redirect su login
- [x] se loggato aggiungo o rimuovo preferito in base allo stato
- [x] gestione error message
- [x] card lista risultati
- [x] chiamata a `/user/favorite-listings` per ottimizzare chiamate
- [x] mostro cuore rosso se è già preferito
- [x] gestisco click sul cuore
- [x] gestione error message
- [x] se non sono loggato redirect su login
- [x] se loggato aggiungo o rimuovo preferito in base allo stato
- [x] lista dei miei preferiti
- [x] non leggo da local storage ma da gluglu dalla rotta `/user/favorite-listings`
- [x] per ogni listing recupero i dettagli da cate
- [x] se listing ancora esiste online mostro i dettagli dell'immobile
- [x] se non esiste più online mostro in grigio i dettagli (vedi screen sotto)
- [x] gestiamo il remove dei preferiti
- [x] gestione error message
- [x] `NEXT_PUBLIC_GLUGLU_PRIVATE_API_ENDPOINT` env var
- [x] locale (puntare oathkeeper locale che punta gluglu di staging ?)
- [x] staging
- [x] produzione
- [x] `NEXT_PUBLIC_KRATOS_ENDPOINT` env var
- [x] locale (puntare kratos locale)
- [x] staging
- [x] produzione
- [x] redirects
- [x] da non loggato, clicco un cuore da lista risultati, poi nella pagina di login NON proseguo con login ma clicco su "ricerca" in basso --> dovrei tornare sulla lista risultati
- [x] rimuovo il messaggio "Gli annunci non più disponibili saranno rimossi in automatico dopo 20 giorni"
- [ ] cancello da lokalise la chiave Favourites_FavouriteList_AutodeleteMessage
- [x] cypress tests
- [x] fixare / cancellare l'attuale favourites cypress test
- [x] scenario da non loggato
- [x] cerco di aggiungere preferito dalla lista risultati, sono rediretto su login
- [x] cerco di aggiungere preferito dal dettaglio di un listing, sono rediretto su login
- [x] scenario da loggato
- [x] aggiungo qualche preferito da lista risultati
- [x] apro dettaglio di qualche listing, controllo stato icona preferito, aggiungo e/o rimuovo qualche preferito
- [x] rivedo la lista dei miei preferiti, ne rimuovo qualcuno
- [x] controllo che l'utente con i preferiti salvati nel local storage salvi i preferiti su backend
- [x] extra: quando faccio login, se trovo cookie dei preferiti vecchi
- [x] ciclo i preferiti e faccio chiamata a gluglu per salvarli su backend
- [x] elimino il cookie
- [x] ~~pensiamo a una soluzione alternativa al context per la gestione dei favorites? (react query..)~~ -> è gia ottimizzato il re-render


## API Design
più snella senza storico rimossi (attenzione, cambia esperienza card)
```
/listing/c39fc6e9-8d0c-4af7-b139-953d8ad0f262/favorite DELETE -> remove/undo favorite
/listing/c39fc6e9-8d0c-4af7-b139-953d8ad0f262/favorite POST -> add favorite
# ma ci serve ??? forse non più
# /listing/c39fc6e9-8d0c-4af7-b139-953d8ad0f262/favorite GET -> check (200 favorite, 404 not-favorite)
/user/favorite-listings GET -> dammi tutte le starred
[
{
"listing_id": "c39fc6e9-8d0c-4af7-b139-953d8ad0f262",
"added_at": "2022-11-14T10:36:41.358Z"
},
{
"listing_id": "bb097bd1-4608-4a5b-8e7c-5c5c39516d51",
"added_at": "2022-11-14T10:36:41.358Z"
},
{
"listing_id": "a70fc6e9-8d0c-4af7-b139-953d8ad0f262",
"added_at": "2022-11-14T10:36:41.358Z"
},
]
```
-----
più ricca, con storico rimossi
```
/listing/c39fc6e9-8d0c-4af7-b139-953d8ad0f262/star GET -> check (200 starred, 404 not-starred or not-found)
/listing/c39fc6e9-8d0c-4af7-b139-953d8ad0f262/star DELETE -> rimuovi
/listing/c39fc6e9-8d0c-4af7-b139-953d8ad0f262/star POST -> aggiungi -> vanno passate nel payload alcune info del listing in caso di rimozione
city : "it/milano"
commercialArea : 109
price : {cents: 35000000, currency: "eur"}
title : "Casa di 109m² in Via Ettore Ponti 2"
/user/starred GET -> dammi tutte le starred -> gluglu prende i dati updated da cate e fa fallback sui salvati solo se non trova
[
{
"id": "c39fc6e9-8d0c-4af7-b139-953d8ad0f262",
"city : "it/milano"
"commercialArea : 109
"price : {cents: 35000000, currency: "eur"}
"title : "Casa di 109m² in Via Ettore Ponti 2"
"image : "https://via.placeholder.com/100"
"ownedByCasavo : false
"online": true
},
{
"id": "bb097bd1-4608-4a5b-8e7c-5c5c39516d51",
"city : "it/milano"
"commercialArea : 109
"price : {cents: 35000000, currency: "eur"}
"title : "Casa di 109m² in Via Ettore Ponti 2"
"image : "https://via.placeholder.com/100"
"ownedByCasavo : false
"online": true
},
{
"id": "a70fc6e9-8d0c-4af7-b139-953d8ad0f262",
"city : "it/milano"
"commercialArea : 109
"price : {cents: 35000000, currency: "eur"}
"title : "Casa di 109m² in Via Ettore Ponti 2"
~"image : "https://via.placeholder.com/100"~
~"ownedByCasavo : false~
"online": false
},
]
```