Some checks failed
🚀 Continuous Integration / 🔧 Backend Tests (18.x) (push) Has been cancelled
🚀 Continuous Integration / 🔧 Backend Tests (20.x) (push) Has been cancelled
🚀 Continuous Integration / 🎨 Frontend Tests (18.x) (push) Has been cancelled
🚀 Continuous Integration / 🎨 Frontend Tests (20.x) (push) Has been cancelled
🚀 Continuous Integration / 🔍 Code Quality (push) Has been cancelled
🚀 Continuous Integration / 🔒 Security Checks (push) Has been cancelled
🚀 Continuous Integration / 🎨 Theme Tests (push) Has been cancelled
🚀 Continuous Integration / ♿ Accessibility Tests (push) Has been cancelled
🚀 Continuous Integration / 📱 Cross-Browser Tests (push) Has been cancelled
🚀 Continuous Integration / 🏗️ Build Tests (push) Has been cancelled
🚀 Continuous Integration / 📊 Performance Tests (push) Has been cancelled
🚀 Continuous Integration / 🎯 Integration Tests (push) Has been cancelled
🚀 Continuous Integration / ✅ All Tests Passed (push) Has been cancelled
215 lines
5.5 KiB
JavaScript
215 lines
5.5 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const Child = require('../models/Child');
|
|
const Task = require('../models/Task');
|
|
const { Reward } = require('../models/Reward');
|
|
|
|
// @route GET /api/public/children
|
|
// @desc Alle Kinder für öffentliche Auswahl laden (ohne Authentifizierung)
|
|
// @access Public
|
|
router.get('/children', async (req, res) => {
|
|
try {
|
|
// Lade alle aktiven Kinder (in einer echten App würde man hier nach Familie filtern)
|
|
const children = await Child.find({ isActive: true })
|
|
.select('name age avatar level points')
|
|
.sort({ name: 1 });
|
|
|
|
res.json({
|
|
success: true,
|
|
children
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim Laden der Kinder:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Server-Fehler beim Laden der Kinder'
|
|
});
|
|
}
|
|
});
|
|
|
|
// @route GET /api/public/child/:id
|
|
// @desc Einzelnes Kind für öffentlichen Zugriff laden
|
|
// @access Public
|
|
router.get('/child/:id', async (req, res) => {
|
|
try {
|
|
const child = await Child.findById(req.params.id)
|
|
.select('name age avatar level points preferences stats');
|
|
|
|
if (!child) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: 'Kind nicht gefunden'
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
child
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim Laden des Kindes:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Server-Fehler beim Laden des Kindes'
|
|
});
|
|
}
|
|
});
|
|
|
|
// @route GET /api/public/tasks/child/:childId
|
|
// @desc Aufgaben für ein Kind laden (öffentlich)
|
|
// @access Public
|
|
router.get('/tasks/child/:childId', async (req, res) => {
|
|
try {
|
|
const { childId } = req.params;
|
|
const { date } = req.query;
|
|
|
|
let dateFilter = {};
|
|
if (date === 'today') {
|
|
const today = new Date();
|
|
today.setHours(0, 0, 0, 0);
|
|
const tomorrow = new Date(today);
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
dateFilter = {
|
|
dueDate: {
|
|
$gte: today,
|
|
$lt: tomorrow
|
|
}
|
|
};
|
|
}
|
|
|
|
const tasks = await Task.find({
|
|
assignedTo: childId,
|
|
...dateFilter
|
|
}).sort({ dueDate: 1, priority: -1 });
|
|
|
|
res.json({
|
|
success: true,
|
|
tasks
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim Laden der Aufgaben:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Server-Fehler beim Laden der Aufgaben'
|
|
});
|
|
}
|
|
});
|
|
|
|
// @route GET /api/public/rewards/shop/:childId
|
|
// @desc Belohnungen für ein Kind laden (öffentlich)
|
|
// @access Public
|
|
router.get('/rewards/shop/:childId', async (req, res) => {
|
|
try {
|
|
const { childId } = req.params;
|
|
|
|
// Lade verfügbare Belohnungen für das Kind
|
|
const rewards = await Reward.find({
|
|
$or: [
|
|
{ targetChildren: childId },
|
|
{ targetChildren: { $size: 0 } } // Globale Belohnungen
|
|
],
|
|
isActive: true
|
|
}).sort({ cost: 1 });
|
|
|
|
res.json({
|
|
success: true,
|
|
rewards
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim Laden der Belohnungen:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Server-Fehler beim Laden der Belohnungen'
|
|
});
|
|
}
|
|
});
|
|
|
|
// @route POST /api/public/tasks/:taskId/complete
|
|
// @desc Aufgabe als erledigt markieren (öffentlich)
|
|
// @access Public
|
|
router.post('/tasks/:taskId/complete', async (req, res) => {
|
|
try {
|
|
const { taskId } = req.params;
|
|
|
|
const task = await Task.findById(taskId);
|
|
if (!task) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: 'Aufgabe nicht gefunden'
|
|
});
|
|
}
|
|
|
|
// Aufgabe als erledigt markieren
|
|
task.status = 'completed';
|
|
task.completedAt = new Date();
|
|
await task.save();
|
|
|
|
res.json({
|
|
success: true,
|
|
message: `Aufgabe "${task.title}" wurde als erledigt markiert! 🎉`,
|
|
task
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim Markieren der Aufgabe:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Server-Fehler beim Markieren der Aufgabe'
|
|
});
|
|
}
|
|
});
|
|
|
|
// @route POST /api/public/rewards/:rewardId/redeem
|
|
// @desc Belohnung einlösen (öffentlich)
|
|
// @access Public
|
|
router.post('/rewards/:rewardId/redeem', async (req, res) => {
|
|
try {
|
|
const { rewardId } = req.params;
|
|
const { childId } = req.body;
|
|
|
|
const reward = await Reward.findById(rewardId);
|
|
if (!reward) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: 'Belohnung nicht gefunden'
|
|
});
|
|
}
|
|
|
|
const child = await Child.findById(childId);
|
|
if (!child) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: 'Kind nicht gefunden'
|
|
});
|
|
}
|
|
|
|
// Prüfen ob genug Punkte vorhanden
|
|
if (child.points.available < reward.cost) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: 'Nicht genügend Punkte verfügbar'
|
|
});
|
|
}
|
|
|
|
// Punkte abziehen
|
|
child.points.available -= reward.cost;
|
|
child.points.spent += reward.cost;
|
|
await child.save();
|
|
|
|
res.json({
|
|
success: true,
|
|
message: `Belohnung "${reward.title}" wurde erfolgreich eingelöst! 🎁`,
|
|
child: {
|
|
points: child.points
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim Einlösen der Belohnung:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Server-Fehler beim Einlösen der Belohnung'
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = router; |