Skip to content

Commit

Permalink
Save link and payment status for an offer
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed May 24, 2024
1 parent 82d5160 commit 2579d8b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
- OFFER_VALIDATION_TIME
- BANK_ENDPOINT
- BANK_CALLBACK
- BANK_PAYMENT_ENDPOINT
- BANK_TOKEN
- GEODISTANCE_API
9 changes: 8 additions & 1 deletion internal/handlers/acmesky/tm_ask_payment_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ func TMAskPaymentLink(client worker.JobClient, job entities.Job) {
return
}

variables["payment_link"] = fmt.Sprintf("%s/%s/pay/", conf.String("bank.endpoint"), response.Id)
variables["payment_link"] = fmt.Sprintf("%s%s", conf.String("bank.payment.endpoint"), response.Id)
variables["flight_price"] = offer.Journey.Cost

offer.PaymentLink = variables["payment_link"].(string)
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
}

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
acmejob.FailJob(client, job)
Expand Down
18 changes: 18 additions & 0 deletions internal/handlers/acmesky/tm_journey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package handlers

import (
"context"

"github.com/charmbracelet/log"

"github.com/acme-sky/workers/internal/db"
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 @@ -19,6 +22,21 @@ func TMJourney(client worker.JobClient, job entities.Job) {
return
}

db, _ := db.GetDb()
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] Offer not found", job.Type, jobKey)
acmejob.FailJob(client, job)
return
}

offer.PaymentPaid = true
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
}

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
acmejob.FailJob(client, job)
Expand Down
18 changes: 18 additions & 0 deletions internal/handlers/acmesky/tm_journey_and_rent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package handlers

import (
"context"

"github.com/charmbracelet/log"

"github.com/acme-sky/workers/internal/db"
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 @@ -19,6 +22,21 @@ func TMJourneyAndRent(client worker.JobClient, job entities.Job) {
return
}

db, _ := db.GetDb()
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] Offer not found", job.Type, jobKey)
acmejob.FailJob(client, job)
return
}

offer.PaymentPaid = true
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
}

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
acmejob.FailJob(client, job)
Expand Down
18 changes: 18 additions & 0 deletions internal/handlers/acmesky/tm_journey_rent_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package handlers

import (
"context"

"github.com/charmbracelet/log"

"github.com/acme-sky/workers/internal/db"
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 @@ -19,6 +22,21 @@ func TMJourneyRentError(client worker.JobClient, job entities.Job) {
return
}

db, _ := db.GetDb()
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] Offer not found", job.Type, jobKey)
acmejob.FailJob(client, job)
return
}

offer.PaymentPaid = true
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
}

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
acmejob.FailJob(client, job)
Expand Down
38 changes: 21 additions & 17 deletions internal/models/offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import (

// Offer model
type Offer struct {
Id uint `gorm:"column:id" json:"id"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
Message string `gorm:"column:message" json:"message"`
Expired string `gorm:"column:expired" json:"expired"`
Token string `gorm:"column:token" json:"token"`
IsUsed bool `gorm:"column:is_used" json:"is_user"`
JourneyId int `json:"-"`
Journey Journey `gorm:"foreignKey:JourneyId" json:"journey"`
UserId int `json:"-"`
User User `gorm:"foreignKey:UserId" json:"user"`
Id uint `gorm:"column:id" json:"id"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
Message string `gorm:"column:message" json:"message"`
Expired string `gorm:"column:expired" json:"expired"`
Token string `gorm:"column:token" json:"token"`
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"`
JourneyId int `json:"-"`
Journey Journey `gorm:"foreignKey:JourneyId" json:"journey"`
UserId int `json:"-"`
User User `gorm:"foreignKey:UserId" json:"user"`
}

type OfferInputFields struct {
Expand Down Expand Up @@ -105,12 +107,14 @@ func NewOffer(in OfferInput) Offer {
)

return Offer{
CreatedAt: time.Now(),
Message: message,
Expired: strconv.FormatInt(time.Now().Add(time.Hour*time.Duration(offerValidationTime)).Unix(), 10),
Token: token,
IsUsed: false,
JourneyId: in.JourneyId,
UserId: in.UserId,
CreatedAt: time.Now(),
Message: message,
Expired: strconv.FormatInt(time.Now().Add(time.Hour*time.Duration(offerValidationTime)).Unix(), 10),
Token: token,
IsUsed: false,
PaymentLink: "",
PaymentPaid: false,
JourneyId: in.JourneyId,
UserId: in.UserId,
}
}

0 comments on commit 2579d8b

Please sign in to comment.