Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Created November 21, 2024 06:20
Show Gist options
  • Save ijunaid8989/eab29f075be2ed12f67e02a69c1bbddd to your computer and use it in GitHub Desktop.
Save ijunaid8989/eab29f075be2ed12f67e02a69c1bbddd to your computer and use it in GitHub Desktop.
def list_plan_types_paginated(
search,
order_by \\ "id",
direction \\ "desc",
limit \\ 50,
cursor_after \\ nil,
cursor_before \\ nil,
opts \\ []
) do
from(p in PlanType, as: :plan_type)
|> join(:left, [p], cp in assoc(p, :core_provider))
|> join(:left, [p], cpt in assoc(p, :core_product_type))
|> join(:left_lateral, [p], cst in subquery(
from st in Truely.Core.SubscriptionType,
join: sp in Truely.Core.SubscriptionTypesPlanType,
on: sp.subscription_type_id == st.id and sp.core_plan_type_id == parent_as(:plan_type).id,
select: %{id: st.id, name: st.name, provider_subscription_type_id: st.provider_subscription_type_id}
))
|> select_merge([p, cp, cpt], %{
core_provider: cp,
core_product_type: cpt
core_subscription_types: fragment("json_agg(?)", cst)
})
|> group_by([p, cp, cpt], [p.id, cp.id, cpt.id])
|> Query.search(search, order_by, direction)
|> Repo.soft_deleted(opts)
|> Repo.paginate(
after: cursor_after,
before: cursor_before,
include_total_count: true,
cursor_fields: [
{String.to_existing_atom(order_by), String.to_existing_atom(direction)}
],
limit: limit
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment