Skip to content

Commit 611377d

Browse files
Persist store_id in TimePromotions read model to filter it later.
1 parent 41a5aef commit 611377d

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module TimePromotions
2+
class AssignStoreToTimePromotion
3+
def call(event)
4+
TimePromotion.find(event.data.fetch(:time_promotion_id)).update!(store_id: event.data.fetch(:store_id))
5+
end
6+
end
7+
end

rails_application/app/read_models/time_promotions/configuration.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@ class TimePromotion < ApplicationRecord
55
scope :current, -> { where("start_time < ? AND end_time > ?", Time.current, Time.current) }
66
end
77

8+
private_constant :TimePromotion
9+
10+
def self.time_promotions_for_store(store_id)
11+
TimePromotion.where(store_id: store_id)
12+
end
13+
14+
def self.current_time_promotions_for_store(store_id)
15+
TimePromotion.where(store_id: store_id).current
16+
end
17+
818
class Configuration
919
def call(event_store)
1020
event_store.subscribe(CreateTimePromotion, to: [Pricing::TimePromotionCreated])
21+
event_store.subscribe(AssignStoreToTimePromotion.new, to: [Stores::TimePromotionRegistered])
1122
end
1223
end
1324
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddStoreIdToTimePromotions < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :time_promotions, :store_id, :uuid
4+
end
5+
end

rails_application/db/schema.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_11_01_151254) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_12_06_153816) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pgcrypto"
@@ -27,6 +27,15 @@
2727
t.datetime "updated_at", null: false
2828
end
2929

30+
create_table "authorizations", force: :cascade do |t|
31+
t.uuid "resource_id"
32+
t.uuid "store_id"
33+
t.datetime "created_at", null: false
34+
t.datetime "updated_at", null: false
35+
t.index ["resource_id", "store_id"], name: "index_authorizations_on_resource_id_and_store_id"
36+
t.index ["store_id"], name: "index_authorizations_on_store_id"
37+
end
38+
3039
create_table "availability_products", force: :cascade do |t|
3140
t.uuid "uid"
3241
t.integer "available"
@@ -263,6 +272,7 @@
263272
t.datetime "created_at", null: false
264273
t.datetime "updated_at", null: false
265274
t.boolean "active", default: false
275+
t.uuid "store_id"
266276
end
267277

268278
add_foreign_key "event_store_events_in_streams", "event_store_events", column: "event_id", primary_key: "event_id"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require "test_helper"
2+
3+
module TimePromotions
4+
class AssignStoreToTimePromotionTest < InMemoryTestCase
5+
cover "TimePromotions*"
6+
7+
def test_store_id_is_set_when_time_promotion_registered_in_store
8+
event_store.publish(time_promotion_created)
9+
event_store.publish(time_promotion_registered_in_store)
10+
11+
assert_equal(store_id, TimePromotion.find(time_promotion_id).store_id)
12+
end
13+
14+
def test_store_id_is_nil_when_time_promotion_not_registered_in_store
15+
event_store.publish(time_promotion_created)
16+
17+
assert_nil(TimePromotion.find(time_promotion_id).store_id)
18+
end
19+
20+
def test_store_id_is_updated_when_time_promotion_registered_in_different_store
21+
store_2_id = SecureRandom.uuid
22+
23+
event_store.publish(time_promotion_created)
24+
event_store.publish(time_promotion_registered_in_store)
25+
26+
assert_equal(store_id, TimePromotion.find(time_promotion_id).store_id)
27+
28+
event_store.publish(time_promotion_registered_in_different_store(store_2_id))
29+
30+
assert_equal(store_2_id, TimePromotion.find(time_promotion_id).store_id)
31+
end
32+
33+
private
34+
35+
def event_store
36+
Rails.configuration.event_store
37+
end
38+
39+
def time_promotion_id
40+
@time_promotion_id ||= SecureRandom.uuid
41+
end
42+
43+
def store_id
44+
@store_id ||= SecureRandom.uuid
45+
end
46+
47+
def time_promotion_created
48+
Pricing::TimePromotionCreated.new(
49+
data: {
50+
time_promotion_id: time_promotion_id,
51+
discount: 10,
52+
start_time: Time.current - 1.hour,
53+
end_time: Time.current + 1.hour,
54+
label: "Test Promotion"
55+
}
56+
)
57+
end
58+
59+
def time_promotion_registered_in_store
60+
Stores::TimePromotionRegistered.new(data: { time_promotion_id: time_promotion_id, store_id: store_id })
61+
end
62+
63+
def time_promotion_registered_in_different_store(different_store_id)
64+
Stores::TimePromotionRegistered.new(data: { time_promotion_id: time_promotion_id, store_id: different_store_id })
65+
end
66+
end
67+
end

0 commit comments

Comments
 (0)