From 875f82b63e84e9cb013f539a928849920acfe359 Mon Sep 17 00:00:00 2001 From: PraserX Date: Wed, 4 Oct 2023 10:01:23 +0200 Subject: [PATCH 1/2] Added schema table, added credit field, other minor updates --- CHANGELOG.md | 7 +++++++ pkg/cmd/gobarista/commands/billing.go | 5 +++-- pkg/cmd/gobarista/commands/database.go | 24 ++++++++++++++++++++++++ pkg/cmd/gobarista/flags.go | 2 +- pkg/cmd/gobarista/helpers/helpers.go | 14 ++++++++++++++ pkg/database/database.go | 20 ++++++++++++++++++++ pkg/mail/emails.go | 10 ++++++++-- pkg/mail/mail.go | 1 + pkg/models/models.go | 8 ++++++++ templates/mjml/bill.cs.mjml | 18 ++++++++++-------- 10 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..00bb75b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# v1.1.0 +- Added models.Schema table and its Version column +- Added credit field to models.User (do `gobarista database migrate`) +- More minor updates and mail layout update + +# v1.0.0 +- Initial implementation \ No newline at end of file diff --git a/pkg/cmd/gobarista/commands/billing.go b/pkg/cmd/gobarista/commands/billing.go index 9e1e5df..b5630ee 100644 --- a/pkg/cmd/gobarista/commands/billing.go +++ b/pkg/cmd/gobarista/commands/billing.go @@ -187,6 +187,7 @@ var BillingPeriodSummary = cli.Command{ fmt.Printf("Unit price: %.2f\n", period.UnitPrice) fmt.Printf("Total quantity: %d\n", period.TotalQuantity) fmt.Printf("Total amount: %.2f\n", period.TotalAmount) + fmt.Printf("Total months: %d\n", period.TotalMonths) fmt.Printf("Closed: %t\n", period.Closed) fmt.Printf("Total bills: %d\n", len(bills)) @@ -222,7 +223,7 @@ var BillingAddBill = cli.Command{ return fmt.Errorf("error: cannot parse int: %v", err) } - _, err = database.SelectUserByID(uint(uid)) + user, err := database.SelectUserByID(uint(uid)) if errors.Is(err, gorm.ErrRecordNotFound) { return fmt.Errorf("error: user not found") } else if err != nil { @@ -250,7 +251,7 @@ var BillingAddBill = cli.Command{ if err != nil { return fmt.Errorf("error: cannot add new bill to billing period: %v", err.Error()) } - logger.Info(fmt.Sprintf("new bill successfully added to billing period: new bill id: %d", id)) + logger.Info(fmt.Sprintf("new bill successfully added to billing period for user_id=%d, user_name=%s: new bill id: %d", uid, user.Firstname+" "+user.Lastname, id)) return nil }, diff --git a/pkg/cmd/gobarista/commands/database.go b/pkg/cmd/gobarista/commands/database.go index 9773507..794d0c4 100644 --- a/pkg/cmd/gobarista/commands/database.go +++ b/pkg/cmd/gobarista/commands/database.go @@ -2,8 +2,11 @@ package commands import ( + "fmt" + "github.com/praserx/gobarista/pkg/cmd/gobarista/helpers" "github.com/praserx/gobarista/pkg/database" + "github.com/praserx/gobarista/pkg/models" "github.com/urfave/cli/v2" ) @@ -23,7 +26,14 @@ var DatabaseInitialize = cli.Command{ if err = helpers.SetupDatabase(ctx); err != nil { return err } + database.RunAutoMigration() + + _, err = database.InsertSchema(models.Schema{Version: models.VERSION}) + if err != nil { + return fmt.Errorf("cannot update schema version for database: %v", err) + } + return nil }, } @@ -35,7 +45,21 @@ var DatabaseMigrate = cli.Command{ if err = helpers.SetupDatabase(ctx); err != nil { return err } + database.RunAutoMigration() + + schema, err := database.SelectVersion() + if err != nil { + return fmt.Errorf("cannot get schema from database for version check: %v", err) + } + + if schema.Version != models.VERSION { + err = database.UpdateVersion(models.VERSION) + if err != nil { + return fmt.Errorf("cannot update schema version for database: %v", err) + } + } + return nil }, } diff --git a/pkg/cmd/gobarista/flags.go b/pkg/cmd/gobarista/flags.go index 8c8068e..1d72e10 100644 --- a/pkg/cmd/gobarista/flags.go +++ b/pkg/cmd/gobarista/flags.go @@ -5,7 +5,7 @@ import "github.com/urfave/cli/v2" var FlagConfig = cli.StringFlag{ Name: "config", - Value: "./.local/config.ini", + Value: ".local/config.ini", Usage: "path to configuration file", Aliases: []string{"c"}, } diff --git a/pkg/cmd/gobarista/helpers/helpers.go b/pkg/cmd/gobarista/helpers/helpers.go index cd63372..8c89545 100644 --- a/pkg/cmd/gobarista/helpers/helpers.go +++ b/pkg/cmd/gobarista/helpers/helpers.go @@ -2,9 +2,12 @@ package helpers import ( + "fmt" + "github.com/praserx/gobarista/pkg/config" "github.com/praserx/gobarista/pkg/database" "github.com/praserx/gobarista/pkg/mail" + "github.com/praserx/gobarista/pkg/models" "github.com/urfave/cli/v2" "gopkg.in/ini.v1" ) @@ -53,5 +56,16 @@ func SetupDatabase(ctx *cli.Context) (err error) { options = append(options, database.WithPath(opts.Path)) database.SetupDatabase(options...) + if (ctx.Command.Name != "initialize") && (ctx.Command.Name != "migrate") { + schema, err := database.SelectVersion() + if err != nil { + return fmt.Errorf("cannot get schema from database for version check: %v", err) + } + + if schema.Version != models.VERSION { + return fmt.Errorf("migrate first: db_version=%d, required_version=%d", schema.Version, models.VERSION) + } + } + return nil } diff --git a/pkg/database/database.go b/pkg/database/database.go index 2e2e515..079d608 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -63,12 +63,32 @@ func Close() { // RunAutoMigration creates all required tables in database. func RunAutoMigration() error { return gdb.AutoMigrate( + &models.Schema{}, &models.User{}, &models.Period{}, &models.Bill{}, ) } +func InsertSchema(schema models.Schema) (int, error) { + obj := schema + result := gdb.Create(&obj) + return int(obj.Version), result.Error +} + +func SelectVersion() (schema models.Schema, err error) { + result := gdb.First(&schema) + return schema, result.Error +} + +func UpdateVersion(version uint) error { + var schema models.Schema + gdb.First(&schema) + return gdb. + Model(&schema). + Update("Version", version).Error +} + func SelectAllUsers() (users []models.User, err error) { result := gdb.Find(&users) return users, result.Error diff --git a/pkg/mail/emails.go b/pkg/mail/emails.go index 2be2503..519d7cf 100644 --- a/pkg/mail/emails.go +++ b/pkg/mail/emails.go @@ -24,17 +24,23 @@ func SendBill(user models.User, period models.Period, bill models.Bill, totalCus pinfo.Amount = fmt.Sprintf("%.2f", bill.Amount) pinfo.VS = fmt.Sprintf("%d", bill.ID) + totalMonths := 1 + if period.TotalMonths != 0 { + totalMonths = period.TotalMonths + } + tvars.BID = fmt.Sprintf("%d", bill.ID) tvars.UID = fmt.Sprintf("%d", user.ID) tvars.Name = fmt.Sprintf("%s %s", user.Firstname, user.Lastname) tvars.Location = user.Location - tvars.Rank = rank.ComputeRank(bill.Quantity / period.TotalMonths) + tvars.Credit = fmt.Sprintf("%d", user.Credit) + tvars.Rank = rank.ComputeRank(bill.Quantity / totalMonths) tvars.PeriodFrom = period.DateFrom.Format("2. 1. 2006") tvars.PeriodTo = period.DateTo.Format("2. 1. 2006") tvars.UnitPrice = fmt.Sprintf("%.2f", period.UnitPrice) tvars.Quantity = fmt.Sprintf("%d", bill.Quantity) tvars.Amount = fmt.Sprintf("%.2f", bill.Amount) - tvars.TotalMonths = fmt.Sprintf("%d", period.TotalMonths) + tvars.TotalMonths = fmt.Sprintf("%d", totalMonths) tvars.TotalQuantity = fmt.Sprintf("%d", period.TotalQuantity) tvars.TotalAverage = fmt.Sprintf("%d", period.TotalQuantity/totalCustomers) tvars.TotalCustomers = fmt.Sprintf("%d", totalCustomers) diff --git a/pkg/mail/mail.go b/pkg/mail/mail.go index f378a67..e4e3277 100644 --- a/pkg/mail/mail.go +++ b/pkg/mail/mail.go @@ -34,6 +34,7 @@ type TemplateVars struct { UID string Name string Location string + Credit string Rank string PeriodFrom string PeriodTo string diff --git a/pkg/models/models.go b/pkg/models/models.go index 42bd4c5..be0c625 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -7,6 +7,13 @@ import ( "gorm.io/gorm" ) +// VERSION of database schema +const VERSION = uint(1) + +type Schema struct { + Version uint `gorm:"primarykey" json:"version"` +} + type User struct { ID uint `gorm:"primarykey" json:"id"` // GORM default CreatedAt time.Time `json:"timestamp"` // GORM default @@ -17,6 +24,7 @@ type User struct { Firstname string `json:"firstname"` Lastname string `json:"lastname"` Location string `json:"location"` + Credit int `json:"credit"` } type Period struct { diff --git a/templates/mjml/bill.cs.mjml b/templates/mjml/bill.cs.mjml index 833c11f..06b9ad6 100644 --- a/templates/mjml/bill.cs.mjml +++ b/templates/mjml/bill.cs.mjml @@ -58,6 +58,7 @@ Zákazník:
Jméno:
Lokalita:
+ Kredit:
Úroveň:

