package store import ( "context" model "git.sendico.io/tech/sxchnge/product/twofactor/model" db "git.sendico.io/tech/sxchnge/shared/database" deps "git.sendico.io/tech/sxchnge/shared/database/dependencies" ri "git.sendico.io/tech/sxchnge/shared/database/repository/index" "git.sendico.io/tech/sxchnge/shared/database/template" mlogger "git.sendico.io/tech/sxchnge/shared/logging" "go.mongodb.org/mongo-driver/v2/bson" ) type Store struct { profiles *template.DBImp[*model.Profile] challenges *template.DBImp[*model.Record] } func Open(logger mlogger.Logger, factory db.Factory) (*Store, error) { d, err := deps.ResolveDependencies(factory) if err != nil { return nil, err } s := &Store{profiles: template.Create[*model.Profile](logger, "two_factor_profiles", d.Database), challenges: template.Create[*model.Record](logger, "two_factor_challenges", d.Database)} retention := int32(86400) err = s.challenges.Repository.CreateIndex(&ri.Definition{Name: "two_factor_expiry", Keys: []ri.Key{{Field: "expiresAt", Sort: ri.Asc}}, TTL: &retention}) if err != nil { return nil, err } return s, nil } func (s *Store) Profile(ctx context.Context, id bson.ObjectID) (*model.Profile, error) { p := &model.Profile{} err := s.profiles.Get(ctx, id, p) return p, err } func (s *Store) CreateProfile(ctx context.Context, p *model.Profile) error { return s.profiles.Create(ctx, p) } func (s *Store) UpdateProfile(ctx context.Context, p *model.Profile) error { return s.profiles.Update(ctx, p) } func (s *Store) Challenge(ctx context.Context, id bson.ObjectID) (*model.Record, error) { r := &model.Record{} err := s.challenges.Get(ctx, id, r) return r, err } func (s *Store) CreateChallenge(ctx context.Context, r *model.Record) error { return s.challenges.Create(ctx, r) } func (s *Store) UpdateChallenge(ctx context.Context, r *model.Record) error { return s.challenges.Update(ctx, r) }