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

feat: Add support for bulk tag operations (GSoC) #2616

Merged
merged 14 commits into from
Oct 28, 2024
7 changes: 7 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -1068,6 +1068,7 @@ type Mutation {
addUserCustomData(dataName: String!, dataValue: Any!, organizationId: ID!): UserCustomData!
addUserImage(file: String!): User!
addUserToUserFamily(familyId: ID!, userId: ID!): UserFamily!
assignToUserTags(input: TagActionsInput!): UserTag
assignUserTag(input: ToggleUserTagAssignInput!): User
blockPluginCreationBySuperadmin(blockUser: Boolean!, userId: ID!): AppUserProfile!
blockUser(organizationId: ID!, userId: ID!): User!
@@ -1129,6 +1130,7 @@ type Mutation {
removeEventAttendee(data: EventAttendeeInput!): User!
removeEventVolunteer(id: ID!): EventVolunteer!
removeEventVolunteerGroup(id: ID!): EventVolunteerGroup!
removeFromUserTags(input: TagActionsInput!): UserTag
removeFundraisingCampaignPledge(id: ID!): FundraisingCampaignPledge!
removeMember(data: UserAndOrganizationInput!): Organization!
removeOrganization(id: ID!): UserData!
@@ -1609,6 +1611,11 @@ type Subscription {
onPluginUpdate: Plugin
}

input TagActionsInput {
currentTagId: ID!
selectedTagIds: [ID!]!
}

scalar Time

input ToggleUserTagAssignInput {
172 changes: 172 additions & 0 deletions src/resolvers/Mutation/assignToUserTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { Types } from "mongoose";
import {
TAG_NOT_FOUND,
USER_NOT_FOUND_ERROR,
USER_NOT_AUTHORIZED_ERROR,
} from "../../constants";
import { errors, requestContext } from "../../libraries";
import type {
InterfaceAppUserProfile,
InterfaceOrganizationTagUser,
InterfaceUser,
} from "../../models";
import {
AppUserProfile,
OrganizationTagUser,
TagUser,
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";

/**
* This function enables an admin to assign multiple tags to users with a specified tag.
* @param _parent - parent of current request
* @param args - payload provided with the request
* @param context - context of entire application
* @remarks The following checks are done:
* 1. If the current user exists and has a profile.
* 2. If the current user is an admin for the organization of the tags.
* 3. If the currentTagId exists and the selected tags exist.
* 4. Assign the tags to users who have the currentTagId.
* @returns Array of tags that were assigned to users.
*/
export const assignToUserTags: MutationResolvers["assignToUserTags"] = async (
_parent,
args,
context,
) => {
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 the currentUser exists.
if (!currentUser) {
throw new errors.NotFoundError(
requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE),
USER_NOT_FOUND_ERROR.CODE,
USER_NOT_FOUND_ERROR.PARAM,
);
}

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]);
}
}
meetulr marked this conversation as resolved.
Show resolved Hide resolved

if (!currentUserAppProfile) {
throw new errors.UnauthorizedError(
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
USER_NOT_AUTHORIZED_ERROR.CODE,
USER_NOT_AUTHORIZED_ERROR.PARAM,
);
}

// Get the current tag object
const currentTag = await OrganizationTagUser.findOne({
_id: args.input.currentTagId,
}).lean();

if (!currentTag) {
throw new errors.NotFoundError(
requestContext.translate(TAG_NOT_FOUND.MESSAGE),
TAG_NOT_FOUND.CODE,
TAG_NOT_FOUND.PARAM,
);
}

// Boolean to determine whether user is an admin of the organization of the current tag.
const currentUserIsOrganizationAdmin = currentUserAppProfile.adminFor.some(
(orgId) =>
orgId &&
new Types.ObjectId(orgId.toString()).equals(currentTag.organizationId),
);
meetulr marked this conversation as resolved.
Show resolved Hide resolved

if (!(currentUserIsOrganizationAdmin || currentUserAppProfile.isSuperAdmin)) {
throw new errors.UnauthorizedError(
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
USER_NOT_AUTHORIZED_ERROR.CODE,
USER_NOT_AUTHORIZED_ERROR.PARAM,
);
}

// Find all users with the currentTagId
const usersWithCurrentTag = await TagUser.find({
tagId: currentTag._id,
}).lean();

const userIdsWithCurrentTag = usersWithCurrentTag.map(
(userTag) => userTag.userId,
);

// Validate selected tags
const selectedTags = await OrganizationTagUser.find({
_id: { $in: args.input.selectedTagIds },
}).lean();

// Check if all requested tags were found
if (selectedTags.length !== args.input.selectedTagIds.length) {
throw new errors.NotFoundError(
requestContext.translate(TAG_NOT_FOUND.MESSAGE),
TAG_NOT_FOUND.CODE,
TAG_NOT_FOUND.PARAM,
);
}
meetulr marked this conversation as resolved.
Show resolved Hide resolved

// Find and assign ancestor tags
const allTagsToAssign = new Set<string>();
for (const tag of selectedTags) {
let currentTagToProcess: InterfaceOrganizationTagUser | null = tag;
while (currentTagToProcess) {
allTagsToAssign.add(currentTagToProcess._id.toString());
if (currentTagToProcess.parentTagId) {
const parentTag: InterfaceOrganizationTagUser | null =
await OrganizationTagUser.findOne({
_id: currentTagToProcess.parentTagId,
}).lean();
currentTagToProcess = parentTag || null;
} else {
currentTagToProcess = null;
}
}
}
meetulr marked this conversation as resolved.
Show resolved Hide resolved

const tagUserDocs = userIdsWithCurrentTag.flatMap((userId) =>
Array.from(allTagsToAssign).map((tagId) => ({
updateOne: {
filter: { userId, tagId: new Types.ObjectId(tagId) },
update: { $setOnInsert: { userId, tagId } },
upsert: true,
setDefaultsOnInsert: true,
},
})),
);
meetulr marked this conversation as resolved.
Show resolved Hide resolved

if (tagUserDocs.length > 0) {
await TagUser.bulkWrite(tagUserDocs);
}

return currentTag;
};
6 changes: 5 additions & 1 deletion src/resolvers/Mutation/index.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import { addUserImage } from "./addUserImage";
import { addUserToUserFamily } from "./addUserToUserFamily";
import { addPeopleToUserTag } from "./addPeopleToUserTag";
import { assignUserTag } from "./assignUserTag";
import { assignToUserTags } from "./assignToUserTags";
import { blockPluginCreationBySuperadmin } from "./blockPluginCreationBySuperadmin";
import { blockUser } from "./blockUser";
import { cancelMembershipRequest } from "./cancelMembershipRequest";
@@ -78,6 +79,7 @@ import { removeUserFamily } from "./removeUserFamily";
import { removeUserFromUserFamily } from "./removeUserFromUserFamily";
import { removeUserImage } from "./removeUserImage";
import { removeUserTag } from "./removeUserTag";
import { removeFromUserTags } from "./removeFromUserTags";
import { resetCommunity } from "./resetCommunity";
import { revokeRefreshTokenForUser } from "./revokeRefreshTokenForUser";
import { saveFcmToken } from "./saveFcmToken";
@@ -127,10 +129,11 @@ export const Mutation: MutationResolvers = {
addUserImage,
addUserToUserFamily,
addPeopleToUserTag,
assignUserTag,
assignToUserTags,
removeUserFamily,
removeUserFromUserFamily,
createUserFamily,
assignUserTag,
blockPluginCreationBySuperadmin,
blockUser,
cancelMembershipRequest,
@@ -197,6 +200,7 @@ export const Mutation: MutationResolvers = {
removeUserCustomData,
removeUserImage,
removeUserTag,
removeFromUserTags,
resetCommunity,
revokeRefreshTokenForUser,
saveFcmToken,
Loading