diff --git a/src/functions/attend-event/handler.ts b/src/functions/attend-event/handler.ts index 02ff9df..b42a71a 100644 --- a/src/functions/attend-event/handler.ts +++ b/src/functions/attend-event/handler.ts @@ -77,8 +77,8 @@ const attendEvent: ValidatedEventAPIGatewayProxyEvent = 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({ @@ -97,6 +97,21 @@ const attendEvent: ValidatedEventAPIGatewayProxyEvent = 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, diff --git a/src/functions/attend-event/schema.ts b/src/functions/attend-event/schema.ts index 7bd246a..3f49ab6 100644 --- a/src/functions/attend-event/schema.ts +++ b/src/functions/attend-event/schema.ts @@ -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; diff --git a/tests/attend-event.test.ts b/tests/attend-event.test.ts index dfe5e85..3703a12 100644 --- a/tests/attend-event.test.ts +++ b/tests/attend-event.test.ts @@ -30,7 +30,7 @@ describe('Attend-Event tests', () => { auth_token: 'mockToken', qr: 'test@test.org', event: 'lunch', - again: false, + limit: 1, }; const path = '/attend-event'; const httpMethod = 'POST'; @@ -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, + }, }, }, }) @@ -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: {},