Skip to content

Commit

Permalink
implemented user and appUserProfile Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AVtheking committed Apr 11, 2024
1 parent 53234da commit 76e8ea8
Show file tree
Hide file tree
Showing 101 changed files with 2,670 additions and 632 deletions.
22 changes: 11 additions & 11 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ services:
volumes:
- redis-data:/data/redis

# talawa-api-container:
# build: .
# ports:
# - 4000:4000
# depends_on:
# - mongodb
# - redis-stack-server
# environment:
# - MONGO_DB_URL=mongodb://mongodb:27017
# - REDIS_HOST=redis-stack-server
# - REDIS_PORT=6379
talawa-api-container:
build: .
ports:
- 4000:4000
depends_on:
- mongodb
- redis-stack-server
environment:
- MONGO_DB_URL=mongodb://mongodb:27017
- REDIS_HOST=redis-stack-server
- REDIS_PORT=6379

volumes:
mongodb-data:
Expand Down
41 changes: 34 additions & 7 deletions src/resolvers/Mutation/addEventAttendee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,58 @@ import {
USER_NOT_MEMBER_FOR_ORGANIZATION,
} from "../../constants";
import { errors, requestContext } from "../../libraries";
import type { InterfaceEvent } from "../../models";
import type {
InterfaceAppUserProfile,
InterfaceEvent,
InterfaceUser,
} from "../../models";
import { AppUserProfile, Event, EventAttendee, User } from "../../models";
import { cacheAppUserProfile } from "../../services/AppUserProfileCache/cacheAppUserProfile";
import { findAppUserProfileCache } from "../../services/AppUserProfileCache/findAppUserProfileCache";
import { cacheEvents } from "../../services/EventCache/cacheEvents";
import { findEventsInCache } from "../../services/EventCache/findEventInCache";
import { cacheUsers } from "../../services/UserCache/cacheUser";
import { findUserInCache } from "../../services/UserCache/findUserInCache";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";

export const addEventAttendee: MutationResolvers["addEventAttendee"] = async (
_parent,
args,
context,
) => {
const currentUser = await User.findOne({
_id: context.userId,
});
let currentUser: InterfaceUser | null;

const userFoundInCache = await findUserInCache([context.userId]);
currentUser = userFoundInCache[0];
if (currentUser === null) {
currentUser = await User.findOne({
_id: context.userId,
}).lean();
if (currentUser !== null) {
await cacheUsers([currentUser]);
}
}
if (currentUser === null) {
throw new errors.NotFoundError(
requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE),
USER_NOT_FOUND_ERROR.CODE,
USER_NOT_FOUND_ERROR.PARAM,
);
}
const currentUserAppProfile = await AppUserProfile.findOne({
userId: currentUser._id,
}).lean();
let currentUserAppProfile: InterfaceAppUserProfile | null;
const appUserProfileFoundInCache = await findAppUserProfileCache([
currentUser.appUserProfileId,
]);
currentUserAppProfile = appUserProfileFoundInCache[0];
if (currentUserAppProfile === null) {
currentUserAppProfile = await AppUserProfile.findOne({
userId: currentUser._id,
}).lean();
if (currentUserAppProfile !== null) {
await cacheAppUserProfile([currentUserAppProfile]);
}
}

if (!currentUserAppProfile) {
throw new errors.UnauthorizedError(
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
Expand Down
38 changes: 31 additions & 7 deletions src/resolvers/Mutation/addOrganizationCustomField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import {
USER_NOT_FOUND_ERROR,
} from "../../constants";
import { errors, requestContext } from "../../libraries";
import type {
InterfaceAppUserProfile,
InterfaceUser} from "../../models";
import {
AppUserProfile,
Organization,
OrganizationCustomField,
User,
} from "../../models";
import { cacheAppUserProfile } from "../../services/AppUserProfileCache/cacheAppUserProfile";
import { findAppUserProfileCache } from "../../services/AppUserProfileCache/findAppUserProfileCache";
import { cacheUsers } from "../../services/UserCache/cacheUser";
import { findUserInCache } from "../../services/UserCache/findUserInCache";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";

/**
Expand All @@ -31,9 +38,17 @@ import type { MutationResolvers } from "../../types/generatedGraphQLTypes";

export const addOrganizationCustomField: MutationResolvers["addOrganizationCustomField"] =
async (_parent, args, context) => {
const currentUser = await User.findOne({
_id: context.userId,
});
let currentUser: InterfaceUser | null;
const userFoundInCache = await findUserInCache([context.userId]);
currentUser = userFoundInCache[0];
if (currentUser === null) {
currentUser = await User.findOne({
_id: context.userId,
}).lean();
if (currentUser !== null) {
await cacheUsers([currentUser]);
}
}

if (!currentUser) {
throw new errors.NotFoundError(
Expand All @@ -42,10 +57,19 @@ export const addOrganizationCustomField: MutationResolvers["addOrganizationCusto
USER_NOT_FOUND_ERROR.PARAM,
);
}
const currentUserAppProfile = await AppUserProfile.findOne({
userId: currentUser._id,
}).lean();

let currentUserAppProfile: InterfaceAppUserProfile | null;
const appUserProfileFoundInCache = await findAppUserProfileCache([
currentUser.appUserProfileId,
]);
currentUserAppProfile = appUserProfileFoundInCache[0];
if (currentUserAppProfile === null) {
currentUserAppProfile = await AppUserProfile.findOne({
userId: currentUser._id,
}).lean();
if (currentUserAppProfile !== null) {
await cacheAppUserProfile([currentUserAppProfile]);
}
}
if (!currentUserAppProfile) {
throw new errors.UnauthorizedError(
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
Expand Down
19 changes: 16 additions & 3 deletions src/resolvers/Mutation/addPledgeToFundraisingCampaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import {
USER_NOT_MADE_PLEDGE_ERROR,
} from "../../constants";
import { errors, requestContext } from "../../libraries";
import type { InterfaceUser} from "../../models";
import { FundraisingCampaign, User } from "../../models";
import {
FundraisingCampaignPledge,
type InterfaceFundraisingCampaignPledges,
} from "../../models/FundraisingCampaignPledge";
import { cacheUsers } from "../../services/UserCache/cacheUser";
import { findUserInCache } from "../../services/UserCache/findUserInCache";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
/**
* This function adds campaign pledge to campaign.
Expand All @@ -31,10 +34,20 @@ export const addPledgeToFundraisingCampaign: MutationResolvers["addPledgeToFundr
args,
context,
): Promise<InterfaceFundraisingCampaignPledges> => {
const currentUser = await User.findOne({
_id: context.userId,
}).lean();
let currentUser: InterfaceUser | null;

const userFoundInCache = await findUserInCache([context.userId]);

currentUser = userFoundInCache[0];

if (currentUser === null) {
currentUser = await User.findOne({
_id: context.userId,
}).lean();
if (currentUser !== null) {
await cacheUsers([currentUser]);
}
}
// Checks whether currentUser exists.
if (!currentUser) {
throw new errors.NotFoundError(
Expand Down
23 changes: 18 additions & 5 deletions src/resolvers/Mutation/addUserCustomData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { UserCustomData } from "../../models/UserCustomData";
import { Organization, User } from "../../models";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { errors, requestContext } from "../../libraries";
import {
ORGANIZATION_NOT_FOUND_ERROR,
USER_NOT_FOUND_ERROR,
} from "../../constants";
import { errors, requestContext } from "../../libraries";
import type { InterfaceUser} from "../../models";
import { Organization, User } from "../../models";
import { UserCustomData } from "../../models/UserCustomData";
import { cacheUsers } from "../../services/UserCache/cacheUser";
import { findUserInCache } from "../../services/UserCache/findUserInCache";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";

/**
* This function enables a user to add data for a custom field for a joined organization.
Expand All @@ -24,7 +27,17 @@ export const addUserCustomData: MutationResolvers["addUserCustomData"] = async (
) => {
const { organizationId, dataName, dataValue } = args;

const currentUser = await User.findOne({ _id: context.userId });
let currentUser: InterfaceUser | null;
const userFoundInCache = await findUserInCache([context.userId]);
currentUser = userFoundInCache[0];
if (currentUser === null) {
currentUser = await User.findOne({
_id: context.userId,
}).lean();
if (currentUser !== null) {
await cacheUsers([currentUser]);
}
}
if (!currentUser) {
throw new errors.NotFoundError(
requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE),
Expand Down
21 changes: 15 additions & 6 deletions src/resolvers/Mutation/addUserImage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { USER_NOT_FOUND_ERROR } from "../../constants";
import { errors, requestContext } from "../../libraries";
import { User } from "../../models";
import type { InterfaceUser } from "../../models/User";
import { USER_NOT_FOUND_ERROR } from "../../constants";
import { cacheUsers } from "../../services/UserCache/cacheUser";
import { findUserInCache } from "../../services/UserCache/findUserInCache";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { uploadEncodedImage } from "../../utilities/encodedImageStorage/uploadEncodedImage";
/**
* This function adds User Image.
Expand All @@ -18,10 +20,17 @@ export const addUserImage: MutationResolvers["addUserImage"] = async (
args,
context,
) => {
const currentUser = await User.findOne({
_id: context.userId,
}).lean();

let currentUser: InterfaceUser | null;
const userFoundInCache = await findUserInCache([context.userId]);
currentUser = userFoundInCache[0];
if (currentUser === null) {
currentUser = await User.findOne({
_id: context.userId,
}).lean();
if (currentUser !== null) {
await cacheUsers([currentUser]);
}
}
// Checks whether currentUser exists.
if (!currentUser) {
throw new errors.NotFoundError(
Expand Down
18 changes: 14 additions & 4 deletions src/resolvers/Mutation/addUserToUserFamily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "dotenv/config";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { errors, requestContext } from "../../libraries";
import { adminCheck } from "../../utilities/userFamilyAdminCheck";
import type { InterfaceUser} from "../../models";
import { User } from "../../models";
import { UserFamily } from "../../models/userFamily";
import type { InterfaceUserFamily } from "../../models/userFamily";
Expand All @@ -10,6 +11,8 @@ import {
USER_ALREADY_MEMBER_ERROR,
USER_NOT_FOUND_ERROR,
} from "../../constants";
import { findUserInCache } from "../../services/UserCache/findUserInCache";
import { cacheUsers } from "../../services/UserCache/cacheUser";
/**
* This function adds user to the family.
* @param _parent - parent of current request
Expand All @@ -28,10 +31,17 @@ export const addUserToUserFamily: MutationResolvers["addUserToUserFamily"] =
_id: args.familyId,
}).lean();

const currentUser = await User.findById({
_id: context.userId,
});

let currentUser: InterfaceUser | null;
const userFoundInCache = await findUserInCache([context.userId]);
currentUser = userFoundInCache[0];
if (currentUser === null) {
currentUser = await User.findOne({
_id: context.userId,
}).lean();
if (currentUser !== null) {
await cacheUsers([currentUser]);
}
}
// Checks whether user with _id === args.userId exists.
if (currentUser === null) {
throw new errors.NotFoundError(
Expand Down
45 changes: 37 additions & 8 deletions src/resolvers/Mutation/adminRemoveEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ import {
USER_NOT_FOUND_ERROR,
} from "../../constants";
import { errors, requestContext } from "../../libraries";
import { AppUserProfile, Event, Organization, User } from "../../models";
import type {
InterfaceAppUserProfile,
InterfaceUser} from "../../models";
import {
AppUserProfile,
Event,
Organization,
User,
} from "../../models";
import { cacheAppUserProfile } from "../../services/AppUserProfileCache/cacheAppUserProfile";
import { findAppUserProfileCache } from "../../services/AppUserProfileCache/findAppUserProfileCache";
import { cacheEvents } from "../../services/EventCache/cacheEvents";
import { deleteEventFromCache } from "../../services/EventCache/deleteEventFromCache";
import { findEventsInCache } from "../../services/EventCache/findEventInCache";
import { cacheOrganizations } from "../../services/OrganizationCache/cacheOrganizations";
import { findOrganizationsInCache } from "../../services/OrganizationCache/findOrganizationsInCache";
import { cacheUsers } from "../../services/UserCache/cacheUser";
import { findUserInCache } from "../../services/UserCache/findUserInCache";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { adminCheck } from "../../utilities";
/**
Expand Down Expand Up @@ -80,10 +92,17 @@ export const adminRemoveEvent: MutationResolvers["adminRemoveEvent"] = async (
);
}

const currentUser = await User.findOne({
_id: context.userId,
}).lean();

let currentUser: InterfaceUser | null;
const userFoundInCache = await findUserInCache([context.userId]);
currentUser = userFoundInCache[0];
if (currentUser === null) {
currentUser = await User.findOne({
_id: context.userId,
}).lean();
if (currentUser !== null) {
await cacheUsers([currentUser]);
}
}
// Checks whether currentUser exists.
if (!currentUser) {
throw new errors.NotFoundError(
Expand All @@ -92,9 +111,19 @@ export const adminRemoveEvent: MutationResolvers["adminRemoveEvent"] = async (
USER_NOT_FOUND_ERROR.PARAM,
);
}
const currentUserAppProfile = await AppUserProfile.findOne({
userId: currentUser._id,
}).lean();
let currentUserAppProfile: InterfaceAppUserProfile | null;
const appUserProfileFoundInCache = await findAppUserProfileCache([
currentUser.appUserProfileId?.toString(),
]);
currentUserAppProfile = appUserProfileFoundInCache[0];
if (currentUserAppProfile === null) {
currentUserAppProfile = await AppUserProfile.findOne({
userId: currentUser._id,
}).lean();
if (currentUserAppProfile !== null) {
await cacheAppUserProfile([currentUserAppProfile]);
}
}
if (!currentUserAppProfile) {
throw new errors.UnauthorizedError(
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
Expand Down
Loading

0 comments on commit 76e8ea8

Please sign in to comment.