Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE User's Weekly Dashboard #49

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 118 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,124 @@ Response: 422 Unprocessable Entity
"error": "Phone number must be in the format '555-555-5555'"
}
```
### User Dashboard

Get login credentials: <br>
`Refer to Companies "Get login credentials" above`

**Make sure to not only create/login a user, but to have that user also create a Company/Job Application/Contact for your Postman scripts. Refer to above endpoints to do so and make sure that user is the one creating the other resources**

#### Get Dashboard
Request:

```
GET /api/v1/users/:user_id/dashboard

Authorization: Bearer Token - put in token for user
```
Successful Response:

```
{
"data": {
"id": "5",
"type": "dashboard",
"attributes": {
"id": 5,
"name": "Danny DeVito",
"email": "[email protected]",
"dashboard": {
"weekly_summary": {
"job_applications": [
{
"id": 1,
"position_title": "Jr. CTO",
"date_applied": "2024-10-31",
"status": 1,
"notes": "Fingers crossed!",
"job_description": "Looking for Turing grad/jr dev to be CTO",
"application_url": "www.example.com",
"contact_information": "[email protected]",
"created_at": "2024-12-14T17:20:41.979Z",
"updated_at": "2024-12-14T17:20:41.979Z",
"company_id": 1,
"user_id": 5
},
{
"id": 2,
"position_title": " CTO",
"date_applied": "2024-10-31",
"status": 2,
"notes": "Fingers crossed!",
"job_description": "Looking for Turing grad/jr dev to be CTO",
"application_url": "www.testexample.com",
"contact_information": "[email protected]",
"created_at": "2024-12-14T17:37:28.465Z",
"updated_at": "2024-12-14T17:37:28.465Z",
"company_id": 2,
"user_id": 5
}
],
"contacts": [
{
"id": 1,
"first_name": "Jonny",
"last_name": "Smith",
"email": "[email protected]",
"phone_number": "555-785-5555",
"notes": "Good contact for XYZ",
"created_at": "2024-12-14T17:55:21.875Z",
"updated_at": "2024-12-14T17:55:21.875Z",
"user_id": 5,
"company_id": 1
},
{
"id": 2,
"first_name": "Josnny",
"last_name": "Smsith",
"email": "[email protected]",
"phone_number": "555-785-5555",
"notes": "Good contact for XYZ",
"created_at": "2024-12-15T01:57:14.557Z",
"updated_at": "2024-12-15T01:57:14.557Z",
"user_id": 5,
"company_id": 1
}
],
"companies": [
{
"id": 1,
"user_id": 5,
"name": "New Company",
"website": "www.company.com",
"street_address": "123 Main St",
"city": "New York",
"state": "NY",
"zip_code": "10001",
"notes": "This is a new company.",
"created_at": "2024-12-14T17:20:10.909Z",
"updated_at": "2024-12-14T17:20:10.909Z"
},
{
"id": 2,
"user_id": 5,
"name": "New Company1",
"website": "www.company1.com",
"street_address": "1231 Main St",
"city": "New York",
"state": "NY",
"zip_code": "10001",
"notes": "This is a new company1.",
"created_at": "2024-12-14T17:37:24.153Z",
"updated_at": "2024-12-14T17:37:24.153Z"
}
]
}
}
}
}
}
```

# Authentication, User Roles, and Authorization

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v1/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ContactsController < ApplicationController
before_action :authenticate_user

def index
authorize Contact
contacts = @current_user.contacts
if contacts.empty?
render json: { data: [], message: "No contacts found" }, status: :ok
Expand All @@ -13,6 +14,7 @@ def index
end

def create
authorize Contact
contact = @current_user.contacts.new(contact_params)
if contact.save
render json: ContactsSerializer.new(contact), status: :created
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/api/v1/dashboards_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Api::V1::DashboardsController < ApplicationController
before_action :authenticate_user

def show
user = current_user
authorize user
render json: DashboardSerializer.new(user), status: :ok
end
end
6 changes: 4 additions & 2 deletions app/controllers/api/v1/job_applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Api::V1::JobApplicationsController < ApplicationController
before_action :authenticate_user

def create
user = User.find(params[:user_id])
user = authorize User.find(params[:user_id])

job_application = user.job_applications.build(job_application_params)

Expand Down Expand Up @@ -34,9 +34,11 @@ def show


def index
job_applications = @current_user.job_applications
authorize JobApplication
job_applications = policy_scope(JobApplication)
render json: JobApplicationSerializer.new(job_applications), status: :ok
end

private

def job_application_params
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/api/v1/users/job_applications_controller.rb

This file was deleted.

5 changes: 3 additions & 2 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Api
module V1
class UsersController < ApplicationController

def create
authorize User
user = User.new(user_params)
authorize user

if user.save
render json: UserSerializer.new(user), status: :created
else
Expand All @@ -19,7 +21,6 @@ def index

def show
@user = authorize User.find(params[:id])

render json: UserSerializer.new(User.find(params[:id]))
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ApplicationController < ActionController::API
include Pundit::Authorization
after_action :verify_authorized

# temporary current_user testing stub until we add in authentication

def current_user
@current_user ||= self.authenticate_user
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say, "When we add authentication" do you mean the authentication using JWT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and that pseudo is deleted. I was mocking and stubbing prior to introduction of JWT, and finally got rid of the remnants.

Expand Down
3 changes: 3 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Company < ApplicationRecord
rolify strict: true
belongs_to :user
has_many :contacts
has_many :job_applications

validates :name, presence: true
validates :website, presence: true
validates :street_address, presence: true
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class User < ApplicationRecord

has_many :companies, dependent: :destroy
has_many :job_applications, dependent: :destroy
has_many :contacts, dependent: :destroy

validates :name, presence: true
validates :email, presence: true, uniqueness: true
Expand Down
27 changes: 18 additions & 9 deletions app/policies/contact_policy.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
class ContactPolicy < ApplicationPolicy
# NOTE: Up to Pundit v2.3.1, the inheritance was declared as
# `Scope < Scope` rather than `Scope < ApplicationPolicy::Scope`.
# In most cases the behavior will be identical, but if updating existing
# code, beware of possible changes to the ancestors:
# https://gist.github.com/Burgestrand/4b4bc22f31c8a95c425fc0e30d7ef1f5

def index?
admin? || user.present?
end

def create?
admin? || user.present?
end

class Scope < ApplicationPolicy::Scope
# NOTE: Be explicit about which records you allow access to!
# def resolve
# scope.all
# end

def resolve
if admin?
scope.all
elsif user?
scope.where(user: user)
else
scope.none
end
end
end
end
17 changes: 17 additions & 0 deletions app/policies/dashboard_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class DashboardPolicy < ApplicationPolicy

def show?
user == record
end

class Scope < ApplicationPolicy::Scope

# def resolve
# if user?
# scope.all
# else
# scope.none
# end
# end
end
end
25 changes: 25 additions & 0 deletions app/policies/job_application_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class JobApplicationPolicy < ApplicationPolicy

def index?
user.present?
end

def create?
record.user_id == user.id
end

def show?
record.user_id == user.id
end

class Scope < ApplicationPolicy::Scope

def resolve
if user?
scope.where(user_id: user.id)
else
scope.none
end
end
end
end
22 changes: 0 additions & 22 deletions app/policies/job_policy.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def resolve #resolve determines which records a user is allowed to access
if admin? #method inherited from ApplicationPolicy to check role of current user(:admin)
scope.all
elsif user?#method inherited from ApplicationPolicy to check role of current user(:user)
scope.where(id: user.id)
scope.where(id: user.id) || scope.where(user_id: user.id)
else
scope.none
end
Expand Down
14 changes: 14 additions & 0 deletions app/serializers/dashboard_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class DashboardSerializer
include JSONAPI::Serializer
attributes :id, :name, :email

attribute :dashboard do |user|
{
weekly_summary: {
job_applications: user.job_applications,
contacts: user.contacts,
companies: user.companies
}
}
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
resources :job_applications, only: [:create, :index, :show]
resources :companies, only: [:create, :index]
resources :contacts, only: [:create, :index]

resource :dashboard, only: :show
end

resources :sessions, only: :create
Expand Down
7 changes: 7 additions & 0 deletions spec/policies/company_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
end
end

permissions :index? do
it "allows an admin or a user to view all the companies" do
expect(subject).to permit(user, Company.new)
expect(subject).to permit(admin, Company.new)
end
end

permissions ".scope" do
let(:scope) { Pundit.policy_scope!(current_user, Company) }

Expand Down
Loading