Skip to content

Commit

Permalink
Setup new states for version
Browse files Browse the repository at this point in the history
Resolves ops-713
Resolves ops-712
Resolves ops-711
  • Loading branch information
zacksiri committed Jun 19, 2024
1 parent b2071dd commit 34796cd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/polar/streams.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Polar.Streams do
to: Version.Manager,
as: :create

defdelegate deactivate_previous_versions(version),
defdelegate deactivate_previous_versions(event, version),
to: Version.Manager,
as: :deactivate_previous

Expand Down
2 changes: 1 addition & 1 deletion lib/polar/streams/version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Polar.Streams.Version do
import Ecto.Query, only: [from: 2]

schema "versions" do
field :current_state, :string, default: "active"
field :current_state, :string, default: "created"
field :serial, :string

belongs_to :product, Product
Expand Down
16 changes: 5 additions & 11 deletions lib/polar/streams/version/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@ defmodule Polar.Streams.Version.Manager do
%Version{product_id: product.id}
|> Version.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, version} = result ->
deactivate_previous(version)

result

error ->
error
end
end

def deactivate_previous(version) do
def deactivate_previous(event, version) do
basic_setting = Globals.get("basic")
bot = Polar.Accounts.Automation.get_bot!()

Expand All @@ -34,7 +25,10 @@ defmodule Polar.Streams.Version.Manager do
)
|> Repo.all()
|> Enum.map(fn v ->
Eventful.Transit.perform(v, bot, "deactivate")
Eventful.Transit.perform(v, bot, "deactivate",
comment: "New version activated.",
parameters: %{"event_id" => event.id}
)
end)
end
end
12 changes: 12 additions & 0 deletions lib/polar/streams/version/transitions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ defmodule Polar.Streams.Version.Transitions do

alias Polar.Streams.Version

Version
|> transition(
[from: "created", to: "testing", via: "test"],
fn changes -> transit(changes) end
)

Version
|> transition(
[from: "testing", to: "active", via: "activate"],
fn changes -> transit(changes, Version.Triggers) end
)

Version
|> transition(
[from: "active", to: "inactive", via: "deactivate"],
Expand Down
9 changes: 9 additions & 0 deletions lib/polar/streams/version/triggers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Polar.Streams.Version.Triggers do
use Eventful.Trigger

alias Polar.Streams
alias Polar.Streams.Version

Version
|> trigger([currently: "active"], &Streams.deactivate_previous_versions/2)
end
14 changes: 10 additions & 4 deletions test/polar/streams/product/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ defmodule Polar.Streams.Product.ManagerTest do
setup do
password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})
bot = bot_fixture(%{password: password})

:ok
{:ok, bot: bot}
end

describe "filter" do
setup do
setup %{bot: bot} do
{:ok, %Product{} = without_active_versions} =
Streams.create_product(%{
aliases: ["alpine/3.19", "alpine/3.19/default"],
Expand All @@ -44,7 +44,7 @@ defmodule Polar.Streams.Product.ManagerTest do
}
})

{:ok, _version} =
{:ok, version} =
Streams.create_version(with_active_versions, %{
serial: "20240209_13:00",
items: [
Expand All @@ -65,6 +65,12 @@ defmodule Polar.Streams.Product.ManagerTest do
]
})

{:ok, %{resource: testing_version}} =
Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: _active_version}} =
Eventful.Transit.perform(testing_version, bot, "activate")

{:ok,
without_active_versions: without_active_versions,
with_active_versions: with_active_versions}
Expand Down
6 changes: 3 additions & 3 deletions test/polar/streams/version/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Polar.Streams.Version.ManagerTest do
setup do
password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})
bot = bot_fixture(%{password: password})

{:ok, product} =
Streams.create_product(%{
Expand All @@ -25,7 +25,7 @@ defmodule Polar.Streams.Version.ManagerTest do
}
})

{:ok, product: product}
{:ok, product: product, bot: bot}
end

describe "create_version" do
Expand All @@ -43,7 +43,7 @@ defmodule Polar.Streams.Version.ManagerTest do
%{existing_version: version}
end

test "creating a new version deactivates old versions", %{
test "activating a new version deactivates old versions", %{
product: product,
existing_version: existing_version
} do
Expand Down

0 comments on commit 34796cd

Please sign in to comment.