customerdb

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const CustomerCollection = "customers"

CustomerCollection is the MongoDB collection name that holds all customers.

Variables

View Source
var (
	ErrSourceRegistered   = errors.New("source already registered")
	ErrEmptySourceName    = errors.New("source name is empty")
	ErrDeleteNotSupported = errors.New("source does not support deletes")
	ErrUnknownSource      = errors.New("unknown source")
)

Common errors when working with customer sources.

View Source
var DefaultSourceManager = NewSourceManager()

DefaultSourceManager is the default customer source manager.

View Source
var (
	// ErrNotFound is returned when the requested document daoes not exist.
	ErrNotFound = errors.New("document not found")
)

Functions

This section is empty.

Types

type Customer

type Customer struct {
	ID                  primitive.ObjectID `bson:"_id,omitempty"`
	CustomerID          string             `bson:"cid,omitempty"`
	Group               string             `bson:"group,omitempty"`
	Name                string             `bson:"name,omitempty"`
	Firstname           string             `bson:"firstname,omitempty"`
	Title               string             `bson:"title,omitempty"`
	Street              string             `bson:"street,omitempty"`
	CityCode            int                `bson:"cityCode,omitempty"`
	City                string             `bson:"city,omitempty"`
	PhoneNumbers        []string           `bson:"phoneNumbers,omitempty"`
	MailAddresses       []string           `bson:"mailAddresses,omitempty"`
	NameMetaphone       string             `bson:"nameMetaphone,omitempty"`
	VaccinationReminder bool               `bson:"vaccinationReminder,omitempty"`

	// CreateAt holds the time this customer has first been imported.
	CreatedAt time.Time

	// ModifiedAt holds the last time this customer has been updated
	// or re-imported
	ModifiedAt time.Time

	// LinkedTo may be set to link customer or contact data from multiple
	// sources.
	LinkedTo string `bson:"linkedTo,omitempty"`

	// Metadata holds additional metadata for the customer.
	// It is not part of the hash calculation.
	Metadata map[string]interface{} `bson:"metadata,omitempty"`
	Source   string                 `bson:"customerSource,omitempty"`
}

Customer defines the customer record.

func (*Customer) Hash

func (cu *Customer) Hash() string

Hash returns a hash of the customer.

type Database

type Database interface {
	// CreateCustomer creates a new customer.
	CreateCustomer(ctx context.Context, cu *Customer) error

	// UpdateCustomer replaces an existing customer
	UpdateCustomer(ctx context.Context, cu *Customer) error

	// CustomerByCID returns the customer by it's customer-id
	CustomerByCID(ctx context.Context, source string, cid string) (*Customer, error)

	// FilterCustomer filters all customers according to filter.
	FilterCustomer(ctx context.Context, filter bson.M, textScore bool) ([]*Customer, error)

	// FuzzySearchName searches for a customer by name usign fuzzy-search
	FuzzySearchName(ctx context.Context, name string) ([]*Customer, error)

	// SearchCustomerByName searches for all customers that matches
	// name.
	SearchCustomerByName(ctx context.Context, name string) ([]*Customer, error)

	// DeleteCustomer deletes the customer identified by source and cid.
	DeleteCustomer(ctx context.Context, id string) error

	// Cursor returns a cursor for all objects in filter.
	Cursor(ctx context.Context, filter bson.M) (*mongo.Cursor, error)

	Stats() *dbutils.Stats
}

Database encapsulates access to the MongoDB database.

func New

func New(ctx context.Context, url, dbName string) (Database, error)

New connects to the MongoDB server at URL and returns a new database interface.

func NewWithClient

func NewWithClient(ctx context.Context, dbName string, client *mongo.Client) (Database, error)

NewWithClient is like New but uses an already existing mongodb client.

type Source

type Source struct {
	// Name is the name of the customer source and should be set as the "Source"
	// field on each customer record created by this source.
	Name string

	// Description is a human readable description of the source.
	Description string

	// Metadata holds customer source metadata that is serialized
	// as JSON via the customerapi. Values must not change after
	// the source has been registered.
	Metadata map[string]interface{}

	// DeleteFunc should delete the customer from source. Upon successful
	// deletion the customer is also deleted from the internal database.
	DeleteFunc func(ctx context.Context, cus *Customer) error

	// UpdateFunc should update the representation of cus in the source.
	UpdateFunc func(ctx context.Context, cus *Customer) error

	// CreateFunc should create a new customer in source that mirrors the
	// data stored in cus. cus is a new customer object that is not yet
	// associated with any other customer source. CreateFunc should update
	// the customer metadata as it needs. Upon successful return
	// cus is actually created by the database inside the local collection.
	// Note that cus does not yet have an ID property specified.
	CreateFunc func(ctx context.Context, cus *Customer) error
}

Source describes a source of customer records. They are typically registered by importers and allow to further interact with the third-party store (where supported).

type SourceManager

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

SourceManager manages registered customer sources.

func NewSourceManager

func NewSourceManager() *SourceManager

NewSourceManager returns a new source manager.

func (*SourceManager) Delete

func (mng *SourceManager) Delete(ctx context.Context, cus *Customer) error

Delete tries to delete the customer from it's origin source. If no customer source is specified, the source does not exist or does not support deletes (i.e. DeleteFunc is unset) an error is returned. Otherwise, the result of source.DeleteFunc is returned.

func (*SourceManager) ListSources

func (mng *SourceManager) ListSources() []Source

ListSources returns a list of customer sources.

func (*SourceManager) Register

func (mng *SourceManager) Register(s Source) error

Register registers the customer source s at mng. It is an error if the source name is empty of if a source with the same name is already registered.

Jump to

Keyboard shortcuts

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