From 0ccabe71b3cd4200a1edc4a002ce833e7807774e Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 24 Feb 2024 21:18:56 +0100 Subject: [PATCH] migration script --- config/host.go | 36 +++++++++++++++++- main.go | 7 ++++ migrateDB.go | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 migrateDB.go diff --git a/config/host.go b/config/host.go index 60e3b1c..c414a9c 100644 --- a/config/host.go +++ b/config/host.go @@ -1,11 +1,43 @@ package config type Host struct { - CustomerSeed int + PaymentMethods []string + Platforms []string + Items []HostItem + CustomerSeed int +} + +type HostItem struct { + Name string + Price float64 } func NewHost() *Host { return &Host{ - CustomerSeed: 286, + CustomerSeed: 286, + PaymentMethods: []string{"Card", "Cash", "Cheque", "Transfer"}, + Platforms: []string{"Booking", "AirBnb", "TripAdvisor", "Other"}, + Items: []HostItem{ + { + Name: "T2", + Price: 59.0, + }, + { + Name: "T3", + Price: 80.0, + }, + { + Name: "Airport", + Price: 25.0, + }, + { + Name: "Port", + Price: 20.0, + }, + { + Name: "Taxes", + Price: 1.5, + }, + }, } } diff --git a/main.go b/main.go index e977b8f..9587137 100644 --- a/main.go +++ b/main.go @@ -34,5 +34,12 @@ func main() { log.Fatalf("error migrating the database %s\n", err) } + if os.Getenv("MIGRATE_DB") != "" { + err := MigrateDB(db) + if err != nil { + log.Fatalln(err) + } + } + server.New(booking.NewService(db), pdf.NewPdfService(), config.NewHost()).Start() } diff --git a/migrateDB.go b/migrateDB.go new file mode 100644 index 0000000..ed401ef --- /dev/null +++ b/migrateDB.go @@ -0,0 +1,101 @@ +package main + +import ( + "fmt" + "log" + "time" + + "github.com/rjNemo/rentease/config" + "github.com/rjNemo/rentease/internal/booking" + u "github.com/rjNemo/underscore" + "gorm.io/gorm" +) + +type Invoice struct { + To time.Time + From time.Time + Platform string + Name string + PhoneNumber string `gorm:"column:phoneNumber"` + Room string + PaymentMethod string `gorm:"column:paymentMethod"` + Status string + Id int + Price float64 `gorm:"type:decimal(10,2)"` + PlatformFees float64 `gorm:"type:decimal(10,2);column:platformFees"` + CustomerNumber int `gorm:"column:customersNumber"` + WithTaxes bool `gorm:"column:withTaxes"` +} + +func (Invoice) TableName() string { + return "Invoice" +} + +var hc *config.Host = config.NewHost() + +func MigrateDB(db *gorm.DB) error { + return db.Transaction(func(tx *gorm.DB) error { + invoices := make([]Invoice, 0) + if err := tx.Find(&invoices).Error; err != nil { + return err + } + // log.Println(invoices) + // - for each row in Invoice table, + for _, i := range invoices { + // create a booking strcut based on the invoice value, commit + fmt.Println(" ") + b := &booking.Booking{ + From: i.From, + To: i.To, + PhoneNumber: i.PhoneNumber, + CustomerNumber: i.CustomerNumber, + Platform: i.Platform, + PlatformFees: i.PlatformFees, + Name: i.Name, + } + if err := tx.Create(b).Error; err != nil { + return err + } + log.Printf("%+v\n", b) + // take the new booking id and create a item struct based on the Invoice value, commit + item := &booking.Item{ + BookingId: b.Id, + Item: i.Room, + Quantity: computeQty(i), + Price: priceFromConfig(i, hc), + PaymentMethod: i.PaymentMethod, + PaymentStatus: i.Status, + } + if err := tx.Create(item).Error; err != nil { + return err + } + log.Printf("%+v\n", item) + if i.WithTaxes { + taxItem := &booking.Item{ + BookingId: b.Id, + Item: "Taxes", + Quantity: computeQty(i) * i.CustomerNumber, + Price: 1.5, + PaymentMethod: i.PaymentMethod, + PaymentStatus: i.Status, + } + if err := tx.Create(taxItem).Error; err != nil { + return err + } + log.Printf("%+v\n", taxItem) + } + } + return nil + }) +} + +func priceFromConfig(i Invoice, hc *config.Host) float64 { + itm, _ := u.Find(hc.Items, func(hi config.HostItem) bool { + return hi.Name == i.Room + }) + return itm.Price +} + +func computeQty(i Invoice) int { + return int(i.To.Sub(i.From) / 86400000000000) +}