forked from hockeyboyjoshua0902-max/msgteens
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
212 lines (176 loc) · 8.61 KB
/
server.js
File metadata and controls
212 lines (176 loc) · 8.61 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
/**
* MSG Teens — Backend Server
* Node.js + Express
*
* Endpoints:
* POST /api/signup — register a new member
* GET /api/members — list all members (admin)
* POST /api/submit-story — submit a teen story for review
* GET /api/stories — get approved stories
* POST /api/award-points — award points to a member
* GET /api/leaderboard — get current month leaderboard
*
* Storage: JSON flat files (no database needed to start)
* Upgrade path: swap db.js for MongoDB/Postgres later
*/
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const app = express();
const PORT = process.env.PORT || 3000;
// ── Middleware ─────────────────────────────────────────────
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public'))); // serve index.html from /public
// ── Flat-file "database" helpers ──────────────────────────
const DATA_DIR = path.join(__dirname, 'data');
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR);
function readDB(file) {
const fp = path.join(DATA_DIR, file);
if (!fs.existsSync(fp)) return [];
return JSON.parse(fs.readFileSync(fp, 'utf8'));
}
function writeDB(file, data) {
fs.writeFileSync(path.join(DATA_DIR, file), JSON.stringify(data, null, 2));
}
function uid() {
return crypto.randomBytes(6).toString('hex');
}
// ── Current month key e.g. "2026-05" ─────────────────────
function monthKey() {
const d = new Date();
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`;
}
// ══════════════════════════════════════════════════════════
// POST /api/signup
// Body: { name, email, school, role }
// ══════════════════════════════════════════════════════════
app.post('/api/signup', (req, res) => {
const { name, email, school, role } = req.body;
if (!name || !email) {
return res.status(400).json({ error: 'Name and email are required.' });
}
const members = readDB('members.json');
// prevent duplicate emails
if (members.find(m => m.email.toLowerCase() === email.toLowerCase())) {
return res.status(409).json({ error: 'This email is already registered.' });
}
const member = {
id: uid(),
name,
email: email.toLowerCase(),
school: school || '',
role: role || 'teen',
points: {}, // { "2026-05": 12, "2026-06": 7 }
joinedAt: new Date().toISOString()
};
members.push(member);
writeDB('members.json', members);
console.log(`[signup] New member: ${name} <${email}>`);
res.status(201).json({ message: 'Welcome to MSG Teens!', memberId: member.id });
});
// ══════════════════════════════════════════════════════════
// GET /api/members (simple admin — add auth before going live)
// ══════════════════════════════════════════════════════════
app.get('/api/members', (req, res) => {
const members = readDB('members.json');
// strip emails for public exposure — keep for admin
const safe = members.map(({ id, name, school, role, points, joinedAt }) =>
({ id, name, school, role, points, joinedAt })
);
res.json(safe);
});
// ══════════════════════════════════════════════════════════
// POST /api/submit-story
// Body: { teenName, submitterName, submitterEmail, description, imageUrl? }
// ══════════════════════════════════════════════════════════
app.post('/api/submit-story', (req, res) => {
const { teenName, submitterName, submitterEmail, description, imageUrl } = req.body;
if (!teenName || !submitterName || !description) {
return res.status(400).json({ error: 'teenName, submitterName, and description are required.' });
}
const stories = readDB('stories.json');
const story = {
id: uid(),
teenName,
submitterName,
submitterEmail: submitterEmail || '',
description,
imageUrl: imageUrl || '',
status: 'pending', // pending | approved | rejected
submittedAt: new Date().toISOString()
};
stories.push(story);
writeDB('stories.json', stories);
console.log(`[story] Submitted for: ${teenName} by ${submitterName}`);
res.status(201).json({ message: 'Story submitted! We will review it soon.', storyId: story.id });
});
// ══════════════════════════════════════════════════════════
// GET /api/stories — only approved stories go public
// ══════════════════════════════════════════════════════════
app.get('/api/stories', (req, res) => {
const stories = readDB('stories.json');
const approved = stories.filter(s => s.status === 'approved');
res.json(approved);
});
// ══════════════════════════════════════════════════════════
// POST /api/award-points
// Body: { memberId, action }
// action: "small" (1pt) | "medium" (3pt) | "big" (5pt)
// ══════════════════════════════════════════════════════════
const POINT_VALUES = { small: 1, medium: 3, big: 5 };
app.post('/api/award-points', (req, res) => {
const { memberId, action } = req.body;
if (!memberId || !action) {
return res.status(400).json({ error: 'memberId and action are required.' });
}
const pts = POINT_VALUES[action];
if (!pts) {
return res.status(400).json({ error: 'action must be small, medium, or big.' });
}
const members = readDB('members.json');
const member = members.find(m => m.id === memberId);
if (!member) {
return res.status(404).json({ error: 'Member not found.' });
}
const month = monthKey();
member.points[month] = (member.points[month] || 0) + pts;
writeDB('members.json', members);
console.log(`[points] +${pts} to ${member.name} (total this month: ${member.points[month]})`);
res.json({
message: `Awarded ${pts} point(s) to ${member.name}`,
monthlyTotal: member.points[month]
});
});
// ══════════════════════════════════════════════════════════
// GET /api/leaderboard — top 10 for current month
// ══════════════════════════════════════════════════════════
app.get('/api/leaderboard', (req, res) => {
const members = readDB('members.json');
const month = monthKey();
const ranked = members
.map(m => ({ id: m.id, name: m.name, school: m.school, points: m.points[month] || 0 }))
.filter(m => m.points > 0)
.sort((a, b) => b.points - a.points)
.slice(0, 10)
.map((m, i) => ({ rank: i + 1, ...m }));
res.json({ month, leaderboard: ranked });
});
// ── Health check ──────────────────────────────────────────
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', time: new Date().toISOString() });
});
// ── Start ─────────────────────────────────────────────────
app.listen(PORT, () => {
console.log(`\nMSG Teens server running on http://localhost:${PORT}`);
console.log('Data stored in ./data/');
console.log('Routes:');
console.log(' POST /api/signup');
console.log(' GET /api/members');
console.log(' POST /api/submit-story');
console.log(' GET /api/stories');
console.log(' POST /api/award-points');
console.log(' GET /api/leaderboard\n');
});