Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1736184348,
"lastModified": 1741348424,
"owner": "cachix",
"repo": "devenv",
"rev": "07219f00c633f756d1f0cc5bb6c4c311b5c4cb0d",
"rev": "8f8c96bb1e0c6a59a97592328dc61b9fdbe7474b",
"type": "github"
},
"original": {
Expand Down Expand Up @@ -40,10 +40,10 @@
]
},
"locked": {
"lastModified": 1737465171,
"lastModified": 1741379162,
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "9364dc02281ce2d37a1f55b6e51f7c0f65a75f17",
"rev": "b5a62751225b2f62ff3147d0a334055ebadcd5cc",
"type": "github"
},
"original": {
Expand Down Expand Up @@ -89,10 +89,10 @@
},
"nixpkgs-unstable": {
"locked": {
"lastModified": 1736798957,
"lastModified": 1741513245,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9abb87b552b7f55ac8916b6fc9e5cb486656a2f3",
"rev": "e3e32b642a31e6714ec1b712de8c91a3352ce7e1",
"type": "github"
},
"original": {
Expand Down
3 changes: 1 addition & 2 deletions tololo/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@ config :tololo,
],
pubsub: Tololo.PubSub,
geocoding_endpoint: "https://nominatim.openstreetmap.org/search",
business_name: "Sushi",
from_location: {-33.04534, -71.4447094},
# set to 0 to disable stale cleaner
days_for_stale: 2,
min_done_distance_meters: 50,
ash_domains: [
TololoCore.Deliveries,
TololoCore.Products,
TololoCore.Carts,
TololoCore.Brands,
Tololo.Accounts,
Tololo.Extensions.TelegramBot.Ash.Users
],
Expand Down
20 changes: 20 additions & 0 deletions tololo/core/lib/brands.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule TololoCore.Brands do
@moduledoc """
Store the Brand associated.
"""

use Ash.Domain,
otp_app: :tololo,
extensions: [AshGraphql, AshAdmin.Domain],
validate_config_inclusion?: false

admin do
show?(true)
show_resources([TololoCore.Brands.Brand, TololoCore.Brands.Branch])
end

resources do
resource TololoCore.Brands.Brand
resource TololoCore.Brands.Branch
end
end
103 changes: 103 additions & 0 deletions tololo/core/lib/brands/branch.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
defmodule TololoCore.Brands.Branch do
@moduledoc """
Define the data fields for storing store branch information.
"""

use Ash.Resource,
otp_app: :tololo,
domain: TololoCore.Brands,
extensions: [AshGraphql.Resource, AshAdmin.Resource],
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
notifiers: [TololoCore.Kafka.AshNotifier]

use Gettext, backend: TololoCore.Gettext
alias Ash.Changeset

graphql do
type :branch

queries do
get :get_branch, :read
end

mutations do
end
end

admin do
end

postgres do
table "branches"
repo Tololo.Repo

references do
end
end

field_policies do
field_policy :* do
description "the rest of the fields don't require any special policies"
authorize_if always()
end
end

code_interface do
define :create
define :update
define :read, get?: true
end

actions do
defaults [:read, :destroy, create: :*, update: :*]
end

policies do
bypass always() do
description "admin has access to every action"
authorize_if actor_attribute_equals(:access_level, :admin)
end

policy action_type(:read) do
description "read access is always allowed"
authorize_if always()
end
end

attributes do
uuid_v7_primary_key :id

attribute :name, :string do
allow_nil? false
sensitive? false
public? true
end

attribute :address, :string do
allow_nil? false
sensitive? false
public? true
end

attribute :latitude, :float do
allow_nil? false
sensitive? false
public? true
end

attribute :longitude, :float do
allow_nil? false
sensitive? false
public? true
end

timestamps()
end

relationships do
belongs_to :brand, TololoCore.Brands.Brand do
public? true
end
end
end
92 changes: 92 additions & 0 deletions tololo/core/lib/brands/brand.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
defmodule TololoCore.Brands.Brand do
@moduledoc """
Define the data fields for storing store brand information.
"""

use Ash.Resource,
otp_app: :tololo,
domain: TololoCore.Brands,
extensions: [AshGraphql.Resource, AshAdmin.Resource],
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
notifiers: [TololoCore.Kafka.AshNotifier]

use Gettext, backend: TololoCore.Gettext
alias Ash.Changeset

graphql do
type :brand

queries do
get :get_brand, :read
end

mutations do
end
end

admin do
end

postgres do
table "brands"
repo Tololo.Repo

references do
end
end

field_policies do
field_policy :* do
description "the rest of the fields don't require any special policies"
authorize_if always()
end
end

code_interface do
define :create
define :update
end

actions do
defaults [:read, :destroy, create: :*, update: :*]
end

policies do
bypass always() do
description "admin has access to every action"
authorize_if actor_attribute_equals(:access_level, :admin)
end

policy action_type(:read) do
description "read access is always allowed"
authorize_if always()
end
end

attributes do
uuid_v7_primary_key :id

attribute :name, :string do
allow_nil? false
sensitive? false
public? true
end

# Define a Headquarters Tololo URL to obtain
# Common brand and products information
attribute :hq_url, :string do
allow_nil? true
sensitive? true
public? false
end

timestamps()
end

relationships do
has_one :branch, TololoCore.Brands.Branch do
public? true
end
end
end
19 changes: 11 additions & 8 deletions tololo/core/lib/deliveries/delivery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,17 @@ defmodule TololoCore.Deliveries.Delivery do
:to_notes
]

