[UNEXPECTED ERROR in xsitelist:
%ArgumentError{message: "nil given for :zid. Comparison with nil is forbidden as it is unsafe. Instead write a query with is_nil/1, for example: is_nil(s.zid)"}
Code:
#- This only runs on Yehro Base
cond do
params["a"] == "lead" ->
if lead = CuratorWeb.Repo.get_by(CuratorWeb.Lead, zid: params["id"], site_id: site_id) do
lead = CuratorWeb.Repo.preload(lead, [:user, :post])
%{
"id" => lead.id,
"title" => lead.title,
"data" => lead.data,
"customer" => lead.customer,
"inserted_at" => lead.inserted_at,
"post" => %{"title" => lead.post.title, "permalink" => lead.post.permalink}
}
else
nil
end
params["a"] == "new" && params["key"] == "69.87.217.246" ->
if geo = CuratorWeb.Utils.geocode_geonames(params["name"]) do
zname =
geo["name"]
|> String.downcase()
|> String.replace(~r/[^a-z0-9]+/, "")
|> String.trim()
cname = "#{zname}.yehro.com"
s = %{
zid: Ecto.UUID.generate(),
cname: cname,
parent_id: 59,
name: geo["name"],
address: "#{geo["name"]}, #{geo["adminName1"]}",
email: "yehro@biznitos.com",
status: true,
logo: "https://s3.biznitos.com/curator/suploads/cc0f664d-da61-4037-8d36-b99bf50d31ef.png",
country_code: String.downcase(geo["countryCode"])
}
CuratorWeb.Site.changeset(%CuratorWeb.Site{}, s)
|> CuratorWeb.Repo.insert()
|> case do
{:ok, site} ->
#- Add a user_site record
us = %{
zid: Ecto.UUID.generate(),
user_id: 2,
site_id: site.id,
role: "admin",
last_ip: params["key"],
}
CuratorWeb.UserSite.changeset(%CuratorWeb.UserSite{}, us)
|> CuratorWeb.Repo.insert()
{:redirect, "/xr/xsitelist?a=rebuild&zid=#{site.zid}&key=#{params["key"]}"}
{:error, reason} ->
IO.inspect reason
{:redirect, "/sites"}
end
else
{:redirect, "/sites"}
end
params["a"] == "rebuild" && params["key"] == "69.87.217.246" ->
if site = CuratorWeb.Repo.get_by(CuratorWeb.Site, zid: params["zid"]) do
#- Get the geographical info
data = %{settings: site.settings || %{}}
data =
if geo = CuratorWeb.Utils.geocode_geonames(site.address) do
data
|> Map.put(:country_code, String.downcase(geo["countryCode"]))
|> Map.put(:city, geo["name"])
|> Map.put(:region, geo["adminName1"])
|> put_in([:settings, "lat"], geo["lat"])
|> put_in([:settings, "lng"], geo["lng"])
else
data
end
#- Build a list of nearby places (gpt-4 is fast enough)
prompt = "Return a list of the 10 biggest populated places near to #{site.address} as a comma separated list. Include #{site.address} also. Order by most populated. All places absolutely must be in the same country. It does not matter if your information is ourdated. Do not add any other information besides the list itself. Items in the list must be in the same country."
data =
if result = CuratorWeb.Utils.openai_chat(prompt, "gpt-4") do
data
|> Map.put(:kind, result[:content])
|> Map.put(:description, "Find the best service providers to help with your business, school and life in #{result[:content]}")
else
data
end
CuratorWeb.Site.changeset(site, data)
|> CuratorWeb.Repo.update()
end
{:redirect, "/sites"}
params["a"] == "delete" && params["key"] == "69.87.217.246" ->
if site = CuratorWeb.Repo.get_by(CuratorWeb.Site, zid: params["zid"]) do
CuratorWeb.Repo.transaction(fn ->
from(p in CuratorWeb.Post, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
from(p in CuratorWeb.Action, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
from(p in CuratorWeb.File, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
from(p in CuratorWeb.Lead, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
from(p in CuratorWeb.Redirect, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
from(p in CuratorWeb.Template, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
from(p in CuratorWeb.Translation, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
from(p in CuratorWeb.UserSite, where: p.site_id == ^site.id) |> CuratorWeb.Repo.delete_all()
CuratorWeb.Repo.delete(site)
end)
end
{:redirect, "/sites"}
params["a"] == "update" && params["key"] == "69.87.217.246" ->
if site = CuratorWeb.Repo.get_by(CuratorWeb.Site, zid: params["zid"]) do
data = params["data"]
CuratorWeb.Site.changeset(site, data)
|> CuratorWeb.Repo.update()
end
{:redirect, "/sites"}
params["a"] == "edit" && params["key"] == "69.87.217.246" ->
site = CuratorWeb.Repo.get_by(CuratorWeb.Site, cname: params["cname"])
site = Map.drop(site, [:__meta__, :__struct__])
Jason.decode!(Jason.encode!(site))
true ->
sites =
CuratorWeb.Site
|> where([s], s.parent_id == ^59)
|> order_by([asc: :name])
|> select([s], %{"id" => s.id, "settings" => s.settings, "zid" => s.zid, "name" => s.name, "email" => s.email, "cname" => s.cname, "kind" => s.kind, "address" => s.address})
|> CuratorWeb.Repo.all()
#- Get basic stats last 30 days
ids = Enum.map(sites, fn(s) -> s["id"] end)
views =
CuratorWeb.Action
|> where([s], s.site_id in ^ids and s.action == ^"view")
|> where([s], fragment("inserted_at >= now() - interval '30 days'"))
|> group_by([s], s.site_id)
|> select([s], %{site_id: s.site_id, num: count(s.action)})
|> CuratorWeb.Repo.all()
leads =
CuratorWeb.Lead
|> where([s], s.site_id in ^ids)
|> where([s], fragment("inserted_at >= now() - interval '30 days'"))
|> group_by([s], s.site_id)
|> select([s], %{site_id: s.site_id, num: count(s.id)})
|> CuratorWeb.Repo.all()
blogs =
CuratorWeb.Post
|> where([s], s.site_id in ^ids)
|> where([s], not is_nil(s.pubdate))
|> where([s], s.type == ^"blog")
|> group_by([s], s.site_id)
|> select([s], %{site_id: s.site_id, num: count(s.id)})
|> CuratorWeb.Repo.all()
sites =
sites
|> Enum.map(fn(s) ->
v = Enum.find(views, fn(v) -> v.site_id == s["id"] end) || %{num: 0}
l = Enum.find(leads, fn(l) -> l.site_id == s["id"] end) || %{num: 0}
b = Enum.find(blogs, fn(l) -> l.site_id == s["id"] end) || %{num: 0}
s
|> Map.put("views", v.num)
|> Map.put("leads", l.num)
|> Map.put("blogs", b.num)
end)
sites = Enum.sort_by(sites, fn(s) -> s["views"] end, :desc)
%{"sites" => sites}
end
]