Files
Server/services/userService.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

84 lines
2.5 KiB
JavaScript

const userRepo = require("../repositories/usersRepo")
const userBadgeRepo = require("../repositories/userBadgeRepository")
const checkinRepo = require("../repositories/checkinRepository")
const cache = require("../modules/cache")
async function getProfile({ userId }) {
const cachedUser = cache.users.get(userId)
if (cachedUser) {
return cachedUser
}
return await userRepo.findById(userId)
}
async function updateProfile({ userId, profile = { displayName, avatarURL } }) {
cache.users.delete(userId)
return await userRepo.updateProfile(userId, profile.displayName, profile.avatarURL)
}
async function deleteAccount({ userId }) {
return await userRepo.remove(userId)
}
async function isLocal({ userId }) {
return await userRepo.isLocal(userId)
}
async function createUser({ identifier, serverId, remoteId = null, profile = { displayName, avatarUrl } }) {
return await userRepo.create(identifier, serverId, remoteId, profile.displayName, profile.avatarUrl)
}
async function getShadowUser({ serverId, remoteId }) {
return await userRepo.findByServerAndRemoteId(serverId, remoteId)
}
async function getUserBadges({ userId }) {
return await userBadgeRepo.findUserBadges(userId)
}
async function countUserBadges({ userId }) {
return await userBadgeRepo.countUserBadges(userId)
}
async function hasBadge({ userId, badgeId }) {
return await userBadgeRepo.hasBadge(userId, badgeId)
}
async function createCheckin({ userId, stationId, platformId, latitude, longitude, distance }) {
const checkinId = await checkinRepo.createCheckin(userId, stationId, platformId, latitude, longitude, distance)
cache.users.delete(userId)
return checkinId
}
async function getUserCheckins({ userId, limit = 50, offset = 0 }) {
return await checkinRepo.findCheckinsByUserId(userId, limit, offset)
}
async function getUserCheckinsCount({ userId }) {
return await checkinRepo.countCheckinsByUser(userId)
}
async function getUserUniqueStations({ userId }) {
return await checkinRepo.findUniqueStationsByUser(userId)
}
async function getUserStationCheckinsCount({ userId, stationId }) {
return await checkinRepo.countCheckinsByUserAndStation(userId, stationId)
}
module.exports = {
isLocal,
getProfile,
createUser,
updateProfile,
deleteAccount,
getShadowUser,
getUserBadges,
countUserBadges,
hasBadge,
createCheckin,
getUserCheckins,
getUserCheckinsCount,
getUserUniqueStations,
getUserStationCheckinsCount
}