Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
plausible-analytics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
varac-projects
plausible-analytics
Commits
b1594c63
Commit
b1594c63
authored
3 years ago
by
Uku Taht
Browse files
Options
Downloads
Patches
Plain Diff
Adjust API key limits for enterprise plans
parent
cc4966a1
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/plausible/billing/billing.ex
+24
-2
24 additions, 2 deletions
lib/plausible/billing/billing.ex
test/plausible/billing/billing_test.exs
+58
-0
58 additions, 0 deletions
test/plausible/billing/billing_test.exs
with
82 additions
and
2 deletions
lib/plausible/billing/billing.ex
+
24
−
2
View file @
b1594c63
...
...
@@ -18,14 +18,18 @@ defmodule Plausible.Billing do
changeset
=
Subscription
.
changeset
(%
Subscription
{},
format_subscription
(
params
))
Repo
.
insert
(
changeset
)
|>
check_lock_status
Repo
.
insert
(
changeset
)
|>
check_lock_status
|>
maybe_adjust_api_key_limits
end
def
subscription_updated
(
params
)
do
subscription
=
Repo
.
get_by!
(
Subscription
,
paddle_subscription_id:
params
[
"subscription_id"
])
changeset
=
Subscription
.
changeset
(
subscription
,
format_subscription
(
params
))
Repo
.
update
(
changeset
)
|>
check_lock_status
Repo
.
update
(
changeset
)
|>
check_lock_status
|>
maybe_adjust_api_key_limits
end
def
subscription_cancelled
(
params
)
do
...
...
@@ -236,5 +240,23 @@ defmodule Plausible.Billing do
defp
check_lock_status
(
err
),
do
:
err
defp
maybe_adjust_api_key_limits
({
:ok
,
subscription
})
do
plan
=
Repo
.
get_by
(
Plausible
.
Billing
.
EnterprisePlan
,
user_id:
subscription
.
user_id
,
paddle_plan_id:
subscription
.
paddle_plan_id
)
if
plan
do
user_id
=
subscription
.
user_id
api_keys
=
from
(
key
in
Plausible
.
Auth
.
ApiKey
,
where:
key
.
user_id
==
^
user_id
)
Repo
.
update_all
(
api_keys
,
set:
[
hourly_request_limit:
plan
.
hourly_api_request_limit
])
end
{
:ok
,
subscription
}
end
defp
maybe_adjust_api_key_limits
(
err
),
do
:
err
defp
paddle_api
(),
do
:
Application
.
fetch_env!
(
:plausible
,
:paddle_api
)
end
This diff is collapsed.
Click to expand it.
test/plausible/billing/billing_test.exs
+
58
−
0
View file @
b1594c63
...
...
@@ -286,6 +286,34 @@ defmodule Plausible.BillingTest do
refute
Repo
.
reload!
(
site
)
.
locked
end
test
"if user upgraded to an enterprise plan, their API key limits are automatically adjusted"
do
user
=
insert
(
:user
)
plan
=
insert
(
:enterprise_plan
,
user:
user
,
paddle_plan_id:
@plan_id
,
hourly_api_request_limit:
10_000
)
api_key
=
insert
(
:api_key
,
user:
user
,
hourly_request_limit:
1
)
Billing
.
subscription_created
(%{
"alert_name"
=>
"subscription_created"
,
"subscription_id"
=>
@subscription_id
,
"subscription_plan_id"
=>
@plan_id
,
"update_url"
=>
"update_url.com"
,
"cancel_url"
=>
"cancel_url.com"
,
"passthrough"
=>
user
.
id
,
"status"
=>
"active"
,
"next_bill_date"
=>
"2019-06-01"
,
"unit_price"
=>
"6.00"
,
"currency"
=>
"EUR"
})
assert
Repo
.
reload!
(
api_key
)
.
hourly_request_limit
==
plan
.
hourly_api_request_limit
end
end
describe
"subscription_updated"
do
...
...
@@ -332,6 +360,36 @@ defmodule Plausible.BillingTest do
refute
Repo
.
reload!
(
site
)
.
locked
end
test
"if user upgraded to an enterprise plan, their API key limits are automatically adjusted"
do
user
=
insert
(
:user
)
subscription
=
insert
(
:subscription
,
user:
user
)
plan
=
insert
(
:enterprise_plan
,
user:
user
,
paddle_plan_id:
"new-plan-id"
,
hourly_api_request_limit:
10_000
)
api_key
=
insert
(
:api_key
,
user:
user
,
hourly_request_limit:
1
)
Billing
.
subscription_updated
(%{
"alert_name"
=>
"subscription_updated"
,
"subscription_id"
=>
subscription
.
paddle_subscription_id
,
"subscription_plan_id"
=>
"new-plan-id"
,
"update_url"
=>
"update_url.com"
,
"cancel_url"
=>
"cancel_url.com"
,
"passthrough"
=>
user
.
id
,
"old_status"
=>
"past_due"
,
"status"
=>
"active"
,
"next_bill_date"
=>
"2019-06-01"
,
"new_unit_price"
=>
"12.00"
,
"currency"
=>
"EUR"
})
assert
Repo
.
reload!
(
api_key
)
.
hourly_request_limit
==
plan
.
hourly_api_request_limit
end
end
describe
"subscription_cancelled"
do
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment