Skip to content

Commit

Permalink
Billing APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
stage-rl committed Dec 8, 2023
1 parent 14e9d83 commit be81f6f
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 17 deletions.
14 changes: 7 additions & 7 deletions app/controllers/api/admin/boxes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
class Api::Admin::BoxesController < ActionController::Base
include AuditableApiEvents
before_action :set_tenant
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from ActiveRecord::RecordNotFound, with: :save_exception
rescue_from ActionController::ParameterMissing, with: :save_exception

def create
begin
@box = @tenant.boxes.new(box_params)
rescue ActionController::ParameterMissing, ActiveRecord::NotFound => e
@exception = e
end

@box = @tenant.boxes.new(box_params)
return if @box.save

render :error, status: :unprocessable_entity
Expand All @@ -25,4 +21,8 @@ def set_tenant
def box_params
params.require(:box).permit(:id, :name, :short_name, :uri, :color, :api_connection_id)
end

def save_exception(exception)
@exception = exception
end
end
15 changes: 6 additions & 9 deletions app/controllers/api/admin/tenants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
class Api::Admin::TenantsController < ActionController::Base
include AuditableApiEvents
before_action :set_tenant, only: %i[destroy]
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from ActiveRecord::RecordNotFound, with: :save_exception
rescue_from ActionController::ParameterMissing, with: :save_exception

def create
begin
@tenant, @admin, @group_membership = Tenant.create_with_admin(tenant_params)
rescue ActionController::ParameterMissing => e
@error = e
end
@tenant, @admin, @group_membership = Tenant.create_with_admin(tenant_params)
render :error, status: :unprocessable_entity unless @group_membership
log_api_call(:create_tenant_api_called)
end

def destroy
return if @tenant.destroy

render json: { message: @tenant.errors.full_messages[0] }, status: :unprocessable_entity
render :error, status: :unprocessable_entity
log_api_call(:create_tenant_api_called)
end

Expand All @@ -30,7 +27,7 @@ def tenant_params
params.require(:tenant).permit(:name, { admin: [:name, :email] })
end

def not_found
render json: { message: 'not found' }, status: :not_found
def save_exception(exception)
@exception = exception
end
end
42 changes: 42 additions & 0 deletions app/controllers/api/billing/tenants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Api::Billing::TenantsController < ActionController::Base
include AuditableApiEvents
before_action :set_tenant
rescue_from ActiveRecord::RecordNotFound, with: :save_exception
rescue_from ActionController::ParameterMissing, with: :save_exception

def users_count
@users_count = @tenant.users.count
render :error, status: :unprocessable_entity unless @tenant
end

def messages_per_period
from_period = Time.zone.parse(params[:from])
till_period = Time.zone.parse(params[:till])
if @tenant && from_period && till_period
@messages_per_period = Message.joins(thread: :box).where(box: { tenant_id: @tenant.id }).where("messages.created_at between ? and ?", from_period, till_period).count
else
@period_error = "From period missing" unless from_period
@period_error = "Till period missing" unless till_period
render :error_messages_per_period, status: :unprocessable_entity
end
end

def messages_count
@messages_count = Message.joins(thread: :box).where(box: { tenant_id: @tenant.id }).count
render :error, status: :unprocessable_entity unless @tenant
end

private

def set_tenant
@tenant = Tenant.find(params[:tenant_id])
end

def tenant_params
params.require(:tenant).permit(:name, { admin: [:name, :email] })
end

def save_exception(exception)
@exception = exception
end
end
2 changes: 1 addition & 1 deletion app/views/api/admin/tenants/error.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
json.message @error ||
json.message @exception ||
@tenant.errors.full_messages[0] ||
@admin.errors.full_messages[0] ||
@group_membership.errors.full_messages[0]
1 change: 1 addition & 0 deletions app/views/api/billing/tenants/error.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.message @error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.message @period_error || @error
1 change: 1 addition & 0 deletions app/views/api/billing/tenants/messages_count.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.messages_count @messages_count
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.messages_per_period @messages_per_period
1 change: 1 addition & 0 deletions app/views/api/billing/tenants/users_count.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.users_count @users_count
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@
resources :boxes
end
end
namespace :billing do
resources :tenants, only: [] do
get :users_count
get :messages_per_period
get :messages_count
end
end
end

get :auth, path: 'prihlasenie', to: 'sessions#login'
Expand Down
28 changes: 28 additions & 0 deletions test/integration/billing_api_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "test_helper"

class BillingApiTest < ActionDispatch::IntegrationTest
test "can read number of users" do
tenant = tenants(:solver)
get "/api/billing/tenants/#{tenant.id}/users_count", params: {}, as: :json
assert_response :success
json_response = JSON.parse(response.body)
assert json_response["users_count"].positive?
end

test "can read number of messages per period" do
tenant = tenants(:solver)
get "/api/billing/tenants/#{tenant.id}/messages_per_period",
params: { from: Time.zone.now - 100.days, till: Time.zone.now }, as: :json
assert_response :success
json_response = JSON.parse(response.body)
assert json_response["messages_per_period"].positive?
end

test "can read number of messages" do
tenant = tenants(:solver)
get "/api/billing/tenants/#{tenant.id}/messages_count", params: {}, as: :json
assert_response :success
json_response = JSON.parse(response.body)
assert json_response["messages_count"].positive?
end
end

0 comments on commit be81f6f

Please sign in to comment.