Skip to content

Commit

Permalink
Update booking ID on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
zdevaty committed Apr 6, 2024
1 parent efadfc1 commit 794650d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func collectResources(config apiserver.Configuration) error {
a.ElionaID = booking.BookingID.Int32
changedBookings = append(changedBookings, a)
booking.ExchangeChangeKey = null.StringFrom(a.ExchangeChangeKey)
err := conf.UpdateBooking(booking)
err := conf.UpdateBookingExchangeChangeID(booking)
if err != nil {
log.Error("conf", "updating booking: %v", err)
return err
Expand Down
46 changes: 36 additions & 10 deletions booking/booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"context"
"encoding/json"
"errors"
"ews/appdb"
"ews/conf"
"ews/model"
"fmt"
"net/http"
"time"

"github.com/eliona-smart-building-assistant/go-utils/log"
"github.com/gorilla/websocket"
"github.com/volatiletech/null/v8"
)

type client struct {
Expand All @@ -25,18 +28,27 @@ func NewClient(baseURL string) *client {
}

func (c *client) Book(bookings []model.Booking) error {
fmt.Println(bookings)
var convertedBookings []bookingRequest
for _, b := range bookings {
convertedBookings = append(convertedBookings, bookingRequest{
convertedBooking := bookingRequest{
BookingID: b.ElionaID,
AssetIds: []int{int(b.AssetID)},
OrganizerID: b.OrganizerEmail,
Start: b.Start,
End: b.End,
})
}
responseBooking, err := c.book(convertedBooking)
if err != nil {
return err
}
if err := conf.UpsertBooking(appdb.Booking{
ExchangeID: null.StringFrom(b.ExchangeID),
ExchangeChangeKey: null.StringFrom(b.ExchangeChangeKey),
BookingID: null.Int32From(responseBooking.Id),
}); err != nil {
return fmt.Errorf("upserting booking id: %v", err)
}
}
return c.book(convertedBookings)
return nil
}

type bookingRequest struct {
Expand All @@ -47,23 +59,37 @@ type bookingRequest struct {
End time.Time `json:"end"`
}

func (c *client) book(bookings []bookingRequest) error {
type bookingResponse struct {
Id int32 `json:"id"`
AssetIds []int32 `json:"assetIds"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
OrganizerID string `json:"organizerID"`
OrganizerName string `json:"organizerName"`
}

func (c *client) book(bookings bookingRequest) (bookingResponse, error) {
body, err := json.Marshal(bookings)
if err != nil {
return err
return bookingResponse{}, err
}

resp, err := http.Post(c.BaseURL+"/sync/bookings", "application/json", bytes.NewBuffer(body))
if err != nil {
return err
return bookingResponse{}, err
}
defer resp.Body.Close()

if resp.StatusCode >= 300 {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
return bookingResponse{}, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

return nil
var respBody bookingResponse
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
return bookingResponse{}, fmt.Errorf("error parsing response body: %v", err)
}

return respBody, nil
}

type BookingsSubscriptionRequest struct {
Expand Down
11 changes: 10 additions & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,19 @@ func GetBookingByExchangeID(exchangeID string) (appdb.Booking, error) {
return *booking, nil
}

func UpdateBooking(booking appdb.Booking) error {
func UpdateBookingExchangeChangeID(booking appdb.Booking) error {
_, err := booking.UpdateG(
context.Background(),
boil.Whitelist(appdb.BookingColumns.ExchangeChangeKey),
)
return err
}

func UpsertBooking(booking appdb.Booking) error {
return booking.UpsertG(
context.Background(), true,
[]string{appdb.BookingColumns.ExchangeID},
boil.Whitelist(appdb.BookingColumns.ExchangeChangeKey),
boil.Infer(),
)
}

0 comments on commit 794650d

Please sign in to comment.