2019-07-26 14:34:23 +00:00
|
|
|
defmodule BackendWeb.AdminController do
|
2019-08-21 12:30:47 +00:00
|
|
|
alias Backend.{Api, Auth, Instance}
|
2019-07-26 14:34:23 +00:00
|
|
|
use BackendWeb, :controller
|
|
|
|
|
|
|
|
action_fallback BackendWeb.FallbackController
|
|
|
|
|
|
|
|
def show(conn, _params) do
|
|
|
|
[token] = get_req_header(conn, "token")
|
|
|
|
|
|
|
|
with {:ok, domain} <- Auth.verify_token(token) do
|
2019-08-23 13:08:05 +00:00
|
|
|
instance = Api.get_instance(domain)
|
2019-07-26 14:34:23 +00:00
|
|
|
render(conn, "show.json", instance: instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, params) do
|
|
|
|
[token] = get_req_header(conn, "token")
|
|
|
|
|
|
|
|
with {:ok, domain} <- Auth.verify_token(token) do
|
|
|
|
%{"optIn" => opt_in, "optOut" => opt_out} = params
|
|
|
|
|
2019-08-02 21:09:09 +00:00
|
|
|
# Make sure to update ElasticSearch so that the instance is no longer returned in search results
|
|
|
|
es_instance =
|
2019-08-29 16:54:34 +00:00
|
|
|
Api.get_instance(domain)
|
2019-08-02 21:09:09 +00:00
|
|
|
|> Map.put(:opt_in, opt_in)
|
|
|
|
|> Map.put(:opt_out, opt_out)
|
|
|
|
|
|
|
|
Elasticsearch.put_document!(Backend.Elasticsearch.Cluster, es_instance, "instances")
|
|
|
|
|
|
|
|
ecto_instance = %Instance{
|
2019-07-26 14:34:23 +00:00
|
|
|
domain: domain,
|
|
|
|
opt_in: opt_in,
|
|
|
|
opt_out: opt_out
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:09:09 +00:00
|
|
|
with {:ok, updated_instance} <- Api.update_instance(ecto_instance) do
|
2019-07-26 14:34:23 +00:00
|
|
|
render(conn, "show.json", instance: updated_instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|