Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bjvoth/3d7959a0c663e9f40bb1cff89b536735 to your computer and use it in GitHub Desktop.
Save bjvoth/3d7959a0c663e9f40bb1cff89b536735 to your computer and use it in GitHub Desktop.
EP-3117: Duplicate shipping fulfillment remediation script and results
{:ok, since} = DateTime.from_naive(~N[2018-06-21 13:26:08.003], "Etc/UTC")
recent = Packlane.Mongo.find(:mongo, "fulfillments", %{tags: "courier"}) |> Enum.to_list |> Enum.filter(fn fulfillment -> DateTime.compare(fulfillment["created_at"], since) == :gt end)
iex(packlane@127.0.0.1)5> length recent
1671
iex(packlane@127.0.0.1)6> all_tracking_numbers = recent |> Enum.filter(fn f -> is_list(f["tracking_numbers"]) end) |> Enum.filter(fn f -> Map.get(f, "details") end) |> Enum.reduce([], fn f, a -> a ++ f["trackin
g_numbers"] end)
["781521295386", "781521474177", "781521571940", "781521864590", "781522545857",
"781522546198", "781522546213", "781522546614", "781522547687", "781522547724",
"781522548503", "781522548960", "781522570056", "781522570365", "781522570906",
"781522571637", "781522571660", "781523098706", "781523098691", "781523098897",
"781523100217", "781523099871", "781523100916", "781523100592", "781523101614",
"781523101669", "781523102518", "781523496542", "781523779905", "781523780324",
"781523781261", "781523781695", "781523782279", "781523782647", "781523782831",
"781523783687", "781523784411", "781523784640", "781523785521", "781523785955",
"781523786778", "781523787395", "781523787546", "781523788369", "781523788760",
"781523789126", "781523789387", "781523790060", "781523790141", "781523790840",
...]
iex(packlane@127.0.0.1)7> delete_these = recent |> Enum.filter(fn f -> is_binary(f["tracking_numbers"]) end) |> Enum.filter(fn f -> f["tracking_numbers"] in all_tracking_numbers end) |> Enum.map(fn f -> f["_id"
] end)
** (return value was a long list of BSON objects) -> #BSON.ObjectId<5b498833f1b0ff3d851b2509>,
iex(packlane@127.0.0.1)8> length delete_these
202
iex(packlane@127.0.0.1)9> length all_tracking_numbers
2751
iex(packlane@127.0.0.1)10> Packlane.Mongo.delete_many(:mongo, "fulfillments", %{"_id" => %{"$in" => delete_these}})
{:ok, %Mongo.DeleteResult{deleted_count: 202}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment