Skip to content

Instantly share code, notes, and snippets.

@stavro
Last active April 1, 2016 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stavro/bcea9420ae9a46729f55423aac4b5749 to your computer and use it in GitHub Desktop.
Save stavro/bcea9420ae9a46729f55423aac4b5749 to your computer and use it in GitHub Desktop.
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
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
@stavro
Copy link
Author

stavro commented Apr 1, 2016

err gives:

%Ecto.InvalidChangesetError{action: :insert,
 changeset: %Ecto.Changeset{action: :insert,
  changes: %{appointment_id: "a76b5f6c-1124-4abf-b926-74fbb265fa19",
    awaiting_reply: true, client_id: "f56e7c8b-97ff-4adb-aac4-29603a58ad6f",
    flagged: false, id: nil, inserted_at: nil, message: "Don't forget!",
    number: "+18055555555", reply: nil, updated_at: nil},
  constraints: [%{constraint: "index_confirmations_on_receiving",
     field: :number, message: "has already been taken", type: :unique}],
  errors: [number: "has already been taken"], filters: %{},
  model: %Sched.Confirmation.Schema{__meta__: #Ecto.Schema.Metadata<:built>,
   appointment: #Ecto.Association.NotLoaded<association :appointment is not loaded>,
   appointment_id: nil, awaiting_reply: true,
   client: #Ecto.Association.NotLoaded<association :client is not loaded>,
   client_id: nil, flagged: false, id: nil, inserted_at: nil, message: nil,
   number: nil, reply: nil, updated_at: nil}, optional: [], opts: [],
  params: %{"appointment_id" => "a76b5f6c-1124-4abf-b926-74fbb265fa19",
    "client_id" => "f56e7c8b-97ff-4adb-aac4-29603a58ad6f",
    "message" => "Don't forget!", "number" => "+18055555555"}, prepare: [],
  repo: SchedWeb.Repo,
  required: [:appointment_id, :client_id, :message, :number],
  types: %{appointment_id: :binary_id, awaiting_reply: :boolean,
    client_id: :binary_id, flagged: :boolean, id: :binary_id,
    inserted_at: Ecto.DateTime, message: :string, number: :string,
    reply: :string, updated_at: Ecto.DateTime}, valid?: false, validations: []}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment