Skip to content

Commit

Permalink
Event caching init
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-0311 committed Sep 15, 2023
1 parent d3978b5 commit 47e32bf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
24 changes: 21 additions & 3 deletions src/resolvers/Mutation/adminRemoveEvent.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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]);
}

Check warning on line 46 in src/resolvers/Mutation/adminRemoveEvent.ts

View check run for this annotation

Codecov / codecov/patch

src/resolvers/Mutation/adminRemoveEvent.ts#L45-L46

Added lines #L45 - L46 were not covered by tests
}

// Checks whether event exists.
if (!event) {
Expand Down Expand Up @@ -101,6 +117,8 @@ export const adminRemoveEvent: MutationResolvers["adminRemoveEvent"] = async (
_id: event._id,
});

await deleteEventFromCache(event._id);

// Returns the deleted event.
return event;
};
6 changes: 3 additions & 3 deletions src/services/EventCache/deleteEventFromCache.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const key = `event:${event._id}`;
const key = `event:${eventId}`;

await EventCache.del(key);

Expand Down
10 changes: 5 additions & 5 deletions src/services/EventCache/findEventInCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down
18 changes: 16 additions & 2 deletions tests/resolvers/Mutation/adminRemoveEvent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,17 +68,24 @@ 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,
},
{
$set: {
organization: Types.ObjectId().toString(),
},
},
{
new: true,
}
);

if (updatedEvent !== null) {
await cacheEvents([updatedEvent]);
}

const args: MutationAdminRemoveEventArgs = {
eventId: testEvent?.id,
};
Expand All @@ -94,17 +102,23 @@ 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,
},
{
$set: {
organization: testOrganization?._id,
},
},
{
new: true,
}
);

if (updatedEvent !== null) {
await cacheEvents([updatedEvent]);
}
const args: MutationAdminRemoveEventArgs = {
eventId: testEvent?.id,
};
Expand Down

0 comments on commit 47e32bf

Please sign in to comment.