Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Oct 25, 2024
1 parent e18b739 commit 8a5fb72
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/resolvers/Mutation/addPeopleToUserTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
85 changes: 39 additions & 46 deletions src/resolvers/UserTag/usersToAssignTo.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 3 additions & 1 deletion src/typeDefs/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8a5fb72

Please sign in to comment.