|
| 1 | +defmodule Algora.Jobs do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + import Ecto.Changeset |
| 5 | + import Ecto.Query |
| 6 | + |
| 7 | + alias Algora.Accounts |
| 8 | + alias Algora.Bounties.LineItem |
| 9 | + alias Algora.Jobs.JobApplication |
| 10 | + alias Algora.Jobs.JobPosting |
| 11 | + alias Algora.Payments |
| 12 | + alias Algora.Payments.Transaction |
| 13 | + alias Algora.Repo |
| 14 | + alias Algora.Util |
| 15 | + |
| 16 | + require Logger |
| 17 | + |
| 18 | + def price, do: Money.new(:USD, 499, no_fraction_if_integer: true) |
| 19 | + |
| 20 | + def list_jobs(opts \\ []) do |
| 21 | + JobPosting |
| 22 | + |> where([j], j.status == :active) |
| 23 | + |> order_by([j], desc: j.inserted_at) |
| 24 | + |> maybe_filter_by_tech_stack(opts[:tech_stack]) |
| 25 | + |> maybe_limit(opts[:limit]) |
| 26 | + |> Repo.all() |
| 27 | + |> Repo.preload(:user) |
| 28 | + end |
| 29 | + |
| 30 | + def create_job_posting(attrs) do |
| 31 | + %JobPosting{} |
| 32 | + |> JobPosting.changeset(attrs) |
| 33 | + |> Repo.insert() |
| 34 | + end |
| 35 | + |
| 36 | + defp maybe_filter_by_tech_stack(query, nil), do: query |
| 37 | + defp maybe_filter_by_tech_stack(query, []), do: query |
| 38 | + |
| 39 | + defp maybe_filter_by_tech_stack(query, tech_stack) do |
| 40 | + where(query, [j], fragment("? && ?", j.tech_stack, ^tech_stack)) |
| 41 | + end |
| 42 | + |
| 43 | + defp maybe_limit(query, nil), do: query |
| 44 | + defp maybe_limit(query, limit), do: limit(query, ^limit) |
| 45 | + |
| 46 | + @spec create_payment_session(job_posting: JobPosting.t()) :: |
| 47 | + {:ok, String.t()} | {:error, atom()} |
| 48 | + def create_payment_session(job_posting) do |
| 49 | + line_items = [%LineItem{amount: price(), title: "Job posting - #{job_posting.company_name}"}] |
| 50 | + |
| 51 | + gross_amount = LineItem.gross_amount(line_items) |
| 52 | + group_id = Nanoid.generate() |
| 53 | + |
| 54 | + Repo.transact(fn -> |
| 55 | + with {:ok, user} <- |
| 56 | + Accounts.get_or_register_user(job_posting.email, %{ |
| 57 | + type: :organization, |
| 58 | + display_name: job_posting.company_name |
| 59 | + }), |
| 60 | + {:ok, _charge} <- |
| 61 | + %Transaction{} |
| 62 | + |> change(%{ |
| 63 | + id: Nanoid.generate(), |
| 64 | + provider: "stripe", |
| 65 | + type: :charge, |
| 66 | + status: :initialized, |
| 67 | + user_id: user.id, |
| 68 | + job_id: job_posting.id, |
| 69 | + gross_amount: gross_amount, |
| 70 | + net_amount: gross_amount, |
| 71 | + total_fee: Money.zero(:USD), |
| 72 | + line_items: Util.normalize_struct(line_items), |
| 73 | + group_id: group_id, |
| 74 | + idempotency_key: "session-#{Nanoid.generate()}" |
| 75 | + }) |
| 76 | + |> Algora.Validations.validate_positive(:gross_amount) |
| 77 | + |> Algora.Validations.validate_positive(:net_amount) |
| 78 | + |> foreign_key_constraint(:user_id) |
| 79 | + |> unique_constraint([:idempotency_key]) |
| 80 | + |> Repo.insert(), |
| 81 | + {:ok, session} <- |
| 82 | + Payments.create_stripe_session( |
| 83 | + user, |
| 84 | + Enum.map(line_items, &LineItem.to_stripe/1), |
| 85 | + %{ |
| 86 | + description: "Job posting - #{job_posting.company_name}", |
| 87 | + metadata: %{"version" => Payments.metadata_version(), "group_id" => group_id} |
| 88 | + }, |
| 89 | + success_url: "#{AlgoraWeb.Endpoint.url()}/jobs?status=paid", |
| 90 | + cancel_url: "#{AlgoraWeb.Endpoint.url()}/jobs?status=canceled" |
| 91 | + ) do |
| 92 | + {:ok, session.url} |
| 93 | + end |
| 94 | + end) |
| 95 | + end |
| 96 | + |
| 97 | + def create_application(job_id, user) do |
| 98 | + %JobApplication{} |
| 99 | + |> JobApplication.changeset(%{job_id: job_id, user_id: user.id}) |
| 100 | + |> Repo.insert() |
| 101 | + end |
| 102 | + |
| 103 | + def list_user_applications(user) do |
| 104 | + JobApplication |
| 105 | + |> where([a], a.user_id == ^user.id) |
| 106 | + |> select([a], a.job_id) |
| 107 | + |> Repo.all() |
| 108 | + |> MapSet.new() |
| 109 | + end |
| 110 | +end |
0 commit comments