-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_log_backend.txt
More file actions
379 lines (352 loc) · 27.9 KB
/
git_log_backend.txt
File metadata and controls
379 lines (352 loc) · 27.9 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
commit 3b059ba5183c86dec2f51c8b30a6ef68a57f5135
Author: Sankar Ganesh <b.sankarganesh@gmail.com>
Date: Sun Mar 1 14:41:25 2026 +0530
style: fix puzzle empty cells and optimize completion layout
diff --git a/investcraft/backend/src/routes/puzzles.js b/investcraft/backend/src/routes/puzzles.js
index 1133acf..01a8b37 100644
--- a/investcraft/backend/src/routes/puzzles.js
+++ b/investcraft/backend/src/routes/puzzles.js
@@ -20,11 +20,11 @@ router.get('/daily', async (req, res) => {
if (!result.rows[0]) {
// Find the most voted ticker for TODAY
const voteResult = await pool.query(
- `SELECT ticker, COUNT(*) as vote_count
+ `SELECT ticker, COUNT(*) as vote_count, MIN(created_at) as first_vote
FROM puzzle_votes
WHERE vote_date = $1
GROUP BY ticker
- ORDER BY vote_count DESC
+ ORDER BY vote_count DESC, first_vote ASC
LIMIT 1`,
[today]
);
@@ -107,6 +107,24 @@ router.post('/vote', authenticate, async (req, res) => {
}
});
+// POST /api/puzzles/track-click - Track shared links
+router.post('/track-click', async (req, res) => {
+ const pool = getPool();
+ const { promoterId, ref } = req.body;
+
+ try {
+ await pool.query(
+ `INSERT INTO share_clicks (promoter_id, ref_page)
+ VALUES ($1, $2)`,
+ [promoterId || null, ref || 'direct']
+ );
+ res.json({ success: true });
+ } catch (err) {
+ logger.error('Error tracking share click', { error: err.message });
+ res.status(500).json({ error: 'Failed to track click' });
+ }
+});
+
// GET /api/puzzles ΓÇö paginated list
router.get('/', async (req, res) => {
const pool = getPool();
@@ -169,8 +187,8 @@ router.post('/:id/complete', authenticate, async (req, res) => {
else if (last_played === today) newStreak = streak;
await pool.query(
- 'UPDATE users SET total_score = $1, streak = $2, last_played = $3 WHERE id = $4',
- [realTotal, newStreak, today, userId]
+ 'UPDATE users SET total_score = $1, best_score = GREATEST(best_score, $2), streak = $3, last_played = $4 WHERE id = $5',
+ [realTotal, score, newStreak, today, userId]
);
res.json({ success: true, score, streak: newStreak, realTotal });
commit bdf8162c1ecf6d9eab17bb6aae30ba5bce1e99e3
Author: Sankar Ganesh <b.sankarganesh@gmail.com>
Date: Wed Feb 25 19:32:50 2026 +0530
feat(tests): add backend and frontend Jest testing suites for 70%+ coverage
diff --git a/investcraft/backend/src/routes/puzzles.js b/investcraft/backend/src/routes/puzzles.js
index e90755c..1133acf 100644
--- a/investcraft/backend/src/routes/puzzles.js
+++ b/investcraft/backend/src/routes/puzzles.js
@@ -140,10 +140,21 @@ router.post('/:id/complete', authenticate, async (req, res) => {
await pool.query(
`INSERT INTO game_sessions (user_id, puzzle_id, score, moves_used, completed, time_taken)
VALUES ($1, $2, $3, $4, true, $5)
- ON CONFLICT (user_id, puzzle_id) DO NOTHING`,
+ ON CONFLICT (user_id, puzzle_id)
+ DO UPDATE SET
+ score = GREATEST(game_sessions.score, EXCLUDED.score),
+ moves_used = LEAST(game_sessions.moves_used, EXCLUDED.moves_used),
+ time_taken = LEAST(game_sessions.time_taken, EXCLUDED.time_taken)`,
[userId, puzzleId, score, movesUsed, timeTaken]
);
+ // Compute absolute total score from high-scores to prevent infinite farming
+ const aggResult = await pool.query(
+ `SELECT SUM(score) as real_total FROM game_sessions WHERE user_id = $1`,
+ [userId]
+ );
+ const realTotal = parseInt(aggResult.rows[0].real_total) || 0;
+
// Streak logic
const userRes = await pool.query(
'SELECT last_played, streak FROM users WHERE id = $1',
@@ -158,11 +169,11 @@ router.post('/:id/complete', authenticate, async (req, res) => {
else if (last_played === today) newStreak = streak;
await pool.query(
- 'UPDATE users SET total_score = total_score + $1, streak = $2, last_played = $3 WHERE id = $4',
- [score, newStreak, today, userId]
+ 'UPDATE users SET total_score = $1, streak = $2, last_played = $3 WHERE id = $4',
+ [realTotal, newStreak, today, userId]
);
- res.json({ success: true, score, streak: newStreak });
+ res.json({ success: true, score, streak: newStreak, realTotal });
} catch (err) {
logger.error('Error completing puzzle', { error: err.message });
res.status(500).json({ error: 'Failed to save result' });
commit 6b58a24419fa812961758c3954aef754152dfad2
Author: Sankar Ganesh <b.sankarganesh@gmail.com>
Date: Mon Feb 23 18:22:58 2026 +0530
feat: nostalgic brand trivia and high-density zero-scrollbar puzzle completion UI
diff --git a/investcraft/backend/src/routes/puzzles.js b/investcraft/backend/src/routes/puzzles.js
index 6c0fd1a..e90755c 100644
--- a/investcraft/backend/src/routes/puzzles.js
+++ b/investcraft/backend/src/routes/puzzles.js
@@ -1,12 +1,12 @@
const express = require('express');
-const router = express.Router();
+const router = express.Router();
const { getPool } = require('../config/database');
const { authenticate } = require('../middleware/auth');
const logger = require('../utils/logger');
// GET /api/puzzles/daily
router.get('/daily', async (req, res) => {
- const pool = getPool();
+ const pool = getPool();
const today = new Date().toISOString().split('T')[0];
try {
@@ -16,12 +16,57 @@ router.get('/daily', async (req, res) => {
[today]
);
- // Fall back to a random puzzle if none scheduled today
+ // If no puzzle is scheduled for TODAY, handle the logic to find one based on votes
if (!result.rows[0]) {
- result = await pool.query(
- `SELECT id, company_name, ticker, logo_url, difficulty, sector, hint
- FROM puzzles ORDER BY RANDOM() LIMIT 1`
+ // Find the most voted ticker for TODAY
+ const voteResult = await pool.query(
+ `SELECT ticker, COUNT(*) as vote_count
+ FROM puzzle_votes
+ WHERE vote_date = $1
+ GROUP BY ticker
+ ORDER BY vote_count DESC
+ LIMIT 1`,
+ [today]
);
+
+ let chosenTicker = null;
+ if (voteResult.rows[0]) {
+ chosenTicker = voteResult.rows[0].ticker;
+ }
+
+ if (chosenTicker) {
+ // Try to find a puzzle with that ticker that hasn't been played today (or ever, normally shouldn't reuse, but we'll accept any)
+ result = await pool.query(
+ `SELECT id, company_name, ticker, logo_url, difficulty, sector, hint
+ FROM puzzles WHERE ticker = $1 ORDER BY RANDOM() LIMIT 1`,
+ [chosenTicker]
+ );
+ }
+
+ // Fall back to a random puzzle if no votes or no puzzle found for the voted ticker
+ if (!result.rows[0]) {
+ result = await pool.query(
+ `SELECT id, company_name, ticker, logo_url, difficulty, sector, hint
+ FROM puzzles
+ WHERE scheduled_date IS NULL
+ ORDER BY RANDOM() LIMIT 1`
+ );
+ // If all puzzles have been scheduled, just pick a random one
+ if (!result.rows[0]) {
+ result = await pool.query(
+ `SELECT id, company_name, ticker, logo_url, difficulty, sector, hint
+ FROM puzzles ORDER BY RANDOM() LIMIT 1`
+ );
+ }
+ }
+
+ // Schedule the chosen puzzle for today
+ if (result.rows[0]) {
+ await pool.query(
+ `UPDATE puzzles SET scheduled_date = $1 WHERE id = $2`,
+ [today, result.rows[0].id]
+ );
+ }
}
res.json(result.rows[0] || null);
@@ -31,11 +76,42 @@ router.get('/daily', async (req, res) => {
}
});
+// POST /api/puzzles/vote - Vote for tomorrow's puzzle ticker
+router.post('/vote', authenticate, async (req, res) => {
+ const pool = getPool();
+ const userId = req.user.id;
+ const { ticker } = req.body;
+
+ if (!ticker) {
+ return res.status(400).json({ error: 'Ticker is required' });
+ }
+
+ // Calculate tomorrow's date
+ const tomorrow = new Date();
+ tomorrow.setDate(tomorrow.getDate() + 1);
+ const tomorrowStr = tomorrow.toISOString().split('T')[0];
+
+ try {
+ await pool.query(
+ `INSERT INTO puzzle_votes (user_id, ticker, vote_date)
+ VALUES ($1, $2, $3)
+ ON CONFLICT (user_id, vote_date)
+ DO UPDATE SET ticker = EXCLUDED.ticker, created_at = NOW()`,
+ [userId, ticker, tomorrowStr]
+ );
+
+ res.json({ success: true, message: 'Vote recorded for tomorrow!' });
+ } catch (err) {
+ logger.error('Error recording puzzle vote', { error: err.message });
+ res.status(500).json({ error: 'Failed to record vote' });
+ }
+});
+
// GET /api/puzzles ΓÇö paginated list
router.get('/', async (req, res) => {
- const pool = getPool();
- const page = Math.max(1, parseInt(req.query.page) || 1);
- const limit = 10;
+ const pool = getPool();
+ const page = Math.max(1, parseInt(req.query.page) || 1);
+ const limit = 10;
const offset = (page - 1) * limit;
try {
@@ -55,9 +131,9 @@ router.get('/', async (req, res) => {
// POST /api/puzzles/:id/complete ΓÇö save game result (authenticated)
router.post('/:id/complete', authenticate, async (req, res) => {
- const pool = getPool();
+ const pool = getPool();
const puzzleId = parseInt(req.params.id);
- const userId = req.user.id;
+ const userId = req.user.id;
const { score = 0, movesUsed = 0, timeTaken = 0 } = req.body;
try {
@@ -74,12 +150,12 @@ router.post('/:id/complete', authenticate, async (req, res) => {
[userId]
);
const { last_played, streak } = userRes.rows[0];
- const today = new Date().toISOString().split('T')[0];
+ const today = new Date().toISOString().split('T')[0];
const yesterday = new Date(Date.now() - 86_400_000).toISOString().split('T')[0];
let newStreak = 1;
if (last_played === yesterday) newStreak = streak + 1;
- else if (last_played === today) newStreak = streak;
+ else if (last_played === today) newStreak = streak;
await pool.query(
'UPDATE users SET total_score = total_score + $1, streak = $2, last_played = $3 WHERE id = $4',
commit b4a0622ea6e56533c42fdc949d538b262aa09665
Author: Sankar Ganesh <b.sankarganesh@gmail.com>
Date: Tue Feb 17 22:33:38 2026 +0530
feat(investcraft): Task 3c - add puzzles routes
diff --git a/investcraft/backend/src/routes/puzzles.js b/investcraft/backend/src/routes/puzzles.js
new file mode 100644
index 0000000..6c0fd1a
--- /dev/null
+++ b/investcraft/backend/src/routes/puzzles.js
@@ -0,0 +1,96 @@
+const express = require('express');
+const router = express.Router();
+const { getPool } = require('../config/database');
+const { authenticate } = require('../middleware/auth');
+const logger = require('../utils/logger');
+
+// GET /api/puzzles/daily
+router.get('/daily', async (req, res) => {
+ const pool = getPool();
+ const today = new Date().toISOString().split('T')[0];
+
+ try {
+ let result = await pool.query(
+ `SELECT id, company_name, ticker, logo_url, difficulty, sector, hint
+ FROM puzzles WHERE scheduled_date = $1`,
+ [today]
+ );
+
+ // Fall back to a random puzzle if none scheduled today
+ if (!result.rows[0]) {
+ result = await pool.query(
+ `SELECT id, company_name, ticker, logo_url, difficulty, sector, hint
+ FROM puzzles ORDER BY RANDOM() LIMIT 1`
+ );
+ }
+
+ res.json(result.rows[0] || null);
+ } catch (err) {
+ logger.error('Error fetching daily puzzle', { error: err.message });
+ res.status(500).json({ error: 'Failed to fetch puzzle' });
+ }
+});
+
+// GET /api/puzzles ΓÇö paginated list
+router.get('/', async (req, res) => {
+ const pool = getPool();
+ const page = Math.max(1, parseInt(req.query.page) || 1);
+ const limit = 10;
+ const offset = (page - 1) * limit;
+
+ try {
+ const result = await pool.query(
+ `SELECT id, company_name, ticker, difficulty, sector, scheduled_date
+ FROM puzzles
+ ORDER BY scheduled_date DESC
+ LIMIT $1 OFFSET $2`,
+ [limit, offset]
+ );
+ res.json({ puzzles: result.rows, page });
+ } catch (err) {
+ logger.error('Error fetching puzzles', { error: err.message });
+ res.status(500).json({ error: 'Failed to fetch puzzles' });
+ }
+});
+
+// POST /api/puzzles/:id/complete ΓÇö save game result (authenticated)
+router.post('/:id/complete', authenticate, async (req, res) => {
+ const pool = getPool();
+ const puzzleId = parseInt(req.params.id);
+ const userId = req.user.id;
+ const { score = 0, movesUsed = 0, timeTaken = 0 } = req.body;
+
+ try {
+ await pool.query(
+ `INSERT INTO game_sessions (user_id, puzzle_id, score, moves_used, completed, time_taken)
+ VALUES ($1, $2, $3, $4, true, $5)
+ ON CONFLICT (user_id, puzzle_id) DO NOTHING`,
+ [userId, puzzleId, score, movesUsed, timeTaken]
+ );
+
+ // Streak logic
+ const userRes = await pool.query(
+ 'SELECT last_played, streak FROM users WHERE id = $1',
+ [userId]
+ );
+ const { last_played, streak } = userRes.rows[0];
+ const today = new Date().toISOString().split('T')[0];
+ const yesterday = new Date(Date.now() - 86_400_000).toISOString().split('T')[0];
+
+ let newStreak = 1;
+ if (last_played === yesterday) newStreak = streak + 1;
+ else if (last_played === today) newStreak = streak;
+
+ await pool.query(
+ 'UPDATE users SET total_score = total_score + $1, streak = $2, last_played = $3 WHERE id = $4',
+ [score, newStreak, today, userId]
+ );
+
+ res.json({ success: true, score, streak: newStreak });
+ } catch (err) {
+ logger.error('Error completing puzzle', { error: err.message });
+ res.status(500).json({ error: 'Failed to save result' });
+ }
+});
+
+module.exports = router;