some reorganization for inserting multiple shifts, setting up dev seeds
This commit is contained in:
parent
686db55e8b
commit
4c673508b5
8 changed files with 95 additions and 61 deletions
|
@ -4,4 +4,9 @@ defmodule Shift73k.Repo do
|
||||||
adapter: Ecto.Adapters.Postgres
|
adapter: Ecto.Adapters.Postgres
|
||||||
|
|
||||||
use Scrivener, page_size: 10
|
use Scrivener, page_size: 10
|
||||||
|
|
||||||
|
def timestamp(%{} = attrs) do
|
||||||
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||||
|
Map.merge(attrs, %{inserted_at: now, updated_at: now})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -87,6 +87,14 @@ defmodule Shift73k.Shifts do
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_multiple(shift_attrs) when is_list(shift_attrs) do
|
||||||
|
try do
|
||||||
|
Repo.insert_all(Shift, shift_attrs)
|
||||||
|
rescue
|
||||||
|
e in Postgrex.Error -> {:error, e.message}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Updates a shift.
|
Updates a shift.
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ defmodule Shift73k.Shifts.Shift do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
|
|
||||||
@primary_key {:id, :binary_id, autogenerate: true}
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
@foreign_key_type :binary_id
|
@foreign_key_type :binary_id
|
||||||
schema "shifts" do
|
schema "shifts" do
|
||||||
|
@ -31,14 +33,13 @@ defmodule Shift73k.Shifts.Shift do
|
||||||
:time_end,
|
:time_end,
|
||||||
:user_id
|
:user_id
|
||||||
])
|
])
|
||||||
|
|> validate_required([
|
||||||
# |> validate_required([
|
:subject,
|
||||||
# :subject,
|
:date,
|
||||||
# :date,
|
:time_zone,
|
||||||
# :time_zone,
|
:time_start,
|
||||||
# :time_start,
|
:time_end,
|
||||||
# :time_end,
|
:user_id
|
||||||
# :user_id
|
])
|
||||||
# ])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,13 +21,11 @@ defmodule Shift73k.Shifts.Templates do
|
||||||
Repo.all(ShiftTemplate)
|
Repo.all(ShiftTemplate)
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_shift_templates_by_user_id(user_id) do
|
def list_shift_templates_by_user(user_id) do
|
||||||
q =
|
from(s in ShiftTemplate)
|
||||||
from s in ShiftTemplate,
|
|> where([s], s.user_id == ^user_id)
|
||||||
where: s.user_id == ^user_id,
|
|> order_by([s], [fragment("lower(?)", s.subject), s.time_start])
|
||||||
order_by: [fragment("lower(?)", s.subject), s.time_start]
|
|> Repo.all()
|
||||||
|
|
||||||
Repo.all(q)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -77,6 +77,14 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do
|
||||||
def shift_length(len_min) when is_integer(len_min) and len_min >= 0, do: len_min
|
def shift_length(len_min) when is_integer(len_min) and len_min >= 0, do: len_min
|
||||||
def shift_length(len_min) when is_integer(len_min) and len_min < 0, do: 1440 + len_min
|
def shift_length(len_min) when is_integer(len_min) and len_min < 0, do: 1440 + len_min
|
||||||
|
|
||||||
def shift_length(time_end, time_start),
|
def shift_length(time_end, time_start) do
|
||||||
do: shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start})
|
shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start})
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get shift attrs from shift template
|
||||||
|
def attrs(%ShiftTemplate{} = shift_template) do
|
||||||
|
shift_template
|
||||||
|
|> Map.from_struct()
|
||||||
|
|> Map.drop([:__meta__, :id, :inserted_at, :updated_at, :user, :is_fave_of_user])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -81,7 +81,7 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do
|
||||||
|
|
||||||
defp init_shift_templates(%{assigns: %{current_user: user}} = socket) do
|
defp init_shift_templates(%{assigns: %{current_user: user}} = socket) do
|
||||||
shift_templates =
|
shift_templates =
|
||||||
Templates.list_shift_templates_by_user_id(user.id)
|
Templates.list_shift_templates_by_user(user.id)
|
||||||
|> Stream.map(fn t -> shift_template_option(t, user.fave_shift_template_id) end)
|
|> Stream.map(fn t -> shift_template_option(t, user.fave_shift_template_id) end)
|
||||||
|> Enum.concat([@custom_shift_opt])
|
|> Enum.concat([@custom_shift_opt])
|
||||||
|
|
||||||
|
@ -252,12 +252,7 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("select-day", %{"day" => day}, socket) do
|
def handle_event("select-day", %{"day" => day}, socket) do
|
||||||
selected_days =
|
selected_days = update_selected_days(socket.assigns.selected_days, day)
|
||||||
case Enum.member?(socket.assigns.selected_days, day) do
|
|
||||||
false -> [day | socket.assigns.selected_days]
|
|
||||||
true -> Enum.reject(socket.assigns.selected_days, fn d -> d == day end)
|
|
||||||
end
|
|
||||||
|
|
||||||
{:noreply, assign(socket, :selected_days, selected_days)}
|
{:noreply, assign(socket, :selected_days, selected_days)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -277,17 +272,15 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("save-days", _params, socket) do
|
def handle_event("save-days", _params, socket) do
|
||||||
# 1. collect attrs from loaded shift template
|
# 1. collect attrs from loaded shift template
|
||||||
shift_data = shift_data_from_template(socket.assigns.shift_template)
|
shift_data = ShiftTemplate.attrs(socket.assigns.shift_template)
|
||||||
|
|
||||||
# 2. create list of shift attrs to insert
|
# 2. fashion list of shift attrs and insert
|
||||||
to_insert =
|
socket.assigns.selected_days
|
||||||
Enum.map(socket.assigns.selected_days, &shift_from_day_and_shift_data(&1, shift_data))
|
|> Stream.map(&Date.from_iso8601!/1)
|
||||||
|
|> Stream.map(&Map.put(shift_data, :date, &1))
|
||||||
# 3. insert the data
|
|> Enum.map(&Repo.timestamp/1)
|
||||||
{status, msg} = insert_shifts(to_insert, length(socket.assigns.selected_days))
|
|> Shifts.create_multiple()
|
||||||
|
|> handle_create_multiple_result(socket)
|
||||||
socket
|
|
||||||
|> put_flash(status, msg)
|
|
||||||
|> assign(:selected_days, [])
|
|> assign(:selected_days, [])
|
||||||
|> assign_known_shifts()
|
|> assign_known_shifts()
|
||||||
|> live_noreply()
|
|> live_noreply()
|
||||||
|
@ -320,35 +313,25 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do
|
||||||
|> live_noreply()
|
|> live_noreply()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp shift_data_from_template(shift_template) do
|
defp handle_create_multiple_result(result, socket) do
|
||||||
shift_template
|
day_count = length(socket.assigns.selected_days)
|
||||||
|> Map.from_struct()
|
|
||||||
|> Map.drop([:__meta__, :id, :inserted_at, :updated_at, :user, :is_fave_of_user])
|
|
||||||
end
|
|
||||||
|
|
||||||
defp shift_from_day_and_shift_data(day, shift_data) do
|
{status, msg} =
|
||||||
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
case result do
|
||||||
date_data = %{date: Date.from_iso8601!(day), inserted_at: now, updated_at: now}
|
{:error, errmsg} ->
|
||||||
Map.merge(shift_data, date_data)
|
{:error, "Ope, problem error inserting shifts, page the dev! Message: #{errmsg}"}
|
||||||
end
|
|
||||||
|
|
||||||
defp insert_shifts(to_insert, day_count) do
|
{n, _} ->
|
||||||
Multi.new()
|
s = (n > 1 && "s") || ""
|
||||||
|> Multi.insert_all(:insert_all, Shift, to_insert)
|
|
||||||
|> Repo.transaction()
|
|
||||||
|> case do
|
|
||||||
{:ok, %{insert_all: {n, _}}} ->
|
|
||||||
s = (n > 1 && "s") || ""
|
|
||||||
|
|
||||||
if n == day_count do
|
if n == day_count do
|
||||||
{:success, "Successfully assigned shift to #{n} day#{s}"}
|
{:success, "Successfully assigned shift to #{n} day#{s}"}
|
||||||
else
|
else
|
||||||
{:warning, "Some error, only #{n} day#{s} inserted but #{day_count} were selected"}
|
{:warning, "Some error, only #{n} day#{s} inserted but #{day_count} were selected"}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
_ ->
|
put_flash(socket, status, msg)
|
||||||
{:error, "Ope, unknown error inserting shifts, page the dev"}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def shifts_to_show(day_shifts) do
|
def shifts_to_show(day_shifts) do
|
||||||
|
@ -356,4 +339,11 @@ defmodule Shift73kWeb.ShiftAssignLive.Index do
|
||||||
do: Enum.take(day_shifts, 1),
|
do: Enum.take(day_shifts, 1),
|
||||||
else: day_shifts
|
else: day_shifts
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp update_selected_days(selected_days, day) do
|
||||||
|
case Enum.member?(selected_days, day) do
|
||||||
|
false -> [day | selected_days]
|
||||||
|
true -> Enum.reject(selected_days, fn d -> d == day end)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -65,7 +65,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
|
||||||
|
|
||||||
defp assign_shift_templates(socket) do
|
defp assign_shift_templates(socket) do
|
||||||
%User{id: uid} = socket.assigns.current_user
|
%User{id: uid} = socket.assigns.current_user
|
||||||
user_shifts = Templates.list_shift_templates_by_user_id(uid)
|
user_shifts = Templates.list_shift_templates_by_user(uid)
|
||||||
assign(socket, :shift_templates, user_shifts)
|
assign(socket, :shift_templates, user_shifts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -121,3 +121,27 @@ for user <- Accounts.list_users() do
|
||||||
|
|
||||||
Repo.insert_all(ShiftTemplate, user_shifts)
|
Repo.insert_all(ShiftTemplate, user_shifts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#####
|
||||||
|
# insert shifts for each user?
|
||||||
|
alias Shift73k.Shifts
|
||||||
|
alias Shift73k.Shifts.Templates
|
||||||
|
|
||||||
|
for user <- Accounts.list_users() do
|
||||||
|
# build a date range for the time from 120 days ago to 120 days from now
|
||||||
|
today = Date.utc_today()
|
||||||
|
date_range = Date.range(Date.add(today, -120), Date.add(today, 120))
|
||||||
|
|
||||||
|
# get 3 random shift templates for user
|
||||||
|
st_list = Templates.list_shift_templates_by_user(user.id) |> Enum.take_random(3)
|
||||||
|
|
||||||
|
for st <- st_list do
|
||||||
|
days_to_schedule = Enum.take_random(date_range, 47)
|
||||||
|
shift_data = ShiftTemplate.attrs(st)
|
||||||
|
|
||||||
|
days_to_schedule
|
||||||
|
|> Stream.map(&Map.put(shift_data, :date, &1))
|
||||||
|
|> Enum.map(&Repo.timestamp/1)
|
||||||
|
|> Shifts.create_multiple()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue