Files
apskel-pos-backend/internal/repository/organization_setting_repository.go
T

42 lines
1.3 KiB
Go
Raw Normal View History

package repository
import (
"context"
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
)
// OrganizationSettingRepository reads organization_settings, the key-value settings
// that apply to every outlet of an organization (docs/prd-point-coin.md F2).
type OrganizationSettingRepository interface {
// Get returns the stored value of a key and whether one is stored. A key that has
// never been set is not an error: the caller falls back to the key's default.
Get(ctx context.Context, organizationID uuid.UUID, key string) (string, bool, error)
}
type organizationSettingRepository struct {
db *gorm.DB
}
func NewOrganizationSettingRepository(db *gorm.DB) OrganizationSettingRepository {
return &organizationSettingRepository{db: db}
}
func (r *organizationSettingRepository) Get(ctx context.Context, organizationID uuid.UUID, key string) (string, bool, error) {
var values []*string
err := DBFromContext(ctx, r.db).WithContext(ctx).
Table("organization_settings").
Where("organization_id = ? AND key = ?", organizationID, key).
Limit(1).
Pluck("value", &values).Error
if err != nil {
return "", false, fmt.Errorf("failed to get organization setting %s: %w", key, err)
}
if len(values) == 0 || values[0] == nil {
return "", false, nil
}
return *values[0], true, nil
}