mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-28 09:24:24 +01:00
(api) filter reservations by date
This commit is contained in:
parent
65a7c5c73b
commit
8ef114ed43
@ -1,6 +1,7 @@
|
||||
# Changelog Fab-manager
|
||||
|
||||
- Improved upgrade script
|
||||
- OpenAPI reservation endpoint can be filtered by date
|
||||
- Fix a bug: empty advanced accounting code is not defaulted to the general setting
|
||||
- Fix a bug: invalid style in accounting codes settings
|
||||
- Fix a bug: wrong namespace for task cart_operator
|
||||
|
@ -1,9 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'concerns/accountings_filters_concern'
|
||||
|
||||
# authorized 3rd party softwares can fetch the accounting lines through the OpenAPI
|
||||
class OpenAPI::V1::AccountingController < OpenAPI::V1::BaseController
|
||||
extend OpenAPI::ApiDoc
|
||||
include Rails::Pagination
|
||||
include AccountingsFiltersConcern
|
||||
expose_doc
|
||||
|
||||
def index
|
||||
@ -16,10 +19,10 @@ class OpenAPI::V1::AccountingController < OpenAPI::V1::BaseController
|
||||
@lines = AccountingLine.order(date: :desc)
|
||||
.includes(:invoicing_profile, invoice: :payment_gateway_object)
|
||||
|
||||
@lines = @lines.where('date >= ?', Time.zone.parse(params[:after])) if params[:after].present?
|
||||
@lines = @lines.where('date <= ?', Time.zone.parse(params[:before])) if params[:before].present?
|
||||
@lines = @lines.where(invoice_id: may_array(params[:invoice_id])) if params[:invoice_id].present?
|
||||
@lines = @lines.where(line_type: may_array(params[:type])) if params[:type].present?
|
||||
@lines = filter_by_after(@lines, params)
|
||||
@lines = filter_by_before(@lines, params)
|
||||
@lines = filter_by_invoice(@lines, params)
|
||||
@lines = filter_by_line_type(@lines, params)
|
||||
|
||||
@lines = @lines.page(page).per(per_page)
|
||||
paginate @lines, per_page: per_page
|
||||
|
@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Filter the list of accounting lines by the given parameters
|
||||
module AccountingsFiltersConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
# @param lines [ActiveRecord::Relation<AccountingLine>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_after(lines, filters)
|
||||
return lines if filters[:after].blank?
|
||||
|
||||
lines.where('date >= ?', Time.zone.parse(filters[:after]))
|
||||
end
|
||||
|
||||
# @param lines [ActiveRecord::Relation<AccountingLine>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_before(lines, filters)
|
||||
return lines if filters[:before].blank?
|
||||
|
||||
lines.where('date <= ?', Time.zone.parse(filters[:before]))
|
||||
end
|
||||
|
||||
# @param lines [ActiveRecord::Relation<AccountingLine>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_invoice(lines, filters)
|
||||
return lines if filters[:invoice_id].blank?
|
||||
|
||||
lines.where(invoice_id: may_array(filters[:invoice_id]))
|
||||
end
|
||||
|
||||
# @param lines [ActiveRecord::Relation<AccountingLine>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_line_type(lines, filters)
|
||||
return lines if filters[:type].blank?
|
||||
|
||||
lines.where(line_type: may_array(filters[:type]))
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,53 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Filter the list of reservations by the given parameters
|
||||
module ReservationsFiltersConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
# @param reservations [ActiveRecord::Relation<Reservation>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_after(reservations, filters)
|
||||
return reservations if filters[:after].blank?
|
||||
|
||||
reservations.where('reservations.created_at >= ?', Time.zone.parse(filters[:after]))
|
||||
end
|
||||
|
||||
# @param reservations [ActiveRecord::Relation<Reservation>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_before(reservations, filters)
|
||||
return reservations if filters[:before].blank?
|
||||
|
||||
reservations.where('reservations.created_at <= ?', Time.zone.parse(filters[:before]))
|
||||
end
|
||||
|
||||
# @param reservations [ActiveRecord::Relation<Reservation>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_user(reservations, filters)
|
||||
return reservations if filters[:user_id].blank?
|
||||
|
||||
reservations.where(statistic_profiles: { user_id: may_array(filters[:user_id]) })
|
||||
end
|
||||
|
||||
# @param reservations [ActiveRecord::Relation<Reservation>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_reservable_type(reservations, filters)
|
||||
return reservations if filters[:reservable_type].blank?
|
||||
|
||||
reservations.where(reservable_type: format_type(filters[:reservable_type]))
|
||||
end
|
||||
|
||||
# @param reservations [ActiveRecord::Relation<Reservation>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_reservable_id(reservations, filters)
|
||||
return reservations if filters[:reservable_id].blank?
|
||||
|
||||
reservations.where(reservable_id: may_array(filters[:reservable_id]))
|
||||
end
|
||||
|
||||
# @param type [String]
|
||||
def format_type(type)
|
||||
type.singularize.classify
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Filter the list of subscriptions by the given parameters
|
||||
module SubscriptionsFiltersConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
# @param subscriptions [ActiveRecord::Relation<Subscription>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_after(subscriptions, filters)
|
||||
return subscriptions if filters[:after].blank?
|
||||
|
||||
subscriptions.where('created_at >= ?', Time.zone.parse(filters[:after]))
|
||||
end
|
||||
|
||||
# @param subscriptions [ActiveRecord::Relation<Subscription>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_before(subscriptions, filters)
|
||||
return subscriptions if filters[:before].blank?
|
||||
|
||||
subscriptions.where('created_at <= ?', Time.zone.parse(filters[:before]))
|
||||
end
|
||||
|
||||
# @param subscriptions [ActiveRecord::Relation<Subscription>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_user(subscriptions, filters)
|
||||
return subscriptions if filters[:user_id].blank?
|
||||
|
||||
subscriptions.where(statistic_profiles: { user_id: may_array(filters[:user_id]) })
|
||||
end
|
||||
|
||||
# @param subscriptions [ActiveRecord::Relation<Subscription>]
|
||||
# @param filters [ActionController::Parameters]
|
||||
def filter_by_plan(subscriptions, filters)
|
||||
return subscriptions if filters[:plan_id].blank?
|
||||
|
||||
subscriptions.where(plan_id: may_array(filters[:plan_id]))
|
||||
end
|
||||
end
|
||||
end
|
@ -1,9 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'concerns/reservations_filters_concern'
|
||||
|
||||
# public API controller for resources of type Reservation
|
||||
class OpenAPI::V1::ReservationsController < OpenAPI::V1::BaseController
|
||||
extend OpenAPI::ApiDoc
|
||||
include Rails::Pagination
|
||||
include ReservationsFiltersConcern
|
||||
expose_doc
|
||||
|
||||
def index
|
||||
@ -11,9 +14,11 @@ class OpenAPI::V1::ReservationsController < OpenAPI::V1::BaseController
|
||||
.includes(slots_reservations: :slot, statistic_profile: :user)
|
||||
.references(:statistic_profiles)
|
||||
|
||||
@reservations = @reservations.where(statistic_profiles: { user_id: may_array(params[:user_id]) }) if params[:user_id].present?
|
||||
@reservations = @reservations.where(reservable_type: format_type(params[:reservable_type])) if params[:reservable_type].present?
|
||||
@reservations = @reservations.where(reservable_id: may_array(params[:reservable_id])) if params[:reservable_id].present?
|
||||
@reservations = filter_by_after(@reservations, params)
|
||||
@reservations = filter_by_before(@reservations, params)
|
||||
@reservations = filter_by_user(@reservations, params)
|
||||
@reservations = filter_by_reservable_type(@reservations, params)
|
||||
@reservations = filter_by_reservable_id(@reservations, params)
|
||||
|
||||
@reservations = @reservations.page(page).per(per_page)
|
||||
paginate @reservations, per_page: per_page
|
||||
@ -21,10 +26,6 @@ class OpenAPI::V1::ReservationsController < OpenAPI::V1::BaseController
|
||||
|
||||
private
|
||||
|
||||
def format_type(type)
|
||||
type.singularize.classify
|
||||
end
|
||||
|
||||
def page
|
||||
params[:page] || 1
|
||||
end
|
||||
|
@ -1,9 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'concerns/subscriptions_filters_concern'
|
||||
|
||||
# authorized 3rd party softwares can fetch the subscriptions through the OpenAPI
|
||||
class OpenAPI::V1::SubscriptionsController < OpenAPI::V1::BaseController
|
||||
extend OpenAPI::ApiDoc
|
||||
include Rails::Pagination
|
||||
include SubscriptionsFiltersConcern
|
||||
expose_doc
|
||||
|
||||
def index
|
||||
@ -11,10 +14,10 @@ class OpenAPI::V1::SubscriptionsController < OpenAPI::V1::BaseController
|
||||
.includes(:plan, statistic_profile: :user)
|
||||
.references(:statistic_profile, :plan)
|
||||
|
||||
@subscriptions = @subscriptions.where('created_at >= ?', Time.zone.parse(params[:after])) if params[:after].present?
|
||||
@subscriptions = @subscriptions.where('created_at <= ?', Time.zone.parse(params[:before])) if params[:before].present?
|
||||
@subscriptions = @subscriptions.where(plan_id: may_array(params[:plan_id])) if params[:plan_id].present?
|
||||
@subscriptions = @subscriptions.where(statistic_profiles: { user_id: may_array(params[:user_id]) }) if params[:user_id].present?
|
||||
@subscriptions = filter_by_after(@subscriptions, params)
|
||||
@subscriptions = filter_by_before(@subscriptions, params)
|
||||
@subscriptions = filter_by_plan(@subscriptions, params)
|
||||
@subscriptions = filter_by_user(@subscriptions, params)
|
||||
|
||||
@subscriptions = @subscriptions.page(page).per(per_page)
|
||||
paginate @subscriptions, per_page: per_page
|
||||
|
@ -15,6 +15,8 @@ class OpenAPI::V1::ReservationsDoc < OpenAPI::V1::BaseDoc
|
||||
api :GET, "/#{API_VERSION}/reservations", 'Reservations index'
|
||||
description 'Index of reservations made by users, paginated. Ordered by *created_at* descendant.'
|
||||
param_group :pagination
|
||||
param :after, DateTime, optional: true, desc: 'Filter reservations to those created after the given date.'
|
||||
param :before, DateTime, optional: true, desc: 'Filter reservations to those created before the given date.'
|
||||
param :user_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various users.'
|
||||
param :reservable_type, %w[Event Machine Space Training], optional: true, desc: 'Scope the request to a specific type of reservable.'
|
||||
param :reservable_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various reservables.'
|
||||
|
@ -46,6 +46,19 @@ class OpenApi::ReservationsTest < ActionDispatch::IntegrationTest
|
||||
assert_equal [3], reservations[:reservations].pluck(:user_id).uniq
|
||||
end
|
||||
|
||||
test 'list all reservations with dates filtering' do
|
||||
get '/open_api/v1/reservations?after=2012-01-01T00:00:00+02:00&before=2012-12-31T23:59:59+02:00', headers: open_api_headers(@token)
|
||||
assert_response :success
|
||||
assert_equal Mime[:json], response.content_type
|
||||
|
||||
reservations = json_response(response.body)
|
||||
assert reservations[:reservations].count.positive?
|
||||
assert(reservations[:reservations].all? do |line|
|
||||
date = Time.zone.parse(line[:created_at])
|
||||
date >= '2012-01-01'.to_date && date <= '2012-12-31'.to_date
|
||||
end)
|
||||
end
|
||||
|
||||
test 'list all machine reservations for a user' do
|
||||
get '/open_api/v1/reservations?reservable_type=Machine&user_id=3', headers: open_api_headers(@token)
|
||||
assert_response :success
|
||||
|
@ -20,20 +20,38 @@ class OpenApi::SubscriptionsTest < ActionDispatch::IntegrationTest
|
||||
test 'list subscriptions with pagination' do
|
||||
get '/open_api/v1/subscriptions?page=1&per_page=5', headers: open_api_headers(@token)
|
||||
assert_response :success
|
||||
assert_equal Mime[:json], response.content_type
|
||||
|
||||
subscriptions = json_response(response.body)
|
||||
assert subscriptions[:subscriptions].count <= 5
|
||||
end
|
||||
|
||||
test 'list all subscriptions for a user' do
|
||||
get '/open_api/v1/subscriptions?user_id=3', headers: open_api_headers(@token)
|
||||
assert_response :success
|
||||
|
||||
subscriptions = json_response(response.body)
|
||||
assert_not_empty subscriptions[:subscriptions]
|
||||
assert_equal [3], subscriptions[:subscriptions].pluck(:user_id).uniq
|
||||
end
|
||||
|
||||
test 'list all subscriptions for a user with pagination' do
|
||||
get '/open_api/v1/subscriptions?user_id=3&page=1&per_page=5', headers: open_api_headers(@token)
|
||||
assert_response :success
|
||||
|
||||
subscriptions = json_response(response.body)
|
||||
assert_not_empty subscriptions[:subscriptions]
|
||||
assert_equal [3], subscriptions[:subscriptions].pluck(:user_id).uniq
|
||||
assert subscriptions[:subscriptions].count <= 5
|
||||
end
|
||||
|
||||
test 'list all subscriptions for a plan with pagination' do
|
||||
get '/open_api/v1/subscriptions?plan_id=1&page=1&per_page=5', headers: open_api_headers(@token)
|
||||
assert_response :success
|
||||
|
||||
subscriptions = json_response(response.body)
|
||||
assert_not_empty subscriptions[:subscriptions]
|
||||
assert_equal [1], subscriptions[:subscriptions].pluck(:plan_id).uniq
|
||||
assert subscriptions[:subscriptions].count <= 5
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user