mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-11 13:16:50 +00:00
Enhance the booking sync logic to trim and match item names more robustly, falling back to creating a generic item when no host item is found. Add unit tests for item creation and fallback behavior in booking sync.
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package booking
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/rjNemo/rentease/internal/config"
|
|
)
|
|
|
|
func (bs Service) ParseFromAPI(rawContent string) (*Booking, error) {
|
|
b, err := bs.parser.Parse(rawContent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
items := b.Items
|
|
b = bs.Create(b.From, b.To, b.Name, b.PhoneNumber, b.Email, string(b.Platform), b.CustomerNumber, b.PlatformFees, b.ExternalID)
|
|
|
|
hostItems := config.NewHost().Items
|
|
createdItems := make([]Item, 0, len(items))
|
|
for _, itm := range items {
|
|
itemName := strings.TrimSpace(itm.Item)
|
|
hostItem, ok := findHostItem(hostItems, itemName)
|
|
if ok {
|
|
for _, created := range bs.CreateItem(b.ID, hostItem, itm.Quantity, itm.Price, itm.PaymentMethod, b.CustomerNumber, string(b.Platform)) {
|
|
createdItems = append(createdItems, *created)
|
|
}
|
|
continue
|
|
}
|
|
|
|
fallbackItem := &Item{
|
|
BookingID: b.ID,
|
|
Item: itemName,
|
|
Quantity: itm.Quantity,
|
|
Price: itm.Price,
|
|
PaymentMethod: itm.PaymentMethod,
|
|
PaymentStatus: "Pending",
|
|
}
|
|
|
|
if err := bs.store.CreateItem(fallbackItem); err != nil {
|
|
return nil, fmt.Errorf("failed to create item %q for booking %d: %w", itemName, b.ID, err)
|
|
}
|
|
createdItems = append(createdItems, *fallbackItem)
|
|
}
|
|
|
|
b.Items = createdItems
|
|
|
|
return b, nil
|
|
}
|
|
|
|
func findHostItem(hostItems map[string]config.HostItem, itemName string) (config.HostItem, bool) {
|
|
if hostItem, ok := hostItems[itemName]; ok {
|
|
return hostItem, true
|
|
}
|
|
|
|
for key, hostItem := range hostItems {
|
|
if strings.EqualFold(key, itemName) {
|
|
return hostItem, true
|
|
}
|
|
}
|
|
|
|
return config.HostItem{}, false
|
|
}
|