Skip to content

Commit 97db615

Browse files
committed
track tip transfers accurately
1 parent 3efc3ea commit 97db615

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

scripts/database_migration.exs

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ defmodule DatabaseMigration do
4949
{"Attempt", Attempt},
5050
{"Claim", Claim},
5151
{"BountyCharge", Transaction},
52-
{"BountyTransfer", Transaction},
5352
{"BountyTransfer", Tip},
53+
{"BountyTransfer", Transaction},
5454
{"GithubInstallation", Installation},
5555
{"StripeAccount", Account},
5656
{"StripeCustomer", Customer},
@@ -441,21 +441,61 @@ defmodule DatabaseMigration do
441441
}
442442
end
443443

444+
defp transform({"BountyTransfer", Tip}, row, db) do
445+
claim = db |> Map.get("Claim", []) |> Enum.find(&(&1["id"] == row["claim_id"]))
446+
447+
github_user = db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == row["github_user_id"]))
448+
449+
user = db |> Map.get("User", []) |> Enum.find(&(&1["id"] == github_user["user_id"]))
450+
451+
bounty = db |> Map.get("Bounty", []) |> Enum.find(&(&1["id"] == claim["bounty_id"]))
452+
453+
amount = Money.from_integer(String.to_integer(row["amount"]), row["currency"])
454+
455+
if !bounty do
456+
raise "Bounty not found: #{inspect(row)}"
457+
end
458+
459+
if !user do
460+
raise "User not found: #{inspect(row)}"
461+
end
462+
463+
if bounty["type"] == "tip" do
464+
%{
465+
"id" => bounty["id"] <> user["id"],
466+
"amount" => amount,
467+
"status" => nil,
468+
"ticket_id" => bounty["task_id"],
469+
"owner_id" => bounty["org_id"],
470+
"creator_id" => bounty["poster_id"],
471+
"recipient_id" => user["id"],
472+
"inserted_at" => bounty["created_at"],
473+
"updated_at" => bounty["updated_at"]
474+
}
475+
end
476+
end
477+
444478
defp transform({"BountyTransfer", Transaction}, row, db) do
445479
claim = db |> Map.get("Claim", []) |> Enum.find(&(&1["id"] == row["claim_id"]))
446480

481+
bounty = db |> Map.get("Bounty", []) |> Enum.find(&(&1["id"] == claim["bounty_id"]))
482+
447483
github_user = db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == claim["github_user_id"]))
448484

449485
user = db |> Map.get("User", []) |> Enum.find(&(&1["id"] == github_user["user_id"]))
450486

451487
amount = Money.from_integer(String.to_integer(row["amount"]), row["currency"])
452488

453-
if !claim || !user do
454-
raise "Claim or User not found: #{inspect(row)}"
489+
if !bounty do
490+
raise "Bounty not found: #{inspect(row)}"
491+
end
492+
493+
if !user do
494+
raise "User not found: #{inspect(row)}"
455495
end
456496

457497
# TODO: add corresponding credit & debit transactions
458-
%{
498+
row = %{
459499
"id" => row["id"],
460500
"provider" => "stripe",
461501
"provider_id" => row["transfer_id"],
@@ -481,46 +521,20 @@ defmodule DatabaseMigration do
481521
"contract_id" => nil,
482522
"original_contract_id" => nil,
483523
"timesheet_id" => nil,
484-
"bounty_id" => claim["bounty_id"],
524+
"bounty_id" => nil,
485525
"tip_id" => nil,
486526
"linked_transaction_id" => nil,
487527
"inserted_at" => row["created_at"],
488528
"updated_at" => row["updated_at"],
489-
"claim_id" => claim["id"]
529+
"claim_id" => nil
490530
}
491-
end
492-
493-
defp transform({"BountyTransfer", Tip}, row, db) do
494-
claim = db |> Map.get("Claim", []) |> Enum.find(&(&1["id"] == row["claim_id"]))
495-
496-
github_user = db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == row["github_user_id"]))
497-
498-
user = db |> Map.get("User", []) |> Enum.find(&(&1["id"] == github_user["user_id"]))
499-
500-
bounty = db |> Map.get("Bounty", []) |> Enum.find(&(&1["id"] == claim["bounty_id"]))
501-
502-
amount = Money.from_integer(String.to_integer(row["amount"]), row["currency"])
503-
504-
if !bounty do
505-
raise "Bounty not found: #{inspect(row)}"
506-
end
507-
508-
if !user do
509-
raise "User not found: #{inspect(row)}"
510-
end
511531

512532
if bounty["type"] == "tip" do
513-
%{
514-
"id" => row["id"],
515-
"amount" => amount,
516-
"status" => nil,
517-
"ticket_id" => bounty["task_id"],
518-
"owner_id" => bounty["org_id"],
519-
"creator_id" => bounty["poster_id"],
520-
"recipient_id" => user["id"],
521-
"inserted_at" => bounty["created_at"],
522-
"updated_at" => bounty["updated_at"]
523-
}
533+
Map.put(row, "tip_id", bounty["id"] <> user["id"])
534+
else
535+
row
536+
|> Map.put("bounty_id", claim["bounty_id"])
537+
|> Map.put("claim_id", claim["id"])
524538
end
525539
end
526540

0 commit comments

Comments
 (0)