2025-01-13 18:26:27 +01:00
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-01-13 21:19:34 +01:00
|
|
|
"fmt"
|
2025-01-13 18:26:27 +01:00
|
|
|
|
|
|
|
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
|
|
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO: consolidate with loaded flags
|
|
|
|
accountCreatedFlag = 9
|
|
|
|
)
|
|
|
|
|
|
|
|
// handle custodial registration.
|
|
|
|
//
|
|
|
|
// TODO: implement account created in userstore instead, so that
|
|
|
|
// the need for persister and state use here is eliminated (it
|
|
|
|
// introduces concurrency risks)
|
2025-01-13 21:19:34 +01:00
|
|
|
func (eh *EventsUpdater) handleCustodialRegistration(ctx context.Context, ev any) error {
|
|
|
|
o, ok := ev.(*apievent.EventCustodialRegistration)
|
|
|
|
if !ok {
|
|
|
|
fmt.Errorf("invalid event for custodial registration")
|
|
|
|
}
|
|
|
|
return eh.HandleCustodialRegistration(ctx, o)
|
|
|
|
}
|
|
|
|
|
2025-01-13 22:39:15 +01:00
|
|
|
func (eu *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *apievent.EventCustodialRegistration) error {
|
|
|
|
pe, userStore, err := eu.getStore(ctx)
|
2025-01-13 18:26:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-01-13 22:39:15 +01:00
|
|
|
identity, err := store.IdentityFromAddress(ctx, userStore, ev.Account)
|
2025-01-13 18:26:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-01-13 22:39:15 +01:00
|
|
|
err = pe.Load(identity.SessionId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
st := pe.GetState()
|
2025-01-13 18:26:27 +01:00
|
|
|
st.SetFlag(accountCreatedFlag)
|
2025-01-13 22:39:15 +01:00
|
|
|
return pe.Save(identity.SessionId)
|
2025-01-13 18:26:27 +01:00
|
|
|
}
|