-
Notifications
You must be signed in to change notification settings - Fork 40
Consume endpoint #895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ekvanox
wants to merge
13
commits into
main
Choose a base branch
from
consume-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Consume endpoint #895
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
884898d
feat: Add QR code scanning for ticket consumption
ekvanox 9cc26d0
fix: resolve linting and formatting issues
ekvanox 8357d77
feat: add visual indication of recent consumption
ekvanox 7de17d6
fix: missing translations
ekvanox 974f1a6
chore: remove snaplet config files
ekvanox 5e3866d
chore: reset config.json to default
ekvanox 75d21f1
feat: minor improvements
ekvanox 40f9d58
Merge branch 'main' into consume-endpoint
ekvanox 7a330ba
Update src/routes/(app)/events/[slug]/scan/[consumable]/+page.svelte
ekvanox b34e6ef
Merge branch 'main' into consume-endpoint
ekvanox 56bb2f9
Fix: feedback from pr
ekvanox 8c4579f
refactor: svelte 4->5
ekvanox 90a526c
Merge branch 'main' into consume-endpoint
ekvanox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <script lang="ts"> | ||
| import { useQRScanner } from "$lib/hooks/useQRScanner"; | ||
| import * as m from "$paraglide/messages"; | ||
|
|
||
| interface Props { | ||
| onScan: (text: string) => void; | ||
| } | ||
|
|
||
| let { onScan }: Props = $props(); | ||
|
|
||
| let videoElement: HTMLVideoElement | undefined = $state(); | ||
| let errorMessage = $state(""); | ||
|
|
||
| const { initialize } = useQRScanner(); | ||
|
|
||
| $effect(() => { | ||
| if (videoElement) { | ||
| initialize(videoElement, (text) => { | ||
| onScan(text); | ||
| }).then((result) => { | ||
| if (result.error) { | ||
| errorMessage = result.error; | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| </script> | ||
|
|
||
| <div class="w-100 flex justify-center p-4"> | ||
| <div class="w-100 max-w-500px"> | ||
| {#if errorMessage} | ||
| <p class="p-4 text-center text-red-500"> | ||
| {m.events_camera_init_failure()}<br /> | ||
| Error: {errorMessage} | ||
| </p> | ||
| {:else} | ||
| <video bind:this={videoElement} class="w-100" playsinline> | ||
| <track kind="captions" src="" label="English captions" srclang="en" /> | ||
| </video> | ||
| {/if} | ||
| </div> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { BrowserMultiFormatReader } from "@zxing/library"; | ||
| import { onDestroy } from "svelte"; | ||
|
|
||
| export function useQRScanner() { | ||
| const codeReader = new BrowserMultiFormatReader(); | ||
|
|
||
| const initialize = async ( | ||
| videoElement: HTMLVideoElement, | ||
| onResult?: (text: string) => void, | ||
| ): Promise<{ error?: string }> => { | ||
| try { | ||
| const videoInputDevices = await codeReader.listVideoInputDevices(); | ||
| console.log("Available devices:", videoInputDevices); | ||
|
|
||
| if (videoInputDevices.length === 0) { | ||
| throw new Error("No camera devices found"); | ||
| } | ||
|
|
||
| // Try to get the back camera first, then front camera, then first available camera | ||
| const selectedDevice = | ||
| videoInputDevices.find((device) => | ||
| device.label.toLowerCase().includes("back"), | ||
| ) || | ||
| videoInputDevices.find((device) => | ||
| device.label.toLowerCase().includes("front"), | ||
| ) || | ||
| videoInputDevices[0]; | ||
|
|
||
| if (!selectedDevice) throw new Error("No suitable camera device found"); | ||
|
|
||
| await codeReader.decodeFromVideoDevice( | ||
| selectedDevice.deviceId, | ||
| videoElement, | ||
| (result) => { | ||
| if (result && onResult) { | ||
| onResult(result.getText()); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| return {}; | ||
| } catch (error) { | ||
| console.error("Camera initialization error:", error); | ||
| return { error: `Camera error: ${error}` }; | ||
| } | ||
| }; | ||
|
|
||
| const reset = () => { | ||
| codeReader.reset(); | ||
| }; | ||
|
|
||
| // Cleanup when component is destroyed | ||
| onDestroy(() => { | ||
| reset(); | ||
| }); | ||
|
|
||
| return { | ||
| initialize, | ||
| reset, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { PrismaClient } from "@prisma/client"; | ||
|
|
||
| export const consumeConsumable = async ( | ||
| prisma: PrismaClient, | ||
| consumableId: string, | ||
| ): Promise<Message> => { | ||
|
||
| try { | ||
| await prisma.consumable.update({ | ||
| where: { | ||
| id: consumableId, | ||
| }, | ||
| data: { | ||
| consumedAt: new Date(), | ||
| }, | ||
| }); | ||
| } catch (e) { | ||
| if (e instanceof Error) | ||
| return { | ||
| message: e.message, | ||
| type: "error", | ||
| }; | ||
| return { | ||
| message: "Kunde inte konsumera biljetten.", | ||
| type: "error", | ||
| }; | ||
| } | ||
| return { | ||
| message: "Biljetten har konsumerats.", | ||
| type: "success", | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { BASIC_EVENT_FILTER } from "$lib/events/events"; | ||
| import apiNames from "$lib/utils/apiNames"; | ||
| import { authorize } from "$lib/utils/authorization"; | ||
| import { redirect } from "$lib/utils/redirect"; | ||
| import type { PageServerLoad } from "./$types"; | ||
|
|
||
| export const load: PageServerLoad = async ({ locals, url }) => { | ||
| const { prisma, user } = locals; | ||
| authorize(apiNames.EVENT.READ, user); | ||
|
|
||
| // Get page number from URL query params (default to 1) | ||
| const page = parseInt(url.searchParams.get("page") || "1"); | ||
| const pageSize = 12; | ||
|
|
||
| // Calculate pagination values | ||
| const skip = (page - 1) * pageSize; | ||
|
|
||
| // Get total count for pagination | ||
| const totalEvents = await prisma.event.count({ | ||
| where: { | ||
| ...BASIC_EVENT_FILTER(), | ||
| // Only include events with tickets available | ||
| tickets: { | ||
| some: { | ||
| stock: { | ||
| gt: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Calculate total pages | ||
| const totalPages = Math.ceil(totalEvents / pageSize); | ||
|
|
||
| // Fetch paginated events | ||
| const events = await prisma.event.findMany({ | ||
| where: { | ||
| ...BASIC_EVENT_FILTER(), | ||
| tickets: { | ||
| some: { | ||
| stock: { | ||
| gt: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| orderBy: { | ||
| startDatetime: "desc", | ||
| }, | ||
| skip, | ||
| take: pageSize, | ||
| include: { | ||
| tags: true, | ||
| going: { | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }, | ||
| interested: { | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }, | ||
| author: true, | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| events, | ||
| pagination: { | ||
| currentPage: page, | ||
| totalPages, | ||
| totalEvents, | ||
| hasNextPage: page < totalPages, | ||
| hasPrevPage: page > 1, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export const actions = { | ||
| async selectEvent({ request }) { | ||
| const formData = await request.formData(); | ||
| const eventSlug = formData.get("eventSlug")?.toString(); | ||
|
|
||
| if (!eventSlug) { | ||
| return { success: false, message: "No event slug provided" }; | ||
| } | ||
|
|
||
| // Redirect to the event's QR page | ||
| throw redirect(303, `/events/${eventSlug}/scan`); | ||
| }, | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
pageimport is added but doesn't appear to be used in the visible code changes. Consider removing unused imports unless they're used elsewhere in the file.