Skip to content

Instantly share code, notes, and snippets.

@ZarishIqbal
Last active January 26, 2022 10:38
Show Gist options
  • Save ZarishIqbal/0e242ed26fa79c257ee92c39113122d8 to your computer and use it in GitHub Desktop.
Save ZarishIqbal/0e242ed26fa79c257ee92c39113122d8 to your computer and use it in GitHub Desktop.
def run([
"tip_create_duplicate_accounts"
]) do
alias Sarkar.Utils.General, as: Utils
Application.ensure_all_started(:edmarkaz)
# Step 1: Set password for all schools
password = ######
# Step 2: Generate school ids from 2..9
school_ids = 2..9 |> Enum.map(fn x -> "tip3pk#{x}" end)
# Step 3: Load schools, prepare writes
school_ids
|> Enum.each(fn school_id ->
# get old school id
old_id = "PK#{String.at(school_id, 6)}"
{:ok, res} =
EdMarkaz.DB.Postgres.query(
EdMarkaz.DB,
"SELECT time, type, path, value, client_id
FROM writes
WHERE school_id=$1
order by time asc",
[old_id]
)
# Create MISchool referral table entry
referral = %{
"school_name" => school_id,
"city" => "",
"office" => "",
"user" => "",
"notes" => school_id,
"agent_name" => "",
"owner_name" => "KP Government",
"owner_phone" => "",
"school_type" => "",
"package_name" => "TALEEM3",
"type_of_login" => "",
"owner_other_job" => "",
"association_name" => "",
"area_manager_name" => "",
"computer_operator" => "",
"owner_easypaisa_number" => "",
"previous_software_name" => "",
"previous_management_system" => ""
}
#Insert into auth and MISchool referral table
case Postgrex.transaction(
EdMarkaz.DB,
fn conn ->
case EdMarkaz.DB.Postgres.query(
EdMarkaz.DB,
"INSERT INTO auth (id, password, type) values ($1, $2, $3)",
["tip3pk#{school_id}", Utils.hash(password, 52), "tip-school"]
) do
{:ok, resp} ->
{:ok, resp}
{:error, err} ->
DBConnection.rollback(
conn,
err
)
end
case EdMarkaz.DB.Postgres.query(
conn,
"INSERT INTO mischool_referrals (id, time, value) VALUES ($1, $2, $3)",
[school_id, :os.system_time(:millisecond), referral]
) do
{:ok, resp} ->
{:ok, resp}
{:error, err} ->
DBConnection.rollback(
conn,
err
)
end
end,
pool: DBConnection.Poolboy
) do
{:ok, _resp} ->
Sarkar.Store.School.save(school_id, %{
"package_info" => %{
"date" => :os.system_time(:millisecond),
"path" => ["db","package_info"],
"value" => %{
"paid" => true,
"trial_period" => 15,
"date" => :os.system_time(:millisecond)
},
"type" => "MERGE",
"client_id" => "backend"
},
"max_students" => %{
"date" => :os.system_time(:millisecond),
"path" => ["db", "max_limit"],
"value" => -1,
"type" => "MERGE",
"client_id" => "backend"
},
"targeted_instruction_access" => %{
"date" => :os.system_time(:millisecond),
"path" => ["db", "targeted_instruction_access"],
"value" => true,
"type" => "MERGE",
"client_id" => "backend"
},
"onboarding" => %{
"date" => :os.system_time(:millisecond),
"path" => ["db", "onboarding", "stage"],
"value" => "COMPLETED",
"type" => "MERGE",
"client_id" => "backend"
},
"settings" => %{
"date" => :os.system_time(:millisecond),
"path" => ["db", "settings","schoolName"],
"value" => school_id,
"type" => "MERGE",
"client_id" => "backend"
}
})
IO.puts("Account creation has been completed for #{school_id}")
{:error, err} ->
IO.inspect(err.postgres.detail)
end
#prepare mapped_writes
mapped_writes =
res.rows
|> Enum.filter(fn [_, _, path, _, _] ->
path_str = Enum.join(path, ",")
!String.contains?(path_str, "targeted_instruction")
end)
|> Enum.map(fn [time, type, path, value, client_id] ->
%{
"date" => time,
"type" => type,
"path" => path,
"value" => value,
"client_id" => client_id
}
end)
IO.inspect(mapped_writes |> length)
# Step: 4 Save writes in both flattened_school and writes table
save_school_writes([school_id], mapped_writes)
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment