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

I112 public events get route #114

Merged
merged 4 commits into from
Nov 10, 2023
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
34 changes: 32 additions & 2 deletions controllers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Event } 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 {
findEvent,
findEventFromCalendar,
findPublicEvent,
} from '../services/eventsService';

/**
* Route used to post event
Expand Down Expand Up @@ -44,6 +48,7 @@ const postEvent = async (
ownerId: userId,
calendarId: eventData.calendarId,
eventTypeId: 1,
isPrivate: eventData.isPrivate,
Attendees: {
createMany: { data: allAttendeesData },
},
Expand Down Expand Up @@ -166,4 +171,29 @@ const getCalendarEvents = async (req: Request, res: Response): Promise<any> => {
}
};

export { postEvent, patchEvent, getEvents, getCalendarEvents };
/**
* Get public event start and end time (child events)
*
* @param req {Object} - Express request object
* @param res {Object} - Express response object
*/
const getPublicEvents = async (req: Request, res: Response): Promise<any> => {
try {
const { startTime, endTime } = req.query;

const eventResponse: any = await findPublicEvent(
Number(startTime),
Number(endTime)
);

if (!eventResponse) {
return res.boom(Boom.notFound('Events not found'));
}
return res.status(200).send({ data: eventResponse });
} catch (err) {
logger.error('Error while getting event', { err });
return res.boom(Boom.badImplementation());
}
};

export { postEvent, patchEvent, getEvents, getCalendarEvents, getPublicEvents };
10 changes: 10 additions & 0 deletions middlewares/validators/zod-schemas/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const postEventSchema = z.object({
endTime: z.number(),
calendarId: z.number(),
attendees: z.array(z.string()).optional(),
isPrivate: z.boolean(),
// TODO: Recuring event to be added
})
.strict(),
Expand All @@ -32,6 +33,7 @@ const patchEventSchema = z.object({
endTime: z.number().optional(),
calendarId: z.number().optional(),
attendees: z.array(z.string()).optional(),
isPrivate: z.boolean(),
// TODO: Recuring event to be added
})
.strict(),
Expand All @@ -53,9 +55,17 @@ const getCalenderEventSchema = z.object({
}),
});

const getPublicEventSchema = z.object({
query: z.object({
startTime: z.preprocess((a) => Number(a), z.number().positive()).optional(),
endTime: z.preprocess((a) => Number(a), z.number().positive()).optional(),
}),
});

export {
postEventSchema,
patchEventSchema,
getEventSchema,
getCalenderEventSchema,
getPublicEventSchema,
};
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ model Event {
EventType EventType? @relation(fields: [eventTypeId], references: [id])
calendarId Int
Calendar Calendar? @relation(fields: [calendarId], references: [id])
isPrivate Boolean @default(true)
isDeleted Boolean @default(false)
RecurringEvent RecurringEvent[]
Attendees Attendees[]
Expand Down
4 changes: 4 additions & 0 deletions routes/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getEvents,
postEvent,
patchEvent,
getPublicEvents,
} from '../controllers/events';
import authenticate from '../middlewares/authenticate';
import { validate } from '../middlewares/validators/validator';
Expand All @@ -12,6 +13,7 @@ import {
getEventSchema,
postEventSchema,
patchEventSchema,
getPublicEventSchema,
} from '../middlewares/validators/zod-schemas/events';
import {
calendarBelongsToUser,
Expand All @@ -21,6 +23,7 @@ import {
const router = Router();

/* eslint-disable @typescript-eslint/no-misused-promises */
router.get('/', authenticate, validate(getPublicEventSchema), getPublicEvents);
router.post(
'/',
authenticate,
Expand Down Expand Up @@ -49,6 +52,7 @@ router.get(
calendarBelongsToUser,
getCalendarEvents
);

/* eslint-disable @typescript-eslint/no-misused-promises */

export default router;
39 changes: 39 additions & 0 deletions services/eventsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ const findEventFromCalendar = async (
}
};

const findPublicEvent = async (
startTime: number,
endTime: number
): Promise<eventWithAttendees[]> => {
try {
const whereCondition: {} = {
isDeleted: false,
isPrivate: false,
...(startTime && { startTime: { gte: new Date(startTime) } }),
...(endTime && { endTime: { lte: new Date(endTime) } }),
};

// TODO: add pagination

const event: eventWithAttendees[] = await prisma.event.findMany({
where: whereCondition,
include: {
Attendees: {
select: {
attendee: {
select: {
email: true,
},
},
},
},
},
});

return event;
} catch (err) {
logger.error('parentEvent: error while finding parent event', {
err,
});
throw err;
}
};

const findCalendarWithId = async (
calendarId: number
): Promise<Calendar | null> => {
Expand All @@ -89,6 +127,7 @@ const findEventWithId = async (eventId: number): Promise<Event | null> => {
export {
findEvent,
findEventFromCalendar,
findPublicEvent,
findCalendarWithId,
findEventWithId,
};
Loading