Skip to content

Commit

Permalink
Orders (rauversion#66)
Browse files Browse the repository at this point in the history
* orders stripe

* QR code, ticket display

* order test

* controller tests

* orders, activate ticket

* tweaks

* tests

* fix player dup

* app

* tbk mall

* oauth token twitch

* transbank pst code

* my tickets

* ticket filters

* events comment sections

* events overview, purchases info

* handle purchase order with zero count tickets

* ticket purchase from tbk

* fix duplicate tbk gen tickets

* Event performers (rauversion#65)

* blob utils, dry

* event performers, added

* pot extract

* prose invert on track description

* Auto stash before checking out "origin/orders"

* user invitations

* live view upgrade

* hosts

* free ticket added, handled success message

* forms

* pot extract

* profile header image

* event tickets force to log in
  • Loading branch information
michelson authored Sep 26, 2022
1 parent b966dde commit 932227d
Show file tree
Hide file tree
Showing 85 changed files with 3,529 additions and 997 deletions.
2 changes: 1 addition & 1 deletion .vscode/pry.code-snippets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
// Place your chaskiq workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// Place your Rauversion workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
Expand Down
4 changes: 2 additions & 2 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
padding: 15px;
/*margin-bottom: 20px;*/
border: 1px solid transparent;
border-radius: 4px;
/*border-radius: 4px;*/
}

.alert-info {
Expand Down Expand Up @@ -85,7 +85,7 @@
margin: 15vh auto;
padding: 20px;
/*border: 1px solid #888;*/
width: 80%;
/*width: 80%;*/
}

.phx-modal-close {
Expand Down
4 changes: 2 additions & 2 deletions assets/js/hooks/infinite_scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ InfiniteScroll = {
const target = this.el?.attributes["phx-target"]?.nodeValue
window.addEventListener("scroll", e => {
if(this.pending == this.page() && scrollAt() > 90){
console.log(this.el.dataset.paginateEnd)
console.log(this.el.dataset.page, this.el.dataset.totalPages)
//console.log(this.el.dataset.paginateEnd)
//console.log(this.el.dataset.page, this.el.dataset.totalPages)
this.pending = this.page()
this.pushEventTo(target, "paginate", {})
}
Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ config :ueberauth, Ueberauth,
zoom: {Ueberauth.Strategy.Zoom, [ignores_csrf_attack: true]},
twitter: {Ueberauth.Strategy.Twitter, [ignores_csrf_attack: true]},
discord: {Ueberauth.Strategy.Discord, [ignores_csrf_attack: true]},
stripe: {Ueberauth.Strategy.Stripe, [ignores_csrf_attack: true]}
stripe: {Ueberauth.Strategy.Stripe, [ignores_csrf_attack: true]},
twitch: {Ueberauth.Strategy.Twitch, [ignores_csrf_attack: true]}
# facebook: { Ueberauth.Strategy.Facebook, [ opt1: "value", opts2: "value" ] },
# github: { Ueberauth.Strategy.Github, [ opt1: "value", opts2: "value" ] }
]
Expand Down
5 changes: 5 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ config :rauversion, Rauversion.Repo,
pool_size: 10,
stacktrace: true

# config/dev.exs
config :mux,
access_token_id: System.get_env("MUX_TOKEN_ID"),
access_token_secret: System.get_env("MUX_TOKEN_SECRET")

config :rauversion, :domain, System.get_env("HOST", "https://rauversion.com")
config :rauversion, :app_name, System.get_env("APP_NAME", "rauversion")

Expand Down
9 changes: 9 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ config :ueberauth, Ueberauth.Strategy.Stripe.OAuth,
client_id: System.get_env("STRIPE_CLIENT_ID"),
client_secret: System.get_env("STRIPE_CLIENT_SECRET")

config :ueberauth, Ueberauth.Strategy.Twitch.OAuth,
client_id: System.get_env("TWITCH_CLIENT_ID"),
client_secret: System.get_env("TWITCH_CLIENT_SECRET")

config :rauversion, tbk_mall_id: System.get_env("TBK_MALL_ID")
# rau own commerce to send fees to
config :rauversion, tbk_commerce_id: System.get_env("TBK_COMMERCE_ID")
config :rauversion, tbk_api_key: System.get_env("TBK_API_KEY")
config :rauversion, platform_event_fee: System.get_env("PLATFORM_EVENTS_FEE")
# config/runtime.exs is executed for all environments, including
# during releases. It is executed after compilation and before the
# system starts, so it is typically used to load production configuration
Expand Down
27 changes: 27 additions & 0 deletions docs/accounts.livemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Accounts

```elixir
alias Rauversion.Accounts
alias Rauversion.Accounts.User
```

## send invitation

```elixir
user = Accounts.invite_user(%User{}, %{email: "[email protected]"})
```

```elixir
alias RauversionWeb.Router.Helpers, as: Routes

conn = RauversionWeb.Endpoint

Accounts.deliver_user_invitation_instructions(
user,
&Routes.user_invitation_url(conn, :accept, &1)
)
```

```elixir
Routes.user_invitation_path(conn, :update_user, "123", 1, [])
```
61 changes: 61 additions & 0 deletions docs/tbk.livemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Rauversion

## Transbank account

Card type Detail Outcome
VISA 4051 8856 0044 6623 CVV 123 any expiration date Generate approved transactions.

AMEX 3700 0000 0002 032 CVV 1234 any expiration date Generate approved transactions.

MASTERCARD 5186 0595 5959 0568 CVV 123 any expiration date Generate declined transactions.

Redcompra 4051 8842 3993 7763 Genera transacciones aprobadas (para operaciones que permiten débito Redcompra)

Redcompra 4511 3466 6003 7060 Genera transacciones aprobadas (para operaciones que permiten débito Redcompra)

Redcompra 5186 0085 4123 3829 Genera transacciones rechazadas (para operaciones que permiten débito Redcompra)

Prepago VISA 4051 8860 0005 6590 CVV 123 cualquier fecha de expiración Generate approved transactions.

Prepago MASTERCARD 5186 1741 1062 9480 CVV 123

cualquier fecha de expiración Generate declined transactions.
When an authentication form with RUT and password appears, RUT 11.111.111-1 and password 123 must be used .

<!-- livebook:{"break_markdown":true} -->

### STATUS OF TRANSATION

```elixir
commerce_code = Transbank.Common.IntegrationCommerceCodes.webpay_plus_mall()
api_key = Transbank.Common.IntegrationApiKeys.webpay()
environment = Transbank.Webpay.WebpayPlus.MallTransaction.default_environment()
trx = Transbank.Webpay.WebpayPlus.MallTransaction.new(commerce_code, api_key, environment)
# token = "01abd19e55cbd787859f2345ae09b8c9c52e22388d72921c074ddb6d42c6aecf"
token = "01ab5bd6112f806c74b54ec640de73373a86190444b03d75973633367a100d62"

trx = Transbank.Webpay.WebpayPlus.MallTransaction.new(commerce_code, api_key, environment)

{:ok, data} = Transbank.Webpay.WebpayPlus.MallTransaction.commit(trx, token)

IO.inspect(data)

Transbank.Webpay.WebpayPlus.MallTransaction.status(trx, token)
```

```elixir
a = %{data: [%{count: 0, ticket_id: 1}, %{count: 0, ticket_id: 2}]}

a[:data] |> Enum.filter(fn x -> x.count != 0 end)

Enum.to_list(1..1)
```

```elixir
{platform_fee, _} = Float.parse(Application.get_env(:rauversion, :platform_event_fee))
total = 2000
fee_amount = total / (platform_fee * 100.0)

platform_fee
Kernel.round(fee_amount)
```
32 changes: 31 additions & 1 deletion lib/rauversion/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule Rauversion.Accounts do
from(u in User,
left_join: m in assoc(u, :followings),
# on: [follower_id: ^user.id],
where: not is_nil(u.username),
where: is_nil(m.id),
preload: [:avatar_blob, :avatar_attachment]
)
Expand Down Expand Up @@ -176,6 +177,11 @@ defmodule Rauversion.Accounts do
|> Repo.update()
end

def update_transbank(user, attrs \\ %{}) do
User.transbank_changeset(user, attrs)
|> Repo.update()
end

def avatar_url(user) do
# a = Rauversion.Accounts.get_user_by_username("michelson") |> Rauversion.Repo.preload(:avatar_blob)
case user do
Expand Down Expand Up @@ -447,6 +453,13 @@ defmodule Rauversion.Accounts do
Rauversion.OauthCredentials.get_oauth_credential_by_uid!(uid) |> Repo.preload(:user)
end

def find_by_credential_provider(provider, user_id) do
Repo.get_by(Rauversion.OauthCredentials.OauthCredential, %{
provider: provider,
user_id: user_id
})
end

def get_or_create_user(user_data) do
# %{
# avatar: "https://lh3.googleusercontent.com/a-/AFdZucqqdrPKRYQdJKA2ClMex-anYYwHjRrihFERqtcvcw=s96-c",
Expand Down Expand Up @@ -481,7 +494,7 @@ defmodule Rauversion.Accounts do
email: user_data.email,
username: username,
first_name: user_data.name,
password: "123456"
password: SecureRandom.urlsafe_base64(10)
}

Rauversion.Accounts.User.oauth_registration(%User{}, attrs)
Expand Down Expand Up @@ -525,6 +538,13 @@ defmodule Rauversion.Accounts do

# invitations

def invite_user(%User{} = user, attrs \\ %{}) do
attrs = Map.merge(attrs, %{password: SecureRandom.urlsafe_base64(10)})

User.invitation_changeset(user, attrs)
|> Repo.insert()
end

def change_user_invitation(%User{} = user, attrs \\ %{}) do
User.invitation_changeset(user, attrs)
end
Expand Down Expand Up @@ -575,4 +595,14 @@ defmodule Rauversion.Accounts do
|> Ecto.Multi.update(:user, user_update_changeset)
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, ["invitation"]))
end

# tickets

def get_event_ticket(current_user, ticket) do
ticket
|> Ecto.assoc(:purchased_tickets)
|> where([p], p.event_ticket_id == ^ticket.id)
|> limit(1)
|> Repo.one()
end
end
23 changes: 23 additions & 0 deletions lib/rauversion/accounts/settings.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Rauversion.Accounts.Settings do
use Ecto.Schema
import Ecto.Changeset

embedded_schema do
field :pst_enabled, :boolean
field :tbk_commerce_code, :string
field :tbk_test_mode, :boolean
end

@required_fields []
@optional_fields [
:pst_enabled,
:tbk_commerce_code,
:tbk_test_mode
]

def changeset(struct, attrs) do
struct
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
end
63 changes: 37 additions & 26 deletions lib/rauversion/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ defmodule Rauversion.Accounts.User do

use ActiveStorage.Attached.Model
use ActiveStorage.Attached.HasOne, name: :avatar, model: "User"
use ActiveStorage.Attached.HasOne, name: :profile_header, model: "User"

schema "users" do
field :type, :string
field :first_name, :string
field :last_name, :string
field :username, :string
Expand All @@ -23,6 +25,8 @@ defmodule Rauversion.Accounts.User do
embeds_one :notification_settings, Rauversion.Accounts.NotificationSettings,
on_replace: :update

embeds_one :settings, Rauversion.Accounts.Settings, on_replace: :update

# field :settings
has_many :articles, Rauversion.Posts.Post, on_delete: :nilify_all

Expand Down Expand Up @@ -56,6 +60,13 @@ defmodule Rauversion.Accounts.User do

has_one(:avatar_blob, through: [:avatar_attachment, :blob])

has_one(:profile_header_attachment, ActiveStorage.Attachment,
where: [record_type: "User", name: "profile_header"],
foreign_key: :record_id
)

has_one(:profile_header_blob, through: [:profile_header_attachment, :blob])

timestamps()
end

Expand All @@ -66,7 +77,8 @@ defmodule Rauversion.Accounts.User do
def profile_changeset(user, attrs, _opts \\ []) do
user
|> validate_contact_fields(attrs)
|> process_avatar(attrs)
|> Rauversion.BlobUtils.process_one_upload(attrs, "avatar")
|> Rauversion.BlobUtils.process_one_upload(attrs, "profile_header")
end

def notifications_changeset(user, attrs, _opts \\ []) do
Expand All @@ -77,12 +89,36 @@ defmodule Rauversion.Accounts.User do
)
end

def transbank_changeset(user, attrs, _opts \\ []) do
user
|> cast(attrs, [:username, :first_name, :last_name, :country, :bio, :city])
|> cast_embed(:settings,
with: &Rauversion.Accounts.Settings.changeset/2
)
end

def changeset(user, attrs, opts \\ []) do
user
|> validate_contact_fields(attrs)
|> registration_changeset(attrs, opts)
end

def invitation_changeset(user, attrs) do
user
|> cast(attrs, [:email, :password])
|> validate_email()
|> validate_password([])
end

def accept_invitation_changeset(user, attrs) do
user
|> cast(attrs, [:username, :password])
|> validate_required([:username])
|> unsafe_validate_unique(:username, Rauversion.Repo)
|> unique_constraint(:username)
|> validate_password([])
end

def oauth_registration_update(user, attrs) do
user
|> cast(attrs, [:email, :username, :first_name, :last_name])
Expand All @@ -102,31 +138,6 @@ defmodule Rauversion.Accounts.User do
|> validate_contact_fields(attrs)
end

def process_avatar(user, attrs) do
case attrs do
%{"avatar" => _avatar = nil} ->
user

%{"avatar" => _avatar} ->
blob =
ActiveStorage.Blob.create_and_upload!(
%ActiveStorage.Blob{},
io: {:path, attrs["avatar"].path},
filename: attrs["avatar"].filename,
content_type: attrs["avatar"].content_type,
identify: true
)

avatar = user.data.__struct__.avatar(user.data)
avatar.__struct__.attach(avatar, blob)

user

_ ->
user
end
end

@doc """
A user changeset for registration.
Expand Down
4 changes: 1 addition & 3 deletions lib/rauversion/accounts/user_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ defmodule Rauversion.Accounts.UserNotifier do
Hi #{user.email},
You can confirm your account by visiting the URL below:
You have been invited to Rauversion, You can confirm your account by visiting the URL below:
#{url}
If you didn't create an account with us, please ignore this.
==============================
""")
end
Expand Down
Loading

0 comments on commit 932227d

Please sign in to comment.