Packages
ami
0.2.0
0.3.9
0.3.8
retired
0.3.7
retired
0.3.6
retired
0.3.5
retired
0.3.4
retired
0.3.3
retired
0.3.2
retired
0.3.1
retired
0.3.0
retired
0.2.9
retired
0.2.8
retired
0.2.7
retired
0.2.6
retired
0.2.5
retired
0.2.3
retired
0.2.2
retired
0.2.1
retired
0.2.0
retired
0.1.9
retired
0.1.8
retired
0.1.7
retired
0.1.6
retired
0.1.5
retired
0.1.4
retired
Package containing all AMI Models shared between micro-services.
Retired package: Security issue - retiring
Current section
Files
Jump to
Current section
Files
lib/ami/event.ex
defmodule Ami.Event do
use Ecto.Schema
import Ecto.{
Query
}
alias Ami.{
User,
Repo,
# Community,
CommunityEvent
}
schema "events" do
@timestamps_opts [type: :naive_datetime_usec]
field(:title, :string)
field(:body, :string)
field(:event_type, :integer)
field(:starts_on, :utc_datetime_usec)
field(:ends_on, :utc_datetime_usec)
field(:created_at, :utc_datetime_usec)
field(:updated_at, :utc_datetime_usec)
field(:weekly_recurrence, :boolean, default: false)
field(:whole_community, :boolean, default: false)
belongs_to(:author, User, foreign_key: :user_id)
has_many(:community_events, CommunityEvent)
has_many(:communities, through: [:community_events, :community])
end
defp start_date_for(frequency) do
case frequency do
"daily" ->
Timex.beginning_of_day(Timex.now())
"weekly" ->
Timex.beginning_of_week(Timex.now())
"monthly" ->
Timex.beginning_of_month(Timex.now())
end
end
def fetch_nonmember_news(frequency) do
start_date = start_date_for(frequency)
Repo.all(
from(
e in __MODULE__,
where: e.event_type == 0 and e.starts_on >= ^start_date,
or_where: e.event_type == 0 and e.weekly_recurrence == true
)
)
end
def fetch_member_news(%Ami.User{} = user, frequency) do
user = Repo.preload(user, [:communities])
user_communities_ids = Enum.map(user.communities, fn com -> com.id end)
start_date = start_date_for(frequency)
community_news =
Repo.all(
from(
e in __MODULE__,
where: e.event_type == 1,
where: e.starts_on >= ^start_date,
where: e.whole_community == true,
or_where: e.whole_community == true and e.weekly_recurrence == true
)
)
|> Enum.concat(
Repo.all(
from(
e in __MODULE__,
where: e.event_type == 1,
where: e.weekly_recurrence == false,
join: c in assoc(e, :communities),
where: c.id in ^user_communities_ids,
where: e.starts_on >= ^start_date
)
)
)
|> Enum.concat(
Repo.all(
from(
e in __MODULE__,
where: e.event_type == 1,
join: c in assoc(e, :communities),
where: c.id in ^user_communities_ids,
where: e.weekly_recurrence == true
)
)
)
|> Enum.uniq()
if length(community_news) == 0, do: fetch_nonmember_news(frequency), else: community_news
end
end