From 47e32bf447ffeb1df6c30715d755ee27965aa79b Mon Sep 17 00:00:00 2001 From: Kanishka Date: Sat, 16 Sep 2023 01:06:36 +0530 Subject: [PATCH] Event caching init --- src/resolvers/Mutation/adminRemoveEvent.ts | 24 ++++++++++++++++--- .../EventCache/deleteEventFromCache.ts | 6 ++--- src/services/EventCache/findEventInCache.ts | 10 ++++---- .../Mutation/adminRemoveEvent.spec.ts | 18 ++++++++++++-- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/resolvers/Mutation/adminRemoveEvent.ts b/src/resolvers/Mutation/adminRemoveEvent.ts index ca7e2471cf..6f5dd4a0dd 100644 --- a/src/resolvers/Mutation/adminRemoveEvent.ts +++ b/src/resolvers/Mutation/adminRemoveEvent.ts @@ -1,6 +1,7 @@ import type { MutationResolvers } from "../../types/generatedGraphQLTypes"; import { errors, requestContext } from "../../libraries"; import { adminCheck } from "../../utilities"; +import type { InterfaceEvent } from "../../models"; import { User, Organization, Event } from "../../models"; import { USER_NOT_FOUND_ERROR, @@ -9,6 +10,9 @@ import { } from "../../constants"; import { findOrganizationsInCache } from "../../services/OrganizationCache/findOrganizationsInCache"; import { cacheOrganizations } from "../../services/OrganizationCache/cacheOrganizations"; +import { findEventsInCache } from "../../services/EventCache/findEventInCache"; +import { cacheEvents } from "../../services/EventCache/cacheEvents"; +import { deleteEventFromCache } from "../../services/EventCache/deleteEventFromCache"; /** * This function enables an admin to remove a event * @param _parent - parent of current request @@ -26,9 +30,21 @@ export const adminRemoveEvent: MutationResolvers["adminRemoveEvent"] = async ( args, context ) => { - const event = await Event.findOne({ - _id: args.eventId, - }).lean(); + let event: InterfaceEvent | null; + + const eventFoundInCache = await findEventsInCache([args.eventId]); + + event = eventFoundInCache[0]; + + if (eventFoundInCache[0] === null) { + event = await Event.findOne({ + _id: args.eventId, + }).lean(); + + if (event !== null) { + await cacheEvents([event]); + } + } // Checks whether event exists. if (!event) { @@ -101,6 +117,8 @@ export const adminRemoveEvent: MutationResolvers["adminRemoveEvent"] = async ( _id: event._id, }); + await deleteEventFromCache(event._id); + // Returns the deleted event. return event; }; diff --git a/src/services/EventCache/deleteEventFromCache.ts b/src/services/EventCache/deleteEventFromCache.ts index fd7d8c74b7..b203ac904a 100644 --- a/src/services/EventCache/deleteEventFromCache.ts +++ b/src/services/EventCache/deleteEventFromCache.ts @@ -1,10 +1,10 @@ import EventCache from "../redisCache"; -import type { InterfaceEvent } from "../../models"; +import type { Types } from "mongoose"; export async function deleteEventFromCache( - event: InterfaceEvent + eventId: Types.ObjectId ): Promise { - const key = `event:${event._id}`; + const key = `event:${eventId}`; await EventCache.del(key); diff --git a/src/services/EventCache/findEventInCache.ts b/src/services/EventCache/findEventInCache.ts index 908afed220..4ff7264a4f 100644 --- a/src/services/EventCache/findEventInCache.ts +++ b/src/services/EventCache/findEventInCache.ts @@ -37,17 +37,17 @@ export async function findEventsInCache( }) : [], - createdAt: new Date(eventObj.createdAt), - organization: Types.ObjectId(eventObj.organization), startDate: new Date(eventObj.startDate), - endDate: eventObj?.endDate ? new Date(eventObj.endDate) : null, + ...(eventObj?.endDate ? { endDate: new Date(eventObj.endDate) } : {}), // Conditional removal of endDate field - startTime: eventObj?.startTime ? new Date(eventObj.startTime) : null, + ...(eventObj?.startTime + ? { startTime: new Date(eventObj.startTime) } + : {}), // Conditional removal of startTime field - endTime: eventObj?.endTime ? new Date(eventObj.endTime) : null, + ...(eventObj?.endTime ? { endTime: new Date(eventObj.endTime) } : {}), // Conditional removal of endTime field creator: Types.ObjectId(eventObj.creator), }; diff --git a/tests/resolvers/Mutation/adminRemoveEvent.spec.ts b/tests/resolvers/Mutation/adminRemoveEvent.spec.ts index 645ea79695..132f0d788c 100644 --- a/tests/resolvers/Mutation/adminRemoveEvent.spec.ts +++ b/tests/resolvers/Mutation/adminRemoveEvent.spec.ts @@ -20,6 +20,7 @@ import type { import type { TestEventType } from "../../helpers/events"; import { createTestEvent } from "../../helpers/events"; import { cacheOrganizations } from "../../../src/services/OrganizationCache/cacheOrganizations"; +import { cacheEvents } from "../../../src/services/EventCache/cacheEvents"; let testUser: TestUserType; let testOrganization: TestOrganizationType; @@ -67,7 +68,7 @@ describe("resolvers -> Mutation -> adminRemoveEvent", () => { it(`throws NotFoundError if no organization exists with _id === event.organization for event with _id === args.eventId`, async () => { try { - await Event.updateOne( + const updatedEvent = await Event.findOneAndUpdate( { _id: testEvent?._id, }, @@ -75,9 +76,16 @@ describe("resolvers -> Mutation -> adminRemoveEvent", () => { $set: { organization: Types.ObjectId().toString(), }, + }, + { + new: true, } ); + if (updatedEvent !== null) { + await cacheEvents([updatedEvent]); + } + const args: MutationAdminRemoveEventArgs = { eventId: testEvent?.id, }; @@ -94,7 +102,7 @@ describe("resolvers -> Mutation -> adminRemoveEvent", () => { it(`throws NotFoundError if no user exists with _id === context.userId`, async () => { try { - await Event.updateOne( + const updatedEvent = await Event.findOneAndUpdate( { _id: testEvent?._id, }, @@ -102,9 +110,15 @@ describe("resolvers -> Mutation -> adminRemoveEvent", () => { $set: { organization: testOrganization?._id, }, + }, + { + new: true, } ); + if (updatedEvent !== null) { + await cacheEvents([updatedEvent]); + } const args: MutationAdminRemoveEventArgs = { eventId: testEvent?.id, };