Current section

Files

Jump to
ami lib ami profile.ex
Raw

lib/ami/profile.ex

defmodule Ami.Profile do
use Ecto.Schema
use Arc.Ecto.Schema
use Timex
import Ecto.{
Query,
Changeset
}
alias Ami.{
Job,
Repo,
User,
City,
Country,
Education
}
schema "profiles" do
@timestamps_opts [type: :naive_datetime_usec]
field(:first_name, :string)
field(:last_name, :string)
field(:age, :integer)
field(:gender, :string)
field(:alternative_email, :string)
field(:phone, :string)
field(:skype, :string)
field(:linkedin, :string)
field(:google_plus, :string)
field(:twitter, :string)
field(:biography, :string)
field(:created_at, :utc_datetime_usec)
field(:updated_at, :utc_datetime_usec)
field(:image, Ami.ProfileImageUploader.Type)
field(:facebook, :string)
field(:cover_photo, Ami.ProfileImageUploader.Type)
field(:cv, :string)
field(:completed, :boolean)
field(:view_count, :integer)
field(:settings, :map)
belongs_to(:user, User)
belongs_to(:city, City)
belongs_to(:country, Country)
has_many(:jobs, Job)
has_many(:educations, Education)
end
def changeset(profile, attrs \\ %{}) do
profile
|> cast(attrs, [:first_name, :last_name, :phone, :phone_cc])
|> validate_required([:phone, :first_name, :last_name, :phone_cc])
|> unsafe_validate_unique([:phone], Repo)
end
def settings_changeset(profile, attrs \\ %{}) do
profile
|> cast(attrs, [:first_name, :last_name, :settings, :phone, :phone_cc])
|> unsafe_validate_unique([:phone], Repo)
end
def privacy_notify_settings_changeset(profile, attrs \\ %{}) do
profile
|> cast(attrs, [:settings])
end
def bio_settings_changeset(profile, attrs \\ %{}) do
profile
|> cast(attrs, [:first_name, :last_name, :biography, :age, :gender, :country_id, :city_id, :user_id])
|> cast_attachments(attrs, [:image, :cover_photo])
|> cast_assoc(:jobs)
|> cast_assoc(:educations)
end
def image_url({id, image}) do
case image do
nil -> "/images/profiles/nav_profile_default.png"
_ -> "https://ami-aws-s3.s3.amazonaws.com/uploads/profile/image/#{id}/#{image}"
end
end
def small_thumb_image_url({id, image}) do
case image do
nil ->
"/images/profiles/nav_profile_default.png"
_ ->
case Application.get_env(:ami_web, :env) do
:dev ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/profile/image/#{id}/small_thumb_#{image}"
:prod ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/profile/image/#{id}/small_thumb_#{image}"
:staging ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/profile/image/#{id}/small_thumb_#{image}"
:test ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/profile/image/#{id}/small_thumb_#{image}"
_ ->
""
end
end
end
def created_since_in_week(profile) do
Interval.new(from: profile.created_at, until: DateTime.utc_now())
|> Interval.duration(:weeks)
end
# NOTE: maybe useless now
def uploader_image_url(profile) do
case profile.image do
nil ->
"/images/profiles/nav_profile_default.png"
%{file_name: file_name, updated_at: _} ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/profile/image/#{profile.id}/#{file_name}"
end
end
# NOTE: maybe useless now
def uploader_coverphoto_url(profile) do
case profile.cover_photo do
nil ->
"/images/profiles/nav_profile_default.png"
%{file_name: file_name, updated_at: _} ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/profile/cover_photo/#{profile.id}/#{file_name}"
end
end
end