-
-
Save stavro/bcea9420ae9a46729f55423aac4b5749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Sched.Confirmation.Schema do | |
use SchedWeb.Web, :model | |
@primary_key {:id, :binary_id, autogenerate: true} | |
schema "confirmations" do | |
field :message, :string | |
field :number, :string | |
field :awaiting_reply, :boolean, default: true | |
field :flagged, :boolean, default: false | |
field :reply, :string | |
belongs_to :appointment, Sched.Appointment.Schema, type: :binary_id | |
belongs_to :client, Sched.Client.Schema, type: :binary_id | |
timestamps | |
end | |
@required_on_creation ~w( | |
appointment_id | |
client_id | |
message | |
number | |
) | |
@optional_on_update ~w( | |
reply | |
flagged | |
message | |
number | |
awaiting_reply | |
) | |
def create(params) do | |
%__MODULE__{} | |
|> cast(params, @required_on_creation, ~w()) | |
|> unique_constraint(:number, name: :index_confirmations_on_receiving) | |
end | |
def update(model, params) do | |
model | |
|> cast(params, ~w(), @optional_on_update) | |
end | |
def confirm(model) do | |
model | |
|> change(flagged: false, awaiting_reply: false) | |
# |> cast_assoc(:appointment, with: &Sched.Appointment.Schema.confirm/2, required: true) | |
# TODO: Ecto 2.0 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try do | |
# Worst case scenario: We will never send a text message to a client before committing to our database of the request. | |
# This will prevent two text messages ever from going out for the same event | |
confirmation = ConfirmationSchema.create(%{appointment_id: appointment_id, client_id: client_id, message: body, number: number}) |> @repo.insert!() | |
{:ok, message} = Sched.SMS.send(to: number, body: body) | |
confirmation = ConfirmationSchema.update(confirmation, %{message: message.body, number: message.to}) |> @repo.update!() | |
{:ok, message.status} | |
rescue | |
err in [Ecto.InvalidChangesetError] -> | |
IO.inspect err | |
# Repo is no longer accessible now. | |
{:error, :already_sent} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
err gives: