Update registrations
This commit is contained in:
parent
fdf59f660a
commit
f498f8672c
|
@ -18,6 +18,14 @@ defmodule TuskerPush do
|
|||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@spec update_registration(Registration.t(), Map.t()) ::
|
||||
{:ok, Registration.t()} | {:error, Ecto.Changeset.t()}
|
||||
def update_registration(registration, params) do
|
||||
registration
|
||||
|> Registration.update_changeset(params)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@spec unregister(String.t()) :: :ok | {:error, :no_registration | Ecto.Changeset.t()}
|
||||
def unregister(id) when is_binary(id) do
|
||||
with registration when not is_nil(registration) <- Repo.get(Registration, id),
|
||||
|
|
|
@ -27,7 +27,7 @@ defmodule TuskerPush.Registration do
|
|||
timestamps()
|
||||
end
|
||||
|
||||
@required_fields [
|
||||
@create_fields [
|
||||
:storekit_original_transaction_id,
|
||||
:apns_environment,
|
||||
:apns_device_token,
|
||||
|
@ -36,7 +36,19 @@ defmodule TuskerPush.Registration do
|
|||
|
||||
def create_changeset(registration \\ %__MODULE__{}, params) do
|
||||
registration
|
||||
|> cast(params, @required_fields)
|
||||
|> validate_required(@required_fields)
|
||||
|> cast(params, @create_fields)
|
||||
|> validate_required(@create_fields)
|
||||
end
|
||||
|
||||
@update_fields [
|
||||
:apns_environment,
|
||||
:apns_device_token,
|
||||
:push_version
|
||||
]
|
||||
|
||||
def update_changeset(registration, params) do
|
||||
registration
|
||||
|> cast(params, @update_fields)
|
||||
|> validate_required(@update_fields)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,10 +24,52 @@ defmodule TuskerPushWeb.RegistrationsController do
|
|||
endpoint: url(~p"/mastodon/push/#{id}")
|
||||
})
|
||||
else
|
||||
{:env, nil} ->
|
||||
{:error, %Changeset{valid?: false} = changeset} ->
|
||||
errors =
|
||||
changeset.errors
|
||||
|> Enum.map(fn {k, {reason, _}} -> %{key: k, reason: reason} end)
|
||||
|
||||
conn
|
||||
|> put_status(400)
|
||||
|> json(%{error: "invalid apns environment"})
|
||||
|> json(%{error: "validation failed", fields: errors})
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed creating registration: #{inspect(reason)}")
|
||||
|
||||
conn
|
||||
|> put_status(500)
|
||||
|> json(%{error: "unknown error", fields: []})
|
||||
end
|
||||
end
|
||||
|
||||
def create(conn, _) do
|
||||
conn
|
||||
|> put_status(400)
|
||||
|> json(%{error: "missing required parameters"})
|
||||
end
|
||||
|
||||
def update(conn, %{
|
||||
"id" => id,
|
||||
"environment" => env,
|
||||
"device_token" => token,
|
||||
"push_version" => version
|
||||
}) do
|
||||
params = %{
|
||||
apns_environment: env,
|
||||
apns_device_token: token,
|
||||
push_version: version
|
||||
}
|
||||
|
||||
with {:registration, registration} when not is_nil(registration) <-
|
||||
{:registration, TuskerPush.get_registration(id)},
|
||||
{:ok, _} <- TuskerPush.update_registration(registration, params) do
|
||||
conn
|
||||
|> json(%{id: id, endpoint: url(~p"/mastodon/push/#{id}")})
|
||||
else
|
||||
{:registration, nil} ->
|
||||
conn
|
||||
|> put_status(404)
|
||||
|> json(%{error: "not found", fields: []})
|
||||
|
||||
{:error, %Changeset{valid?: false} = changeset} ->
|
||||
errors =
|
||||
|
@ -36,18 +78,18 @@ defmodule TuskerPushWeb.RegistrationsController do
|
|||
|
||||
conn
|
||||
|> put_status(400)
|
||||
|> json(%{errors: errors})
|
||||
|> json(%{error: "validation failed", fields: errors})
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed creating registration: #{inspect(reason)}")
|
||||
Logger.error("Failed updating registration: #{inspect(reason)}")
|
||||
|
||||
conn
|
||||
|> put_status(500)
|
||||
|> json(%{error: "unknown error"})
|
||||
|> json(%{error: "unknown error", fields: []})
|
||||
end
|
||||
end
|
||||
|
||||
def create(conn, _) do
|
||||
def update(conn, _) do
|
||||
conn
|
||||
|> put_status(400)
|
||||
|> json(%{error: "missing required parameters"})
|
||||
|
|
|
@ -8,7 +8,7 @@ defmodule TuskerPushWeb.Router do
|
|||
scope "/app", TuskerPushWeb do
|
||||
pipe_through :api
|
||||
|
||||
resources "/registrations", RegistrationsController, only: [:create, :delete]
|
||||
resources "/registrations", RegistrationsController, only: [:create, :update, :delete]
|
||||
end
|
||||
|
||||
scope "/mastodon", TuskerPushWeb do
|
||||
|
|
Loading…
Reference in New Issue