analytics/lib/mix/tasks/create_free_subscription.ex
Adrian Gruntkowski 17b12ddaeb
Implement basics of Teams (#4658)
* Extend schemas with new fields and relationships for teams

* Implement listing sites and sites with invitations with teams

* Implement creating invitations with teams

* Implement accepting invites with teams

* Add `Teams.SiteTransfer` schema

* Implement creating ownership transfers

* Implement accepting site transfer between teams

* Make results shapes from `Teams.Memberships` role functions more consistent

* Remove :team relation from ApiKey schema

* Pass and provision team on subscription creation

* Pass and provision team on enterprise plan creation

* Implement creating site for a team

* Keep team in sync during legacy ownership transfer and invitations

* Resolve conflict in `Teams.get_or_create` without transaction

* Abstract `GracePeriod` manipulation behind `Plausible.Users`

* Put `User.start_trial` behind `Plausible.Users` API

* Sync team fields on user update, if team exists

* Sync cleaning invitations, updating and removing members

* Transfer invitations too

* Implement backfill script

* Allow separate pg repo for backfill script

* Rollback purposefully at the end

* Update backfill script with parallel processing

* Use `IS DISTINCT FROM` when comparing nullable fields

* Handle no teams to backfill case gracefully when reporting

* Parallelize guest memberships backfill

* Remove transaction wrapping and query timeouts

* Make team sync check more granular and fix formatting

* Wrap single team backfill in a transatction for consistent restarts

* Make invitation and site transfer backfills preserve invitation ID

* Update migration repo config for easier dev access

* Backfill teams for users with subscriptions without sites

* Log timestamps

* Put teams sync behind a compile-time flag

* Keep timestamps in sync and fix subscriptions backfill

* Fix formatting

* Make credo happy

* Don't `use Plausible.Migration` to avoid dialyzer complaining

None of the tooling from there is used anywhere and `@repo` can
be defined directly in the migration script.

* Drop SSL workarounds in the backfill script

---------

Co-authored-by: Adam Rutkowski <hq@mtod.org>
2024-10-21 07:35:23 +00:00

28 lines
671 B
Elixir

defmodule Mix.Tasks.CreateFreeSubscription do
use Mix.Task
use Plausible.Repo
require Logger
alias Plausible.Billing.Subscription
# coveralls-ignore-start
def run([user_id]) do
Application.ensure_all_started(:plausible)
execute(user_id)
end
def run(_), do: IO.puts("Usage - mix create_free_subscription <user_id>")
def execute(user_id) do
user = Repo.get(Plausible.Auth.User, user_id)
{:ok, team} = Plausible.Teams.get_or_create(user)
Subscription.free(%{user_id: user_id, team_id: team.id})
|> Repo.insert!()
Plausible.Teams.sync_team(user)
IO.puts("Created a free subscription for user: #{user.name}")
end
end