managers

package
v0.0.0-...-8ad1ab3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2023 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package managers implements all "manager" structs. These structs are meant to be a layer between API handlers and the database. Managers implement business logic that directly interacts with the database and external services, exposing a high level API.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidEmail        = errors.New("Invalid email address")
	ErrInvalidLogin        = errors.New("Invalid login") // Invalid email/password
	ErrInvalidOldPassword  = errors.New("Invalid old password")
	ErrInvalidTokenPurpose = errors.New("Invalid token purpose") // Invalid token purpose
	ErrEmailExists         = errors.New("Email exists")          // Another user has the same email
	ErrInvalidToken        = errors.New("Invalid token")         // Invalid/unknown token
	ErrPasswordTooWeak     = errors.New("Password too weak")
	ErrUnknownError        = errors.New("Unknown error") // Unexpected error returned by external services
)
View Source
var (
	ErrBusinessAlreadyExists = errors.New("Business already exists")
	ErrTooManyMenuImages     = errors.New("Too many menu images")
	ErrNoSuchBusiness        = errors.New("Business not found")
)
View Source
var (
	ErrInvalidItem        = errors.New("Invalid item")        // no such item or item already used
	ErrInvalidTransaction = errors.New("Invalid transaction") // transaction finished
	ErrItemBadCardId      = errors.New("Owned item vcard id does not match that of provided vcard")
	ErrInvalidAction      = errors.New("NoActionType is not a valid action when finalizing transaction")
	ErrInvalidActionSet   = errors.New("Invalid action set - does not match started transaction details")
)
View Source
var (
	ErrVirtualCardAlreadyExists = errors.New("Virtual card already exists")
	ErrNoSuchVirtualCard        = errors.New("Virtual card not found")
	ErrNoSuchItemDefinition     = errors.New("Item definition not found")
	ErrAboveMaxAmount           = errors.New("Attempt to buy item above max item amount")
	ErrNotEnoughPoints          = errors.New("Attempt to buy item with not enough points")
	ErrBeforeStartDate          = errors.New("Attempt to buy item before start date")
	ErrAfterEndDate             = errors.New("Attempt to buy item after end date")
	ErrUnavailableItem          = errors.New("Attempt to buy an unavailable item")
	ErrWithdrawnItem            = errors.New("Attempt to buy a withdrawn item")
	ErrItemCantBeReturned       = errors.New("Item can't be returned")
)
View Source
var CardTypes = []CardType{
	{
		PublicId: "biedronka",
		Name:     "Moja Biedronka",
		Code:     Ean13,
		ImageUrl: "biedronka.png",
	},
	{
		PublicId: "kaufland",
		Name:     "Kaufland Card",
		Code:     Qr,
		ImageUrl: "kaufland.png",
	},
}
View Source
var ErrCardAlreadyExists = errors.New("Card already exists")
View Source
var ErrCardDoesNotExist = errors.New("Card does not exist")
View Source
var ErrInvalidArgs = errors.New("Arguments passed to manager are invalid")
View Source
var ErrInvalidCardType = errors.New("Invalid card type")
View Source
var ErrInvalidItemDetails = errors.New("Invalid item details received")
View Source
var ErrItemAlreadyWithdrawn = errors.New("Item already withdrawn")
View Source
var ErrUnknownItem = errors.New("Item id is not recognized")

Functions

func InitUrls

func InitUrls(baseUrl string)

Types

type AuthManager

type AuthManager interface {
	// Creates a new user account from UserDetails struct. Returns a database object and, session token and
	// matching token secret for the user. Sends a verification email.
	Create(userDetails UserDetails) (*User, *Token, string, error)

	// Checks if email and password match any user. If yes, returns the database object and serssion token
	// for that user.
	Login(email string, password string) (*User, *Token, string, error)

	// Checks if token id and secret match any session token. If yes, invalidates the token.
	Logout(tokenId string, tokenSecret string) (*User, *Token, error)

	// Checks if token id and secret match any email token. If yes, invalidates the token and changes
	// EmailVerified in user's database object to true.
	ConfirmEmail(tokenId string, tokenSecret string) (*User, error)

	// Changes password of user, if oldPassword matches user.PasswordHash.
	ChangePassword(user *User, oldPassword string, newPassword string) (*User, error)

	// Changes email of user, if no other user has the same email. Changes user.EmailVerified to false,
	// sends a new verification email.
	ChangeEmail(user *User, newEmail string) (*User, error)
}

type AuthManagerImpl

type AuthManagerImpl struct {
	// contains filtered or unexported fields
}

func CreateAuthManagerImpl

func CreateAuthManagerImpl(baseServices BaseServices,
	emailService EmailService, tokenService TokenService,
	emailSubject string, emailBodyTemplate string) *AuthManagerImpl

func (*AuthManagerImpl) ChangeEmail

func (manager *AuthManagerImpl) ChangeEmail(user *User, newEmail string) (*User, error)

func (*AuthManagerImpl) ChangePassword

func (manager *AuthManagerImpl) ChangePassword(user *User, oldPassword string, newPassword string) (*User, error)

