-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.message @error |
1 change: 1 addition & 0 deletions
1
app/views/api/billing/tenants/error_messages_per_period.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.message @period_error || @error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.messages_count @messages_count |
1 change: 1 addition & 0 deletions
1
app/views/api/billing/tenants/messages_per_period.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.messages_per_period @messages_per_period |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.users_count @users_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |