Skip to content

Commit

Permalink
Merge pull request #57 from HackRU/feat-event-points
Browse files Browse the repository at this point in the history
feat: implement points in attend-event
  • Loading branch information
ethxng authored Sep 20, 2024
2 parents 4fd4402 + ebaec16 commit 4a53d3e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
19 changes: 17 additions & 2 deletions src/functions/attend-event/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ const attendEvent: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (ev
$push: { [`day_of.event.${hackEvent}.time`]: currentTime },
}
);
} else if (event.body.again === false) {
// if can only attend this event once and user has already attended
} else if (attendEvent.day_of.event[hackEvent].attend >= event.body.limit) {
// if attended this event the max times allowed as per limit
return {
statusCode: 409,
body: JSON.stringify({
Expand All @@ -97,6 +97,21 @@ const attendEvent: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (ev
);
}

if (event.body.points) {
const points = db.getCollection('f24-points-syst');
const userPoints = await points.findOne({ email: event.body.qr });
if (!userPoints) await points.insertOne({ email: event.body.qr, balance: 0, total_points: 0 });

if (event.body.points < 0)
await points.updateOne({ email: event.body.qr }, { $inc: { balance: event.body.points } });
else if (event.body.points > 0) {
await points.updateOne(
{ email: event.body.qr },
{ $inc: { balance: event.body.points, total_points: event.body.points } }
);
}
}

// return success case
return {
statusCode: 200,
Expand Down
5 changes: 3 additions & 2 deletions src/functions/attend-event/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export default {
auth_token: { type: 'string' },
qr: { type: 'string', format: 'email' },
event: { type: 'string' },
again: { type: 'boolean', default: true },
points: { type: 'number' },
limit: { type: 'number' },
},
required: ['auth_email', 'auth_token', 'qr', 'event'],
required: ['auth_email', 'auth_token', 'qr', 'event', 'limit'],
} as const;
8 changes: 4 additions & 4 deletions tests/attend-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Attend-Event tests', () => {
auth_token: 'mockToken',
qr: '[email protected]',
event: 'lunch',
again: false,
limit: 1,
};
const path = '/attend-event';
const httpMethod = 'POST';
Expand Down Expand Up @@ -83,12 +83,13 @@ describe('Attend-Event tests', () => {

// case 4
it('user tries to check into an event the second time but it can only be attended once', async () => {
userData.again = false;
findOneMock
.mockReturnValueOnce({
day_of: {
event: {
lunch: 1,
lunch: {
attend: 1,
},
},
},
})
Expand All @@ -113,7 +114,6 @@ describe('Attend-Event tests', () => {

// case 5
it('success check-in to an event', async () => {
userData.again = true;
findOneMock
.mockReturnValueOnce({
day_of: {},
Expand Down

0 comments on commit 4a53d3e

Please sign in to comment.