diff --git a/README.md b/README.md index ba08880..0902118 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,6 @@ - POSTGRES_DB - POSTGRES_USER - POSTGRES_PASSWORD -- AIRLINES -- AIRLINE_LOGIN_USERNAME -- AIRLINE_LOGIN_PASSWORD - OFFER_VALIDATION_TIME - BANK_ENDPOINT - BANK_CALLBACK diff --git a/docker-compose.yml b/docker-compose.yml index 4d91a1f..68580eb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -180,9 +180,6 @@ services: - RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672/ - SENTRY_DSN=${SENTRY_DSN} - DATABASE_DSN=${DATABASE_DSN} - - AIRLINES=${AIRLINES} - - AIRLINE_LOGIN_USERNAME=${AIRLINE_LOGIN_USERNAME} - - AIRLINE_LOGIN_PASSWORD=${AIRLINE_LOGIN_PASSWORD} - OFFER_VALIDATION_TIME=${OFFER_VALIDATION_TIME} - BANK_ENDPOINT=${BANK_ENDPOINT} - BANK_PAYMENT_ENDPOINT=${BANK_PAYMENT_ENDPOINT} diff --git a/internal/db/db.go b/internal/db/db.go index 57a62ea..01c01b0 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -25,6 +25,7 @@ func InitDb(dsn string) (*gorm.DB, error) { &models.Journey{}, &models.Offer{}, &models.Rent{}, + &models.Airline{}, &models.User{}, ) } diff --git a/internal/handlers/acmesky/st_get_user_interests.go b/internal/handlers/acmesky/st_get_user_interests.go index fcf452b..6ef0c87 100644 --- a/internal/handlers/acmesky/st_get_user_interests.go +++ b/internal/handlers/acmesky/st_get_user_interests.go @@ -2,11 +2,9 @@ package handlers import ( "context" - "strings" "github.com/charmbracelet/log" - "github.com/acme-sky/workers/internal/config" "github.com/acme-sky/workers/internal/db" acmejob "github.com/acme-sky/workers/internal/job" "github.com/acme-sky/workers/internal/models" @@ -38,15 +36,11 @@ func STGetUserInterests(client worker.JobClient, job entities.Job) { variables["interests"] = interests - conf, err := config.GetConfig() - - if err != nil { - log.Warnf("[%s] [%d] Error loading the config: %s", job.Type, jobKey, err.Error()) - acmejob.FailJob(client, job) - return + var airlines models.Airline + if err := db.Find(&airlines).Error; err != nil { + panic("can't find airlines") } - - variables["airlines"] = strings.Split(conf.String("airlines"), ",") + variables["airlines"] = airlines request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables) if err != nil { diff --git a/internal/handlers/acmesky/tm_book_journey.go b/internal/handlers/acmesky/tm_book_journey.go index 8fdb2ea..315ef74 100644 --- a/internal/handlers/acmesky/tm_book_journey.go +++ b/internal/handlers/acmesky/tm_book_journey.go @@ -6,7 +6,6 @@ import ( "github.com/charmbracelet/log" - "github.com/acme-sky/workers/internal/config" "github.com/acme-sky/workers/internal/db" "github.com/acme-sky/workers/internal/http" acmejob "github.com/acme-sky/workers/internal/job" @@ -43,7 +42,14 @@ func TMBookJourney(client worker.JobClient, job entities.Job) { // This flight ID refers to the airline flight ID var flight2_id int = 0 - endpoint := fmt.Sprintf("%s/flights/filter/", flight1.Airline) + var flight1Airline models.Airline + if err := db.Where("name = ?", flight1.Airline).First(&flight1Airline).Error; err != nil { + log.Errorf("[%s] [%d] Airline not found", job.Type, jobKey) + acmejob.FailJob(client, job) + return + } + + endpoint := fmt.Sprintf("%s/flights/filter/", flight1Airline.Endpoint) payload := map[string]interface{}{ "code": flight1.Code, "departure_airport": flight1.DepartureAirport, @@ -55,7 +61,7 @@ func TMBookJourney(client worker.JobClient, job entities.Job) { response, err := http.MakeRequest(endpoint, payload) if err != nil { - log.Errorf("[%s] [%d] Error for airline `%s`: %s", job.Type, jobKey, flight1.Airline, err.Error()) + log.Errorf("[%s] [%d] Error for airline `%s`: %s", job.Type, jobKey, flight1Airline.Endpoint, err.Error()) acmejob.FailJob(client, job) return } else { @@ -75,7 +81,13 @@ func TMBookJourney(client worker.JobClient, job entities.Job) { } if flight2 != nil { - endpoint := fmt.Sprintf("%s/flights/filter/", flight2.Airline) + var flight2Airline models.Airline + if err := db.Where("name = ?", flight2.Airline).First(&flight2Airline).Error; err != nil { + log.Errorf("[%s] [%d] Airline not found", job.Type, jobKey) + acmejob.FailJob(client, job) + return + } + endpoint := fmt.Sprintf("%s/flights/filter/", flight2Airline.Endpoint) payload := map[string]interface{}{ "code": flight2.Code, "departure_airport": flight2.DepartureAirport, @@ -87,7 +99,7 @@ func TMBookJourney(client worker.JobClient, job entities.Job) { response, err := http.MakeRequest(endpoint, payload) if err != nil { - log.Errorf("[%s] [%d] Error for airline `%s`: %s", job.Type, jobKey, flight2.Airline, err.Error()) + log.Errorf("[%s] [%d] Error for airline `%s`: %s", job.Type, jobKey, flight2Airline.Endpoint, err.Error()) acmejob.FailJob(client, job) return } else { @@ -107,12 +119,10 @@ func TMBookJourney(client worker.JobClient, job entities.Job) { } } - conf, _ := config.GetConfig() - - endpoint = fmt.Sprintf("%s/login/", flight1.Airline) + endpoint = fmt.Sprintf("%s/login/", flight1Airline.Endpoint) payload = map[string]interface{}{ - "username": conf.String("airline.login.username"), - "password": conf.String("airline.login.password"), + "username": flight1Airline.LoginUsername, + "password": flight1Airline.LoginPassword, } token, err := http.MakeLogin(endpoint, payload) @@ -122,7 +132,7 @@ func TMBookJourney(client worker.JobClient, job entities.Job) { return } - endpoint = fmt.Sprintf("%s/journeys/", flight1.Airline) + endpoint = fmt.Sprintf("%s/journeys/", flight1Airline.Endpoint) payload = map[string]interface{}{ "departure_flight_id": flight1_id, "cost": offer.Journey.Cost, diff --git a/internal/handlers/acmesky/tm_compute_distance_user_airport.go b/internal/handlers/acmesky/tm_compute_distance_user_airport.go index 38b071e..58f2928 100644 --- a/internal/handlers/acmesky/tm_compute_distance_user_airport.go +++ b/internal/handlers/acmesky/tm_compute_distance_user_airport.go @@ -66,7 +66,13 @@ func TMComputeDistanceUserAirport(client worker.JobClient, job entities.Job) { return } - endpoint := fmt.Sprintf("%s/airports/code/%s/", offer.Journey.Flight1.Airline, offer.Journey.Flight1.DepartureAirport) + var flight1Airline models.Airline + if err := db.Where("name = ?", offer.Journey.Flight1.Airline).First(&flight1Airline).Error; err != nil { + log.Errorf("[%s] [%d] Airline not found", job.Type, jobKey) + acmejob.FailJob(client, job) + return + } + endpoint := fmt.Sprintf("%s/airports/code/%s/", flight1Airline.Endpoint, offer.Journey.Flight1.DepartureAirport) airport, err := http.GetAirportInfo(endpoint) if err != nil { log.Errorf("[%s] [%d] Can't find info for departure airport: %s", job.Type, jobKey, err.Error()) diff --git a/internal/handlers/acmesky/tm_search_flight_on_airline.go b/internal/handlers/acmesky/tm_search_flight_on_airline.go index 59a4c6d..041e416 100644 --- a/internal/handlers/acmesky/tm_search_flight_on_airline.go +++ b/internal/handlers/acmesky/tm_search_flight_on_airline.go @@ -8,6 +8,7 @@ import ( "github.com/acme-sky/workers/internal/http" acmejob "github.com/acme-sky/workers/internal/job" + "github.com/acme-sky/workers/internal/models" "github.com/camunda/zeebe/clients/go/v8/pkg/entities" "github.com/camunda/zeebe/clients/go/v8/pkg/worker" ) @@ -29,24 +30,24 @@ func TMSearchFlightsOnAirline(client worker.JobClient, job entities.Job) { airlines := variables["airlines"].([]interface{}) index := int(variables["loopCounter"].(float64)) - 1 - airline := airlines[index] + airline := airlines[index].(models.Airline) interests := variables["interests"].([]interface{}) if len(interests) == 0 { - log.Warnf("Error for airline `%s`: there is no interest", airline) + log.Warnf("Error for airline `%s`: there is no interest", airline.Name) acmejob.FailJob(client, job) return } if index < 0 || index >= len(interests) { - log.Errorf("Error for airline `%s`: index out of range %d", airline, index) + log.Errorf("Error for airline `%s`: index out of range %d", airline.Name, index) acmejob.FailJob(client, job) return } flights := []map[string]interface{}{} - endpoint := fmt.Sprintf("%s/flights/filter/", airline) + endpoint := fmt.Sprintf("%s/flights/filter/", airline.Endpoint) for i := 0; i < len(interests); i++ { interest := interests[i].(map[string]interface{}) interestId := int(interest["id"].(float64)) @@ -62,12 +63,12 @@ func TMSearchFlightsOnAirline(client worker.JobClient, job entities.Job) { response, err := http.MakeRequest(endpoint, payload) if err != nil { - log.Errorf("Error for airline `%s`: %s", airline, err.Error()) + log.Errorf("Error for airline `%s`: %s", airline.Name, err.Error()) } else { if response.Count > 0 { for _, data := range response.Data { data["user_id"] = user["ID"] - data["airline"] = airline + data["airline"] = airline.Name data["interest_id"] = interestId flights = append(flights, data) } @@ -88,14 +89,14 @@ func TMSearchFlightsOnAirline(client worker.JobClient, job entities.Job) { response, err = http.MakeRequest(endpoint, payload) if err != nil { - log.Errorf("Error for airline `%s`: %s", airline, err.Error()) + log.Errorf("Error for airline `%s`: %s", airline.Name, err.Error()) continue } if response.Count > 0 { for _, data := range response.Data { data["user_id"] = user["ID"] - data["airline"] = airline + data["airline"] = airline.Name data["interest_id"] = interestId flights = append(flights, data) } diff --git a/internal/job/job.go b/internal/job/job.go index d323322..30c8150 100644 --- a/internal/job/job.go +++ b/internal/job/job.go @@ -2,10 +2,11 @@ package job import ( "context" - "strings" "sync" "github.com/acme-sky/workers/internal/config" + "github.com/acme-sky/workers/internal/db" + "github.com/acme-sky/workers/internal/models" "github.com/charmbracelet/log" "github.com/camunda/zeebe/clients/go/v8/pkg/commands" @@ -179,9 +180,14 @@ func CreateClient(pid string) *zbc.Client { log.Infof(response.String()) + db, _ := db.GetDb() + var airlines models.Airline + if err := db.Find(&airlines).Error; err != nil { + panic("can't find airlines") + } // Airlines must be loaded for the first time as variables 'cause the timer // trigger executed every hour. - variables := map[string]interface{}{"airlines": strings.Split(conf.String("airlines"), ",")} + variables := map[string]interface{}{"airlines": airlines} var instance commands.CreateInstanceCommandStep3 if instance, err = client.NewCreateInstanceCommand().BPMNProcessId(ProcessId).LatestVersion().VariablesFromMap(variables); err != nil { diff --git a/internal/models/airline.go b/internal/models/airline.go new file mode 100644 index 0000000..d68a203 --- /dev/null +++ b/internal/models/airline.go @@ -0,0 +1,15 @@ +package models + +import ( + "time" +) + +// Airline model +type Airline struct { + Id uint `gorm:"column:id" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + Name string `gorm:"column:name" json:"name"` + LoginUsername string `gorm:"column:login_username" json:"login_username"` + LoginPassword string `gorm:"column:login_password" json:"login_password"` + Endpoint string `gorm:"column:endpoint" json:"endpoint"` +} diff --git a/internal/models/rent.go b/internal/models/rent.go index 103cf6c..10e5987 100644 --- a/internal/models/rent.go +++ b/internal/models/rent.go @@ -8,8 +8,8 @@ import ( type Rent struct { Id uint `gorm:"column:id" json:"id"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - Name string `gorm:"name" json:"name"` - Latitude float32 `gorm:"latitude" json:"latitude"` - Longitude float32 `gorm:"longitude" json:"longitude"` - Endpoint string `gorm:"endpoint" json:"endpoint"` + Name string `gorm:"column:name" json:"name"` + Latitude float32 `gorm:"column:latitude" json:"latitude"` + Longitude float32 `gorm:"column:longitude" json:"longitude"` + Endpoint string `gorm:"column:endpoint" json:"endpoint"` }