mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-21 15:54:22 +01:00
(feat) OpenAPI endpoint to fetch subscription data
This commit is contained in:
parent
74ebad6dc0
commit
37989aec63
@ -1,5 +1,6 @@
|
|||||||
# Changelog Fab-manager
|
# Changelog Fab-manager
|
||||||
|
|
||||||
|
- OpenAPI endpoint to fetch subscription data
|
||||||
- Fix a bug: invalid date display in negative timezones
|
- Fix a bug: invalid date display in negative timezones
|
||||||
|
|
||||||
## v5.6.10 2023 February 02
|
## v5.6.10 2023 February 02
|
||||||
|
43
app/controllers/open_api/v1/subscriptions_controller.rb
Normal file
43
app/controllers/open_api/v1/subscriptions_controller.rb
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# authorized 3rd party softwares can fetch the subscriptions through the OpenAPI
|
||||||
|
class OpenAPI::V1::SubscriptionsController < OpenAPI::V1::BaseController
|
||||||
|
extend OpenAPI::ApiDoc
|
||||||
|
include Rails::Pagination
|
||||||
|
expose_doc
|
||||||
|
|
||||||
|
def index
|
||||||
|
@subscriptions = Subscription.order(created_at: :desc)
|
||||||
|
.includes(:plan, statistic_profile: :user)
|
||||||
|
.references(:statistic_profile, :plan)
|
||||||
|
|
||||||
|
@subscriptions = @subscriptions.where('created_at >= ?', DateTime.parse(params[:after])) if params[:after].present?
|
||||||
|
@subscriptions = @subscriptions.where('created_at <= ?', DateTime.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 = @subscriptions.page(page).per(per_page)
|
||||||
|
@pageination_meta = pageination_meta
|
||||||
|
paginate @subscriptions, per_page: per_page
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def page
|
||||||
|
params[:page] || 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def per_page
|
||||||
|
params[:per_page] || 20
|
||||||
|
end
|
||||||
|
|
||||||
|
def pageination_meta
|
||||||
|
total_count = Subscription.count
|
||||||
|
{
|
||||||
|
total_count: total_count,
|
||||||
|
total_pages: (total_count / per_page.to_f).ceil,
|
||||||
|
page: page.to_i,
|
||||||
|
page_size: per_page.to_i
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
56
app/doc/open_api/v1/subscriptions_doc.rb
Normal file
56
app/doc/open_api/v1/subscriptions_doc.rb
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# openAPI documentation for subscriptions endpoints
|
||||||
|
class OpenAPI::V1::SubscriptionsDoc < OpenAPI::V1::BaseDoc
|
||||||
|
resource_description do
|
||||||
|
short 'Subscriptions'
|
||||||
|
desc 'Subscriptions'
|
||||||
|
formats FORMATS
|
||||||
|
api_version API_VERSION
|
||||||
|
end
|
||||||
|
|
||||||
|
include OpenAPI::V1::Concerns::ParamGroups
|
||||||
|
|
||||||
|
doc_for :index do
|
||||||
|
api :GET, "/#{API_VERSION}/subscriptions", 'Subscriptions index'
|
||||||
|
description "Index of users' subscriptions, with optional pagination. Order by *created_at* descendant."
|
||||||
|
param_group :pagination
|
||||||
|
param :user_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various users.'
|
||||||
|
param :plan_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various plans.'
|
||||||
|
example <<-SUBSCRIPTIONS
|
||||||
|
# /open_api/v1/subscriptions?user_id=211&page=1&per_page=3
|
||||||
|
{
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"id": 2809,
|
||||||
|
"user_id": 211,
|
||||||
|
"created_at": "2022-08-26T09:41:02.426+02:00",
|
||||||
|
"expiration_date": "2022-09-26T09:41:02.427+02:00",
|
||||||
|
"canceled_at": null,
|
||||||
|
"plan_id": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2783,
|
||||||
|
"user_id": 211,
|
||||||
|
"created_at": "2022-06-06T20:03:33.470+02:00",
|
||||||
|
"expiration_date": "2022-07-06T20:03:33.470+02:00",
|
||||||
|
"canceled_at": null,
|
||||||
|
"plan_id": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2773,
|
||||||
|
"user_id": 211,
|
||||||
|
"created_at": "2021-12-23T19:26:36.852+01:00",
|
||||||
|
"expiration_date": "2022-01-23T19:26:36.852+01:00",
|
||||||
|
"canceled_at": null,
|
||||||
|
"plan_id": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"total_pages": 3,
|
||||||
|
"total_count": 9,
|
||||||
|
"page": 1,
|
||||||
|
"page_siez": 3
|
||||||
|
}
|
||||||
|
SUBSCRIPTIONS
|
||||||
|
end
|
||||||
|
end
|
10
app/views/open_api/v1/subscriptions/index.json.jbuilder
Normal file
10
app/views/open_api/v1/subscriptions/index.json.jbuilder
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
json.data @subscriptions do |subscription|
|
||||||
|
json.extract! subscription, :id, :created_at, :expiration_date, :canceled_at, :plan_id
|
||||||
|
json.user_id subscription.statistic_profile.user_id
|
||||||
|
end
|
||||||
|
json.total_pages @pageination_meta[:total_pages]
|
||||||
|
json.total_count @pageination_meta[:total_count]
|
||||||
|
json.page @pageination_meta[:page]
|
||||||
|
json.page_siez @pageination_meta[:page_size]
|
@ -282,6 +282,7 @@ Rails.application.routes.draw do
|
|||||||
resources :events
|
resources :events
|
||||||
resources :availabilities
|
resources :availabilities
|
||||||
resources :accounting
|
resources :accounting
|
||||||
|
resources :subscriptions
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
36
test/integration/open_api/subscriptions_test.rb
Normal file
36
test/integration/open_api/subscriptions_test.rb
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module OpenApi; end
|
||||||
|
|
||||||
|
class OpenApi::SubscriptionsTest < ActionDispatch::IntegrationTest
|
||||||
|
def setup
|
||||||
|
@token = OpenAPI::Client.find_by(name: 'minitest').token
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'list all subscriptions' do
|
||||||
|
get '/open_api/v1/subscriptions', headers: open_api_headers(@token)
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'list all subscriptions with pagination' do
|
||||||
|
get '/open_api/v1/subscriptions?page=1&per_page=5', headers: open_api_headers(@token)
|
||||||
|
assert_response :success
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user