generated from azures04/Base-REST-API
Add badges, checkins, and transport endpoints
Introduce gamification and check-in support plus expanded transport/admin APIs. Added DDL for checkins, badges and user_badges. New repositories: badgeRepository, userBadgeRepository, checkinRepository. New routes: admin badge management, admin transport (lines/platforms/stations), public transport endpoints, user checkins and stations endpoints, and user badges on /me. Services updated: adminService (badge CRUD & user assignment), userService (checkins, badge queries), authService (hasAdminRights). Minor package.json name update. These changes implement database, repo, service and route wiring for badges, user_badins and checkin features and additional transport admin routes.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
const banRepo = require("../repositories/banRepo")
|
||||
const badgeRepo = require("../repositories/badgeRepository")
|
||||
const userBadgeRepo = require("../repositories/userBadgeRepository")
|
||||
|
||||
async function banUser(userId, message, expiresAt) {
|
||||
const existingBan = await banRepo.isBanned(userId)
|
||||
@@ -31,8 +33,91 @@ async function isUserBanned(userId) {
|
||||
return true
|
||||
}
|
||||
|
||||
async function createBadge({ name, description, icon, condition }) {
|
||||
const existing = await badgeRepo.findAllBadges()
|
||||
if (existing.some(b => b.name === name)) {
|
||||
throw new DefaultError(409, "Badge already exists", "Duplicate badge name")
|
||||
}
|
||||
return await badgeRepo.createBadge(name, description, icon, condition)
|
||||
}
|
||||
|
||||
async function getAllBadges() {
|
||||
return await badgeRepo.findAllBadges()
|
||||
}
|
||||
|
||||
async function getBadgeById({ badgeId }) {
|
||||
const badge = await badgeRepo.findBadgeById(badgeId)
|
||||
if (!badge) {
|
||||
throw new DefaultError(404, "Badge not found", "Badge ID does not exist")
|
||||
}
|
||||
return badge
|
||||
}
|
||||
|
||||
async function updateBadge({ badgeId, updateData }) {
|
||||
const badge = await badgeRepo.findBadgeById(badgeId)
|
||||
if (!badge) {
|
||||
throw new DefaultError(404, "Badge not found", "Badge ID does not exist")
|
||||
}
|
||||
|
||||
const allowedFields = ["name", "description", "icon", "condition"]
|
||||
const filteredData = Object.keys(updateData)
|
||||
.filter(key => allowedFields.includes(key))
|
||||
.reduce((obj, key) => {
|
||||
obj[key] = updateData[key]
|
||||
return obj
|
||||
}, {})
|
||||
|
||||
if (Object.keys(filteredData).length === 0) {
|
||||
throw new DefaultError(400, "No valid fields to update", "Invalid update data")
|
||||
}
|
||||
|
||||
return await badgeRepo.updateBadge(badgeId, filteredData)
|
||||
}
|
||||
|
||||
async function deleteBadge({ badgeId }) {
|
||||
const badge = await badgeRepo.findBadgeById(badgeId)
|
||||
if (!badge) {
|
||||
throw new DefaultError(404, "Badge not found", "Badge ID does not exist")
|
||||
}
|
||||
return await badgeRepo.deleteBadge(badgeId)
|
||||
}
|
||||
|
||||
async function assignBadgeToUser({ userId, badgeId }) {
|
||||
const badge = await badgeRepo.findBadgeById(badgeId)
|
||||
if (!badge) {
|
||||
throw new DefaultError(404, "Badge not found", "Badge ID does not exist")
|
||||
}
|
||||
|
||||
const hasAlready = await userBadgeRepo.hasBadge(userId, badgeId)
|
||||
if (hasAlready) {
|
||||
throw new DefaultError(409, "User already has this badge", "Duplicate badge assignment")
|
||||
}
|
||||
|
||||
return await userBadgeRepo.assignBadgeToUser(userId, badgeId)
|
||||
}
|
||||
|
||||
async function removeBadgeFromUser({ userId, badgeId }) {
|
||||
const badge = await badgeRepo.findBadgeById(badgeId)
|
||||
if (!badge) {
|
||||
throw new DefaultError(404, "Badge not found", "Badge ID does not exist")
|
||||
}
|
||||
|
||||
const success = await userBadgeRepo.removeBadgeFromUser(userId, badgeId)
|
||||
if (!success) {
|
||||
throw new DefaultError(404, "Badge not assigned to user", "User does not have this badge")
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
banUser,
|
||||
unbanUser,
|
||||
isUserBanned
|
||||
isUserBanned,
|
||||
createBadge,
|
||||
getAllBadges,
|
||||
getBadgeById,
|
||||
updateBadge,
|
||||
deleteBadge,
|
||||
assignBadgeToUser,
|
||||
removeBadgeFromUser
|
||||
}
|
||||
Reference in New Issue
Block a user