Files
adopt-a-street/backend/routes/events.js
William Valentin e7396c10d6 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>
2025-11-01 10:42:51 -07:00

124 lines
3.1 KiB
JavaScript

const express = require("express");
const mongoose = require("mongoose");
const Event = require("../models/Event");
const User = require("../models/User");
const auth = require("../middleware/auth");
const { asyncHandler } = require("../middleware/errorHandler");
const {
createEventValidation,
eventIdValidation,
} = require("../middleware/validators/eventValidator");
const { paginate, buildPaginatedResponse } = require("../middleware/pagination");
const {
awardEventParticipationPoints,
checkAndAwardBadges,
} = require("../services/gamificationService");
const router = express.Router();
// Get all events (with pagination)
router.get(
"/",
paginate,
asyncHandler(async (req, res) => {
const { skip, limit, page } = req.pagination;
const events = await Event.find()
.sort({ date: -1 })
.skip(skip)
.limit(limit)
.populate("participants", ["name", "profilePicture"]);
const totalCount = await Event.countDocuments();
res.json(buildPaginatedResponse(events, totalCount, page, limit));
}),
);
// Create an event
router.post(
"/",
auth,
createEventValidation,
asyncHandler(async (req, res) => {
const { title, description, date, location } = req.body;
const newEvent = new Event({
title,
description,
date,
location,
});
const event = await newEvent.save();
res.json(event);
}),
);
// RSVP to an event
router.put(
"/rsvp/:id",
auth,
eventIdValidation,
asyncHandler(async (req, res) => {
const session = await mongoose.startSession();
session.startTransaction();
try {
const event = await Event.findById(req.params.id).session(session);
if (!event) {
await session.abortTransaction();
session.endSession();
return res.status(404).json({ msg: "Event not found" });
}
// Check if the user has already RSVPed
if (
event.participants.filter(
(participant) => participant.toString() === req.user.id,
).length > 0
) {
await session.abortTransaction();
session.endSession();
return res.status(400).json({ msg: "Already RSVPed" });
}
event.participants.unshift(req.user.id);
await event.save({ session });
// Update user's events array
const user = await User.findById(req.user.id).session(session);
if (!user.events.includes(event._id)) {
user.events.push(event._id);
await user.save({ session });
}
// Award points for event participation
const { transaction } = await awardEventParticipationPoints(
req.user.id,
event._id,
session
);
// Check and award badges
const newBadges = await checkAndAwardBadges(req.user.id, session);
await session.commitTransaction();
session.endSession();
res.json({
participants: event.participants,
pointsAwarded: transaction.amount,
newBalance: transaction.balanceAfter,
badgesEarned: newBadges,
});
} catch (err) {
await session.abortTransaction();
session.endSession();
throw err;
}
}),
);
module.exports = router;