-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3869 from rubyforgood/event-source
Initial event sourcing implementation
- Loading branch information
Showing
56 changed files
with
1,180 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class AdjustmentEvent < Event | ||
# @param adjustment [Adjustment] | ||
def self.publish(adjustment) | ||
create( | ||
eventable: adjustment, | ||
organization_id: adjustment.organization_id, | ||
event_time: Time.zone.now, | ||
data: EventTypes::InventoryPayload.new( | ||
items: EventTypes::EventLineItem.from_line_items(adjustment.line_items, to: adjustment.storage_location_id) | ||
) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class AuditEvent < Event | ||
serialize :data, EventTypes::StructCoder.new(EventTypes::AuditPayload) | ||
|
||
# @param audit [Audit] | ||
def self.publish(audit) | ||
create( | ||
eventable: audit, | ||
organization_id: audit.organization_id, | ||
event_time: Time.zone.now, | ||
data: EventTypes::AuditPayload.new( | ||
storage_location_id: audit.storage_location_id, | ||
items: EventTypes::EventLineItem.from_line_items(audit.line_items, to: audit.storage_location_id) | ||
) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class DistributionDestroyEvent < Event | ||
# @param distribution [Distribution] | ||
def self.publish(distribution) | ||
create( | ||
eventable: distribution, | ||
organization_id: distribution.organization_id, | ||
event_time: Time.zone.now, | ||
data: EventTypes::InventoryPayload.new( | ||
items: EventTypes::EventLineItem.from_line_items(distribution.line_items, to: distribution.storage_location_id) | ||
) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class DistributionEvent < Event | ||
# @param distribution [Distribution] | ||
def self.publish(distribution) | ||
create( | ||
eventable: distribution, | ||
organization_id: distribution.organization_id, | ||
event_time: Time.zone.now, | ||
data: EventTypes::InventoryPayload.new( | ||
items: EventTypes::EventLineItem.from_line_items(distribution.line_items, from: distribution.storage_location_id) | ||
) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class DonationDestroyEvent < Event | ||
# @param donation [Donation] | ||
def self.publish(donation) | ||
create( | ||
eventable: donation, | ||
organization_id: donation.organization_id, | ||
event_time: Time.zone.now, | ||
data: EventTypes::InventoryPayload.new( | ||
items: EventTypes::EventLineItem.from_line_items(donation.line_items, from: donation.storage_location_id) | ||
) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class DonationEvent < Event | ||
# @param donation [Donation] | ||
def self.publish(donation) | ||
create( | ||
eventable: donation, | ||
organization_id: donation.organization_id, | ||
event_time: Time.zone.now, | ||
data: EventTypes::InventoryPayload.new( | ||
items: EventTypes::EventLineItem.from_line_items(donation.line_items, to: donation.storage_location_id) | ||
) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Types | ||
include Dry.Types() | ||
end | ||
|
||
module EventTypes | ||
class AuditPayload < InventoryPayload | ||
attribute :storage_location_id, Types::Integer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Types | ||
include Dry.Types() | ||
end | ||
|
||
module EventTypes | ||
class EventItem < Dry::Struct | ||
transform_keys(&:to_sym) | ||
attribute :item_id, Types::Integer | ||
attribute :quantity, Types::Integer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Types | ||
include Dry.Types() | ||
end | ||
|
||
module EventTypes | ||
class EventLineItem < Dry::Struct | ||
transform_keys(&:to_sym) | ||
attribute :quantity, Types::Integer # can be positive or negative | ||
attribute :item_id, Types::Integer | ||
attribute :item_value_in_cents, Types::Integer | ||
attribute :from_storage_location, Types::Integer.optional | ||
attribute :to_storage_location, Types::Integer.optional | ||
|
||
# @param line_item [LineItem] | ||
# @param from [Integer] | ||
# @param to [Integer] | ||
# @return [EventLineItem] | ||
def self.from_line_item(line_item, from: nil, to: nil) | ||
new( | ||
quantity: line_item.quantity, | ||
item_id: line_item.item_id, | ||
item_value_in_cents: line_item.item.value_in_cents, | ||
from_storage_location: from, | ||
to_storage_location: to | ||
) | ||
end | ||
|
||
# @param line_item [Array<LineItem>] | ||
# @param from [Integer] | ||
# @param to [Integer] | ||
# @return [Array<EventLineItem>] | ||
def self.from_line_items(line_items, from: nil, to: nil) | ||
line_items.map { |i| from_line_item(i, from: from, to: to) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Types | ||
include Dry.Types() | ||
end | ||
|
||
module EventTypes | ||
class EventStorageLocation < Dry::Struct | ||
transform_keys(&:to_sym) | ||
|
||
attribute :id, Types::Integer | ||
attribute :items, Types::Hash.map(Types::Coercible::Integer, EventTypes::EventItem) | ||
|
||
# @param storage_location [StorageLocation] | ||
# @return [EventTypes::EventStorageLocation] | ||
def self.from(storage_location) | ||
new(id: storage_location.id, items: {}) | ||
end | ||
|
||
def reset! | ||
items.clear | ||
end | ||
|
||
# @param item_id [Integer] | ||
# @param quantity [Integer] | ||
def set_inventory(item_id, quantity) | ||
items[item_id] = EventTypes::EventItem.new(item_id: item_id, quantity: quantity) | ||
end | ||
|
||
# @param item_id [Integer] | ||
# @param quantity [Integer] | ||
# @param validate [Boolean] | ||
def reduce_inventory(item_id, quantity, validate: true) | ||
if validate | ||
if items[item_id].nil? | ||
raise "Item #{item_id} not found in storage location #{id}" | ||
end | ||
if items[item_id].quantity < quantity | ||
raise "Could not reduce quantity by #{quantity} for item #{item_id} in storage location #{id} - current quantity is #{items[item_id].quantity}" | ||
end | ||
end | ||
current_quantity = items[item_id]&.quantity || 0 | ||
items[item_id] = EventTypes::EventItem.new(item_id: item_id, quantity: current_quantity - quantity) | ||
end | ||
|
||
# @param item_id [Integer] | ||
# @param quantity [Integer] | ||
def add_inventory(item_id, quantity) | ||
current_quantity = items[item_id]&.quantity || 0 | ||
items[item_id] = EventTypes::EventItem.new(item_id: item_id, quantity: current_quantity + quantity) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Types | ||
include Dry.Types() | ||
end | ||
|
||
module EventTypes | ||
class Inventory < Dry::Struct | ||
transform_keys(&:to_sym) | ||
attribute :organization_id, Types::Integer | ||
attribute :storage_locations, Types::Hash.map(Types::Coercible::Integer, EventTypes::EventStorageLocation) | ||
|
||
# @param organization_id [Integer] | ||
# @return [EventTypes::Inventory] | ||
def self.from(organization_id) | ||
org = Organization.find(organization_id) | ||
new(organization_id: organization_id, | ||
storage_locations: org.storage_locations.map { |s| [s.id, EventTypes::EventStorageLocation.from(s)] }.to_h) | ||
end | ||
|
||
def move_item(item_id:, quantity:, from_location: nil, to_location: nil, validate: true) | ||
if from_location | ||
if storage_locations[from_location].nil? && validate | ||
raise "Storage location #{from_location} not found!" | ||
end | ||
storage_locations[from_location] ||= EventTypes::EventStorageLocation.new(id: from_location, items: {}) | ||
storage_locations[from_location].reduce_inventory(item_id, quantity, validate: validate) | ||
end | ||
if to_location | ||
storage_locations[to_location].add_inventory(item_id, quantity) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Types | ||
include Dry.Types() | ||
end | ||
|
||
module EventTypes | ||
class InventoryPayload < Dry::Struct | ||
transform_keys(&:to_sym) | ||
|
||
attribute :items, Types::Array.of(EventTypes::EventLineItem) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# see https://discourse.dry-rb.org/t/dry-struct-and-serializer/1517/2 | ||
module EventTypes | ||
class StructCoder | ||
attr_reader :struct | ||
|
||
def initialize(struct) | ||
@struct = struct | ||
end | ||
|
||
def dump(s) | ||
s.to_hash | ||
end | ||
|
||
def load(h) | ||
return if h.nil? | ||
struct.new(h.with_indifferent_access) | ||
end | ||
end | ||
end |
Oops, something went wrong.