mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
170 lines
4.3 KiB
Go
170 lines
4.3 KiB
Go
package booking
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rjNemo/rentease/internal/config"
|
|
)
|
|
|
|
func (bs Service) ParseFromApi(rawContent string) (*Booking, error) {
|
|
b, err := ParseBooking(rawContent)
|
|
log.Printf("parsed booking: %+v", b)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
b = bs.Create(b.From, b.To, b.Name, b.PhoneNumber, b.Email, b.Platform, b.CustomerNumber, b.PlatformFees, b.ExternalId)
|
|
itm := b.Items[0]
|
|
if item, ok := config.NewHost().Items[itm.Item]; ok {
|
|
bs.CreateItem(b.Id, item, itm.Quantity, itm.Price, itm.PaymentMethod, b.CustomerNumber, b.Platform)
|
|
}
|
|
|
|
return b, nil
|
|
}
|
|
|
|
func ParseBooking(content string) (*Booking, error) {
|
|
content = strings.ReplaceAll(strings.TrimSpace(content), "\u00a0", " ")
|
|
|
|
arrivalDate, err := extractDate(`Date d'arrivée `, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
departureDate, err := extractDate(`Date de départ `, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
stayLength, err := extractInt(`Durée de séjour : (\d+) nuits`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
customerName, err := extractString(`Nom du client : \n\s+([A-Za-z\s]+)`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
customerName = strings.SplitN(customerName, "\n", 2)[0]
|
|
customerEmail, err := extractString(`[\w\.\-]+@[\w\.\-]+\.\w+`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
customerNumber, err := extractInt(`Nombre de personnes : \s*\n\s*(\d+)`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commissionAmount, err := extractFloat(`Commission : € (\d+,\d+)`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
itemName, err := extractString(`Maison . Chambre(s?) \((T2|T3)`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
externalId, err := extractString(`Numéro de réservation : \n\s+(\d+)`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
standardRate, err := extractFloat(`Standard Rate\n\s+€ (\d+)`, content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Booking{
|
|
From: *formatDate(arrivalDate),
|
|
To: *formatDate(departureDate),
|
|
Name: customerName,
|
|
Email: customerEmail,
|
|
Platform: "Booking",
|
|
CustomerNumber: customerNumber,
|
|
PlatformFees: commissionAmount,
|
|
ExternalId: &externalId,
|
|
Items: []Item{
|
|
{
|
|
Item: itemName,
|
|
Quantity: stayLength,
|
|
Price: standardRate,
|
|
PaymentMethod: "Card",
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func extractDate(pattern, content string) (string, error) {
|
|
re := regexp.MustCompile(pattern + `(lun|mar|mer|jeu|ven|sam|dim)\. \d{1,2} (janv|févr|mars|avr|mai|juin|juil|août|sept|oct|nov|déc)\.? \d{4}`)
|
|
dateMatch := re.FindString(content)
|
|
|
|
if dateMatch == "" {
|
|
return "", fmt.Errorf("date not found")
|
|
}
|
|
|
|
// Regular expression to remove the prefix
|
|
rePrefix := regexp.MustCompile(pattern + `\w+\.\s*`)
|
|
dateString := rePrefix.ReplaceAllString(dateMatch, "")
|
|
return dateString, nil
|
|
}
|
|
|
|
func extractInt(pattern, content string) (int, error) {
|
|
re := regexp.MustCompile(pattern)
|
|
match := re.FindStringSubmatch(content)
|
|
if len(match) > 1 {
|
|
val, err := strconv.Atoi(match[1])
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return val, nil
|
|
}
|
|
return 0.0, fmt.Errorf("no match for %s", pattern)
|
|
}
|
|
|
|
func extractFloat(pattern, content string) (float64, error) {
|
|
re := regexp.MustCompile(pattern)
|
|
match := re.FindStringSubmatch(content)
|
|
if len(match) > 1 {
|
|
val, err := strconv.ParseFloat(strings.ReplaceAll(match[1], ",", "."), 64)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return val, nil
|
|
}
|
|
return 0.0, fmt.Errorf("no match for %s", pattern)
|
|
}
|
|
|
|
func extractString(pattern, content string) (string, error) {
|
|
re := regexp.MustCompile(pattern)
|
|
match := re.FindStringSubmatch(content)
|
|
if len(match) > 1 {
|
|
return strings.TrimSpace(match[1]), nil
|
|
} else if len(match) > 0 {
|
|
return strings.TrimSpace(match[0]), nil
|
|
}
|
|
return "", fmt.Errorf("pattern %s not found", pattern)
|
|
}
|
|
|
|
func formatDate(date string) *time.Time {
|
|
months := map[string]string{
|
|
"janv.": "01", "févr.": "02", "mar.": "03", "avr.": "04",
|
|
"mai": "05", "juin": "06", "juil.": "07", "août": "08",
|
|
"sep.": "09", "oct.": "10", "nov.": "11", "déc.": "12",
|
|
}
|
|
parts := strings.Split(date, " ")
|
|
dateString := fmt.Sprintf("%s-%02s-%02s", parts[2], months[parts[1]], parts[0])
|
|
|
|
t, err := time.Parse(time.DateOnly, dateString)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return nil
|
|
}
|
|
return &t
|
|
}
|