Skip to main content

preference.go

preference.go - Overview

  1. Overview This file defines the core logic for managing preferences at both the organization and user levels. It provides functions for retrieving, updating, and sanitizing preference values, taking into account default values and scope restrictions.

  2. Detailed Documentation

type usecase

type usecase struct {
store preferencetypes.PreferenceStore
defaultMap map[string]preferencetypes.Preference
}
  • Purpose: usecase struct holds the dependencies required for preference management.
    • store: An interface for interacting with the preference storage.
    • defaultMap: A map of preference IDs to their default Preference values.

func NewPreference

func NewPreference(store preferencetypes.PreferenceStore, defaultMap map[string]preferencetypes.Preference) preference.Usecase
  • Purpose: Creates a new preference.Usecase instance.
  • Parameters:
    • store: preferencetypes.PreferenceStore - The preference storage implementation.
    • defaultMap: map[string]preferencetypes.Preference - A map containing default preferences.
  • Returns: preference.Usecase - A new preference usecase.

func (*usecase) GetOrgPreference

func (usecase *usecase) GetOrgPreference(ctx context.Context, preferenceID string, orgID string) (*preferencetypes.GettablePreference, error)
  • Purpose: Retrieves an organization-level preference. If no preference is found for the organization, the default value is returned.
  • Parameters:
    • ctx: context.Context - The context for the request.
    • preferenceID: string - The ID of the preference to retrieve.
    • orgID: string - The ID of the organization.
  • Returns: *preferencetypes.GettablePreference - The retrieved preference value, or the default if not found. Returns an error if the preference ID is invalid or not enabled for the organization scope.

func (*usecase) UpdateOrgPreference

func (usecase *usecase) UpdateOrgPreference(ctx context.Context, preferenceID string, preferenceValue interface{}, orgID string) error
  • Purpose: Updates an organization-level preference.
  • Parameters:
    • ctx: context.Context - The context for the request.
    • preferenceID: string - The ID of the preference to update.
    • preferenceValue: interface{} - The new value for the preference.
    • orgID: string - The ID of the organization.
  • Returns: error - An error if the preference ID is invalid, not enabled for the organization scope, the value is invalid, or there is an issue with storage.

func (*usecase) GetAllOrgPreferences

func (usecase *usecase) GetAllOrgPreferences(ctx context.Context, orgID string) ([]*preferencetypes.PreferenceWithValue, error)
  • Purpose: Retrieves all organization preferences along with their metadata.
  • Parameters:
    • ctx: context.Context - The context for the request.
    • orgID: string - The ID of the organization.
  • Returns: []*preferencetypes.PreferenceWithValue - A slice of preferences with their values. Returns an error if there is an issue with storage.

func (*usecase) GetUserPreference

func (usecase *usecase) GetUserPreference(ctx context.Context, preferenceID string, orgID string, userID string) (*preferencetypes.GettablePreference, error)
  • Purpose: Retrieves a user-level preference. It checks for user-specific preferences first, then falls back to organization-level preferences, and finally to the default value.
  • Parameters:
    • ctx: context.Context - The context for the request.
    • preferenceID: string - The ID of the preference to retrieve.
    • orgID: string - The ID of the organization.
    • userID: string - The ID of the user.
  • Returns: *preferencetypes.GettablePreference - The retrieved preference value, or the default if not found. Returns an error if the preference ID is invalid or not enabled for the user scope.

func (*usecase) UpdateUserPreference

func (usecase *usecase) UpdateUserPreference(ctx context.Context, preferenceID string, preferenceValue interface{}, userID string) error
  • Purpose: Updates a user-level preference.
  • Parameters:
    • ctx: context.Context - The context for the request.
    • preferenceID: string - The ID of the preference to update.
    • preferenceValue: interface{} - The new value for the preference.
    • userID: string - The ID of the user.
  • Returns: error - An error if the preference ID is invalid, not enabled for the user scope, the value is invalid, or there is an issue with storage.

func (*usecase) GetAllUserPreferences

func (usecase *usecase) GetAllUserPreferences(ctx context.Context, orgID string, userID string) ([]*preferencetypes.PreferenceWithValue, error)
  • Purpose: Retrieves all user preferences along with their metadata, considering both organization and user-specific settings.
  • Parameters:
    • ctx: context.Context - The context for the request.
    • orgID: string - The ID of the organization.
    • userID: string - The ID of the user.
  • Returns: []*preferencetypes.PreferenceWithValue - A slice of preferences with their values. Returns an error if there is an issue with storage.

Include in Getting Started: NO