Created
February 17, 2020 17:35
-
-
Save grigor-aramyan/a93dd15233ba565ab964b5e544df81cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reset_user_password(conn, %{"email" => email} = _params) do | |
{ user, is_org } = | |
case Repo.get_by(Innovator, email: email) do | |
i = %Innovator{} -> | |
{ i, false } | |
_ -> | |
{ nil, nil } | |
end | |
{ user, is_org } = | |
case Repo.get_by(Organization, email: email) do | |
o = %Organization{} -> | |
{ o, true } | |
_ -> | |
{ user, is_org } | |
end | |
new_password = | |
:crypto.strong_rand_bytes(10) | |
|> Base.url_encode64 | |
|> binary_part(0, 10) | |
case is_org do | |
true -> | |
case Accounts.update_organization(user, %{password: new_password}) do | |
{:ok, _org = %Organization{}} -> | |
render(conn, "simple_response.json", response: %{ | |
success: new_password, | |
error: "" | |
}) | |
{:error, _} -> | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "Could not reset your password! Try later, please" | |
}) | |
end | |
false -> | |
case Accounts.update_innovator(user, %{password: new_password}) do | |
{:ok, _inn = %Innovator{}} -> | |
render(conn, "simple_response.json", response: %{ | |
success: new_password, | |
error: "" | |
}) | |
{:error, _} -> | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "Could not reset your password! Try later, please" | |
}) | |
end | |
_ -> | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "No user with provided credentials!" | |
}) | |
end | |
end | |
def password_reset_initiated(conn, %{"email" => em} = _params) do | |
email = | |
Emails.email_for_password_reset(em) | |
try do | |
case Mailer.deliver_now(email) do | |
e = %Bamboo.Email{} -> | |
IO.inspect(e, label: "delivered") | |
render(conn, "simple_response.json", response: %{ | |
success: "True", | |
error: "" | |
}) | |
_ -> | |
render(conn, "simple_response.json", response: %{ | |
success: "False", | |
error: "Wasn't able to send your message. Try later, please!" | |
}) | |
end | |
rescue | |
Bamboo.ApiError -> | |
render(conn, "simple_response.json", response: %{ | |
success: "False", | |
error: "Wasn't able to send your message. Check your data validity, please!" | |
}) | |
end | |
end | |
def buy_subscription(conn, %{"user_id" => user_id, "user_is_org" => user_is_org, | |
"tariff_plan_name" => tariff_plan_name, "tariff_plan_price" => _amount, | |
"tariff_plan_length" => tariff_plan_length} = _params) do | |
# user_is_org: "1" => true, "0" => false | |
uri = | |
"#{@ameria_api_uri}/InitPayment" | |
tariff_plan_id = | |
if user_is_org == "1" do | |
case Repo.get_by(OrganizationsPlan, name: tariff_plan_name) do | |
op = %OrganizationsPlan{} -> op.id | |
_ -> 0 | |
end | |
else | |
case Repo.get_by(InnovatorsPlan, name: tariff_plan_name) do | |
ip = %InnovatorsPlan{} -> ip.id | |
_ -> 0 | |
end | |
end | |
user = | |
if user_is_org == "1" do | |
case Accounts.get_organization!(user_id) do | |
o = %Organization{} -> o | |
_ -> nil | |
end | |
else | |
case Accounts.get_innovator!(user_id) do | |
i = %Innovator{} -> i | |
_ -> nil | |
end | |
end | |
encode_opaque = | |
"#{user_id}:::#{user_is_org}:::#{tariff_plan_id}:::#{tariff_plan_length}" | |
:dets.open_file(:order_ids, [type: :set]) | |
data_list = | |
:dets.lookup(:order_ids, "last_order_id") | |
order_id = | |
if length(data_list) > 0 do | |
{_, id} = hd(data_list) | |
:dets.insert(:order_ids, {"last_order_id", id + 1}) | |
id + 1 | |
else | |
:dets.insert(:order_ids, {"last_order_id", 1}) | |
1 | |
end | |
IO.inspect(order_id, label: "Order id") | |
back_uri = | |
"#{@site_uri}/api/buy_subscription_callback" | |
body = | |
"{\"ClientID\": \"#{@ameria_vpos_client_id}\", | |
\"Username\": \"#{@ameria_vpos_username}\", | |
\"Password\": \"#{@ameria_vpos_password}\", | |
\"Description\": \"test description\", | |
\"BackURL\": \"#{back_uri}\", | |
\"Opaque\": \"#{encode_opaque}\", | |
\"OrderID\": #{2269010 + order_id}, | |
\"Amount\": #{10} | |
}" | |
headers = | |
[{"Content-Type", "application/json"}] | |
account_registered_days = | |
NaiveDateTime.diff(Timex.to_naive_datetime(Timex.now()), user.inserted_at) / 60 / 60 / 24 | |
if ((user_is_org == "0") && (tariff_plan_name == "Free")) do | |
case Accounts.update_innovator(user, %{ | |
innovators_plan_id: tariff_plan_id, | |
tariff_subscription_date: Date.to_string(DateTime.utc_now()), | |
tariff_expiry_date: Date.to_string(Timex.shift(Timex.now(), days: tariff_plan_length))}) do | |
{:ok, _i = %Innovator{}} -> | |
render(conn, "simple_response.json", response: %{payment_uri: ""}) | |
{:error, _} -> | |
render(conn, "simple_response.json", response: %{payment_uri: ""}) | |
end | |
else if ((user_is_org == "1") && (tariff_plan_name == "Basic") | |
&& (tariff_plan_length == 30) && (account_registered_days < 3)) do | |
case Accounts.update_organization(user, %{ | |
organizations_plan_id: tariff_plan_id, | |
tariff_subscription_date: Date.to_string(DateTime.utc_now()), | |
tariff_expiry_date: Date.to_string(Timex.shift(Timex.now(), days: tariff_plan_length))}) do | |
{:ok, _i = %Organization{}} -> | |
render(conn, "simple_response.json", response: %{payment_uri: ""}) | |
{:error, _} -> | |
render(conn, "simple_response.json", response: %{payment_uri: ""}) | |
end | |
else | |
case HTTPoison.post(uri, body, headers) do | |
{:ok, response} -> | |
IO.inspect(response.body, label: "Post output") | |
case Poison.decode(response.body) do | |
{:ok, body_json} -> | |
if (body_json["ResponseCode"] == 1 && body_json["ResponseMessage"] == "OK") do | |
uri = | |
#{}"https://services.ameriabank.am/VPOS/Payments/Pay?id=#{body_json["PaymentID"]}" | |
"https://servicestest.ameriabank.am/VPOS/Payments/Pay?id=#{body_json["PaymentID"]}" | |
IO.puts("got uri") | |
render(conn, "simple_response.json", response: %{payment_uri: uri}) | |
else | |
IO.puts("invalid res code") | |
render(conn, "simple_response.json", response: %{payment_uri: ""}) | |
end | |
{:error, _} -> | |
IO.puts("unable decode res") | |
render(conn, "simple_response.json", response: %{payment_uri: ""}) | |
end | |
{:error, reason} -> | |
IO.puts("unable to get res") | |
IO.inspect(reason, label: "Reason of failure") | |
render(conn, "simple_response.json", response: %{payment_uri: ""}) | |
end | |
end | |
end | |
end | |
def create_full_desc_req_notification(conn, %{"notification_type_code" => notification_type_code, | |
"requested_idea_id" => idea_id, "addressed_to" => receiver_id, "addressed_to_is_org" => receiver_is_org, | |
"requested_from" => reqed_from, "requested_from_is_org" => reqed_is_org, "token" => token} = params) do | |
remaining_ideas = | |
case Accounts.check_subscription(reqed_from, reqed_is_org) do | |
{:ok, ri, _rc, _r_scheme, _rg} -> ri | |
_ -> 0 | |
end | |
if remaining_ideas > 0 do | |
idea = | |
Content.get_idea!(idea_id) | |
reqed_user = | |
if reqed_is_org do | |
Accounts.get_organization!(reqed_from) | |
else | |
Accounts.get_innovator!(reqed_from) | |
end | |
complete_desc_req_body = | |
if reqed_is_org do | |
"Organization #{reqed_user.name} requested full description of your idea \"#{idea.idea_name}\"" | |
else | |
"Innovator #{reqed_user.full_name} requested full description of your idea \"#{idea.idea_name}\"" | |
end | |
desc_req_accepted_body = | |
if reqed_is_org do | |
"Organization #{reqed_user.name} accepted your request of full description of \"#{idea.idea_name}\" idea" | |
else | |
"Innovator #{reqed_user.full_name} accepted your request of full description of \"#{idea.idea_name}\" idea" | |
end | |
desc_req_rejected_body = | |
if reqed_is_org do | |
"Organization #{reqed_user.name} rejected your request of full description of \"#{idea.idea_name}\" idea" | |
else | |
"Innovator #{reqed_user.full_name} rejected your request of full description of \"#{idea.idea_name}\" idea" | |
end | |
notification_type = | |
cond do | |
notification_type_code == 0 -> "COMPLETE_DESC_REQ" | |
notification_type_code == 1 -> "DESC_REQ_ACCEPTED" | |
notification_type_code == 2 -> "DESC_REQ_REJECTED" | |
end | |
notification_title = | |
cond do | |
notification_type_code == 0 -> "Full Description Request" | |
notification_type_code == 1 -> "Full Description Request Accepted" | |
notification_type_code == 2 -> "Full Description Request Rejected" | |
end | |
notification_body = | |
cond do | |
notification_type_code == 0 -> complete_desc_req_body | |
notification_type_code == 1 -> desc_req_accepted_body | |
notification_type_code == 2 -> desc_req_rejected_body | |
end | |
final_params = | |
params | |
|> Map.delete("token") | |
|> Map.delete("notification_type_code") | |
|> Map.put("notification_type", notification_type) | |
|> Map.put("title", notification_title) | |
|> Map.put("body", notification_body) | |
case Auth.update_token_time(token) do | |
{:ok, _t = %Token{}} -> | |
case Auth.find_token(token) do | |
_tok = %Token{} -> | |
q = | |
from n in Notification, | |
where: n.notification_type == ^notification_type and n.requested_idea_id == ^idea_id and n.addressed_to == ^receiver_id and n.addressed_to_is_org == ^receiver_is_org and n.requested_from == ^reqed_from and n.requested_from_is_org == ^reqed_is_org, | |
#order_by: [desc: c.id], | |
select: n | |
notifs = Repo.all(q) | |
if (length(notifs) > 0) do | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "Already requested!" | |
}) | |
else | |
case Social.create_notification(final_params) do | |
{:ok, _nn = %Notification{}} -> | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "" | |
}) | |
{:error, _ch = %Ecto.Changeset{}} -> | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "Can't create request! Try later, please." | |
}) | |
end | |
end | |
_ -> | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "Unauthenticated access!" | |
}) | |
end | |
end | |
else | |
render(conn, "simple_response.json", response: %{ | |
success: "", | |
error: "Error or ideas limit reached!" | |
}) | |
end | |
end | |
def process_search(conn, %{"query" => query, "token" => token, | |
"user_id" => user_id, "user_is_org" => user_is_org} = _params) do | |
case Auth.update_token_time(token) do | |
{:ok, _t = %Token{}} -> | |
case Auth.find_token(token) do | |
_tok = %Token{} -> | |
user = | |
if user_is_org do | |
Accounts.get_organization!(user_id) | |
else | |
Accounts.get_innovator!(user_id) | |
end | |
{region_scheme, user_region} = | |
case user do | |
_i = %Innovator{} -> | |
tp = | |
Tarrifs.get_innovators_plan!(user.innovators_plan_id) | |
case tp.region do | |
"Current country" -> | |
{tp.region, user.country} | |
"Current region" -> | |
{tp.region, user.region} | |
"Current continent" -> | |
{tp.region, user.continent} | |
"Worldwide" -> | |
{"Worldwide", "Worldwide"} | |
end | |
_o = %Organization{} -> | |
tp = | |
Tarrifs.get_organizations_plan!(user.organizations_plan_id) | |
case tp.region do | |
"Current country" -> | |
{tp.region, user.country} | |
"Current region" -> | |
{tp.region, user.region} | |
"Current continent" -> | |
{tp.region, user.continent} | |
"Worldwide" -> | |
{"Worldwide", "Worldwide"} | |
end | |
_ -> | |
{"Current country", "Armenia"} | |
end | |
q = | |
case region_scheme do | |
"Current country" -> | |
from i in Innovator, | |
where: i.country == ^user_region, | |
select: i | |
"Current region" -> | |
from i in Innovator, | |
where: i.region == ^user_region, | |
select: i | |
"Current continent" -> | |
from i in Innovator, | |
where: i.continent == ^user_region, | |
select: i | |
"Worldwide" -> | |
from i in Innovator, | |
where: i.id > 0, | |
select: i | |
end | |
innovators_all = Repo.all(q) | |
innovators_searched = | |
innovators_all | |
|> Enum.filter(fn i -> | |
if (String.contains?(i.full_name, query) or String.contains?(i.description, query) | |
or String.contains?(i.about_me, query) or String.contains?(i.education, query) | |
or String.contains?(i.experience, query) or String.contains?(i.username, query)) do | |
true | |
else | |
false | |
end | |
end) | |
|> Enum.map(fn i -> | |
q = | |
from c in Connection, | |
where: (c.receiver_id == ^i.id and c.receiver_is_organization == false) or (c.sender_id == ^i.id and c.sender_is_organization == false), | |
select: c | |
connections = Repo.all(q) | |
%{ | |
id: i.id, | |
name: i.full_name, | |
pic_uri: i.picture_uri, | |
rating: i.rating, | |
country: i.country, | |
email: i.email, | |
ideas_count: i.generated_ideas_count, | |
innovators_plan_id: i.innovators_plan_id, | |
description: i.description, | |
about_me: i.about_me, | |
education: i.education, | |
experience: i.experience, | |
username: i.username, | |
phone: i.phone, | |
connections_count: length(connections) | |
} | |
end) | |
q2 = | |
case region_scheme do | |
"Current country" -> | |
from o in Organization, | |
where: o.country == ^user_region, | |
select: o | |
"Current region" -> | |
from o in Organization, | |
where: o.region == ^user_region, | |
select: o | |
"Current continent" -> | |
from o in Organization, | |
where: o.continent == ^user_region, | |
select: o | |
"Worldwide" -> | |
from o in Organization, | |
where: o.id > 0, | |
select: o | |
end | |
organizations_all = Repo.all(q2) | |
organizations_searched = | |
organizations_all | |
|> Enum.filter(fn o -> | |
if (String.contains?(o.name, query) or String.contains?(o.description, query) | |
or String.contains?(o.about_us, query) or String.contains?(o.industry, query) | |
or String.contains?(o.interested_industries, query) or String.contains?(o.username, query)) do | |
true | |
else | |
false | |
end | |
end) | |
|> Enum.map(fn ow -> | |
q = | |
from c in Connection, | |
where: (c.receiver_id == ^ow.id and c.receiver_is_organization == true) or (c.sender_id == ^ow.id and c.sender_is_organization == true), | |
select: c | |
connections = Repo.all(q) | |
%{ | |
id: ow.id, | |
name: ow.name, | |
pic_uri: ow.logo_uri, | |
country: ow.country, | |
email: ow.email, | |
complete_ideas_count: 0, | |
organizations_plan_id: ow.organizations_plan_id, | |
description: ow.description, | |
webpage: ow.webpage, | |
about_us: ow.about_us, | |
industry: ow.industry, | |
interested_industries: ow.interested_industries, | |
username: ow.username, | |
phone: ow.phone, | |
rating: ow.rating, | |
connections_count: length(connections) | |
} | |
end) | |
ideas_all = Content.list_ideas | |
ideas_searched = | |
ideas_all | |
|> Enum.filter(fn ia -> | |
if ((String.contains?(ia.short_description, query) or String.contains?(ia.idea_name, query) | |
or String.contains?(ia.industry, query) or String.contains?(ia.tags, query)) | |
and (ia.organization_id == nil)) do | |
true | |
else | |
false | |
end | |
end) | |
|> Enum.map(fn ia -> | |
innovator = Accounts.get_innovator!(ia.innovator_id) | |
%{ | |
id: ia.id, | |
short_description: ia.short_description, | |
idea_name: ia.idea_name, | |
industry: ia.industry, | |
tags: ia.tags, | |
price: ia.price, | |
picture_uris: ia.picture_uris, | |
video_uri: ia.video_uri, | |
innovator_id: ia.innovator_id, | |
innovator_name: innovator.full_name, | |
innovator_pic: innovator.picture_uri | |
} | |
end) | |
posts_all = Content.list_posts() | |
posts_searched = | |
posts_all | |
|> Enum.filter(fn p -> | |
if (String.contains?(p.message, query)) do | |
true | |
else | |
false | |
end | |
end) | |
|> Enum.map(fn p -> | |
author_is_org = | |
if p.innovator_id == nil do | |
true | |
else | |
false | |
end | |
author = | |
if author_is_org do | |
case Accounts.get_organization!(p.organization_id) do | |
a_o = %Organization{} -> | |
a_o | |
_ -> | |
nil | |
end | |
else | |
case Accounts.get_innovator!(p.innovator_id) do | |
a_i = %Innovator{} -> | |
a_i | |
_ -> | |
nil | |
end | |
end | |
author_name = | |
if author_is_org do | |
author.name | |
else | |
author.full_name | |
end | |
author_pic = | |
if author_is_org do | |
author.logo_uri | |
else | |
author.picture_uri | |
end | |
%{ | |
author_name: author_name, | |
author_desc: author.description, | |
author_pic: author_pic, | |
author_id: author.id, | |
post_id: p.id, | |
post_media_file: p.media_file_uri, | |
post_message: p.message, | |
post_likes: p.likes | |
} | |
end) | |
render(conn, "simple_response.json", response: %{ | |
innovators: innovators_searched, | |
organizations: organizations_searched, | |
ideas: ideas_searched, | |
posts: posts_searched | |
}) | |
_ -> | |
render(conn, "simple_response.json", response: %{ | |
innovators: [], | |
organizations: [], | |
ideas: [], | |
posts: [] | |
}) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment