feat(transaction): #101 replace "note" with "party" and "description"
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 4m17s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 4m40s

This commit was merged in pull request #103.
This commit is contained in:
2025-05-17 22:06:52 +02:00
parent 84b7144f7b
commit 989a31afd1
5 changed files with 79 additions and 41 deletions

View File

@@ -137,7 +137,8 @@ func (h TransactionImpl) handleUpdateTransaction() http.HandlerFunc {
TreasureChestId: r.FormValue("treasure-chest-id"), TreasureChestId: r.FormValue("treasure-chest-id"),
Value: r.FormValue("value"), Value: r.FormValue("value"),
Timestamp: r.FormValue("timestamp"), Timestamp: r.FormValue("timestamp"),
Note: r.FormValue("note"), Party: r.FormValue("party"),
Description: r.FormValue("description"),
} }
if input.Id == "new" { if input.Id == "new" {

View File

@@ -0,0 +1,4 @@
ALTER TABLE "transaction" DROP COLUMN note;
ALTER TABLE "transaction" ADD COLUMN party TEXT;
ALTER TABLE "transaction" ADD COLUMN description TEXT;

View File

@@ -73,14 +73,14 @@ func (s TransactionImpl) Add(user *types.User, transactionInput types.Transactio
} }
r, err := tx.NamedExec(` r, err := tx.NamedExec(`
INSERT INTO "transaction" (id, user_id, account_id, treasure_chest_id, value, timestamp, note, error, created_at, created_by) INSERT INTO "transaction" (id, user_id, account_id, treasure_chest_id, value, timestamp, party, description, error, created_at, created_by)
VALUES (:id, :user_id, :account_id, :treasure_chest_id, :value, :timestamp, :note, :error, :created_at, :created_by)`, transaction) VALUES (:id, :user_id, :account_id, :treasure_chest_id, :value, :timestamp, :party, :description, :error, :created_at, :created_by)`, transaction)
err = db.TransformAndLogDbError("transaction Insert", r, err) err = db.TransformAndLogDbError("transaction Insert", r, err)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if transaction.AccountId != nil { if transaction.Error == nil && transaction.AccountId != nil {
r, err = tx.Exec(` r, err = tx.Exec(`
UPDATE account UPDATE account
SET current_balance = current_balance + ? SET current_balance = current_balance + ?
@@ -91,7 +91,7 @@ func (s TransactionImpl) Add(user *types.User, transactionInput types.Transactio
} }
} }
if transaction.TreasureChestId != nil { if transaction.Error == nil && transaction.TreasureChestId != nil {
r, err = tx.Exec(` r, err = tx.Exec(`
UPDATE treasure_chest UPDATE treasure_chest
SET current_balance = current_balance + ? SET current_balance = current_balance + ?
@@ -141,7 +141,7 @@ func (s TransactionImpl) Update(user *types.User, input types.TransactionInput)
return nil, types.ErrInternal return nil, types.ErrInternal
} }
if transaction.AccountId != nil { if transaction.Error == nil && transaction.AccountId != nil {
r, err := tx.Exec(` r, err := tx.Exec(`
UPDATE account UPDATE account
SET current_balance = current_balance - ? SET current_balance = current_balance - ?
@@ -151,7 +151,7 @@ func (s TransactionImpl) Update(user *types.User, input types.TransactionInput)
return nil, err return nil, err
} }
} }
if transaction.TreasureChestId != nil { if transaction.Error == nil && transaction.TreasureChestId != nil {
r, err := tx.Exec(` r, err := tx.Exec(`
UPDATE treasure_chest UPDATE treasure_chest
SET current_balance = current_balance - ? SET current_balance = current_balance - ?
@@ -167,7 +167,7 @@ func (s TransactionImpl) Update(user *types.User, input types.TransactionInput)
return nil, err return nil, err
} }
if transaction.AccountId != nil { if transaction.Error == nil && transaction.AccountId != nil {
r, err := tx.Exec(` r, err := tx.Exec(`
UPDATE account UPDATE account
SET current_balance = current_balance + ? SET current_balance = current_balance + ?
@@ -177,7 +177,7 @@ func (s TransactionImpl) Update(user *types.User, input types.TransactionInput)
return nil, err return nil, err
} }
} }
if transaction.TreasureChestId != nil { if transaction.Error == nil && transaction.TreasureChestId != nil {
r, err := tx.Exec(` r, err := tx.Exec(`
UPDATE treasure_chest UPDATE treasure_chest
SET current_balance = current_balance + ? SET current_balance = current_balance + ?
@@ -195,7 +195,8 @@ func (s TransactionImpl) Update(user *types.User, input types.TransactionInput)
treasure_chest_id = :treasure_chest_id, treasure_chest_id = :treasure_chest_id,
value = :value, value = :value,
timestamp = :timestamp, timestamp = :timestamp,
note = :note, party = :party,
description = :description,
error = :error, error = :error,
updated_at = :updated_at, updated_at = :updated_at,
updated_by = :updated_by updated_by = :updated_by
@@ -275,33 +276,45 @@ func (s TransactionImpl) Delete(user *types.User, id string) error {
_ = tx.Rollback() _ = tx.Rollback()
}() }()
r, err := tx.Exec(` var transaction types.Transaction
UPDATE account err = tx.Get(&transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
SET current_balance = current_balance - (SELECT value FROM "transaction" WHERE id = ? AND user_id = ?) err = db.TransformAndLogDbError("transaction Delete", nil, err)
WHERE id = (SELECT account_id FROM "transaction" WHERE id = ? AND user_id = ?) if err != nil {
`, uuid, user.Id, uuid, user.Id)
err = db.TransformAndLogDbError("transaction Delete", r, err)
if err != nil && err != db.ErrNotFound {
return err
}
r, err = tx.Exec(`
UPDATE treasure_chest
SET current_balance = current_balance - (SELECT value FROM "transaction" WHERE id = ? AND user_id = ?)
WHERE id = (SELECT treasure_chest_id FROM "transaction" WHERE id = ? AND user_id = ?)
`, uuid, user.Id, uuid, user.Id)
err = db.TransformAndLogDbError("transaction Delete", r, err)
if err != nil && err != db.ErrNotFound {
return err return err
} }
r, err = tx.Exec("DELETE FROM \"transaction\" WHERE id = ? AND user_id = ?", uuid, user.Id) if transaction.Error == nil && transaction.AccountId != nil {
r, err := tx.Exec(`
UPDATE account
SET current_balance = current_balance - ?
WHERE id = ?
AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
err = db.TransformAndLogDbError("transaction Delete", r, err)
if err != nil && err != db.ErrNotFound {
return err
}
}
if transaction.Error == nil && transaction.TreasureChestId != nil {
r, err := tx.Exec(`
UPDATE treasure_chest
SET current_balance = current_balance - ?
WHERE id = ?
AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
err = db.TransformAndLogDbError("transaction Delete", r, err)
if err != nil && err != db.ErrNotFound {
return err
}
}
r, err := tx.Exec("DELETE FROM \"transaction\" WHERE id = ? AND user_id = ?", uuid, user.Id)
err = db.TransformAndLogDbError("transaction Delete", r, err) err = db.TransformAndLogDbError("transaction Delete", r, err)
if err != nil { if err != nil {
return err return err
} }
err = tx.Commit() err = tx.Commit()
err = db.TransformAndLogDbError("transaction RecalculateBalances", nil, err) err = db.TransformAndLogDbError("transaction Delete", nil, err)
if err != nil { if err != nil {
return err return err
} }
@@ -375,8 +388,9 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
if err != nil { if err != nil {
return err return err
} }
if transaction.Error != nil { if transaction.Error != nil {
log.Info("err: %s", *transaction.Error) continue
} }
if transaction.AccountId != nil { if transaction.AccountId != nil {
@@ -491,8 +505,14 @@ func (s TransactionImpl) validateAndEnrichTransaction(tx *sqlx.Tx, oldTransactio
return nil, fmt.Errorf("could not parse timestamp: %w", ErrBadRequest) return nil, fmt.Errorf("could not parse timestamp: %w", ErrBadRequest)
} }
if input.Note != "" { if input.Party != "" {
err = validateString(input.Note, "note") err = validateString(input.Party, "party")
if err != nil {
return nil, err
}
}
if input.Description != "" {
err = validateString(input.Description, "description")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -506,7 +526,8 @@ func (s TransactionImpl) validateAndEnrichTransaction(tx *sqlx.Tx, oldTransactio
TreasureChestId: treasureChestUuid, TreasureChestId: treasureChestUuid,
Value: valueInt, Value: valueInt,
Timestamp: timestamp, Timestamp: timestamp,
Note: input.Note, Party: input.Party,
Description: input.Description,
CreatedAt: createdAt, CreatedAt: createdAt,
CreatedBy: createdBy, CreatedBy: createdBy,

View File

@@ -30,13 +30,14 @@ templ EditTransaction(transaction *types.Transaction, accounts []*types.Account,
{{ {{
var ( var (
timestamp time.Time timestamp time.Time
value string
id string id string
cancelUrl string cancelUrl string
) )
note := "" party := ""
description := ""
accountId := "" accountId := ""
value := "0.00"
treasureChestId := "" treasureChestId := ""
if transaction == nil { if transaction == nil {
timestamp = time.Now().UTC().Truncate(time.Minute) timestamp = time.Now().UTC().Truncate(time.Minute)
@@ -45,7 +46,8 @@ templ EditTransaction(transaction *types.Transaction, accounts []*types.Account,
cancelUrl = "/empty" cancelUrl = "/empty"
} else { } else {
timestamp = transaction.Timestamp.UTC().Truncate(time.Minute) timestamp = transaction.Timestamp.UTC().Truncate(time.Minute)
note = transaction.Note party = transaction.Party
description = transaction.Description
if transaction.AccountId != nil { if transaction.AccountId != nil {
accountId = transaction.AccountId.String() accountId = transaction.AccountId.String()
} }
@@ -74,11 +76,18 @@ templ EditTransaction(transaction *types.Transaction, accounts []*types.Account,
value={ timestamp.String() } value={ timestamp.String() }
class="bg-white input datetime" class="bg-white input datetime"
/> />
<label for="note" class="text-sm text-gray-500">Note</label> <label for="party" class="text-sm text-gray-500">Party</label>
<input <input
name="note" name="party"
type="text" type="text"
value={ note } value={ party }
class="mr-auto bg-white input"
/>
<label for="description" class="text-sm text-gray-500">Description</label>
<input
name="description"
type="text"
value={ description }
class="mr-auto bg-white input" class="mr-auto bg-white input"
/> />
<label for="value" class="text-sm text-gray-500">Value (€)</label> <label for="value" class="text-sm text-gray-500">Value (€)</label>

View File

@@ -18,7 +18,9 @@ type Transaction struct {
UserId uuid.UUID `db:"user_id"` UserId uuid.UUID `db:"user_id"`
Timestamp time.Time Timestamp time.Time
Note string Company string
Party string
Description string
// account id is only nil, if the transaction is a deposit to a treasure chest // account id is only nil, if the transaction is a deposit to a treasure chest
AccountId *uuid.UUID `db:"account_id"` AccountId *uuid.UUID `db:"account_id"`
@@ -40,5 +42,6 @@ type TransactionInput struct {
TreasureChestId string TreasureChestId string
Value string Value string
Timestamp string Timestamp string
Note string Party string
Description string
} }