Docker compose con variabili d'ambiente
This commit is contained in:
@@ -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: {},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user