Skip to content

Commit

Permalink
feat: badges system (#493)
Browse files Browse the repository at this point in the history
Co-authored-by: Rui Oliveira <[email protected]>
  • Loading branch information
joaodiaslobo and ruioliveira02 authored Feb 6, 2025
1 parent 6ee9140 commit 6e49532
Show file tree
Hide file tree
Showing 56 changed files with 2,497 additions and 148 deletions.
3 changes: 2 additions & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
import live_select from "live_select"
import { QrScanner, Wheel, Confetti, Countdown, Sorting, CoinFlip, Redirect, CredentialScene , Banner, ReelAnimation, PaytableModal } from "./hooks";
import { QrScanner, Wheel, Confetti, Countdown, Sorting, CoinFlip, Redirect, CredentialScene , Banner, ReelAnimation, PaytableModal, ZipUpload } from "./hooks";

let Hooks = {
QrScanner: QrScanner,
Expand All @@ -36,6 +36,7 @@ let Hooks = {
CredentialScene: CredentialScene,
ReelAnimation: ReelAnimation,
PaytableModal: PaytableModal,
ZipUpload: ZipUpload,
...live_select
};

Expand Down
3 changes: 2 additions & 1 deletion assets/js/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export { CoinFlip } from "./coinflip.js";
export { Redirect } from "./redirect.js";
export { CredentialScene } from "./credential-scene.js";
export { ReelAnimation } from "./reel_animation.js";
export { PaytableModal } from "./paytable_modal.js";
export { PaytableModal } from "./paytable_modal.js";
export { ZipUpload } from "./zip_upload.js";
2 changes: 1 addition & 1 deletion assets/js/hooks/qr_reading.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Html5Qrcode, Html5QrcodeSupportedFormats } from "../../vendor/html5-qrc

export const QrScanner = {
mounted() {
const config = { fps: 4, qrbox: (width, height) => {return { width: width * 0.8, height: height * 0.9 }}};
const config = { fps: 2, qrbox: (width, height) => {return { width: width * 0.8, height: height * 0.9 }}};
this.scanner = new Html5Qrcode(this.el.id, { formatsToSupport: [ Html5QrcodeSupportedFormats.QR_CODE ] });

const onScanSuccess = (decodedText, decodedResult) => {
Expand Down
14 changes: 14 additions & 0 deletions assets/js/hooks/zip_upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import JSZip from "../../vendor/jszip"

export const ZipUpload = {
mounted() {
this.el.addEventListener("input", e => {
e.preventDefault()
let zip = new JSZip()
Array.from(e.target.files).forEach(file => {
zip.file(file.webkitRelativePath || file.name, file, {binary: true})
})
zip.generateAsync({type: "blob"}).then(blob => this.upload("dir", [blob]))
})
}
}
13 changes: 13 additions & 0 deletions assets/vendor/jszip.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

# Configure oban job processing
config :safira, Oban,
engine: Oban.Engines.Basic,
repo: Safira.Repo,
queues: [
badge_conditions: 10,
badge_triggers: 10
]

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
3 changes: 3 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ config :phoenix, :plug_init_mode, :runtime
config :phoenix_live_view,
# Enable helpful, but potentially expensive runtime checks
enable_expensive_runtime_checks: true

# Configure Oban for testing
config :safira, Oban, testing: :manual
29 changes: 29 additions & 0 deletions lib/safira/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Safira.Accounts do
use Safira.Context

alias Safira.Accounts.{Attendee, Course, Credential, Staff, User, UserNotifier, UserToken}
alias Safira.Contest

## Database getters

Expand Down Expand Up @@ -630,6 +631,15 @@ defmodule Safira.Accounts do
credential
|> Credential.changeset(%{attendee_id: attendee.id})
|> Repo.update()
|> case do
{:ok, _} = result ->
# If the credential is successfully linked to the attendee, trigger the badge event
Contest.enqueue_badge_trigger_execution_job(attendee, :link_credential_event)
result

{:error, _} = result ->
result
end
end

@doc """
Expand Down Expand Up @@ -717,6 +727,25 @@ defmodule Safira.Accounts do
|> Repo.one!()
end

@doc """
Gets a single attendee associated to the given credential.
## Examples
iex> get_attendee_from_credential(123)
%Attendee{}
iex> get_attendee_from_credential(456)
nil
"""
def get_attendee_from_credential(credential_id) do
Credential
|> where([c], c.id == ^credential_id)
|> join(:inner, [c], a in assoc(c, :attendee))
|> select([c, a], a)
|> Repo.one()
end

@doc """
Returns the list of courses.
Expand Down
3 changes: 2 additions & 1 deletion lib/safira/accounts/roles/permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Safira.Accounts.Roles.Permissions do
"spotlights" => ["edit"],
"schedule" => ["edit"],
"statistics" => ["show"],
"mailer" => ["send"]
"mailer" => ["send"],
"scanner" => ["show"]
}
end

Expand Down
2 changes: 2 additions & 0 deletions lib/safira/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule Safira.Application do
{Finch, name: Safira.Finch},
# Start the Presence system
SafiraWeb.Presence,
# Start the Oban queue
{Oban, Application.fetch_env!(:safira, Oban)},
# Start Safira web server
SafiraWeb.Endpoint
]
Expand Down
Loading

0 comments on commit 6e49532

Please sign in to comment.