Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core-api): save tenant configuration on tenant #386

Merged
merged 1 commit into from
Sep 13, 2024
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
refactor(core-api): save tenant configuration on tenant
  • Loading branch information
ptitmouton committed Sep 13, 2024
commit a3bc0b072ffd1a282f248ad046a3809c2edba10d
1 change: 1 addition & 0 deletions apps/core-api/.formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
inputs: [
"*.{heex,ex,exs}",
"priv/*/seeds.exs",
"priv/repo/migrations/*.exs",
"{config,lib,test}/**/*.{heex,ex,exs}",
"**/*.md"
]
Expand Down
6 changes: 6 additions & 0 deletions apps/core-api/config/config/logger.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ config :logger, :console,
level: level,
metadata: [:request_id],
backends: [:console, Sentry.LoggerBackend]

config :logger, Sentry.LoggerBackend,
level: :error,
excluded_domains: [],
metadata: [:sentry],
capture_log_messages: true
1 change: 1 addition & 0 deletions apps/core-api/lib/lotta/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Lotta.Accounts do
alias Lotta.{Content, Email, Mailer, Repo, Storage}
alias Lotta.Accounts.{User, UserDevice, UserGroup}
alias Lotta.Storage.File
alias Lotta.Tenants.Tenant

def data() do
Dataloader.Ecto.new(Repo, query: &query/2)
Expand Down
3 changes: 1 addition & 2 deletions apps/core-api/lib/lotta/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ defmodule Lotta.Accounts.User do

%{id: id} ->
UserGroup
|> Ecto.Query.put_query_prefix(Ecto.get_meta(user, :prefix))
|> Repo.get(String.to_integer(id))
|> Repo.get(String.to_integer(id), prefix: Ecto.get_meta(user, :prefix))
end
end)
|> Enum.filter(&(!is_nil(&1)))
Expand Down
2 changes: 1 addition & 1 deletion apps/core-api/lib/lotta/calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Lotta.Calendar do
def get_calendar_event(id), do: Repo.get(CalendarEvent, id, prefix: Repo.get_prefix())

@spec create_calendar(data :: map()) ::
{:ok, Calendar} | {:error, Ecto.Changeset.t(Calendar.t())}
{:ok, Calendar.t()} | {:error, Ecto.Changeset.t(Calendar.t())}
def create_calendar(data) do
data
|> Calendar.changeset()
Expand Down
4 changes: 3 additions & 1 deletion apps/core-api/lib/lotta/calendar/calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ defmodule Lotta.Calendar.Calendar do
end

@spec changeset(data :: map()) :: Ecto.Changeset.t(t())
def changeset(data), do: changeset(%__MODULE__{}, data)

@spec changeset(t(), data :: map()) :: Ecto.Changeset.t(t())
def changeset(struct \\ %__MODULE__{}, data) do
def changeset(struct, data) do
struct
|> cast(data, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
Expand Down
2 changes: 1 addition & 1 deletion apps/core-api/lib/lotta/calendar/renderer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Lotta.Calendar.Renderer do
defp calendar_event(do: block), do: entity("VEVENT", block)

defp string_val(value) when is_list(value), do: Enum.join(value, ",")
defp string_val(value) when is_struct(DateTime), do: to_ics_date(value)
defp string_val(value) when is_struct(value, DateTime), do: to_ics_date(value)
defp string_val(value), do: to_string(value)

defp maybe_append(acc, field, value, opts \\ [])
Expand Down
5 changes: 4 additions & 1 deletion apps/core-api/lib/lotta/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ defmodule Lotta.Messages do
:ok
rescue
e ->
Logger.error(Exception.format(:error, e, __STACKTRACE__))
Logger.error("Could not insert into the conversation_user_last_seen table.", %{
sentry: %{error: e}
})

Sentry.capture_exception(e)
:error
end
Expand Down
6 changes: 3 additions & 3 deletions apps/core-api/lib/lotta/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Lotta.Release do
alias Lotta.Storage
alias Lotta.Repo
alias Lotta.Storage.RemoteStorage
alias Lotta.Tenants.{Tenant, TenantSelector}
alias Lotta.Tenants.{Tenant, TenantDbManager}
alias Ecto.Migrator

@app :lotta
Expand All @@ -29,7 +29,7 @@ defmodule Lotta.Release do
on_each_tenant_repo(fn tenant, pid ->
Logger.notice("Customer #{tenant.title} with schema #{tenant.prefix} is being migrated ...")

TenantSelector.run_migrations(prefix: tenant.prefix, dynamic_repo: pid)
TenantDbManager.run_migrations(prefix: tenant.prefix, dynamic_repo: pid)
end)
end

Expand All @@ -45,7 +45,7 @@ defmodule Lotta.Release do
"Customer #{tenant.title} with schema #{tenant.prefix} is being rolled back ..."
)

