feat(backend): implement complete gamification system
Implement comprehensive points and badges system with MongoDB transactions: Point System: - Create PointTransaction model for transaction history - Award points atomically using MongoDB transactions - Point values: street adoption (+100), task completion (+50), post creation (+10), event participation (+75) - Track balance after each transaction - Support point deduction for reward redemption Badge System: - Create Badge and UserBadge models - Define badge criteria types: street_adoptions, task_completions, post_creations, event_participations, points_earned - Auto-award badges based on user achievements - Badge rarity levels: common, rare, epic, legendary - Track badge progress for users - Prevent duplicate badge awards Gamification Service: - Implement gamificationService.js with 390 lines of logic - awardPoints() with transaction support - checkAndAwardBadges() for auto-awarding - getUserBadgeProgress() for progress tracking - getUserStats() for achievement statistics - Atomic operations prevent double-awarding Integration: - Streets route: Award points and badges on adoption - Tasks route: Award points and badges on completion - Posts route: Award points and badges on creation - Events route: Award points and badges on RSVP - Rewards route: Deduct points on redemption - Badges API: List badges, track progress, view earned badges Updated User Model: - Add points field (default 0) - Add earnedBadges virtual relationship - Add indexes for performance (points for leaderboards) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,39 +1,64 @@
|
||||
const express = require("express");
|
||||
const mongoose = require("mongoose");
|
||||
const Street = require("../models/Street");
|
||||
const User = require("../models/User");
|
||||
const auth = require("../middleware/auth");
|
||||
const { asyncHandler } = require("../middleware/errorHandler");
|
||||
const {
|
||||
createStreetValidation,
|
||||
streetIdValidation,
|
||||
} = require("../middleware/validators/streetValidator");
|
||||
const {
|
||||
awardStreetAdoptionPoints,
|
||||
checkAndAwardBadges,
|
||||
} = require("../services/gamificationService");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Get all streets
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const streets = await Street.find();
|
||||
res.json(streets);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
// Get all streets (with pagination)
|
||||
router.get(
|
||||
"/",
|
||||
asyncHandler(async (req, res) => {
|
||||
const { paginate, buildPaginatedResponse } = require("../middleware/pagination");
|
||||
|
||||
// Parse pagination params
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const limit = Math.min(parseInt(req.query.limit) || 10, 100);
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const streets = await Street.find()
|
||||
.sort({ name: 1 })
|
||||
.skip(skip)
|
||||
.limit(limit)
|
||||
.populate("adoptedBy", ["name", "profilePicture"]);
|
||||
|
||||
const totalCount = await Street.countDocuments();
|
||||
|
||||
res.json(buildPaginatedResponse(streets, totalCount, page, limit));
|
||||
}),
|
||||
);
|
||||
|
||||
// Get single street
|
||||
router.get("/:id", async (req, res) => {
|
||||
try {
|
||||
router.get(
|
||||
"/:id",
|
||||
streetIdValidation,
|
||||
asyncHandler(async (req, res) => {
|
||||
const street = await Street.findById(req.params.id);
|
||||
if (!street) {
|
||||
return res.status(404).json({ msg: "Street not found" });
|
||||
}
|
||||
res.json(street);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
// Create a street
|
||||
router.post("/", auth, async (req, res) => {
|
||||
const { name, location } = req.body;
|
||||
router.post(
|
||||
"/",
|
||||
auth,
|
||||
createStreetValidation,
|
||||
asyncHandler(async (req, res) => {
|
||||
const { name, location } = req.body;
|
||||
|
||||
try {
|
||||
const newStreet = new Street({
|
||||
name,
|
||||
location,
|
||||
@@ -41,34 +66,76 @@ router.post("/", auth, async (req, res) => {
|
||||
|
||||
const street = await newStreet.save();
|
||||
res.json(street);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
// Adopt a street
|
||||
router.put("/adopt/:id", auth, async (req, res) => {
|
||||
try {
|
||||
const street = await Street.findById(req.params.id);
|
||||
if (!street) {
|
||||
return res.status(404).json({ msg: "Street not found" });
|
||||
router.put(
|
||||
"/adopt/:id",
|
||||
auth,
|
||||
streetIdValidation,
|
||||
asyncHandler(async (req, res) => {
|
||||
const session = await mongoose.startSession();
|
||||
session.startTransaction();
|
||||
|
||||
try {
|
||||
const street = await Street.findById(req.params.id).session(session);
|
||||
if (!street) {
|
||||
await session.abortTransaction();
|
||||
session.endSession();
|
||||
return res.status(404).json({ msg: "Street not found" });
|
||||
}
|
||||
|
||||
if (street.status === "adopted") {
|
||||
await session.abortTransaction();
|
||||
session.endSession();
|
||||
return res.status(400).json({ msg: "Street already adopted" });
|
||||
}
|
||||
|
||||
// Check if user has already adopted this street
|
||||
const user = await User.findById(req.user.id).session(session);
|
||||
if (user.adoptedStreets.includes(req.params.id)) {
|
||||
await session.abortTransaction();
|
||||
session.endSession();
|
||||
return res
|
||||
.status(400)
|
||||
.json({ msg: "You have already adopted this street" });
|
||||
}
|
||||
|
||||
// Update street
|
||||
street.adoptedBy = req.user.id;
|
||||
street.status = "adopted";
|
||||
await street.save({ session });
|
||||
|
||||
// Update user's adoptedStreets array
|
||||
user.adoptedStreets.push(street._id);
|
||||
await user.save({ session });
|
||||
|
||||
// Award points for street adoption
|
||||
const { transaction } = await awardStreetAdoptionPoints(
|
||||
req.user.id,
|
||||
street._id,
|
||||
session,
|
||||
);
|
||||
|
||||
// Check and award badges
|
||||
const newBadges = await checkAndAwardBadges(req.user.id, session);
|
||||
|
||||
await session.commitTransaction();
|
||||
session.endSession();
|
||||
|
||||
res.json({
|
||||
street,
|
||||
pointsAwarded: transaction.amount,
|
||||
newBalance: transaction.balanceAfter,
|
||||
badgesEarned: newBadges,
|
||||
});
|
||||
} catch (err) {
|
||||
await session.abortTransaction();
|
||||
session.endSession();
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (street.status === "adopted") {
|
||||
return res.status(400).json({ msg: "Street already adopted" });
|
||||
}
|
||||
|
||||
street.adoptedBy = req.user.id;
|
||||
street.status = "adopted";
|
||||
|
||||
await street.save();
|
||||
|
||||
res.json(street);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user