Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/pearl/accounts/roles/permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Pearl.Accounts.Roles.Permissions do
"staffs" => ["show", "edit", "roles_edit"],
"challenges" => ["show", "edit", "delete"],
"companies" => ["edit"],
"tickets" => ["edit"],
"enrolments" => ["show", "edit"],
"products" => ["show", "edit", "delete"],
"purchases" => ["show", "redeem", "refund"],
Expand Down
4 changes: 4 additions & 0 deletions lib/pearl/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ defmodule Pearl.Accounts.User do
schema "users" do
field :name, :string
field :email, :string
# field :notes, :string
# field :university, :string
# field :course, :string
# field :code, :string
field :handle, :string
field :picture, Pearl.Uploaders.UserPicture.Type
field :password, :string, virtual: true, redact: true
Expand Down
168 changes: 168 additions & 0 deletions lib/pearl/ticket_types.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
defmodule Pearl.TicketTypes do
@moduledoc """
The Ticket Types context
"""
use Pearl.Context

import Ecto.Query, warn: false
alias Pearl.Repo

alias Pearl.Tickets.TicketType

@doc """
Returns the list of ticket types.

## Examples

iex> list_ticket_types()
[%TicketType{}, ...]

"""
def list_ticket_types do
TicketType
|> order_by(:priority)
|> Repo.all()
end

@doc """
Returns the list of active ticket_types.

## Examples

iex> list_active_ticket_types()
[%TicketType{}, ...]

"""

def list_active_ticket_types do
TicketType
|> where([t], t.active == true)
|> order_by(:priority)
|> Repo.all()
end

@doc """
Gets a single ticket type.

Raises `Ecto.NoResultsError` if the TicketType does not exist.

## Examples

iex> get_ticket_type!(123)
%TicketType{}

iex> get_ticket_type!(321)
** (Ecto.NoResultsError)

"""
def get_ticket_type!(id) do
Repo.get!(TicketType, id)
end

@doc """
Creates a ticket type.

## Examples

iex> create_ticket_type(%{field: value})
{:ok, %TicketType{}}

iex> create_ticket_type(%{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def create_ticket_type(attrs \\ %{}) do
%TicketType{}
|> TicketType.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a ticket type.

## Examples

iex> update_ticket_type(ticket_type, %{field: new_value})
{:ok, %TicketType{}}

iex> update_ticket_type(ticket_type, %{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def update_ticket_type(%TicketType{} = ticket_type, attrs) do
ticket_type
|> TicketType.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a ticket type.

## Examples

iex> delete_ticket_type(ticket_type)
{:ok, %TicketType{}}

iex> delete_ticket_type(ticket_type)
{:error, %Ecto.Changeset{}}

"""
def delete_ticket_type(%TicketType{} = ticket_type) do
Repo.delete(ticket_type)
end

@doc """
Archives a ticket type.

iex> archive_ticket_type(ticket_type)
{:ok, %TicketType{}}

iex> archive_ticket_type(ticket_type)
{:error, %Ecto.Changeset{}}
"""
def archive_ticket_type(%TicketType{} = ticket_type) do
ticket_type
|> TicketType.changeset(%{active: false})
|> Repo.update()
end

@doc """
Unarchives a ticket type.

iex> unarchive_ticket_type(ticket_type)
{:ok, %TicketType{}}

iex> unarchive_ticket_type(ticket_type)
{:error, %Ecto.Changeset{}}
"""
def unarchive_ticket_type(%TicketType{} = ticket_type) do
ticket_type
|> TicketType.changeset(%{active: true})
|> Repo.update()
end

@doc """
Returns the next priority a ticket type should have.

## Examples

iex> get_next_ticket_type_priority()
5
"""
def get_next_ticket_type_priority do
(Repo.aggregate(from(t in TicketType), :max, :priority) || -1) + 1
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking ticket types changes.

## Examples

iex> change_ticket_type(ticket_type)
%Ecto.Changeset{data: %TicketType{}}

"""
def change_ticket_type(%TicketType{} = ticket_type, attrs \\ %{}) do
TicketType.changeset(ticket_type, attrs)
end
end
131 changes: 131 additions & 0 deletions lib/pearl/tickets.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
defmodule Pearl.Tickets do
@moduledoc """
The Tickets context.
"""
use Pearl.Context

import Ecto.Query, warn: false
alias Pearl.Repo

alias Pearl.Tickets.Ticket

@doc """
Returns the list of tickets.

## Examples

iex> list_tickets()
[%Ticket{}, ...]

"""
def list_tickets do
Ticket
|> Repo.all()
end

def list_tickets(opts) when is_list(opts) do
Ticket
|> apply_filters(opts)
|> Repo.all()
end

def list_tickets(params) do
Ticket
|> join(:left, [t], u in assoc(t, :user), as: :user)
|> join(:left, [t], tt in assoc(t, :ticket_type), as: :ticket_type)
|> preload([user: u, ticket_type: tt], user: u, ticket_type: tt)
|> Flop.validate_and_run(params, for: Ticket)
end

def list_tickets(%{} = params, opts) when is_list(opts) do
Ticket
|> apply_filters(opts)
|> Flop.validate_and_run(params, for: Ticket)
end

@doc """
Gets a single ticket.

Raises `Ecto.NoResultsError` if the Ticket does not exist.

## Examples

iex> get_ticket!(123)
%Ticket{}

iex> get_ticket!(321)
** (Ecto.NoResultsError)

"""

def get_ticket!(id) do
Ticket
|> preload([:user, :ticket_type])
|> Repo.get!(id)
end

@doc """
Creates a ticket.

## Examples

iex> create_ticket(%{field: value})
{:ok, %Ticket{}}

iex> create_ticket(%{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def create_ticket(attrs \\ %{}) do
%Ticket{}
|> Ticket.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a ticket.

## Examples

iex> update_ticket(ticket, %{field: new_value})
{:ok, %Ticket{}}

iex> update_ticket(ticket, %{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def update_ticket(%Ticket{} = ticket, attrs) do
ticket
|> Ticket.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a ticket.

## Examples

iex> delete_ticket(ticket)
{:ok, %Ticket{}}

iex> delete_ticket(ticket)
{:error, %Ecto.Changeset{}}

"""
def delete_ticket(%Ticket{} = ticket) do
Repo.delete(ticket)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking ticket changes.

## Examples

iex> change_ticket(ticket)
%Ecto.Changeset{data: %Ticket{}}

"""
def change_ticket(%Ticket{} = ticket, attrs \\ %{}) do
Ticket.changeset(ticket, attrs)
end
end
53 changes: 53 additions & 0 deletions lib/pearl/tickets/ticket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Pearl.Tickets.Ticket do
@moduledoc """
Tickets to access the event.
"""

use Pearl.Schema

alias Pearl.Accounts.User
alias Pearl.Repo
alias Pearl.Tickets.TicketType

@derive {
Flop.Schema,
filterable: [:paid, :user_name],
sortable: [:paid, :inserted_at, :ticket_type],
default_limit: 11,
join_fields: [
ticket_type: [
binding: :ticket_type,
field: :name,
path: [:ticket_type, :name],
ecto_type: :string
],
user_name: [
binding: :user,
field: :name,
path: [:user, :name],
ecto_type: :string
]
]
}

@required_fields ~w(paid user_id ticket_type_id)a

schema "tickets" do
field :paid, :boolean

belongs_to :user, User
belongs_to :ticket_type, TicketType, on_replace: :delete

timestamps(type: :utc_datetime)
end

def changeset(ticket, attrs) do
ticket
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
|> unique_constraint(:user_id)
|> cast_assoc(:user, with: &User.profile_changeset/2)
|> unsafe_validate_unique(:user_id, Repo)
|> foreign_key_constraint(:ticket_type_id)
end
end
Loading
Loading