func (*AuthManagerImpl) ConfirmEmail

func (manager *AuthManagerImpl) ConfirmEmail(tokenId string, tokenSecret string) (*User, error)

func (*AuthManagerImpl) Create

func (manager *AuthManagerImpl) Create(userDetails UserDetails) (*User, *Token, string, error)

func (*AuthManagerImpl) Login

func (manager *AuthManagerImpl) Login(email string, password string) (*User, *Token, string, error)

func (*AuthManagerImpl) Logout

func (manager *AuthManagerImpl) Logout(tokenId string, tokenSecret string) (*User, *Token, error)

type BusinessDetails

type BusinessDetails struct {
	Name           string
	Description    string
	Address        string
	GPSCoordinates GPSCoordinates
	NIP            string
	KRS            string
	REGON          string
	OwnerName      string
}

type BusinessManager

type BusinessManager interface {
	Create(user *User, businessDetails *BusinessDetails) (*Business, error)
	ChangeDetails(business *Business, businessDetails *ChangeableBusinessDetails) (*Business, error)
	//explicitly requires user becuase fileStorageService requires user
	AddMenuImage(user *User, business *Business) (*MenuImage, error)
	RemoveMenuImage(menuImage *MenuImage) error

	//? not a fan
	Search(name *string, location *GPSCoordinates, proximityInMeters uint, offset uint, limit uint) ([]Business, error)
	GetById(businessId string, preloadDetails bool) (*Business, error)
}

type BusinessManagerImpl

type BusinessManagerImpl struct {
	// contains filtered or unexported fields
}

func CreateBusinessManagerImpl

func CreateBusinessManagerImpl(baseServices BaseServices, fileStorageService FileStorageService) *BusinessManagerImpl

func (*BusinessManagerImpl) AddMenuImage

func (manager *BusinessManagerImpl) AddMenuImage(user *User, business *Business) (*MenuImage, error)

func (*BusinessManagerImpl) ChangeDetails

func (manager *BusinessManagerImpl) ChangeDetails(business *Business, businessDetails *ChangeableBusinessDetails) (*Business, error)

func (*BusinessManagerImpl) Create

func (manager *BusinessManagerImpl) Create(user *User, businessDetails *BusinessDetails) (*Business, error)

func (*BusinessManagerImpl) GetById

func (manager *BusinessManagerImpl) GetById(businessId string, preloadDetails bool) (*Business, error)

func (*BusinessManagerImpl) RemoveMenuImage

func (manager *BusinessManagerImpl) RemoveMenuImage(menuImage *MenuImage) error

func (*BusinessManagerImpl) Search

func (manager *BusinessManagerImpl) Search(name *string, location *GPSCoordinates, proximityInMeters uint, offset uint, limit uint) ([]Business, error)

NOTE limit offset is not a very good pagination method https://www.citusdata.com/blog/2016/03/30/five-ways-to-paginate/

type CardType

type CardType struct {
	PublicId string
	Name     string
	Code     CodeType
	ImageUrl string
}

type ChangeableBusinessDetails

type ChangeableBusinessDetails struct {
	Name        *string
	Description *string
}

type CodeType

type CodeType string
const (
	Ean13 CodeType = "ean13"
	Qr    CodeType = "qr"
)

type ItemDefinitionManager

type ItemDefinitionManager interface {
	AddItem(user *User, business *Business, details *ItemDetails) (*ItemDefinition, error)
	ChangeItemDetails(item *ItemDefinition, details *ItemDetails) (*ItemDefinition, error)
	WithdrawItem(item *ItemDefinition) (*ItemDefinition, error)
	DeleteItem(item *ItemDefinition) error
	GetForBusiness(business *Business) ([]ItemDefinition, error)
}

type ItemDefinitionManagerImpl

type ItemDefinitionManagerImpl struct {
	// contains filtered or unexported fields
}

func CreateItemDefinitionManagerImpl

func CreateItemDefinitionManagerImpl(baseServices BaseServices, fileStorageService FileStorageService) *ItemDefinitionManagerImpl

func (*ItemDefinitionManagerImpl) AddItem

func (manager *ItemDefinitionManagerImpl) AddItem(user *User, business *Business, details *ItemDetails) (*ItemDefinition, error)

TODO add logs

func (*ItemDefinitionManagerImpl) ChangeItemDetails

func (manager *ItemDefinitionManagerImpl) ChangeItemDetails(item *ItemDefinition, details *ItemDetails) (*ItemDefinition, error)

func (*ItemDefinitionManagerImpl) DeleteItem

func (manager *ItemDefinitionManagerImpl) DeleteItem(item *ItemDefinition) error

func (*ItemDefinitionManagerImpl) GetForBusiness

func (manager *ItemDefinitionManagerImpl) GetForBusiness(business *Business) ([]ItemDefinition, error)

func (*ItemDefinitionManagerImpl) WithdrawItem

func (manager *ItemDefinitionManagerImpl) WithdrawItem(item *ItemDefinition) (*ItemDefinition, error)

