devices

package
v1.44.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = errors.New("record not found")
	ErrInvalidFilter = errors.New("invalid filter")
	ErrMoreThanOne   = errors.New("more than one record")
)
View Source
var (
	ErrInvalidUser = errors.New("invalid user")
)

Functions

func Migrate added in v1.44.0

func Migrate(db *gorm.DB) error

func Module

func Module() fx.Option

Types

type Config

type Config struct {
}

type Device added in v1.44.0

type Device struct {
	DeviceInput

	LastSeen  time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
}

func (Device) IsEmpty added in v1.44.0

func (d Device) IsEmpty() bool

type DeviceInfo added in v1.44.0

type DeviceInfo struct {
	DeviceUpdate

	Name *string
}

type DeviceInput added in v1.44.0

type DeviceInput struct {
	DeviceInfo

	ID     string
	UserID string

	AuthToken string `json:"-"`
}

type DeviceModel added in v1.44.0

type DeviceModel struct {
	models.SoftDeletableModel

	ID        string  `gorm:"primaryKey;type:char(21)"`
	Name      *string `gorm:"type:varchar(128)"`
	AuthToken string  `gorm:"not null;uniqueIndex;type:char(21)"`
	PushToken *string `gorm:"type:varchar(256)"`

	LastSeen time.Time `gorm:"not null;autocreatetime:false;default:CURRENT_TIMESTAMP(3);index:idx_devices_last_seen"`

	UserID string `gorm:"not null;type:varchar(32)"`

	SimCards datatypes.JSONSlice[simCardModel] `gorm:"serializer:json;type:json"`
}

func (*DeviceModel) TableName added in v1.44.0

func (*DeviceModel) TableName() string

type DeviceUpdate added in v1.44.0

type DeviceUpdate struct {
	PushToken *string
	SimCards  []SimCard
}

type Repository added in v1.34.0

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

func NewRepository added in v1.34.0

func NewRepository(db *gorm.DB) *Repository

func (*Repository) Cleanup added in v1.34.0

func (r *Repository) Cleanup(ctx context.Context, until time.Time) (int64, error)

func (*Repository) Exists added in v1.34.0

func (r *Repository) Exists(ctx context.Context, filters ...SelectFilter) (bool, error)

Exists checks if there exists a device with the given filters.

If the device does not exist, it returns false and nil error. If there is an error during the query, it returns false and the error. Otherwise, it returns true and nil error.

func (*Repository) Get added in v1.34.0

func (r *Repository) Get(ctx context.Context, filter ...SelectFilter) (*Device, error)

func (*Repository) Insert added in v1.34.0

func (r *Repository) Insert(ctx context.Context, device DeviceInput) (*Device, error)

func (*Repository) Remove added in v1.34.0

func (r *Repository) Remove(ctx context.Context, filter ...SelectFilter) error

func (*Repository) Select added in v1.34.0

func (r *Repository) Select(ctx context.Context, filter ...SelectFilter) ([]Device, error)

func (*Repository) SetLastSeen added in v1.34.0

func (r *Repository) SetLastSeen(ctx context.Context, id string, lastSeen time.Time) error

func (*Repository) Update added in v1.44.0

func (r *Repository) Update(ctx context.Context, id string, device DeviceUpdate) error

type SelectFilter

type SelectFilter func(*selectFilter)

func ActiveWithin added in v1.25.0

func ActiveWithin(duration time.Duration) SelectFilter

func WithID

func WithID(id string) SelectFilter

func WithToken

func WithToken(token string) SelectFilter

func WithUserID

func WithUserID(userID string) SelectFilter

type Service

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

func NewService

func NewService(
	config Config,
	devices *Repository,
	idGen db.IDGen,
	logger *zap.Logger,
) *Service

func (*Service) Exists added in v1.22.0

func (s *Service) Exists(ctx context.Context, userID string, filter ...SelectFilter) (bool, error)

Exists checks if there exists a device that matches the provided filters.

If the device does not exist, it returns false and nil error. If there is an error during the query, it returns false and the error. Otherwise, it returns true and nil error.

func (*Service) Get

func (s *Service) Get(ctx context.Context, userID string, filter ...SelectFilter) (*Device, error)

Get returns a single device based on the provided filters for a specific user. It ensures that the filter includes the user's ID. If no device matches the criteria, it returns ErrNotFound. If more than one device matches, it returns ErrMoreThanOne.

func (*Service) GetAny added in v1.34.3

func (s *Service) GetAny(ctx context.Context, userID string, deviceID string, duration time.Duration) (*Device, error)

func (*Service) GetByToken added in v1.18.0

func (s *Service) GetByToken(ctx context.Context, token string) (*Device, error)

GetByToken returns a device by token.

This method is used to retrieve a device by its auth token. If the device does not exist, it returns ErrNotFound.

func (*Service) Insert

func (s *Service) Insert(ctx context.Context, userID string, device DeviceInfo) (*Device, error)

func (*Service) Remove added in v1.18.0

func (s *Service) Remove(ctx context.Context, userID string, filter ...SelectFilter) error

Remove removes devices for a specific user that match the provided filters. It ensures that the filter includes the user's ID.

func (*Service) Select

func (s *Service) Select(ctx context.Context, userID string, filter ...SelectFilter) ([]Device, error)

Select returns a list of devices for a specific user that match the provided filters.

func (*Service) SetLastSeen added in v1.30.0

func (s *Service) SetLastSeen(ctx context.Context, batch map[string]time.Time) error

func (*Service) Update added in v1.44.0

func (s *Service) Update(ctx context.Context, id string, device DeviceUpdate) error

type SimCard added in v1.44.0

type SimCard struct {
	SlotIndex   int // Zero-based index of the physical SIM slot (0, 1, ...).
	SimNumber   int // One-based number used by the application.
	PhoneNumber *string
	CarrierName *string
	ICCID       *string
}

Jump to

Keyboard shortcuts

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