@@ -69,6 +70,7 @@ #{{.UID}}
{{.Name}}
{{.Location}}
+ {{.Credit}}
{{.Rank}}

@@ -90,16 +92,16 @@ - Položka - Cena - Množství - Celkem + Položka + Cena + Množství + Celkem - Káva - {{.UnitPrice}} - {{.Quantity}} - {{.Amount}} + Káva + {{.UnitPrice}} + {{.Quantity}} + {{.Amount}} From 7f1ebcf1145a87b4f0cd4698eb1434196c62ccba Mon Sep 17 00:00:00 2001 From: PraserX Date: Wed, 4 Oct 2023 10:07:25 +0200 Subject: [PATCH 2/2] Added mail.SendBill return check, added some logs --- pkg/cmd/gobarista/commands/billing.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/gobarista/commands/billing.go b/pkg/cmd/gobarista/commands/billing.go index b5630ee..56df5be 100644 --- a/pkg/cmd/gobarista/commands/billing.go +++ b/pkg/cmd/gobarista/commands/billing.go @@ -303,7 +303,11 @@ var BillingIssueBills = cli.Command{ return fmt.Errorf("error: cannot get user: user_id=%d: %v", bill.UserID, err) } - mail.SendBill(user, period, bill, len(bills)) + if err = mail.SendBill(user, period, bill, len(bills)); err != nil { + logger.Error(fmt.Sprintf("error: billing e-mail has not been sent for bill_id=%d user_id=%d user_name='%s' user_email:'%s'", bill.ID, user.ID, user.Firstname+" "+user.Lastname, user.Email)) + } else { + logger.Info(fmt.Sprintf("billing e-mail has been sent for bill_id=%d user_id=%d user_name='%s' user_email:'%s'", bill.ID, user.ID, user.Firstname+" "+user.Lastname, user.Email)) + } err = database.UpdateBillOnIssued(bill.ID) if err != nil {