package demoseed import ( "context" "errors" "fmt" "lincodex/backend/internal/port/outbound" ) // ResetVerdict says whether the operator tooling may discard the stand's // database to rebuild the demo world. type ResetVerdict string const ( // ResetAllowedEmpty is a stand without any account: nothing to lose. ResetAllowedEmpty ResetVerdict = "empty" // ResetAllowedSynthetic is a seeded stand whose every account is a // synthetic demo account. ResetAllowedSynthetic ResetVerdict = "synthetic" // ResetRefusedOrdinary is a stand that holds at least one account outside // the synthetic domain. Such an account is ordinary data, and the reset // never deletes it. ResetRefusedOrdinary ResetVerdict = "ordinary_accounts" ) // Allowed reports whether the verdict lets the reset proceed. func (verdict ResetVerdict) Allowed() bool { return verdict == ResetAllowedEmpty || verdict == ResetAllowedSynthetic } // ResetCheck is the read-only answer of CheckReset. It holds counts only. type ResetCheck struct { Verdict ResetVerdict Census outbound.DemoAccountCensus } // DecideReset applies the reset rule to an account census: a database may be // discarded only when it has no account at all or only synthetic demo // accounts. func DecideReset(census outbound.DemoAccountCensus) ResetVerdict { switch { case census.Total == 0: return ResetAllowedEmpty case census.Ordinary() == 0: return ResetAllowedSynthetic default: return ResetRefusedOrdinary } } // Inspector reads the account census and the inventory of a stand. It // changes nothing. type Inspector struct { store outbound.DemoSeedStore } // NewInspector fails without a store. func NewInspector(store outbound.DemoSeedStore) (*Inspector, error) { if store == nil { return nil, errors.New("demoseed.NewInspector - store must be set") } return &Inspector{store: store}, nil } // CheckReset counts the accounts and returns the verdict. func (inspector *Inspector) CheckReset(ctx context.Context) (ResetCheck, error) { census, err := inspector.store.CountAccounts(ctx, SyntheticDomain) if err != nil { return ResetCheck{}, fmt.Errorf("Inspector.CheckReset - count accounts: %w", err) } return ResetCheck{Verdict: DecideReset(census), Census: census}, nil } // Inventory returns the counts of the seed's records. func (inspector *Inspector) Inventory(ctx context.Context) (outbound.DemoInventory, error) { inventory, err := inspector.store.Inventory(ctx, SyntheticDomain, []string{string(FullCategory), TrainingCategory}) if err != nil { return outbound.DemoInventory{}, fmt.Errorf("Inspector.Inventory - count records: %w", err) } return inventory, nil }