From 8a5fb725c64a3b57138a26569ca6570311661255 Mon Sep 17 00:00:00 2001 From: meetul Date: Fri, 25 Oct 2024 19:33:00 +0530 Subject: [PATCH] improve code --- src/resolvers/Mutation/addPeopleToUserTag.ts | 6 +- src/resolvers/UserTag/usersToAssignTo.ts | 85 +++++++++----------- src/typeDefs/mutations.ts | 4 +- 3 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/resolvers/Mutation/addPeopleToUserTag.ts b/src/resolvers/Mutation/addPeopleToUserTag.ts index f70932a7d27..77ad67bae3d 100644 --- a/src/resolvers/Mutation/addPeopleToUserTag.ts +++ b/src/resolvers/Mutation/addPeopleToUserTag.ts @@ -183,11 +183,11 @@ export const addPeopleToUserTag: MutationResolvers["addPeopleToUserTag"] = } } - const tagUserDocs = newAssignments.flatMap((userId) => + const tagUserDocs = newAssignments.flatMap((user) => allAncestorTags.map((tagId) => ({ updateOne: { - filter: { userId, tagId }, - update: { $setOnInsert: { userId, tagId } }, + filter: { userId: user._id, tagId }, + update: { $setOnInsert: { userId: user._id, tagId } }, upsert: true, setDefaultsOnInsert: true, }, diff --git a/src/resolvers/UserTag/usersToAssignTo.ts b/src/resolvers/UserTag/usersToAssignTo.ts index 053589429fe..3a7a1655718 100644 --- a/src/resolvers/UserTag/usersToAssignTo.ts +++ b/src/resolvers/UserTag/usersToAssignTo.ts @@ -1,15 +1,19 @@ import type { UserTagResolvers } from "../../types/generatedGraphQLTypes"; import type { InterfaceUser } from "../../models"; import { User } from "../../models"; +import type { + DefaultGraphQLArgumentError, + GraphQLConnectionTraversalDirection, + ParseGraphQLConnectionCursorArguments, + ParseGraphQLConnectionCursorResult, +} from "../../utilities/graphQLConnection"; + import { - type DefaultGraphQLArgumentError, - type GraphQLConnectionTraversalDirection, - type ParseGraphQLConnectionCursorArguments, - type ParseGraphQLConnectionCursorResult, getCommonGraphQLConnectionSort, parseGraphQLConnectionArguments, transformToDefaultGraphQLConnection, } from "../../utilities/graphQLConnection"; + import { GraphQLError } from "graphql"; import { MAXIMUM_FETCH_LIMIT } from "../../constants"; import { Types } from "mongoose"; @@ -65,58 +69,47 @@ export const usersToAssignTo: UserTagResolvers["usersToAssignTo"] = async ( direction: parsedArgs.direction, }); - const [objectList, totalCountResult] = await Promise.all([ - // find all the users belonging to the current organization - // who haven't been assigned to this tag - User.aggregate([ - // Step 1: Match users whose joinedOrgs contains the orgId - { - $match: { - ...filter, - joinedOrganizations: parent.organizationId, - }, + const commonPipeline = [ + // Step 1: Match users whose joinedOrgs contains the orgId + { + $match: { + ...filter, + joinedOrganizations: parent.organizationId, }, - // Step 2: Perform a left join with TagUser collection on userId - { - $lookup: { - from: "tagusers", // Name of the collection holding TagUser documents - localField: "_id", - foreignField: "userId", - as: "tagUsers", - }, + }, + // Step 2: Perform a left join with TagUser collection on userId + { + $lookup: { + from: "tagusers", // Name of the collection holding TagUser documents + localField: "_id", + foreignField: "userId", + as: "tagUsers", }, - // Step 3: Filter out users that have a tagUser document with the specified tagId - { - $match: { - tagUsers: { - $not: { - $elemMatch: { tagId: parent._id }, - }, + }, + // Step 3: Filter out users that have a tagUser document with the specified tagId + { + $match: { + tagUsers: { + $not: { + $elemMatch: { tagId: parent._id }, }, }, }, + }, + ]; + + // Execute the queries using the common pipeline + const [objectList, totalCountResult] = await Promise.all([ + // First aggregation to get the user list + User.aggregate([ + ...commonPipeline, { $sort: { ...sort }, }, { $limit: parsedArgs.limit }, ]), - User.aggregate([ - { $match: { joinedOrganizations: parent.organizationId } }, - { - $lookup: { - from: "tagusers", - localField: "_id", - foreignField: "userId", - as: "tagUsers", - }, - }, - { - $match: { - tagUsers: { $not: { $elemMatch: { tagId: parent._id } } }, - }, - }, - { $count: "totalCount" }, - ]), + // Second aggregation to count total users + User.aggregate([...commonPipeline, { $count: "totalCount" }]), ]); const totalCount = diff --git a/src/typeDefs/mutations.ts b/src/typeDefs/mutations.ts index 8b110f162db..619845aa920 100644 --- a/src/typeDefs/mutations.ts +++ b/src/typeDefs/mutations.ts @@ -35,7 +35,9 @@ export const mutations = gql` addUserToUserFamily(userId: ID!, familyId: ID!): UserFamily! @auth - addPeopleToUserTag(input: AddPeopleToUserTagInput!): UserTag @auth + addPeopleToUserTag(input: AddPeopleToUserTagInput!): UserTag + @auth + @role(requires: ADMIN) removeUserFromUserFamily(userId: ID!, familyId: ID!): UserFamily! @auth