add owner handle
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
0f2d7126eb
commit
f862abe5a4
12 changed files with 274 additions and 55 deletions
|
|
@ -8,13 +8,14 @@ import (
|
|||
)
|
||||
|
||||
type Account struct {
|
||||
ID int32 `json:"id"`
|
||||
Nom string `json:"nom"`
|
||||
Type string `json:"type"`
|
||||
DeviseReference string `json:"devise_reference"`
|
||||
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"`
|
||||
MasterAccountID *int32 `json:"master_account_id,omitempty"`
|
||||
Objectif *string `json:"objectif,omitempty"`
|
||||
OwnerID int32 `json:"-"`
|
||||
}
|
||||
|
||||
type CreateAccountParams struct {
|
||||
|
|
@ -31,11 +32,27 @@ type CreateEnvelopeParams struct {
|
|||
|
||||
var ErrMasterIsSubAccount = errors.New("master account cannot itself be a sub-account")
|
||||
|
||||
const accountCols = `id, nom, type, devise_reference, plafond, master_account_id, objectif`
|
||||
const accountCols = `id, nom, type, devise_reference, plafond, master_account_id, objectif, owner_id`
|
||||
|
||||
func (s *Store) ListAccounts(ctx context.Context) ([]Account, error) {
|
||||
// VerifyAccountOwner retourne pgx.ErrNoRows si le compte n'appartient pas à ownerID.
|
||||
func (s *Store) VerifyAccountOwner(ctx context.Context, accountID, ownerID int32) error {
|
||||
var exists bool
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT EXISTS(SELECT 1 FROM account WHERE id = $1 AND owner_id = $2)`,
|
||||
accountID, ownerID).Scan(&exists)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) ListAccounts(ctx context.Context, ownerID int32) ([]Account, error) {
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT `+accountCols+` FROM account WHERE master_account_id IS NULL ORDER BY nom`)
|
||||
`SELECT `+accountCols+` FROM account WHERE master_account_id IS NULL AND owner_id = $1 ORDER BY nom`,
|
||||
ownerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -80,22 +97,22 @@ func (s *Store) GetAccountWithEnvelopes(ctx context.Context, id int32) (AccountW
|
|||
return AccountWithEnvelopes{Account: account, Envelopes: envelopes}, nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateAccount(ctx context.Context, p CreateAccountParams) (Account, error) {
|
||||
func (s *Store) CreateAccount(ctx context.Context, p CreateAccountParams, ownerID int32) (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)
|
||||
`INSERT INTO account (nom, type, devise_reference, plafond, owner_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING `+accountCols,
|
||||
p.Nom, p.Type, p.DeviseReference, p.Plafond)
|
||||
p.Nom, p.Type, p.DeviseReference, p.Plafond, ownerID)
|
||||
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) {
|
||||
func (s *Store) CreateEnvelope(ctx context.Context, masterID int32, p CreateEnvelopeParams, ownerID int32) (Account, error) {
|
||||
master, err := s.GetAccount(ctx, masterID)
|
||||
if err != nil {
|
||||
return Account{}, err
|
||||
|
|
@ -103,45 +120,48 @@ func (s *Store) CreateEnvelope(ctx context.Context, masterID int32, p CreateEnve
|
|||
if master.MasterAccountID != nil {
|
||||
return Account{}, ErrMasterIsSubAccount
|
||||
}
|
||||
if master.OwnerID != ownerID {
|
||||
return Account{}, pgx.ErrNoRows
|
||||
}
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`INSERT INTO account (nom, type, devise_reference, master_account_id, objectif)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`INSERT INTO account (nom, type, devise_reference, master_account_id, objectif, owner_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING `+accountCols,
|
||||
p.Nom, master.Type, master.DeviseReference, masterID, p.Objectif)
|
||||
p.Nom, master.Type, master.DeviseReference, masterID, p.Objectif, ownerID)
|
||||
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) {
|
||||
func (s *Store) UpdateAccount(ctx context.Context, id int32, p CreateAccountParams, ownerID int32) (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
|
||||
WHERE id = $1 AND master_account_id IS NULL AND owner_id = $6
|
||||
RETURNING `+accountCols,
|
||||
id, p.Nom, p.Type, p.DeviseReference, p.Plafond)
|
||||
id, p.Nom, p.Type, p.DeviseReference, p.Plafond, ownerID)
|
||||
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) {
|
||||
func (s *Store) UpdateEnvelope(ctx context.Context, id int32, p CreateEnvelopeParams, ownerID int32) (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
|
||||
WHERE id = $1 AND master_account_id IS NOT NULL AND owner_id = $4
|
||||
RETURNING `+accountCols,
|
||||
id, p.Nom, p.Objectif)
|
||||
id, p.Nom, p.Objectif, ownerID)
|
||||
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)
|
||||
func (s *Store) DeleteAccount(ctx context.Context, id int32, ownerID int32) error {
|
||||
_, err := s.pool.Exec(ctx, `DELETE FROM account WHERE id = $1 AND owner_id = $2`, id, ownerID)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue