Skip to content

Commit

Permalink
Save rent for an offer
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Jun 13, 2024
1 parent 3744cc9 commit 96a29ea
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 358 deletions.
685 changes: 351 additions & 334 deletions bpmn/acmesky.bpmn

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"sort"
"time"

"github.com/charmbracelet/log"
Expand All @@ -17,8 +18,9 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

// Task used to find the nearest rent company to the user' address
func TMFindNearestAvailableRentCompany(client worker.JobClient, job entities.Job) {
// Service task used to sort all rent services with key the distance between
// user and rent geolocalizations.
func STSortRentServices(client worker.JobClient, job entities.Job) {
jobKey := job.GetKey()

variables, err := job.GetVariablesAsMap()
Expand Down Expand Up @@ -70,37 +72,38 @@ func TMFindNearestAvailableRentCompany(client worker.JobClient, job entities.Job
acmejob.FailJob(client, job)
return
}
var distances []int

type RentDistance struct {
Id uint
Distance int
}
var distances []RentDistance

for _, rent := range rents {
distance, err := c.FindDistance(ctx, &pb.DistanceRequest{
Origin: &pb.MapPosition{Latitude: userGeometry.Latitude, Longitude: userGeometry.Longitude},
Destination: &pb.MapPosition{Latitude: rent.Latitude, Longitude: rent.Longitude},
})
if err != nil {
log.Warn("[%s] [%d] Can't find distance for %s: %s", job.Type, jobKey, rent.Name, err.Error())
distances = append(distances, 9999999)
log.Warnf("[%s] [%d] Can't find distance for %s: %s", job.Type, jobKey, rent.Name, err.Error())
distances = append(distances, RentDistance{Id: rent.Id, Distance: 999999})
continue
}
distances = append(distances, int(distance.GetDistance()))
distances = append(distances, RentDistance{Id: rent.Id, Distance: int(distance.GetDistance())})
}

if len(distances) == 0 {
if len(rents) == 0 {
log.Errorf("[%s] [%d] There is no available rent company: %s", job.Type, jobKey, err.Error())
acmejob.FailJob(client, job)
return
}

var selectRentIndex = 0
var minRentDistance = distances[0]
for i := 1; i < len(distances); {
if distances[i] < minRentDistance {
minRentDistance = distances[i]
selectRentIndex = i
}
}
sort.Slice(distances, func(i, j int) bool {
return distances[i].Distance > distances[j].Distance
})

variables["rent_company"] = rents[selectRentIndex]
variables["rent_companies"] = distances
variables["rent_status"] = "No"

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
Expand Down
39 changes: 35 additions & 4 deletions internal/handlers/acmesky/tm_ask_for_rent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/charmbracelet/log"

"github.com/acme-sky/workers/internal/db"
"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"
Expand All @@ -25,20 +26,50 @@ func TMAskForRent(client worker.JobClient, job entities.Job) {
db, _ := db.GetDb()
var rent models.Rent

if variables["rent_company"] == nil {
rentCompanies := variables["rent_companies"].([]interface{})
index := int(variables["loopCounter"].(float64)) - 1

if len(rentCompanies) == 0 {
log.Infof("[%s] [%d] You must define a rent_company object", job.Type, jobKey)
acmejob.FailJob(client, job)
return
}
rentCompany := rentCompanies[index].(map[string]interface{})
rentCompanyId := int(rentCompany["Id"].(float64))

rentCompany := variables["rent_company"].(map[string]interface{})
rentCompanyId := int(rentCompany["id"].(float64))
if err := db.Where("id = ?", rentCompanyId).First(&rent).Error; err != nil {
log.Errorf("[%s] [%d] Rent not found %s", job.Type, jobKey, err.Error())
acmejob.FailJob(client, job)
return
}
variables["next_rent_company_to_check"] = 42

var offer models.Offer
if err := db.Where("id = ?", variables["offer_id"]).Preload("Journey").Preload("Journey.Flight1").Preload("Journey.Flight2").Preload("User").First(&offer).Error; err != nil {
log.Errorf("[%s] [%d] Journey not found", job.Type, jobKey)
acmejob.FailJob(client, job)
return
}

response, err := http.MakeRentRequest(rent, offer)

if err != nil {
log.Errorf("[%s] [%d] Error for rent `%s`: %s", job.Type, jobKey, rent.Name, err.Error())
acmejob.FailJob(client, job)
return
} else {
if response.Status == "OK" {
variables["rent_status"] = "Ok"
offer.RentId = response.RentId
if err := db.Save(&offer).Error; err != nil {
log.Errorf("[%s] [%d] Error on saving offer %s", job.Type, jobKey, err.Error())
acmejob.FailJob(client, job)
return
}
log.Infof("[%s] [%d] Rent `%s` is OK with ID `%s`", job.Type, jobKey, rent.Name, response.RentId)
} else {
log.Errorf("[%s] [%d] Rent `%s` is not OK", job.Type, jobKey, rent.Name)
}
}

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions internal/handlers/acmesky/tm_search_flight_on_airline.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func TMSearchFlightsOnAirline(client worker.JobClient, job entities.Job) {
airline := airlines[index]

interests := variables["interests"].([]interface{})

if len(interests) == 0 {
log.Warnf("Error for airline `%s`: there is no interest", airline)
acmejob.FailJob(client, job)
return
}

if index < 0 || index >= len(interests) {
log.Errorf("Error for airline `%s`: index out of range %d", airline, index)
acmejob.FailJob(client, job)
Expand Down
7 changes: 4 additions & 3 deletions internal/http/soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func MakeRentRequest(rent models.Rent, offer models.Offer) (*BookRentResponse, e

params := gosoap.Params{
"PickupAddress": *offer.User.Address,
"Address": offer.Journey.Flight1.DepartureAirport,
"CustomerName": offer.User.Name,
"PickupDate": offer.Journey.Flight1.DepartureTime.Add(-2 * time.Hour),
// FIXME: add "address" field to airport with string
"Address": offer.Journey.Flight1.DepartureAirport,
"CustomerName": offer.User.Name,
"PickupDate": offer.Journey.Flight1.DepartureTime.Add(-2 * time.Hour).Format("02/01/2006 15:04"),
}

res, err := soap.Call("BookRent", params)
Expand Down
2 changes: 2 additions & 0 deletions internal/models/offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Offer struct {
IsUsed bool `gorm:"column:is_used" json:"is_used"`
PaymentLink string `gorm:"column:payment_link" json:"payment_link"`
PaymentPaid bool `gorm:"column:payment_paid" json:"payment_paid"`
RentId string `gorm:"column:rent_id" json:"rent_id"`
JourneyId int `json:"-"`
Journey Journey `gorm:"foreignKey:JourneyId" json:"journey"`
UserId int `json:"-"`
Expand Down Expand Up @@ -114,6 +115,7 @@ func NewOffer(in OfferInput) Offer {
IsUsed: false,
PaymentLink: "",
PaymentPaid: false,
RentId: "",
JourneyId: in.JourneyId,
UserId: in.UserId,
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func main() {
{Name: "TM_Error_On_Book_Journey", Handler: acmeskyHandlers.TMErrorOnBookJourney, Message: &acmejob.MessageCommand{Name: "CM_Received_Bank_Error", CorrelationKey: "0"}},
{Name: "TM_Journey", Handler: acmeskyHandlers.TMJourney, Message: &acmejob.MessageCommand{Name: "CM_Journey", CorrelationKey: "0"}},
{Name: "TM_Compute_Distance_User_Airport", Handler: acmeskyHandlers.TMComputeDistanceUserAirport},
{Name: "TM_Find_Nearest_Available_Rent_Company", Handler: acmeskyHandlers.TMFindNearestAvailableRentCompany},
{Name: "ST_Sort_Rent_Services", Handler: acmeskyHandlers.STSortRentServices},
{Name: "TM_Ask_For_Rent", Handler: acmeskyHandlers.TMAskForRent},
{Name: "TM_Journey_And_Rent", Handler: acmeskyHandlers.TMJourneyAndRent, Message: &acmejob.MessageCommand{Name: "CM_Journey_And_Rent", CorrelationKey: "0"}},
{Name: "TM_Journey_Rent_Error", Handler: acmeskyHandlers.TMJourneyRentError, Message: &acmejob.MessageCommand{Name: "CM_Journey", CorrelationKey: "0"}},
Expand Down

0 comments on commit 96a29ea

Please sign in to comment.