Skip to content

Commit

Permalink
Merge pull request #2 from praserx/v1.1.0
Browse files Browse the repository at this point in the history
Added schema table, added credit field, other minor updates
  • Loading branch information
praserx authored Oct 4, 2023
2 parents f0b7d51 + 7f1ebcf commit 7de5277
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 8 additions & 3 deletions pkg/cmd/gobarista/commands/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -302,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 {
Expand Down
24 changes: 24 additions & 0 deletions pkg/cmd/gobarista/commands/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
},
}
Expand All @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gobarista/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
14 changes: 14 additions & 0 deletions pkg/cmd/gobarista/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/mail/emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type TemplateVars struct {
UID string
Name string
Location string
Credit string
Rank string
PeriodFrom string
PeriodTo string
Expand Down
8 changes: 8 additions & 0 deletions pkg/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
18 changes: 10 additions & 8 deletions templates/mjml/bill.cs.mjml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
Zákazník:</br>
Jméno:</br>
Lokalita:</br>
Kredit:</br>
Úroveň:
</p>
</mj-text>
Expand All @@ -69,6 +70,7 @@
#{{.UID}}</br>
{{.Name}}</br>
{{.Location}}</br>
{{.Credit}}</br>
{{.Rank}}
</p>
</mj-text>
Expand All @@ -90,16 +92,16 @@
<mj-column>
<mj-table font-size="16px" padding="0px">
<tr style="text-align: left;">
<th style="padding: 5px 0px;">Položka</th>
<th style="padding: 5px 0px;">Cena</th>
<th style="padding: 5px 0px;">Množství</th>
<th style="padding: 5px 0px; text-align:right;">Celkem</th>
<th style="padding: 5px 0px;text-align:left;">Položka</th>
<th style="padding: 5px 0px;text-align:left;">Cena</th>
<th style="padding: 5px 0px;text-align:left;">Množství</th>
<th style="padding: 5px 0px;text-align:right;">Celkem</th>
</tr>
<tr>
<td style="padding: 5px 0px;">Káva</td>
<td style="padding: 5px 0px;">{{.UnitPrice}}</td>
<td style="padding: 5px 0px;">{{.Quantity}}</td>
<td style="padding: 5px 0px; text-align:right;">{{.Amount}}</td>
<td style="padding: 5px 0px;text-align:left;">Káva</td>
<td style="padding: 5px 0px;text-align:left;">{{.UnitPrice}}</td>
<td style="padding: 5px 0px;text-align:left;">{{.Quantity}}</td>
<td style="padding: 5px 0px;text-align:right;">{{.Amount}}</td>
</tr>
</mj-table>
</mj-column>
Expand Down

0 comments on commit 7de5277

Please sign in to comment.