Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial event sourcing implementation #3869

Merged
merged 26 commits into from
Oct 15, 2023
Merged

Initial event sourcing implementation #3869

merged 26 commits into from
Oct 15, 2023

Conversation

dorner
Copy link
Collaborator

@dorner dorner commented Sep 4, 2023

This PR shows how we might implement an event sourcing pattern.

The overall approach is as follows:

  • Single Table Inherited Event model with an organization_id.
  • Use dry-structs to model the payloads for the events.
  • Single "aggregate" service class to process events in order and result in an Inventory instance that indicates all inventory for all items across storage locations.
  • Pass a validate method into the event processing to indicate if we should apply business logic rules. By default we should only validate when the event is being created. Otherwise we might end up in a bad state when we change business logic rules after the fact.

Lots of next steps:

  • Implement a check when an event is being created (via validate: true)
  • Provide a method to check the aggregate against the current inventory for the organization
  • Integrate the check in some way (error on controller perhaps)
  • Notify (Slack?) when the check fails (i.e. "regular" model passes but the event source fails).

Once that's in place, we can leave it there for a while until we validate the quality of the data.

@dorner dorner marked this pull request as ready for review September 22, 2023 20:36
# Conflicts:
#	Gemfile
#	app/controllers/adjustments_controller.rb
#	app/services/adjustment_create_service.rb
#	db/schema.rb
@@ -40,6 +40,7 @@ def finalize
ActiveRecord::Base.transaction do
@audit.storage_location.increase_inventory increasing_adjustment
@audit.storage_location.decrease_inventory decreasing_adjustment
AuditEvent.publish(@audit)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried that the @audit.split_differences routine does weird things, should this event record be above the split_difference, or even higher up than that near the @audit.save?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in a transaction, so it doesn't really matter. Either all of the records will be created or none of them will.

event_time: Time.zone.now,
data: EventTypes::InventoryPayload.new(
items: EventTypes::EventLineItem.from_line_items(adjustment.line_items, to: adjustment.storage_location_id)
).as_json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do some of these have .as_json and others don't?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which ones don't?

@dorner dorner changed the title Draft PR for event sourcing Initial event sourcing implementation Sep 27, 2023
@awwaiid awwaiid merged commit a0f48a9 into main Oct 15, 2023
12 checks passed
@awwaiid awwaiid deleted the event-source branch October 15, 2023 14:29
@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

26 similar comments
@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@github-actions
Copy link
Contributor

@dorner: Your PR/Issue Initial event sourcing implementation is part of today's Human Essentials production release: 2023.10.15.
Thank you very much for your contribution!

@dorner dorner mentioned this pull request Oct 15, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants