add external_id to not duplicate bookings

This commit is contained in:
Ruidy 2024-06-15 09:11:56 +02:00
parent ba22fa0ffc
commit c8ab60e07e
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 6 additions and 3 deletions

View file

@ -18,6 +18,7 @@ type Booking struct {
PhoneNumber string PhoneNumber string
Email string Email string
Platform string Platform string
ExternalId string `gorm:"uniqueIndex:booking_external_id"`
Items []Item Items []Item
Id int Id int
CustomerNumber int `gorm:"column:customers"` CustomerNumber int `gorm:"column:customers"`

View file

@ -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, 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 { ) *Booking {
b := &Booking{ b := &Booking{
Name: Name, Name: Name,
@ -48,6 +48,7 @@ func (bs Service) Create(From time.Time, To time.Time, Name string, PhoneNumber
To: To, To: To,
Platform: Platform, Platform: Platform,
PlatformFees: PlatformFees, PlatformFees: PlatformFees,
ExternalId: externalId,
} }
_ = bs.db.Create(b) _ = bs.db.Create(b)
return 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) customerNumber := extractInt(`Nombre de personnes : \s*\n\s*(\d+)`, content)
commissionAmount := extractFloat(`Commission : € (\d+,\d+)`, content) commissionAmount := extractFloat(`Commission : € (\d+,\d+)`, content)
item := extractString(`Maison 1 Chambre \((T2|T3) -`, 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) standardRate := extractFloat(`Standard Rate\n\s+€ (\d+)`, content)
taxQty := (totalAmount - standardRate*float64(stayLength)) / 1.5 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, item, stayLength, standardRate, "Card")
bs.CreateItem(b.Id, "Taxes", int(taxQty), 1.5, "Cash") bs.CreateItem(b.Id, "Taxes", int(taxQty), 1.5, "Cash")

View file

@ -72,7 +72,7 @@ func handleCreateBooking(bs *booking.Service) echo.HandlerFunc {
ts, _ = myTime.ParseFromForm(c.FormValue("to")) ts, _ = myTime.ParseFromForm(c.FormValue("to"))
nb.To = ts 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)) return c.Redirect(http.StatusSeeOther, fmt.Sprintf("%s/%d", constant.RouteBooking, b.Id))
} }
} }