From 2040c523663951028ac0e4d00b3f2a192c6e498f Mon Sep 17 00:00:00 2001 From: Ruba Al Mahmoud Date: Sun, 8 Dec 2024 18:55:43 +0300 Subject: [PATCH] Transform players array in findOpenGames to include only id and username --- api/hooks/customGameHook/index.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/api/hooks/customGameHook/index.js b/api/hooks/customGameHook/index.js index afac7416b..201326de2 100644 --- a/api/hooks/customGameHook/index.js +++ b/api/hooks/customGameHook/index.js @@ -30,24 +30,36 @@ module.exports = function gameHook() { }, findOpenGames: function () { return new Promise(function (resolve, reject) { - const recentUpdateThreshhold = dayjs.utc().subtract(1, 'day') - .toDate(); + const recentUpdateThreshold = dayjs.utc().subtract(1, 'day').toDate(); + Game.find({ status: gameService.GameStatus.CREATED, - createdAt: { '>=': recentUpdateThreshhold }, + createdAt: { '>=': recentUpdateThreshold }, }) - .populate('players') + .populate('players') // Populate the players array .exec(function (error, games) { if (error) { return reject(error); - } else if (!games) { + } else if (!games || games.length === 0) { return reject({ message: "Can't find games" }); } - const openGames = games.filter(({ players }) => players.length < 2); - return resolve(openGames); + + // Filter games with fewer than 2 players and transform the players array + const openGames = games + .filter(({ players }) => players.length < 2) // Keep games with fewer than 2 players + .map((game) => { + // Transform players array to include only id and username + game.players = game.players.map((player) => ({ + id: player.id, // Use 'id' since Sails.js generates it automatically + username: player.username, + })); + return game; // Return the updated game object + }); + + return resolve(openGames); // Resolve with transformed open games }); }); - }, + }, findGame: function (id) { return new Promise(function (resolve, reject) { Game.findOne(id)