Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform players array in findOpenGames and ensure filtered results include id and username only #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions api/hooks/customGameHook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down