Skip to content

Commit

Permalink
Merge pull request #71 from HackRU/buy-ins-in-points
Browse files Browse the repository at this point in the history
Updated points with buy_ins projecttion. added tests
  • Loading branch information
ethxng authored Oct 8, 2024
2 parents ab4eef0 + 14ad7f5 commit 5ebb12e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/functions/points/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ const points: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event)
}

// get users points
const pointUser = await pointsCollection.findOne({ email: email });

const pointUser = await pointsCollection.findOne(
{ email: email },
// eslint-disable-next-line @typescript-eslint/naming-convention
{ projection: { _id: 0, balance: 1, total_points: 1, buy_ins: 1 } }
);

if (!pointUser) {
return {
statusCode: 404,
Expand All @@ -52,12 +58,16 @@ const points: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event)
};
}

// Check if esists
const buyIns = Array.isArray(pointUser.buy_ins) ? pointUser.buy_ins : [];

return {
statusCode: 200,
body: JSON.stringify({
statusCode: 200,
balance: pointUser.balance,
total_points: pointUser.total_points,
buy_ins: buyIns,
}),
};
} catch (error) {
Expand Down
40 changes: 37 additions & 3 deletions tests/points.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,59 @@ describe('Points endpoint', () => {
expect(JSON.parse(result.body).message).toBe('Points not found for this user.');
});

it('should return 200 with balance and total_points for valid user', async () => {
it('should return 200 with balance, total_points, and buy_ins for valid user', async () => {
const userData = {
email: 'valid@email.com',
auth_token: 'validToken',
};

const mockEvent = createEvent(userData, path, httpMethod);
(util.validateToken as jest.Mock).mockReturnValue(true);
const findOneMock = util.MongoDB.getInstance('uri').getCollection('users').findOne as jest.Mock;
findOneMock.mockResolvedValueOnce({ email: 'valid@email.com' }); // User exists
findOneMock.mockResolvedValueOnce({
email: 'valid@email.com',
balance: 100,
total_points: 150,
buy_ins: [
{ prize_id: 'prizeA', buy_in: 50 },
{ prize_id: 'prizeB', buy_in: 30 },
],
}); // Points and buy_ins found

const result = await main(mockEvent, mockContext, jest.fn());

expect(result.statusCode).toBe(200);
const body = JSON.parse(result.body);
expect(body.balance).toBe(100);
expect(body.total_points).toBe(150);
expect(body.buy_ins).toEqual([
{ prize_id: 'prizeA', buy_in: 50 },
{ prize_id: 'prizeB', buy_in: 30 },
]);
});

it('should return 200 with balance, total_points, and empty buy_ins array if not present', async () => {
const userData = {
email: 'valid@email.com',
auth_token: 'validToken',
};
const mockEvent = createEvent(userData, path, httpMethod);
(util.validateToken as jest.Mock).mockReturnValue(true);
const findOneMock = util.MongoDB.getInstance('uri').getCollection('users').findOne as jest.Mock;
findOneMock.mockResolvedValueOnce({ email: 'valid@email.com' }); // User exists
findOneMock.mockResolvedValueOnce({ user_email: 'valid@email.com', balance: 100, total_points: 150 }); // Points found
findOneMock.mockResolvedValueOnce({
email: 'valid@email.com',
balance: 100,
total_points: 150,
}); // Points found, but no buy_ins

const result = await main(mockEvent, mockContext, jest.fn());

expect(result.statusCode).toBe(200);
const body = JSON.parse(result.body);
expect(body.balance).toBe(100);
expect(body.total_points).toBe(150);
expect(body.buy_ins).toEqual([]);
});

it('should return 500 for internal server error', async () => {
Expand Down

0 comments on commit 5ebb12e

Please sign in to comment.