Skip to content

Commit

Permalink
feat: staff curriculum upload (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodiaslobo authored Jan 30, 2024
1 parent 3cccaa8 commit ac3735e
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/mix/tasks/gen.staffs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Mix.Tasks.Gen.Staffs do
"password_confirmation" => password
}

Accounts.create_staff(%{"user" => user})
Accounts.create_staff(%{"user" => user, "nickname" => "staff#{man_num() + 1}"})

IO.puts("#{email}:#{password}")
end)
Expand Down
6 changes: 5 additions & 1 deletion lib/mix/tasks/gen.staffs_from_csv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ defmodule Mix.Tasks.Gen.StaffsFromCsv do
"password_confirmation" => password
}

Accounts.create_staff(%{"user" => user, "is_admin" => convert!(user_csv_entry.admin)})
Accounts.create_staff(%{
"user" => user,
"is_admin" => convert!(user_csv_entry.admin),
"nickname" => user_csv_entry.username
})

IO.puts("#{email}:#{password}")
end)
Expand Down
6 changes: 6 additions & 0 deletions lib/safira/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ defmodule Safira.Accounts do
|> Repo.update()
end

def update_staff_cv(%Staff{} = staff, attrs) do
staff
|> Staff.update_cv_changeset(attrs)
|> Repo.update()
end

def delete_staff(%Staff{} = staff) do
Repo.delete(staff)
end
Expand Down
11 changes: 10 additions & 1 deletion lib/safira/accounts/staff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Safira.Accounts.Staff do
deliver them prizes they win throughout the event
"""
use Ecto.Schema
use Arc.Ecto.Schema
import Ecto.Changeset

alias Safira.Accounts.User
Expand All @@ -12,6 +13,8 @@ defmodule Safira.Accounts.Staff do
schema "staffs" do
field :active, :boolean, default: true
field :is_admin, :boolean, default: false
field :nickname, :string
field :cv, Safira.CV.Type

belongs_to :user, User
has_many :redeems, Redeem
Expand All @@ -21,8 +24,14 @@ defmodule Safira.Accounts.Staff do

def changeset(staff, attrs) do
staff
|> cast(attrs, [:active, :is_admin])
|> cast(attrs, [:active, :is_admin, :nickname])
|> cast_attachments(attrs, [:cv])
|> cast_assoc(:user)
|> validate_required([:active, :is_admin])
end

def update_cv_changeset(staff, attrs) do
staff
|> cast_attachments(attrs, [:cv])
end
end
2 changes: 1 addition & 1 deletion lib/safira/web/uploaders/cv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Safira.CV do
end

def storage_dir(_version, {_file, scope}) do
"uploads/attendee/cvs/" <> "#{scope.id}"
"uploads/user/cvs/" <> "#{scope.id}"
end

def s3_object_headers(version, {file, scope}) do

Check warning on line 53 in lib/safira/web/uploaders/cv.ex

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "scope" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 53 in lib/safira/web/uploaders/cv.ex

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "version" is unused (if the variable is not meant to be used, prefix it with an underscore)
Expand Down
28 changes: 28 additions & 0 deletions lib/safira_web/controllers/cv_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@ defmodule SafiraWeb.CVController do
use SafiraWeb, controller: "1.6"

alias Safira.Accounts
alias Safira.Accounts.Staff

Check warning on line 5 in lib/safira_web/controllers/cv_controller.ex

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

unused alias Staff

alias Safira.CV

action_fallback SafiraWeb.FallbackController

def staff_cv(conn, %{"id" => id}) do
curr_user = Accounts.get_user(conn)
user = Accounts.get_user!(id)

if Accounts.is_staff(conn) and curr_user.id == user.id do
render(conn, "staff_cv.json", staff: Accounts.get_staff!(curr_user.staff.id))
else
{:error, :unauthorized}
end
end

def staff_upload_cv(conn, %{"id" => id, "staff" => staff_params}) do
curr_user = Accounts.get_user(conn)
user = Accounts.get_user!(id)

if Accounts.is_staff(conn) and curr_user.id == user.id do
with {:ok, staff} <-
Accounts.get_staff!(curr_user.staff.id) |> Accounts.update_staff_cv(staff_params) do
conn
|> render("staff_cv.json", staff: staff)
end
else
{:error, :unauthorized}
end
end

def company_cvs(conn, %{"id" => company_id}) do
curr_user = Accounts.get_user(conn)

Expand All @@ -23,6 +50,7 @@ defmodule SafiraWeb.CVController do
if Accounts.is_admin(conn) or (curr_company_id == company.id and company.has_cv_access) do
zip =
Accounts.list_company_attendees(company_id)
|> Enum.concat(Accounts.list_staffs())
|> Enum.filter(fn x -> x.cv != nil end)
|> Enum.map(fn x ->
Zstream.entry(
Expand Down
3 changes: 3 additions & 0 deletions lib/safira_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ defmodule SafiraWeb.Router do
resources "/store/buy", BuyController, only: [:create]
resources "/roulette/prizes", PrizeController, only: [:index, :show]

get "/staff/cv/:id", CVController, :staff_cv
patch "/staff/cv/:id", CVController, :staff_upload_cv

get "/company/attendees/:id", CompanyController, :company_attendees
get "/company/attendees/cvs/:id", CVController, :company_cvs
end
Expand Down
12 changes: 12 additions & 0 deletions lib/safira_web/views/cv_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule SafiraWeb.CVView do
use SafiraWeb, :view

alias Safira.CV

def render("staff_cv.json", %{staff: staff}) do
%{
id: staff.id,
cv: CV.url({staff.cv, staff}, :original)
}
end
end
1 change: 1 addition & 0 deletions priv/repo/migrations/20180724140327_create_staff.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Safira.Repo.Migrations.CreateStaff do
create table(:staffs) do
add :active, :boolean, default: true
add :user_id, references(:users, on_delete: :delete_all)
add :nickname, :string

timestamps()
end
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20231228221359_add_cv_to_staffs.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Safira.Repo.Migrations.AddCvToStaffs do
use Ecto.Migration

def change do
alter table(:staffs) do
add :cv, :string
end
end
end

0 comments on commit ac3735e

Please sign in to comment.