From c8ab60e07e1187a92b0338ef383aaff5899c2097 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 15 Jun 2024 09:11:56 +0200 Subject: [PATCH] add external_id to not duplicate bookings --- internal/booking/models.go | 1 + internal/booking/service.go | 6 ++++-- internal/server/handle_bookings.go | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/booking/models.go b/internal/booking/models.go index dcbbed2..3d03a06 100644 --- a/internal/booking/models.go +++ b/internal/booking/models.go @@ -18,6 +18,7 @@ type Booking struct { PhoneNumber string Email string Platform string + ExternalId string `gorm:"uniqueIndex:booking_external_id"` Items []Item Id int CustomerNumber int `gorm:"column:customers"` diff --git a/internal/booking/service.go b/internal/booking/service.go index 77f1eb1..d6bba68 100644 --- a/internal/booking/service.go +++ b/internal/booking/service.go @@ -37,7 +37,7 @@ func (bs Service) All() []*Line { } func (bs Service) Create(From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Platform string, - CustomerNumber int, PlatformFees float64, + CustomerNumber int, PlatformFees float64, externalId string, ) *Booking { b := &Booking{ Name: Name, @@ -48,6 +48,7 @@ func (bs Service) Create(From time.Time, To time.Time, Name string, PhoneNumber To: To, Platform: Platform, PlatformFees: PlatformFees, + ExternalId: externalId, } _ = bs.db.Create(b) return b @@ -244,11 +245,12 @@ func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { customerNumber := extractInt(`Nombre de personnes : \s*\n\s*(\d+)`, content) commissionAmount := extractFloat(`Commission : € (\d+,\d+)`, content) item := extractString(`Maison 1 Chambre \((T2|T3) -`, content) + externalId := extractString(`Numéro de réservation : \n\s+(\d+)`, content) standardRate := extractFloat(`Standard Rate\n\s+€ (\d+)`, content) taxQty := (totalAmount - standardRate*float64(stayLength)) / 1.5 - b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount) + b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount, externalId) bs.CreateItem(b.Id, item, stayLength, standardRate, "Card") bs.CreateItem(b.Id, "Taxes", int(taxQty), 1.5, "Cash") diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index d2a6360..2383688 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -72,7 +72,7 @@ func handleCreateBooking(bs *booking.Service) echo.HandlerFunc { ts, _ = myTime.ParseFromForm(c.FormValue("to")) nb.To = ts - b := bs.Create(nb.From, nb.To, nb.Name, nb.PhoneNumber, nb.Email, nb.Platform, nb.CustomerNumber, nb.PlatformFees) + b := bs.Create(nb.From, nb.To, nb.Name, nb.PhoneNumber, nb.Email, nb.Platform, nb.CustomerNumber, nb.PlatformFees, "") // TODO: add field in the form return c.Redirect(http.StatusSeeOther, fmt.Sprintf("%s/%d", constant.RouteBooking, b.Id)) } }