Files
Server/repositories/userBadgeRepository.js
T
azures04 b03f5c91e6 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.
2026-09-23 02:09:39 +02:00

64 lines
2.0 KiB
JavaScript

const { pool } = require("../modules/database")
const { DefaultError } = require("../errors/errors")
async function assignBadgeToUser(userId, badgeId) {
try {
const sql = "INSERT INTO `user_badges` (userId, badgeId) VALUES (?, ?)"
const rows = await pool.query(sql, [userId, badgeId])
return rows.insertId
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function findUserBadges(userId) {
try {
const sql = `SELECT ub.*, b.name, b.description, b.icon, b.condition
FROM \`user_badges\` ub
JOIN \`badges\` b ON ub.badgeId = b.id
WHERE ub.userId = ?
ORDER BY ub.unlockedAt DESC`
const rows = await pool.query(sql, [userId])
return rows
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function hasBadge(userId, badgeId) {
try {
const sql = "SELECT COUNT(*) as count FROM `user_badges` WHERE userId = ? AND badgeId = ?"
const rows = await pool.query(sql, [userId, badgeId])
return rows[0].count > 0
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function removeBadgeFromUser(userId, badgeId) {
try {
const sql = "DELETE FROM `user_badges` WHERE userId = ? AND badgeId = ?"
const rows = await pool.query(sql, [userId, badgeId])
return rows.affectedRows > 0
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function countUserBadges(userId) {
try {
const sql = "SELECT COUNT(*) as count FROM `user_badges` WHERE userId = ?"
const rows = await pool.query(sql, [userId])
return rows[0].count
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
module.exports = {
assignBadgeToUser,
findUserBadges,
hasBadge,
removeBadgeFromUser,
countUserBadges
}