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

Add functionality to update acknowledgement with path request #87

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 27 additions & 20 deletions controllers/auth.ts
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can ignore this file as it is to fix the typescript eslint issue and got caught in this PR.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NextFunction, Request, Response } from 'express';
import logger from '../utils/logger';
import * as authService from '../services/authService';
import { apiResponse } from '../@types/apiReponse';
import { MicrosoftOAuthJson } from '../@types/providers';

/**
* Makes authentication call to google statergy
Expand Down Expand Up @@ -87,27 +88,33 @@ const microsoftAuthCallback = (
const rCalUiUrl = new URL(config.get('services.rCalUi.baseUrl'));

try {
return passport.authenticate('microsoft', {}, async (err, _, user) => {
if (err) {
logger.error(err);
return res.boom(Boom.unauthorized('User cannot be authenticated'));
return passport.authenticate(
'microsoft',
{},
async (err: any, _: any, user: { _json: MicrosoftOAuthJson }) => {
if (err) {
logger.error(err);
return res.boom(Boom.unauthorized('User cannot be authenticated'));
}
const userData = await authService.loginOrSignupWithMicrosoft(
user._json
);
const token = authService.generateAuthToken({ userId: userData?.id });

// respond with a cookie
res.cookie(config.get('userAccessToken.cookieName'), token, {
domain: rCalUiUrl.hostname,
expires: new Date(
Date.now() + config.get('userAccessToken.ttl') * 1000
),
httpOnly: true,
secure: true,
sameSite: 'lax',
});

return res.redirect(rCalUiUrl.href);
}
const userData = await authService.loginOrSignupWithMicrosoft(user._json);
const token = authService.generateAuthToken({ userId: userData?.id });

// respond with a cookie
res.cookie(config.get('userAccessToken.cookieName'), token, {
domain: rCalUiUrl.hostname,
expires: new Date(
Date.now() + config.get('userAccessToken.ttl') * 1000
),
httpOnly: true,
secure: true,
sameSite: 'lax',
});

return res.redirect(rCalUiUrl.href);
})(req, res, next);
)(req, res, next);
} catch (err: any) {
logger.error(err);

Expand Down
45 changes: 43 additions & 2 deletions controllers/events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { z } from 'zod';
import { Event, EventType } from '@prisma/client';
import { Request, Response } from 'express';
import Boom from '@hapi/boom';
import prisma from '../prisma/prisma';
import { findEvent, findEventFromCalendar } from '../services/eventsService';
import { getAcknowledgementSchema } from '../middlewares/validators/zod-schemas/events';

/**
* Route used to post event
Expand Down Expand Up @@ -99,7 +101,10 @@ const getEvents = async (req: Request, res: Response): Promise<any> => {
* @param req {Object} - Express request object
* @param res {Object} - Express response object
*/
const getCalendarEvents = async (req: Request, res: Response): Promise<any> => {
const getCalendarEvents = async (
req: Request,
res: Response
): Promise<Response> => {
try {
const { calendarId } = req.params;
const { startTime, endTime } = req.query;
Expand All @@ -120,4 +125,40 @@ const getCalendarEvents = async (req: Request, res: Response): Promise<any> => {
}
};

export { postEvent, getEvents, getCalendarEvents };
type TAcknowledgementStatusReq = z.infer<typeof getAcknowledgementSchema>;

const getAcknowledgementStatus = async (
req: Request,
res: Response
): Promise<Response> => {
const { eventId, attendeeId } = req.params;
const { status } = req.body as TAcknowledgementStatusReq['body'];

const attendeeData = await prisma.attendees.findFirst({
where: {
eventId: Number(eventId),
attendeeId: Number(attendeeId),
},
});

if (attendeeData === null) {
return res.boom(Boom.badRequest('Attendee not found'));
}

const updatedData = await prisma.attendees.update({
where: {
id: attendeeData.id,
},
data: {
acknowledgement: status,
},
});

if (updatedData && updatedData.acknowledgement === status) {
isVivek99 marked this conversation as resolved.
Show resolved Hide resolved
return res.json({ message: 'Acknowledgement status updated' });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move messages to constant files.

Copy link
Collaborator Author

@yesyash yesyash Apr 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the message is specifically used for this controller will this change be required?

} else {
return res.boom(Boom.internal('Something went wrong'));
}
};

export { postEvent, getEvents, getCalendarEvents, getAcknowledgementStatus };
17 changes: 16 additions & 1 deletion middlewares/validators/zod-schemas/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const getEventSchema = z.object({
}),
});

const getAcknowledgementSchema = z.object({
params: z.object({
eventId: z.preprocess((a) => Number(a), z.number().positive()),
attendeeId: z.preprocess((a) => Number(a), z.number().positive()),
}),
body: z.object({
status: z.enum(['ACCEPTED', 'DECLINED', 'TENTATIVE']),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too. Let us keep it in constants

}),
});

const getCalenderEventSchema = z.object({
params: z.object({
calendarId: z.preprocess((a) => Number(a), z.number().positive()),
Expand All @@ -34,4 +44,9 @@ const getCalenderEventSchema = z.object({
}),
});

export { postEventSchema, getEventSchema, getCalenderEventSchema };
export {
postEventSchema,
getEventSchema,
getCalenderEventSchema,
getAcknowledgementSchema,
};
18 changes: 13 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,20 @@ enum RecurringFrequency {
HOURLY
}

enum EventAcknowledgements {
AWAITING
ACCEPTED
DECLINED
TENTATIVE
}

model Attendees {
id Int @id @default(autoincrement())
eventId Int
event Event @relation(fields: [eventId], references: [id])
attendeeId Int
attendee Users @relation(fields: [attendeeId], references: [id])
id Int @id @default(autoincrement())
eventId Int
event Event @relation(fields: [eventId], references: [id])
attendeeId Int
attendee Users @relation(fields: [attendeeId], references: [id])
acknowledgement EventAcknowledgements @default(AWAITING)
}

enum CalendarType {
Expand Down
16 changes: 15 additions & 1 deletion routes/events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Router } from 'express';
import { getCalendarEvents, getEvents, postEvent } from '../controllers/events';
import {
getAcknowledgementStatus,
getCalendarEvents,
getEvents,
postEvent,
} from '../controllers/events';
import authenticate from '../middlewares/authenticate';
import { validate } from '../middlewares/validators/validator';
import {
getAcknowledgementSchema,
getCalenderEventSchema,
getEventSchema,
postEventSchema,
Expand All @@ -13,6 +19,14 @@ const router = Router();
/* eslint-disable @typescript-eslint/no-misused-promises */
router.post('/', authenticate, validate(postEventSchema), postEvent);
router.get('/:eventId', authenticate, validate(getEventSchema), getEvents);

router.patch(
'/:eventId/attendee/:attendeeId',
// authenticate,
validate(getAcknowledgementSchema),
getAcknowledgementStatus
);

router.get(
'/calendar/:calendarId',
authenticate,
Expand Down