Docker compose con variabili d'ambiente

This commit is contained in:
Lorenzo Sanesi
2026-08-10 13:52:13 +02:00
parent f4afaf0792
commit 04ba3d541f
6 changed files with 147 additions and 50 deletions
+1 -5
View File
@@ -19,10 +19,6 @@ module.exports = {
uri:
process.env.MONGO_URI ||
'mongodb://localhost:27017/amici-scommesse',
options: {
user: 'root',
pass: 'root@servovio',
authSource: 'admin',
},
options: {},
},
};
+1 -1
View File
@@ -7,7 +7,7 @@ class UsersController {
async getCurrentUser(req, res, next) {
try {
const user = await this.usersService.getUserById(req.userId);
const user = await this.usersService.getCurrentUserWithRank(req.userId);
return ResponseHandler.success(res, user, 'User retrieved successfully');
} catch (error) {
next(error);
@@ -32,6 +32,27 @@ class UsersRepository {
}
}
async count() {
try {
return await this.User.count();
} catch (error) {
throw new DatabaseError('Failed to count users', error);
}
}
async countWithScoreAbove(score, excludeId) {
try {
return await this.User.count({
where: {
score: { [Op.gt]: score },
id: { [Op.ne]: excludeId },
},
});
} catch (error) {
throw new DatabaseError('Failed to count users above score', error);
}
}
async findById(id, options = {}) {
try {
const user = await this.User.findByPk(id, options);
+11
View File
@@ -13,6 +13,17 @@ class UsersService {
return await this.usersRepository.findById(id);
}
async getCurrentUserWithRank(id) {
const user = await this.usersRepository.findById(id);
const userJson = user.toJSON ? user.toJSON() : { ...user };
const totalUsers = await this.usersRepository.count();
const usersAbove = await this.usersRepository.countWithScoreAbove(userJson.score, id);
const rankPercentile = totalUsers > 1
? Math.max(1, Math.round((usersAbove / totalUsers) * 100))
: 1;
return { ...userJson, rankPercentile };
}
async searchUsers(query, excludeUserId) {
return await this.usersRepository.search(query, excludeUserId);
}