TenantSelector.rollback_migrations(
TenantDbManager.rollback_migrations(
Keyword.merge([prefix: tenant.prefix, dynamic_repo: pid], opts)
)
end)
Expand Down
22 changes: 19 additions & 3 deletions apps/core-api/lib/lotta/storage/Storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Lotta.Storage do

import Ecto.Query

alias Lotta.Tenants
alias Plug.Upload
alias Ecto.Multi
alias Ecto.Changeset
Expand Down Expand Up @@ -312,8 +313,8 @@ defmodule Lotta.Storage do

"""
@doc since: "2.5.0"
@spec get_file(Lotta.Storage.File.id()) :: Lotta.Storage.File.t() | nil
def get_file(id), do: Repo.get(Lotta.Storage.File, id)
@spec get_file(Lotta.Storage.File.id(), opts :: keyword() | nil) :: Lotta.Storage.File.t() | nil
def get_file(id, opts \\ []), do: Repo.get(Lotta.Storage.File, id, opts)

@doc """
Gets a single file_conversion.
Expand Down Expand Up @@ -352,6 +353,9 @@ defmodule Lotta.Storage do
@doc """
Deletes a File AND all its dependend conversions

If the file is referenced by its tenant's logo_image_file_id or background_image_file_id,
the corresponding field will be set to nil.

## Examples

iex> delete_file(file)
Expand All @@ -369,7 +373,9 @@ defmodule Lotta.Storage do
file
|> Repo.preload([:file_conversions, :remote_storage_entity])

Enum.each(file.file_conversions, fn file_conversion ->
file.file_conversions
|> Repo.preload(:remote_storage_entity)
|> Enum.each(fn file_conversion ->
file_conversion =
file_conversion
|> Repo.preload(:remote_storage_entity)
Expand All @@ -385,6 +391,16 @@ defmodule Lotta.Storage do
RemoteStorage.delete(file.remote_storage_entity)
end

tenant = Tenants.get_tenant_by_prefix(Ecto.get_meta(file, :prefix))

if tenant.logo_image_file_id == file.id do
Tenants.update_tenant(tenant, %{logo_image_file_id: nil})
end

if tenant.background_image_file_id == file.id do
Tenants.update_tenant(tenant, %{background_image_file_id: nil})
end

Repo.delete(file)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ defmodule Lotta.Repo.TenantMigrations.AddPublishedToArticles do
flush()

from(a in "articles", where: not is_nil(a.category_id))
|> put_query_prefix(prefix())
|> Repo.update_all(set: [published: true])
|> Repo.update_all([set: [published: true]], prefix: prefix())
end

def down do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ defmodule Lotta.Repo.TenantMigrations.AddHasChangedDefaultPasswordToUsers do
flush()

from("users")
|> put_query_prefix(prefix())
|> Repo.update_all(set: [has_changed_default_password: true])
|> Repo.update_all([set: [has_changed_default_password: true]], prefix: prefix())
end

def down do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defmodule Lotta.Repo.TenantMigrations.AddMessageFile do

def change do
create table(:message_file, primary_key: false) do
add :message_id, references(:messages, on_delete: :delete_all, primary_key: true)
add :file_id, references(:files, type: :uuid, on_delete: :delete_all, primary_key: true)
add :message_id, references(:messages, on_delete: :delete_all), primary_key: true
add :file_id, references(:files, type: :uuid, on_delete: :delete_all), primary_key: true
end

create(index(:message_file, [:message_id]))
Expand Down
Loading
Loading