2020-09-12 18:43:17 -04:00
|
|
|
# Script for populating the database. You can run it as:
|
|
|
|
#
|
|
|
|
# mix run priv/repo/seeds.exs
|
|
|
|
#
|
|
|
|
# Inside the script, you can read and write to any of your
|
|
|
|
# repositories directly:
|
|
|
|
#
|
2021-03-05 19:23:32 -05:00
|
|
|
# Shift73k.Repo.insert!(%Shift73k.SomeSchema{})
|
2020-09-12 18:43:17 -04:00
|
|
|
#
|
|
|
|
# We recommend using the bang functions (`insert!`, `update!`
|
|
|
|
# and so on) as they will fail if something goes wrong.
|
2021-03-04 22:03:27 -05:00
|
|
|
|
2021-03-05 19:23:32 -05:00
|
|
|
alias Shift73k.Repo
|
|
|
|
alias Shift73k.Accounts
|
|
|
|
alias Shift73k.Accounts.User
|
2021-03-04 22:03:27 -05:00
|
|
|
|
|
|
|
############################################################################
|
|
|
|
## INSERTING MOCK USER DATA
|
2020-09-13 05:54:08 -04:00
|
|
|
|
2021-03-06 07:56:54 -05:00
|
|
|
{:ok, _admin} =
|
2021-02-27 08:17:30 -05:00
|
|
|
Accounts.register_user(%{
|
2020-09-14 21:06:10 -04:00
|
|
|
email: "admin@company.com",
|
2021-03-06 18:36:45 -05:00
|
|
|
password: "123456789abC",
|
|
|
|
password_confirmation: "123456789abC",
|
2021-02-27 08:17:30 -05:00
|
|
|
role: Accounts.registration_role()
|
2020-09-14 21:06:10 -04:00
|
|
|
})
|
2020-09-13 05:54:08 -04:00
|
|
|
|
2021-03-06 07:56:54 -05:00
|
|
|
{:ok, _user_1} =
|
2021-02-27 08:17:30 -05:00
|
|
|
Accounts.register_user(%{
|
2020-09-14 21:06:10 -04:00
|
|
|
email: "user1@company.com",
|
2021-03-06 18:36:45 -05:00
|
|
|
password: "123456789abC",
|
|
|
|
password_confirmation: "123456789abC",
|
2021-02-27 08:17:30 -05:00
|
|
|
role: Accounts.registration_role()
|
2020-09-14 21:06:10 -04:00
|
|
|
})
|
2020-09-13 05:54:08 -04:00
|
|
|
|
2021-03-06 07:56:54 -05:00
|
|
|
{:ok, _user_2} =
|
2021-02-27 08:17:30 -05:00
|
|
|
Accounts.register_user(%{
|
2020-09-14 21:06:10 -04:00
|
|
|
email: "user2@company.com",
|
2021-03-06 18:36:45 -05:00
|
|
|
password: "123456789abC",
|
|
|
|
password_confirmation: "123456789abC",
|
2021-02-27 08:17:30 -05:00
|
|
|
role: Accounts.registration_role()
|
2020-09-14 21:06:10 -04:00
|
|
|
})
|
|
|
|
|
2021-03-04 22:03:27 -05:00
|
|
|
# if Mix.env() == :dev do
|
|
|
|
this_path = Path.dirname(__ENV__.file)
|
|
|
|
users_json = Path.join(this_path, "MOCK_DATA_users.json")
|
|
|
|
|
2021-03-07 08:01:46 -05:00
|
|
|
count_to_take = 15
|
2021-03-04 22:03:27 -05:00
|
|
|
|
2021-03-07 08:01:46 -05:00
|
|
|
mock_users = users_json |> File.read!() |> Jason.decode!() |> Enum.take_random(count_to_take)
|
2021-03-04 22:03:27 -05:00
|
|
|
|
2021-03-07 08:01:46 -05:00
|
|
|
extra_mock_users = ~s([
|
|
|
|
{"email":"adam@73k.us","password":"adamadamA1","role":"admin","inserted_at":"2018-12-14T01:01:01Z","confirmed_at":true},
|
|
|
|
{"email":"karen@73k.us","password":"karenkarenA1","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true},
|
|
|
|
{"email":"kat@73k.us","password":"katkatA1","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true}
|
|
|
|
])
|
2021-03-04 22:03:27 -05:00
|
|
|
|
2021-03-22 10:49:45 -04:00
|
|
|
# for random week_start_at values
|
|
|
|
[head | tail] = Shift73k.Util.Dt.weekdays()
|
|
|
|
week_starts = [head | Enum.drop(tail, 4)]
|
|
|
|
|
2021-03-04 22:03:27 -05:00
|
|
|
mock_users =
|
2021-03-07 08:01:46 -05:00
|
|
|
extra_mock_users
|
|
|
|
|> Jason.decode!()
|
2021-03-20 12:45:50 -04:00
|
|
|
|> Stream.concat(mock_users)
|
2021-03-07 08:01:46 -05:00
|
|
|
|> Enum.map(fn e ->
|
2021-03-04 22:03:27 -05:00
|
|
|
add_dt = NaiveDateTime.from_iso8601!(e["inserted_at"])
|
|
|
|
|
|
|
|
%{
|
|
|
|
email: e["email"],
|
|
|
|
role: String.to_existing_atom(e["role"]),
|
|
|
|
hashed_password: Bcrypt.hash_pwd_salt(e["password"]),
|
2021-03-22 10:49:45 -04:00
|
|
|
week_start_at: Enum.at(week_starts, Enum.random(0..2)),
|
2021-03-23 13:29:46 -04:00
|
|
|
calendar_slug: Ecto.UUID.generate(),
|
2021-03-04 22:03:27 -05:00
|
|
|
inserted_at: add_dt,
|
|
|
|
updated_at: add_dt,
|
|
|
|
confirmed_at: (e["confirmed_at"] && NaiveDateTime.add(add_dt, 300, :second)) || nil
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
Repo.insert_all(User, mock_users)
|
|
|
|
# end
|
2021-03-07 08:01:46 -05:00
|
|
|
|
|
|
|
#####
|
|
|
|
# shift tepmlates
|
2021-03-11 16:57:20 -05:00
|
|
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
2021-03-07 08:01:46 -05:00
|
|
|
|
|
|
|
shifts_json = Path.join(this_path, "MOCK_DATA_shift-templates.json")
|
|
|
|
mock_shifts = shifts_json |> File.read!() |> Jason.decode!()
|
|
|
|
|
|
|
|
time_from_mock = fn mock_time ->
|
|
|
|
case String.length(mock_time) do
|
|
|
|
4 -> Time.from_iso8601!("T0#{mock_time}:00")
|
|
|
|
5 -> Time.from_iso8601!("T#{mock_time}:00")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
seconds_day = 86_400
|
|
|
|
seconds_days_14 = seconds_day * 14
|
|
|
|
seconds_half_day = Integer.floor_div(seconds_day, 2)
|
|
|
|
|
|
|
|
for user <- Accounts.list_users() do
|
|
|
|
user_shifts =
|
|
|
|
mock_shifts
|
|
|
|
|> Enum.take_random(:rand.uniform(15) + 5)
|
|
|
|
|> Enum.map(fn e ->
|
|
|
|
seconds_to_add = :rand.uniform(seconds_days_14) + seconds_half_day
|
|
|
|
add_dt = NaiveDateTime.add(user.inserted_at, seconds_to_add)
|
2021-03-11 13:30:30 -05:00
|
|
|
time_start = time_from_mock.(e["time_start"])
|
|
|
|
shift_len_min = e["length_minutes"] || 0
|
|
|
|
shift_length = e["length_hours"] * 60 * 60 + shift_len_min * 60
|
|
|
|
time_end = Time.add(time_start, shift_length) |> Time.truncate(:second)
|
2021-03-07 08:01:46 -05:00
|
|
|
|
|
|
|
%{
|
|
|
|
subject: e["subject"],
|
|
|
|
description: e["description"],
|
|
|
|
location: e["location"],
|
2021-03-21 10:47:53 -04:00
|
|
|
time_zone: Tzdata.zone_list() |> Enum.random(),
|
2021-03-11 13:30:30 -05:00
|
|
|
time_start: time_start,
|
|
|
|
time_end: time_end,
|
2021-03-07 08:01:46 -05:00
|
|
|
user_id: user.id,
|
|
|
|
inserted_at: add_dt,
|
|
|
|
updated_at: add_dt
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
Repo.insert_all(ShiftTemplate, user_shifts)
|
|
|
|
end
|
2021-03-21 19:27:25 -04:00
|
|
|
|
|
|
|
#####
|
|
|
|
# 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
|