-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-booth-editor.ejs
More file actions
141 lines (127 loc) · 4.79 KB
/
admin-booth-editor.ejs
File metadata and controls
141 lines (127 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Cheng Tsz Hung (25017438D)
* Awwab Hamam (22103907D)
*/
let scanner = null;
async function initializeScanner() {
try {
scanner = new Html5QrcodeScanner("reader", {
qrbox: {
width: 250,
height: 250,
},
fps: 5,
});
scanner.render(onScanSuccess, onScanError);
} catch (err) {
console.error('Error initializing scanner:', err);
alert('Could not initialize QR scanner');
}
}
async function onScanSuccess(decodedText) {
try {
if (decodedText && decodedText.split && decodedText.split('.').length === 2) {
const token = decodedText.trim();
const response = await fetch('/api/tickets/validate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token })
});
const contentType = response.headers.get('content-type') || '';
if (!response.ok) {
const text = await response.text();
console.error('Validation response not OK', response.status, text);
displayError('Server error during validation');
if (typeof playErrorSound === 'function') playErrorSound();
return;
}
if (!contentType.includes('application/json')) {
const text = await response.text();
console.error('Validation returned non-JSON response:', text);
displayError('Invalid server response during validation');
if (typeof playErrorSound === 'function') playErrorSound();
return;
}
const data = await response.json();
if (data.valid) {
displayTicketInfo(data.ticketInfo);
if (typeof playSuccessSound === 'function') playSuccessSound();
} else {
displayError(data.message || 'Invalid ticket');
playErrorSound && playErrorSound();
}
return;
}
let ticketCode = decodedText;
try {
const url = new URL(decodedText);
const parts = url.pathname.split('/').filter(Boolean);
if (parts.length) {
ticketCode = parts[parts.length - 1];
}
} catch (err) {
// not a URL, keep decodedText
}
const params = new URLSearchParams(window.location.search);
const scannerKey = params.get('scannerKey');
const body = { ticketCode };
if (scannerKey) body.scannerKey = scannerKey;
const response = await fetch('/api/tickets/validate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
});
const contentType = response.headers.get('content-type') || '';
if (!response.ok) {
const text = await response.text();
console.error('Validation response not OK', response.status, text);
displayError('Server error during validation');
if (typeof playErrorSound === 'function') playErrorSound();
return;
}
if (!contentType.includes('application/json')) {
const text = await response.text();
console.error('Validation returned non-JSON response:', text);
displayError('Invalid server response during validation');
if (typeof playErrorSound === 'function') playErrorSound();
return;
}
const data = await response.json();
if (data.valid) {
displayTicketInfo(data.ticketInfo);
if (typeof playSuccessSound === 'function') playSuccessSound();
} else {
displayError(data.message || 'Invalid ticket');
if (typeof playErrorSound === 'function') playErrorSound();
}
} catch (err) {
console.error('Error validating ticket:', err);
displayError('Could not validate ticket');
}
}
function onScanError(err) {
console.warn(err);
}
function displayTicketInfo(ticketInfo) {
const infoDiv = document.getElementById('ticketInfo');
infoDiv.innerHTML = `
<div class="alert alert-success">
<h4>Valid Ticket!</h4>
<p>Booth: ${ticketInfo.boothNumber}</p>
<p>Owner: ${ticketInfo.userName}</p>
<p>Entry Time: ${new Date().toLocaleTimeString()}</p>
</div>
`;
}
function displayError(message) {
const infoDiv = document.getElementById('ticketInfo');
infoDiv.innerHTML = `
<div class="alert alert-danger">
<h4>Invalid Ticket</h4>
<p>${message}</p>
</div>
`;
}
document.addEventListener('DOMContentLoaded', initializeScanner);