Skip to content

Commit

Permalink
Airlines as database table
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
boozec committed Jul 4, 2024
1 parent 369559b commit c6e726d
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 42 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- AIRLINES
- AIRLINE_LOGIN_USERNAME
- AIRLINE_LOGIN_PASSWORD
- OFFER_VALIDATION_TIME
- BANK_ENDPOINT
- BANK_CALLBACK
Expand Down
3 changes: 0 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func InitDb(dsn string) (*gorm.DB, error) {
&models.Journey{},
&models.Offer{},
&models.Rent{},
&models.Airline{},
&models.User{},
)
}
Expand Down
14 changes: 4 additions & 10 deletions internal/handlers/acmesky/st_get_user_interests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 21 additions & 11 deletions internal/handlers/acmesky/tm_book_journey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
17 changes: 9 additions & 8 deletions internal/handlers/acmesky/tm_search_flight_on_airline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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))
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions internal/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions internal/models/airline.go
Original file line number Diff line number Diff line change
@@ -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"`
}
8 changes: 4 additions & 4 deletions internal/models/rent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit c6e726d

Please sign in to comment.