account/internal/store/account.go
GnomeZworc 1f3ed998df
add envelop
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-06-13 13:41:29 +02:00

147 lines
4.4 KiB
Go

package store
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
type Account struct {
ID int32 `json:"id"`
Nom string `json:"nom"`
Type string `json:"type"`
DeviseReference string `json:"devise_reference"`
Plafond *float64 `json:"plafond,omitempty"`
MasterAccountID *int32 `json:"master_account_id,omitempty"`
Objectif *string `json:"objectif,omitempty"`
}
type CreateAccountParams struct {
Nom string `json:"nom"`
Type string `json:"type"`
DeviseReference string `json:"devise_reference"`
Plafond *float64 `json:"plafond,omitempty"`
}
type CreateEnvelopeParams struct {
Nom string `json:"nom"`
Objectif *string `json:"objectif,omitempty"`
}
var ErrMasterIsSubAccount = errors.New("master account cannot itself be a sub-account")
const accountCols = `id, nom, type, devise_reference, plafond, master_account_id, objectif`
func (s *Store) ListAccounts(ctx context.Context) ([]Account, error) {
rows, err := s.pool.Query(ctx,
`SELECT `+accountCols+` FROM account WHERE master_account_id IS NULL ORDER BY nom`)
if err != nil {
return nil, err
}
return pgx.CollectRows(rows, pgx.RowToStructByName[Account])
}
func (s *Store) GetAccount(ctx context.Context, id int32) (Account, error) {
rows, err := s.pool.Query(ctx,
`SELECT `+accountCols+` FROM account WHERE id = $1`, id)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) ListEnvelopes(ctx context.Context, masterID int32) ([]Account, error) {
rows, err := s.pool.Query(ctx,
`SELECT `+accountCols+` FROM account WHERE master_account_id = $1 ORDER BY nom`, masterID)
if err != nil {
return nil, err
}
return pgx.CollectRows(rows, pgx.RowToStructByName[Account])
}
type AccountWithEnvelopes struct {
Account
Envelopes []Account `json:"envelopes"`
}
func (s *Store) GetAccountWithEnvelopes(ctx context.Context, id int32) (AccountWithEnvelopes, error) {
account, err := s.GetAccount(ctx, id)
if err != nil {
return AccountWithEnvelopes{}, err
}
envelopes, err := s.ListEnvelopes(ctx, id)
if err != nil {
return AccountWithEnvelopes{}, err
}
if envelopes == nil {
envelopes = []Account{}
}
return AccountWithEnvelopes{Account: account, Envelopes: envelopes}, nil
}
func (s *Store) CreateAccount(ctx context.Context, p CreateAccountParams) (Account, error) {
if p.DeviseReference == "" {
p.DeviseReference = "EUR"
}
rows, err := s.pool.Query(ctx,
`INSERT INTO account (nom, type, devise_reference, plafond)
VALUES ($1, $2, $3, $4)
RETURNING `+accountCols,
p.Nom, p.Type, p.DeviseReference, p.Plafond)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) CreateEnvelope(ctx context.Context, masterID int32, p CreateEnvelopeParams) (Account, error) {
master, err := s.GetAccount(ctx, masterID)
if err != nil {
return Account{}, err
}
if master.MasterAccountID != nil {
return Account{}, ErrMasterIsSubAccount
}
rows, err := s.pool.Query(ctx,
`INSERT INTO account (nom, type, devise_reference, master_account_id, objectif)
VALUES ($1, $2, $3, $4, $5)
RETURNING `+accountCols,
p.Nom, master.Type, master.DeviseReference, masterID, p.Objectif)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) UpdateAccount(ctx context.Context, id int32, p CreateAccountParams) (Account, error) {
if p.DeviseReference == "" {
p.DeviseReference = "EUR"
}
rows, err := s.pool.Query(ctx,
`UPDATE account SET nom = $2, type = $3, devise_reference = $4, plafond = $5
WHERE id = $1 AND master_account_id IS NULL
RETURNING `+accountCols,
id, p.Nom, p.Type, p.DeviseReference, p.Plafond)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) UpdateEnvelope(ctx context.Context, id int32, p CreateEnvelopeParams) (Account, error) {
rows, err := s.pool.Query(ctx,
`UPDATE account SET nom = $2, objectif = $3
WHERE id = $1 AND master_account_id IS NOT NULL
RETURNING `+accountCols,
id, p.Nom, p.Objectif)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) DeleteAccount(ctx context.Context, id int32) error {
_, err := s.pool.Exec(ctx, `DELETE FROM account WHERE id = $1`, id)
return err
}