Added a ClassificationsUpdated event
SaveOpportunities now reports the classification codes a save brings into
use (AddedClassifications, in full) and takes out of use
(RemovedClassificationIDs), by comparing the codes in use before and after
the save inside its transaction. The fetcher publishes them as a
ClassificationsUpdated event, {"added":[codes],"removed":[ids]}, never null,
before the save's OpportunitiesUpdated, so clients can keep their list of
classifications without asking the API again.
This commit is contained in:
1 parent
aa06b94870
commit
51d6470649
6 files changed
+287
-3
No files matched your search
@@ -39,8 +39,10 @@ func WithStore(opportunityStore *store.Store) Option {
|
||||
}
|
||||
|
||||
// WithEvents makes the workers publish an events.OpportunitiesUpdated event
|
||||
// to bus whenever a save stores new opportunities or new releases. It needs
|
||||
// WithStore: without a store nothing is saved, so nothing is published.
|
||||
// to bus whenever a save stores new opportunities or new releases, and an
|
||||
// events.ClassificationsUpdated event when it changes the classification
|
||||
// codes in use. It needs WithStore: without a store nothing is saved, so
|
||||
// nothing is published.
|
||||
func WithEvents(bus *events.Bus) Option {
|
||||
return func(f *Fetcher) {
|
||||
f.events = bus
|
||||
@@ -235,11 +237,45 @@ func (f *Fetcher) save(
|
||||
"new_releases", result.NewReleases,
|
||||
)
|
||||
|
||||
f.publishClassificationChanges(result)
|
||||
f.publishUpdates(opportunities, result.OpportunityResults)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// publishClassificationChanges publishes an events.ClassificationsUpdated
|
||||
// event for the classification codes a save, with result, brought into or
|
||||
// out of use, if it changed any and a bus was given WithEvents. It's
|
||||
// published before the save's events.OpportunitiesUpdated, so a client
|
||||
// knows a new code before an opportunity that has it.
|
||||
func (f *Fetcher) publishClassificationChanges(
|
||||
result store.SaveMultipleResult,
|
||||
) {
|
||||
if f.events == nil {
|
||||
return
|
||||
}
|
||||
if len(result.AddedClassifications) == 0 &&
|
||||
len(result.RemovedClassificationIDs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
payload := events.ClassificationsUpdatedPayload{
|
||||
Added: result.AddedClassifications,
|
||||
Removed: result.RemovedClassificationIDs,
|
||||
}
|
||||
if payload.Added == nil {
|
||||
payload.Added = []types.ClassificationCode{}
|
||||
}
|
||||
if payload.Removed == nil {
|
||||
payload.Removed = []int64{}
|
||||
}
|
||||
|
||||
f.events.Publish(events.Event{
|
||||
Type: events.ClassificationsUpdated,
|
||||
Payload: payload,
|
||||
})
|
||||
}
|
||||
|
||||
// publishUpdates publishes an events.OpportunitiesUpdated event for the
|
||||
// opportunities that results, from saving them, show were new or gained new
|
||||
// releases, if there were any and a bus was given WithEvents. An opportunity
|
||||
|
||||
Reference in new issue
Block a user