change set_attribute(
:from_name,
Application.compile_env(:tololo, :business_name, "A business")
)

{lat, lng} = Application.compile_env(:tololo, :from_location, {0, 0})
change set_attribute(:from_latitude, lat)
change set_attribute(:from_longitude, lng)
change fn changeset, _context ->
{lat, lng} = Application.get_env(:tololo, :from_location, {0, 0})
name = Application.get_env(:tololo, :business_name, "A business")

changeset
|> Ash.Changeset.change_attributes(%{
from_name: name,
from_latitude: lat,
from_longitude: lng
})
end
end

create :empty do
Expand Down
14 changes: 13 additions & 1 deletion tololo/lib/tololo/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ defmodule Tololo.Application do
{Finch, name: Tololo.Finch},
TololoWeb.Endpoint,
{AshAuthentication.Supervisor, [otp_app: :tololo]},
TololoCore.Deliveries.StaleCleaner
TololoCore.Deliveries.StaleCleaner,
{Task, fn -> load_config() end}
] ++ extensions

Tololo.GeocodingStore.init()
Expand All @@ -49,4 +50,15 @@ defmodule Tololo.Application do
TololoWeb.Endpoint.config_change(changed, removed)
:ok
end

def load_config do
%{latitude: lat, longitude: lng, name: name} =
case TololoCore.Brands.Branch.read() do
{:ok, branch_config} -> branch_config
_ -> %{latitude: 0, longitude: 0, name: "A business"}
end

Application.put_env(:tololo, :from_location, {lat, lng})
Application.put_env(:tololo, :business_name, name)
end
end
21 changes: 21 additions & 0 deletions tololo/priv/repo/example_seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,29 @@ Enum.each(product_names, fn product_name ->
product ->
Repo.delete!(product)
end

end)

old_brand = TololoCore.Brands.Brand |> Ash.read_one!
if old_brand, do: Repo.delete!(old_brand)

old_branch = TololoCore.Brands.Branch |> Ash.read_one!
if old_branch, do: Repo.delete!(old_branch)

brand = TololoCore.Brands.Brand.create!(%{name: "Sushi"}, authorize?: false)

branch =
TololoCore.Brands.Branch.create!(
%{
name: "Sushi Quilpué",
latitude: -33.04534,
longitude: -71.4447094,
brand_id: brand.id,
address: "Quilpué"
},
authorize?: false
)

set_prices = fn variants, values_combination, price ->
values_combination =
if is_list(values_combination), do: values_combination, else: [values_combination]
Expand Down
Loading
Loading