- Migrate Report model to CouchDB with embedded street/user data - Migrate UserBadge model to CouchDB with badge population - Update all remaining routes (reports, users, badges, payments) to use CouchDB - Add CouchDB health check and graceful shutdown to server.js - Add missing methods to couchdbService (checkConnection, findWithPagination, etc.) - Update Kubernetes deployment manifests for CouchDB support - Add comprehensive CouchDB setup documentation All core functionality now uses CouchDB as primary database while maintaining MongoDB for backward compatibility during transition period. 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
83 lines
1.9 KiB
JavaScript
83 lines
1.9 KiB
JavaScript
const express = require("express");
|
|
const Badge = require("../models/Badge");
|
|
const UserBadge = require("../models/UserBadge");
|
|
const auth = require("../middleware/auth");
|
|
const { asyncHandler } = require("../middleware/errorHandler");
|
|
const { getUserBadgeProgress } = require("../services/gamificationService");
|
|
|
|
const router = express.Router();
|
|
|
|
/**
|
|
* GET /api/badges
|
|
* Get all available badges
|
|
*/
|
|
router.get(
|
|
"/",
|
|
asyncHandler(async (req, res) => {
|
|
const badges = await Badge.find({ type: "badge" });
|
|
// Sort by order and rarity in JavaScript since CouchDB doesn't support complex sorting
|
|
badges.sort((a, b) => {
|
|
if (a.order !== b.order) return a.order - b.order;
|
|
return a.rarity.localeCompare(b.rarity);
|
|
});
|
|
res.json(badges);
|
|
})
|
|
);
|
|
|
|
/**
|
|
* GET /api/badges/progress
|
|
* Get current user's badge progress (requires authentication)
|
|
*/
|
|
router.get(
|
|
"/progress",
|
|
auth,
|
|
asyncHandler(async (req, res) => {
|
|
const progress = await getUserBadgeProgress(req.user.id);
|
|
res.json(progress);
|
|
})
|
|
);
|
|
|
|
/**
|
|
* GET /api/badges/users/:userId
|
|
* Get badges earned by a specific user
|
|
*/
|
|
router.get(
|
|
"/users/:userId",
|
|
asyncHandler(async (req, res) => {
|
|
const { userId } = req.params;
|
|
|
|
const userBadges = await UserBadge.findByUser(userId);
|
|
|
|
// Sort by earnedAt in JavaScript
|
|
userBadges.sort((a, b) => new Date(b.earnedAt) - new Date(a.earnedAt));
|
|
|
|
res.json(
|
|
userBadges.map((ub) => ({
|
|
badge: ub.badge,
|
|
earnedAt: ub.earnedAt,
|
|
progress: ub.progress,
|
|
}))
|
|
);
|
|
})
|
|
);
|
|
|
|
/**
|
|
* GET /api/badges/:badgeId
|
|
* Get a specific badge by ID
|
|
*/
|
|
router.get(
|
|
"/:badgeId",
|
|
asyncHandler(async (req, res) => {
|
|
const { badgeId } = req.params;
|
|
|
|
const badge = await Badge.findById(badgeId);
|
|
if (!badge) {
|
|
return res.status(404).json({ msg: "Badge not found" });
|
|
}
|
|
|
|
res.json(badge);
|
|
})
|
|
);
|
|
|
|
module.exports = router;
|