-
Notifications
You must be signed in to change notification settings - Fork 2
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 #9 from upmaru/feature/ops-570-global-basic-setting
Use basic global config
- Loading branch information
Showing
13 changed files
with
266 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,73 @@ | ||
defmodule Polar.Globals do | ||
alias Polar.Repo | ||
alias Polar.Globals.Basic | ||
alias Polar.Globals.Setting | ||
|
||
@mappings %{ | ||
"basic" => Basic | ||
} | ||
|
||
def get(key) do | ||
Setting | ||
|> Repo.get_by(key: key) | ||
|> case do | ||
nil -> | ||
module = Map.fetch!(@mappings, key) | ||
|
||
struct(module, %{}) | ||
|
||
%Setting{value: value} -> | ||
module = Map.fetch!(@mappings, key) | ||
|
||
module.parse!(:erlang.binary_to_term(value)) | ||
end | ||
end | ||
|
||
def save(key, value) when is_map(value) do | ||
case Repo.get_by(Setting, key: key) do | ||
nil -> | ||
create(key, value) | ||
|
||
%Setting{} = setting -> | ||
update(setting, value) | ||
end | ||
end | ||
|
||
defp create(key, value) when is_map(value) do | ||
module = Map.fetch!(@mappings, key) | ||
|
||
case module.parse(value) do | ||
{:ok, params} = result -> | ||
value = | ||
Map.from_struct(params) | ||
|> :erlang.term_to_binary() | ||
|
||
Repo.insert!(%Setting{key: key, value: value}) | ||
|
||
result | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
defp update(setting, value) when is_map(value) do | ||
module = Map.fetch!(@mappings, setting.key) | ||
|
||
case module.parse(value) do | ||
{:ok, params} = result -> | ||
value = | ||
Map.from_struct(params) | ||
|> :erlang.term_to_binary() | ||
|
||
setting | ||
|> Setting.changeset(%{value: value}) | ||
|> Repo.update!() | ||
|
||
result | ||
|
||
error -> | ||
error | ||
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,30 @@ | ||
defmodule Polar.Globals.Basic do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@primary_key false | ||
|
||
embedded_schema do | ||
field :enable_registration, :boolean, default: true | ||
field :versions_per_product, :integer, default: 1 | ||
end | ||
|
||
def changeset(basic, attrs \\ %{}) do | ||
basic | ||
|> cast(attrs, [:enable_registration, :versions_per_product]) | ||
|> validate_required([:enable_registration, :versions_per_product]) | ||
|> validate_number(:versions_per_product, less_than_or_equal_to: 3) | ||
end | ||
|
||
def parse!(value) do | ||
%__MODULE__{} | ||
|> changeset(value) | ||
|> apply_action!(:insert) | ||
end | ||
|
||
def parse(value) do | ||
%__MODULE__{} | ||
|> changeset(value) | ||
|> apply_action(:insert) | ||
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
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 |
---|---|---|
@@ -1,10 +1,40 @@ | ||
defmodule Polar.Streams.Version.Manager do | ||
alias Polar.Repo | ||
alias Polar.Globals | ||
alias Polar.Streams.Version | ||
|
||
import Ecto.Query, only: [from: 2] | ||
|
||
def create(product, attrs) 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 | ||
basic_setting = Globals.get("basic") | ||
bot = Polar.Accounts.Automation.get_bot!() | ||
|
||
from( | ||
v in Version, | ||
where: | ||
v.product_id == ^version.product_id and | ||
v.current_state == ^"active", | ||
offset: ^basic_setting.versions_per_product, | ||
order_by: [desc: :inserted_at] | ||
) | ||
|> Repo.all() | ||
|> Enum.each(fn v -> | ||
Eventful.Transit.perform(v, bot, "deactivate") | ||
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,39 @@ | ||
defmodule Polar.GlobalsTest do | ||
use Polar.DataCase, async: true | ||
|
||
alias Polar.Globals.Basic | ||
|
||
describe "create" do | ||
test "can create basic setting" do | ||
{:ok, setting} = Polar.Globals.save("basic", %{versions_per_product: 3}) | ||
|
||
assert %Basic{versions_per_product: 3} = setting | ||
end | ||
end | ||
|
||
describe "get" do | ||
setup do | ||
{:ok, _setting} = Polar.Globals.save("basic", %{versions_per_product: 3}) | ||
|
||
:ok | ||
end | ||
|
||
test "can get basic setting" do | ||
assert %Basic{versions_per_product: 3} = Polar.Globals.get("basic") | ||
end | ||
end | ||
|
||
describe "update" do | ||
setup do | ||
{:ok, _setting} = Polar.Globals.save("basic", %{versions_per_product: 3}) | ||
|
||
:ok | ||
end | ||
|
||
test "can change basic setting" do | ||
{:ok, setting} = Polar.Globals.save("basic", %{versions_per_product: 2}) | ||
|
||
assert %Basic{versions_per_product: 2} = setting | ||
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
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
Oops, something went wrong.