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

feat: /leaderboard endpoint #60

Merged
merged 7 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import waiver from '@functions/waiver';
import resume from '@functions/resume';
import resetPassword from '@functions/reset-password';
import forgotPassword from '@functions/forgot-password';
import leaderboard from '@functions/leaderboard';
import points from '@functions/points';

import * as path from 'path';
Expand Down Expand Up @@ -48,6 +49,7 @@ const serverlessConfiguration: AWS = {
discord,
forgotPassword,
resetPassword,
leaderboard,
points,
},
package: { individually: true, patterns: ['!.env*', '.env.vault'] },
Expand Down
1 change: 1 addition & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export { default as waiver } from './waiver';
export { default as resume } from './resume';
export { default as resetPassword } from './reset-password';
export { default as forgotPassword } from './forgot-password';
export { default as leaderboard } from './leaderboard';
export { default as points } from './points';
34 changes: 34 additions & 0 deletions src/functions/leaderboard/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { APIGatewayProxyHandler } from 'aws-lambda';
import { middyfy } from '@libs/lambda';
import { MongoDB } from '../../util';
import * as path from 'path';
import * as dotenv from 'dotenv';
dotenv.config({ path: path.resolve(process.cwd(), '.env') });

const leaderboard: APIGatewayProxyHandler = async () => {
try {
const db = MongoDB.getInstance(process.env.MONGO_URI);
await db.connect();
const points = db.getCollection('f24-points-syst');

const cursor = points.find().sort({ total_points: -1 }).limit(20);
const topPlayers = await cursor.toArray();

return {
statusCode: 200,
body: JSON.stringify(topPlayers),
};
} catch (error) {
console.error('Error loading top 20', error);
return {
statusCode: 500,
body: JSON.stringify({
statusCode: 500,
message: 'Internal Server Error',
error,
}),
};
}
};

export const main = middyfy(leaderboard);
14 changes: 14 additions & 0 deletions src/functions/leaderboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { handlerPath } from '@libs/handler-resolver';

export default {
handler: `${handlerPath(__dirname)}/handler.main`,
events: [
{
http: {
method: 'get',
path: 'leaderboard',
cors: true,
},
},
],
};
41 changes: 41 additions & 0 deletions tests/leaderboard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { main } from '../src/functions/leaderboard/handler';
import { createEvent, mockContext } from './helper';

jest.mock('../src/util', () => ({
MongoDB: {
getInstance: jest.fn().mockReturnValue({
connect: jest.fn(),
disconnect: jest.fn(),
getCollection: jest.fn().mockReturnValue({
find: jest.fn().mockReturnValue({
sort: jest.fn().mockReturnValue({
limit: jest.fn().mockReturnValue({
toArray: jest.fn().mockReturnValue([
{ name: 'Player1', total_points: 150 },
{ name: 'Player2', total_points: 130 },
{ name: 'Player3', total_points: 120 },
{ name: 'Player4', total_points: 110 },
{ name: 'Player5', total_points: 110 },
]),
}),
}),
}),
}),
}),
},
}));
describe('/leaderboard endpoint', () => {
const mockCallback = jest.fn();
const mockEvent = createEvent({}, '/leaderboard', 'GET');
it('Successfully find top 20', async () => {
const res = await main(mockEvent, mockContext, mockCallback);
expect(res.statusCode).toEqual(200);
expect(JSON.parse(res.body)).toEqual([
{ name: 'Player1', total_points: 150 },
{ name: 'Player2', total_points: 130 },
{ name: 'Player3', total_points: 120 },
{ name: 'Player4', total_points: 110 },
{ name: 'Player5', total_points: 110 },
]);
});
});
Loading