Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #282 from Arquisoft/184-app-make-ranking
Browse files Browse the repository at this point in the history
184 app make ranking
  • Loading branch information
Manueluz authored Apr 23, 2024
2 parents 2562da1 + dab7fbf commit 5a38e21
Show file tree
Hide file tree
Showing 17 changed files with 2,636 additions and 441 deletions.
3 changes: 2 additions & 1 deletion game_service/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const YAML = require('yaml')

// My own libs
const authMiddleware = require('./auth/authMiddleware');
const { newGame, next, awnser, update, getGameSettingsByUser, getHistory, setGameSettingsByUser, getNumberOfQuestions,
const { newGame, next, awnser, update, getGameSettingsByUser, getHistory, getHistoryByUser, setGameSettingsByUser, getNumberOfQuestions,
getQuestion
} = require("./game/endpoints");
const { saveQuestionsInDB, deleteOlderQuestions, loadInitialQuestions } = require('./services/questionsService');
Expand All @@ -41,6 +41,7 @@ app.post('/api/game/update', update);
app.post('/api/game/settings', getGameSettingsByUser);
app.post('/api/game/updatesettings', setGameSettingsByUser);
app.post('/api/game/getHistory', getHistory);
app.post('/api/game/getHistoryByUser', getHistoryByUser);
app.post('/api/game/numberofquestions', getNumberOfQuestions);
app.post('/api/game/currentquestion', getQuestion);

Expand Down
28 changes: 27 additions & 1 deletion game_service/game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Game Service', () => {

expect(response.statusCode).toBe(200);
expect(response.text).toBe("Santiago");
})
});

it("Should return 200 with a valid token when accessing game settings", async () =>
{
Expand All @@ -121,6 +121,24 @@ describe('Game Service', () => {
expect(response.statusCode).toBe(200);
});

it("Should return 200 with a valid user id when accessing game history by user", async () =>
{
const response = await request(app)
.post('/api/game/getHistoryByUser')
.send({ token: validToken, userId: '1234' });

expect(response.statusCode).toBe(200);
});

it("Should return 400 with a not valid user id when accessing game history by user", async () =>
{
const response = await request(app)
.post('/api/game/getHistoryByUser')
.send({ token: validToken, userId: undefined });

expect(response.statusCode).toBe(400);
});

it("Should return 200 with a valid token when accessing game settings", async () => {
const response = await request(app)
.post('/api/game/settings')
Expand Down Expand Up @@ -173,4 +191,12 @@ describe('Game Service', () => {

expect(response.statusCode).toBe(400);
});

it("Should return 200 with a valid token when get question", async () => {
let response = await request(app)
.post('/api/game/currentquestion')
.send({ token: validToken });

expect(response.statusCode).toBe(200);
});
})
27 changes: 26 additions & 1 deletion game_service/game/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ const getHistory = async (req,res) => {
return res.status(200).json(games.map(game => game.toJSON()))
}

const getHistoryByUser = async (req, res) =>
{
const userId = req.body.userId;

if (!userId)
{
res.status(400).json({ error: 'user id not valid' });
return;
}

const games = await Game.findAll({
where: {
user_id: userId
},
include: [{
model: Question,
as: 'Questions' // alias defined in the association
}]
});

return res.status(200).json(
games.map(game => game.toJSON())
);
}

const getGameSettingsByUser = async (req, res) =>{
const userId = await jwt.verify(req.body.token, privateKey).user_id;

Expand Down Expand Up @@ -232,4 +257,4 @@ const setGameSettingsByUser = async (req, res) =>{
res.status(200).send(settings);
}

module.exports = {newGame, next, awnser, update, getHistory, getGameSettingsByUser, setGameSettingsByUser, getNumberOfQuestions, getQuestion}
module.exports = {newGame, next, awnser, update, getHistory, getHistoryByUser, getGameSettingsByUser, setGameSettingsByUser, getNumberOfQuestions, getQuestion}
27 changes: 26 additions & 1 deletion userdetails_service/service/gameService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,29 @@ const getHistory = async (req,res) => {
res.status(200).send((await response.json()));
}

module.exports = getHistory;
const getHistoryByUser = async (req, res) =>
{
if (!req.body.userId)
{
res.status(400).json({ error: 'user id is not valid' });
return;
}

let response = await fetch("http://game:8003/api/game/getHistoryByUser", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: req.body.token,
userId: req.body.userId
})
});

res.status(200).send(
await response.json()
);
}

module.exports = { getHistory, getHistoryByUser };
3 changes: 2 additions & 1 deletion userdetails_service/userdetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const YAML = require('yaml')
const authMiddleware = require('./auth/authMiddleware');

const getUsername = require("./service/loginService");
const getHistory = require("./service/gameService");
const { getHistory, getHistoryByUser } = require("./service/gameService");

const port = 8004;
const app = express();
Expand All @@ -30,6 +30,7 @@ app.use('/api/*',authMiddleware); // Auth middleware for the questions API
// Api endpoints
app.post('/api/userdetails/name', getUsername);
app.post('/api/userdetails/history', getHistory);
app.post('/api/userdetails/history-by-user', getHistoryByUser);

// Read the OpenAPI YAML file synchronously
openapiPath='./openapi.yaml'
Expand Down
39 changes: 39 additions & 0 deletions userdetails_service/userdetails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,43 @@ describe('User Details Endpoints', () => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual(mockHistory);
});

it("Should return history if token is valid in getHistoryByUser endpoint", async () => {
// Generate token for the mock user
const token = jwt.sign({ user_id: 1 }, privateKey);

// Mock response from the external service
const mockHistory = [{ game_id: 1, score: 100 }, { game_id: 2, score: 150 }];

// Mocking fetch call
global.fetch = jest.fn().mockResolvedValue({
json: async () => (mockHistory)
});

const response = await request(app)
.post('/api/userdetails/history-by-user')
.send({ token: token, userId: 1 });

expect(response.statusCode).toBe(200);
expect(response.body).toEqual(mockHistory);
});

it("Should return 400 if user id is not valid in getHistoryByUser endpoint", async () => {
// Generate token for the mock user
const token = jwt.sign({ user_id: 1 }, privateKey);

// Mock response from the external service
const mockHistory = [{ game_id: 1, score: 100 }, { game_id: 2, score: 150 }];

// Mocking fetch call
global.fetch = jest.fn().mockResolvedValue({
json: async () => (mockHistory)
});

const response = await request(app)
.post('/api/userdetails/history-by-user')
.send({ token: token, userId: undefined });

expect(response.statusCode).toBe(400);
});
});
Loading

0 comments on commit 5a38e21

Please sign in to comment.