57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package repository
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// WalletMoveCustomer is a customer on either side of an exchange or a transfer.
|
||
|
|
type WalletMoveCustomer struct {
|
||
|
|
ID uuid.UUID
|
||
|
|
OrganizationID uuid.UUID
|
||
|
|
Name string
|
||
|
|
PhoneNumber *string
|
||
|
|
IsDefault bool
|
||
|
|
IsActive bool
|
||
|
|
}
|
||
|
|
|
||
|
|
// WalletMoveRepository reads what exchanges and transfers check before moving a
|
||
|
|
// balance (docs/prd-point-coin.md F4, F5). The balance itself only moves through
|
||
|
|
// WalletProcessor.
|
||
|
|
type WalletMoveRepository interface {
|
||
|
|
// GetCustomer returns ErrWalletNotFound when the customer does not exist.
|
||
|
|
GetCustomer(ctx context.Context, customerID uuid.UUID) (*WalletMoveCustomer, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
type walletMoveRepository struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewWalletMoveRepository(db *gorm.DB) WalletMoveRepository {
|
||
|
|
return &walletMoveRepository{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *walletMoveRepository) GetCustomer(ctx context.Context, customerID uuid.UUID) (*WalletMoveCustomer, error) {
|
||
|
|
return r.customer(ctx, "id = ?", customerID)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *walletMoveRepository) customer(ctx context.Context, where string, arg interface{}) (*WalletMoveCustomer, error) {
|
||
|
|
var c WalletMoveCustomer
|
||
|
|
err := DBFromContext(ctx, r.db).WithContext(ctx).
|
||
|
|
Table("customers").
|
||
|
|
Select("id, organization_id, name, phone_number, is_default, is_active").
|
||
|
|
Where(where, arg).
|
||
|
|
Take(&c).Error
|
||
|
|
if err != nil {
|
||
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
|
|
return nil, ErrWalletNotFound
|
||
|
|
}
|
||
|
|
return nil, fmt.Errorf("failed to get customer: %w", err)
|
||
|
|
}
|
||
|
|
return &c, nil
|
||
|
|
}
|