type ItemDetails

type ItemDetails struct {
	Name        string
	Price       *uint
	Description string
	StartDate   *time.Time
	EndDate     *time.Time
	MaxAmount   *uint
	Available   *bool
}

type ItemWithAction

type ItemWithAction struct {
	Item   *OwnedItem
	Action ActionTypeEnum
}

TODO

type LocalCardDetails

type LocalCardDetails struct {
	Type string
	Code string
	Name string
}

type LocalCardManager

type LocalCardManager interface {
	Create(user *User, details LocalCardDetails) (*LocalCard, error)
	Remove(card *LocalCard) error
}

type LocalCardManagerImpl

type LocalCardManagerImpl struct {
	// contains filtered or unexported fields
}

func CreateLocalCardManagerImpl

func CreateLocalCardManagerImpl(baseServices BaseServices) *LocalCardManagerImpl

func (*LocalCardManagerImpl) Create

func (manager *LocalCardManagerImpl) Create(user *User, details LocalCardDetails) (*LocalCard, error)

func (*LocalCardManagerImpl) Remove

func (manager *LocalCardManagerImpl) Remove(card *LocalCard) error

type TransactionManager

type TransactionManager interface {
	Start(card *VirtualCard, items []OwnedItem) (*Transaction, error)
	Finalize(transaction *Transaction, items []ItemWithAction, points uint64) (*Transaction, error)
}

type TransactionManagerImpl

type TransactionManagerImpl struct {
	// contains filtered or unexported fields
}

func CreateTransactionManagerImpl

func CreateTransactionManagerImpl(baseServices BaseServices) *TransactionManagerImpl

func (*TransactionManagerImpl) Finalize

func (manager *TransactionManagerImpl) Finalize(transaction *Transaction, actions []ItemWithAction, points uint64) (*Transaction, error)

func (*TransactionManagerImpl) Start

func (manager *TransactionManagerImpl) Start(card *VirtualCard, items []OwnedItem) (*Transaction, error)

type UserDetails

type UserDetails struct {
	//FirstName string
	//LastName  string
	Email    string
	Password string
}

type VirtualCardManager

type VirtualCardManager interface {
	// Creates virtual card of business for user
	// businessId passed as string - caller is not required to have "access" to a business object
	Create(user *User, businessId string) (*VirtualCard, error)

	// Removes virtual card
	Remove(virtualCard *VirtualCard) error

	// act of desperation since accessors do not work as expected
	GetForUser(user *User, businessId string) (*VirtualCard, error)

	// Returns items owned by virtual card
	GetOwnedItems(virtualCard *VirtualCard) ([]OwnedItem, error)

	// Returns items owned by virtual card with PublicIds that match ids
	FilterOwnedItems(virtualCard *VirtualCard, ids []string) ([]OwnedItem, error)

	// Creates a new OwnedItem for virtual card if all conditions are met
	// (ex. virtualCard has enough points, item is still/already valid,
	// virtualCard has less items of this type than ItemDefinition.MaxAmount...)
	// itemDefinitionId passed as string - caller is not required to have "access" to the ItemDefinition object
	BuyItem(virtual *VirtualCard, itemDefinitionId string) (*OwnedItem, error)

	// Returns item - item is removed from virtualCard, points are returned to the card.
	// Fails if item was used.
	ReturnItem(ownedItem *OwnedItem) error
}

type VirtualCardManagerImpl

type VirtualCardManagerImpl struct {
	// contains filtered or unexported fields
}

func CreateVirtualCardManagerImpl

func CreateVirtualCardManagerImpl(baseServices BaseServices) *VirtualCardManagerImpl

func (*VirtualCardManagerImpl) BuyItem

func (manager *VirtualCardManagerImpl) BuyItem(virtualCard *VirtualCard, itemDefinitionId string) (*OwnedItem, error)

func (*VirtualCardManagerImpl) Create

func (manager *VirtualCardManagerImpl) Create(user *User, businessId string) (*VirtualCard, error)

func (*VirtualCardManagerImpl) FilterOwnedItems

func (manager *VirtualCardManagerImpl) FilterOwnedItems(virtualCard *VirtualCard, ids []string) ([]OwnedItem, error)

func (*VirtualCardManagerImpl) GetForUser

func (manager *VirtualCardManagerImpl) GetForUser(user *User, businessId string) (*VirtualCard, error)

func (*VirtualCardManagerImpl) GetOwnedItems

func (manager *VirtualCardManagerImpl) GetOwnedItems(virtualCard *VirtualCard) ([]OwnedItem, error)

idk if this is even necessary perhaps adding preloading functionality to accessors would work better

func (*VirtualCardManagerImpl) Remove

func (manager *VirtualCardManagerImpl) Remove(virtualCard *VirtualCard) error

func (*VirtualCardManagerImpl) ReturnItem

func (manager *VirtualCardManagerImpl) ReturnItem(ownedItem *OwnedItem) error

Directories

Path Synopsis
Package mock_managers is a generated GoMock package.
Package mock_managers is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL