refactor: unit testable units and cleaner dep injection
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package livestore
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cloud.google.com/go/firestore"
|
||||
)
|
||||
|
||||
//go:generate mockgen -source ./membershippublisher.go -destination ./mocks/membershippublisher.go
|
||||
|
||||
// MembershipPublisher publishes network membership changes to the live store
|
||||
// (Firestore) that clients subscribe to. Postgres remains the source of truth;
|
||||
// the membership reconciler heals any drift, so callers may log and ignore
|
||||
// publish failures.
|
||||
type MembershipPublisher interface {
|
||||
Add(ctx context.Context, humanId, networkID string) error
|
||||
Remove(ctx context.Context, humanId, networkID string) error
|
||||
}
|
||||
|
||||
func NewMembershipPublisher(fs *firestore.Client) MembershipPublisher {
|
||||
return &firestoreMembershipPublisher{fs: fs}
|
||||
}
|
||||
|
||||
type firestoreMembershipPublisher struct {
|
||||
fs *firestore.Client
|
||||
}
|
||||
|
||||
func (p *firestoreMembershipPublisher) Add(ctx context.Context, humanId, networkID string) error {
|
||||
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
|
||||
"networks": firestore.ArrayUnion(networkID),
|
||||
"updated_at": firestore.ServerTimestamp,
|
||||
}, firestore.MergeAll)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *firestoreMembershipPublisher) Remove(ctx context.Context, humanId, networkID string) error {
|
||||
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
|
||||
"networks": firestore.ArrayRemove(networkID),
|
||||
"updated_at": firestore.ServerTimestamp,
|
||||
}, firestore.MergeAll)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user