-
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.
- Loading branch information
Showing
10 changed files
with
181 additions
and
1 deletion.
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,25 @@ | ||
defmodule Polar.Globals do | ||
alias Polar.Repo | ||
alias Polar.Globals.Basic | ||
alias Polar.Globals.Setting | ||
|
||
@defaults %{ | ||
"basic" => Basic | ||
} | ||
|
||
def get(key) do | ||
Setting | ||
|> Repo.get_by(key: key) | ||
|> case do | ||
nil -> | ||
module = Map.fetch!(@defaults, key) | ||
|
||
struct(module, %{}) | ||
|
||
%Setting{value: value} -> | ||
module = Map.fetch!(@defaults, key) | ||
|
||
module.parse!(value) | ||
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,24 @@ | ||
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 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,37 @@ | ||
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 -> | ||
bot = Polar.Accounts.Automation.get_bot!() | ||
|
||
basic_setting = Globals.get("basic") | ||
|
||
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) | ||
|
||
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
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