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 }