Add registrations

This commit is contained in:
Shadowfacts 2024-04-05 15:04:38 -04:00
parent c55b466fd0
commit fae112fb67
5 changed files with 132 additions and 1 deletions

View File

@ -6,4 +6,29 @@ defmodule TuskerPush do
Contexts are also responsible for managing your data, regardless Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others. if it comes from the database, an external API or others.
""" """
alias TuskerPush.Registration
alias TuskerPush.Repo
@spec register(Map.t()) ::
{:ok, Registration.t()} | {:error, Ecto.Changeset.t()}
def register(params) do
params
|> Registration.create_changeset()
|> Repo.insert()
end
@spec unregister(String.t()) :: :ok | {:error, :no_registration | Ecto.Changeset.t()}
def unregister(id) do
with registration when not is_nil(registration) <- Repo.get(Registration, id),
{:ok, _} <- Repo.delete(registration) do
:ok
else
nil ->
{:error, :no_registration}
{:error, reason} ->
{:error, reason}
end
end
end end

View File

@ -0,0 +1,33 @@
defmodule TuskerPush.Registration do
use Ecto.Schema
import Ecto.Changeset
@type t() :: %__MODULE__{
id: Ecto.UUID.t(),
storekit_original_transaction_id: String.t(),
apns_environment: String.t(),
apns_device_token: String.t(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@primary_key {:id, Ecto.UUID, autogenerate: true}
schema "registrations" do
field :storekit_original_transaction_id, :string
field :apns_environment, Ecto.Enum, values: [:production, :development]
field :apns_device_token, :string
timestamps()
end
@required_fields [:storekit_original_transaction_id, :apns_environment, :apns_device_token]
def create_changeset(registration \\ %__MODULE__{}, params) do
registration
|> cast(params, @required_fields)
|> validate_required(@required_fields)
end
end

View File

@ -0,0 +1,55 @@
defmodule TuskerPushWeb.RegistrationsController do
alias TuskerPush.Registration
alias Ecto.Changeset
use TuskerPushWeb, :controller
require Logger
def create(conn, %{
"transaction_id" => transaction_id,
"environment" => env,
"device_token" => token
}) do
with {:ok, %Registration{id: id}} <-
TuskerPush.register(%{
storekit_original_transaction_id: transaction_id,
apns_environment: env,
apns_device_token: token
}) do
json(conn, %{id: id})
else
{:env, nil} ->
conn
|> put_status(400)
|> json(%{error: "invalid apns environment"})
{:error, %Changeset{valid?: false} = changeset} ->
errors =
changeset.errors
|> Enum.map(fn {k, {reason, _}} -> %{key: k, reason: reason} end)
conn
|> put_status(400)
|> json(%{errors: errors})
{:error, reason} ->
Logger.error("Failed creating registration: #{inspect(reason)}")
conn
|> put_status(500)
|> json(%{error: "unknown error"})
end
end
def create(conn, _) do
conn
|> put_status(400)
|> json(%{error: "missing required parameters"})
end
def delete(conn, %{"id" => id}) do
TuskerPush.unregister(id)
json(conn, %{status: "ok"})
end
end

View File

@ -5,7 +5,9 @@ defmodule TuskerPushWeb.Router do
plug :accepts, ["json"] plug :accepts, ["json"]
end end
scope "/api", TuskerPushWeb do scope "/app", TuskerPushWeb do
pipe_through :api pipe_through :api
resources "/registrations", RegistrationsController, only: [:create, :delete]
end end
end end

View File

@ -0,0 +1,16 @@
defmodule TuskerPush.Repo.Migrations.CreateRegistrations do
use Ecto.Migration
def change do
create table(:registrations, primary_key: false) do
add :id, :uuid, primary_key: true, null: false
add :storekit_original_transaction_id, :string, null: false
add :apns_environment, :string, null: false
add :apns_device_token, :string, null: false
timestamps()
end
end
end