Skip to content

Commit

Permalink
Add an endpoint to fetch organization workers
Browse files Browse the repository at this point in the history
  • Loading branch information
woarewe committed Apr 4, 2023
1 parent c6095fa commit f8c6705
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gem "dry-validation", "~> 1.10"
gem "grape", "~> 1.7"
gem "grape-entity", "~> 1.0.0"
gem "grape-swagger", "~> 1.6"
gem "kaminari", "~> 1.2"
gem "pg", "~> 1.1"
gem "puma", "~> 5.0"
gem "pundit", "~> 2.3.0"
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ GEM
irb (1.6.3)
reline (>= 0.3.0)
json (2.6.3)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
loofah (2.19.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -266,6 +278,7 @@ DEPENDENCIES
grape (~> 1.7)
grape-entity (~> 1.0.0)
grape-swagger (~> 1.6)
kaminari (~> 1.2)
pg (~> 1.1)
puma (~> 5.0)
pundit (~> 2.3.0)
Expand Down
6 changes: 5 additions & 1 deletion app/gateways/rest/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ class API < Grape::API
helpers(
Helpers::Authentication,
Helpers::Validation,
Helpers::Authorization
Helpers::Authorization,
Helpers::Pagination
)

PER_PAGE_LIMIT = 100
PER_PAGE_DEFAULT = 50

before do
authenticate!
end
Expand Down
31 changes: 31 additions & 0 deletions app/gateways/rest/api/helpers/pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module REST
class API
module Helpers
module Pagination
include Validation

def pagination_params!(params)
validated = validate!(params, with: ::REST::Validation::Pagination)
default_pagination_params.merge(validated)
end

def default_pagination_params
{
page: 1,
per_page: PER_PAGE_DEFAULT
}.with_indifferent_access
end

def present_paginated(collection, with:, page:, per_page:)
paginated = collection.page(page).per(per_page).order(created_at: :asc)
{
items: paginated.map { |item| with.new(item).serializable_hash },
last_page: paginated.total_pages
}
end
end
end
end
end
1 change: 1 addition & 0 deletions app/gateways/rest/api/organizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def find_requested_organization!
mount Delete

namespace(:shifts) { mount Shifts }
namespace(:workers) { mount Workers }
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/gateways/rest/api/organizations/shifts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def select_shifts(filters)
.for_organization(requested_organization)
.between(from, to)
.chronologically
.serial
end
end

Expand All @@ -24,7 +25,7 @@ class Contract < Dry::Validation::Contract
end

rule(:from, :to) do
next if values[:to] - values[:from] > MAX_PERIOD.days
next if (values[:to] - values[:from]) <= MAX_PERIOD

key(:period).failure(I18n.t!("rest.api.organizations.shifts.errors.long_period", period: MAX_PERIOD))
end
Expand Down
16 changes: 16 additions & 0 deletions app/gateways/rest/api/organizations/workers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module REST
class API
class Organizations
class Workers < Base
desc "Get organization workers"
get do
authorize! requested_organization, to: :view_workers, with: OrganizationPolicy
pagination_params!(params) => { page:, per_page: }
present_paginated(requested_organization.workers, page:, per_page:, with: Serialization::Worker)
end
end
end
end
end
12 changes: 12 additions & 0 deletions app/gateways/rest/validation/pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module REST
module Validation
class Pagination < Dry::Validation::Contract
params do
optional(:page).filled(:integer, gteq?: 1)
optional(:per_page).filled(:integer, gteq?: 1, lteq?: REST::API::PER_PAGE_LIMIT)
end
end
end
end
1 change: 1 addition & 0 deletions app/models/shift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Shift < ApplicationRecord
scope :for_worker, ->(worker) { where(worker:) }
scope :between, ->(from, to) { where(start_at: from.utc..to.utc) }
scope :chronologically, -> { order(start_at: :asc) }
scope :serial, -> { order(created_at: :asc) }

delegate :organization, to: :worker

Expand Down
1 change: 1 addition & 0 deletions app/policies/organization_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ def update?
end

alias view_shifts? update?
alias view_workers? update?
alias delete? update?
end

0 comments on commit f8c6705

Please sign in to comment.