diff --git a/src/models/TagUser.ts b/src/models/TagUser.ts index cd2ccb8d0c..fd7889ca35 100644 --- a/src/models/TagUser.ts +++ b/src/models/TagUser.ts @@ -30,6 +30,11 @@ const tagUserSchema = new Schema({ ref: "OrganizationTagUser", required: true, }, + organizationId: { + type: Schema.Types.ObjectId, + ref: "Organization", + required: true, + }, tagColor: { type: String, required: false, diff --git a/src/resolvers/Mutation/assignToUserTags.ts b/src/resolvers/Mutation/assignToUserTags.ts index 0cc3e40783..0aa6ad0ce0 100644 --- a/src/resolvers/Mutation/assignToUserTags.ts +++ b/src/resolvers/Mutation/assignToUserTags.ts @@ -153,7 +153,13 @@ export const assignToUserTags: MutationResolvers["assignToUserTags"] = async ( Array.from(allTagsToAssign).map((tagId) => ({ updateOne: { filter: { userId, tagId: new Types.ObjectId(tagId) }, - update: { $setOnInsert: { userId, tagId: new Types.ObjectId(tagId) } }, + update: { + $setOnInsert: { + userId, + tagId: new Types.ObjectId(tagId), + organizationId: currentTag.organizationId, + }, + }, upsert: true, setDefaultsOnInsert: true, }, diff --git a/src/resolvers/Mutation/assignUserTag.ts b/src/resolvers/Mutation/assignUserTag.ts index ee2186642d..d7f076ae8a 100644 --- a/src/resolvers/Mutation/assignUserTag.ts +++ b/src/resolvers/Mutation/assignUserTag.ts @@ -171,7 +171,13 @@ export const assignUserTag: MutationResolvers["assignUserTag"] = async ( const tagUserDocs = allAncestorTags.map((tagId) => ({ updateOne: { filter: { userId: assigneeId, tagId }, - update: { $setOnInsert: { userId: assigneeId, tagId } }, + update: { + $setOnInsert: { + userId: assigneeId, + tagId, + organizationId: tag.organizationId, + }, + }, upsert: true, setDefaultsOnInsert: true, }, diff --git a/src/resolvers/User/tagsAssignedWith.ts b/src/resolvers/User/tagsAssignedWith.ts index c4e08ef8a0..303fb76a0f 100644 --- a/src/resolvers/User/tagsAssignedWith.ts +++ b/src/resolvers/User/tagsAssignedWith.ts @@ -76,6 +76,7 @@ export const tagsAssignedWith: UserResolvers["tagsAssignedWith"] = async ( TagUser.find({ ...filter, userId: parent._id, + organizationId: args.organizationId, }) .sort(sort) .limit(parsedArgs.limit) @@ -85,6 +86,7 @@ export const tagsAssignedWith: UserResolvers["tagsAssignedWith"] = async ( TagUser.find({ userId: parent._id, + organizationId: args.organizationId, }) .countDocuments() .exec(), diff --git a/tests/helpers/tags.ts b/tests/helpers/tags.ts index ddbf70cc8a..06d3f49e57 100644 --- a/tests/helpers/tags.ts +++ b/tests/helpers/tags.ts @@ -79,6 +79,7 @@ export const createAndAssignUsersToTag = async ( await TagUser.create({ userId: user?._id, tagId: tag?._id, + organizationId: tag?.organizationId, }); testUsers.push(user); } @@ -93,6 +94,7 @@ export const createTagsAndAssignToUser = async ( await TagUser.create({ userId: testUser?._id, tagId: testTag?._id, + organizationId: testTag?.organizationId, }); const tags: TestUserTagType[] = [testTag]; @@ -108,6 +110,7 @@ export const createTagsAndAssignToUser = async ( await TagUser.create({ tagId: newTag?._id, userId: testUser?._id, + organizationId: newTag.organizationId, }); } diff --git a/tests/resolvers/Mutation/assignToUserTags.spec.ts b/tests/resolvers/Mutation/assignToUserTags.spec.ts index 9486c2db7e..4d9397d9fb 100644 --- a/tests/resolvers/Mutation/assignToUserTags.spec.ts +++ b/tests/resolvers/Mutation/assignToUserTags.spec.ts @@ -165,10 +165,12 @@ describe("resolvers -> Mutation -> assignToUserTags", () => { TagUser.create({ userId: randomUser2?._id, tagId: testTag2?._id, + organizationId: testTag2?.organizationId, }), TagUser.create({ userId: randomUser3?._id, tagId: testTag2?._id, + organizationId: testTag2?.organizationId, }), ]); @@ -234,10 +236,12 @@ describe("resolvers -> Mutation -> assignToUserTags", () => { TagUser.create({ userId: randomUser2?._id, tagId: newTestTag?._id, + organizationId: newTestTag?.organizationId, }), TagUser.create({ userId: randomUser3?._id, tagId: newTestTag?._id, + organizationId: newTestTag?.organizationId, }), ]); diff --git a/tests/resolvers/Mutation/removeFromUserTags.spec.ts b/tests/resolvers/Mutation/removeFromUserTags.spec.ts index 9ac18b68db..76069c8c38 100644 --- a/tests/resolvers/Mutation/removeFromUserTags.spec.ts +++ b/tests/resolvers/Mutation/removeFromUserTags.spec.ts @@ -168,10 +168,12 @@ describe("resolvers -> Mutation -> removeFromUserTags", () => { TagUser.create({ userId: randomUser2?._id, tagId: testTag2?._id, + organizationId: testTag2?.organizationId, }), TagUser.create({ userId: randomUser3?._id, tagId: testTag2?._id, + organizationId: testTag2?.organizationId, }), ]); @@ -276,10 +278,12 @@ describe("resolvers -> Mutation -> removeFromUserTags", () => { TagUser.create({ userId: randomUser2?._id, tagId: newTestTag?._id, + organizationId: newTestTag?.organizationId, }), TagUser.create({ userId: randomUser3?._id, tagId: newTestTag?._id, + organizationId: newTestTag?.organizationId, }), ]); diff --git a/tests/resolvers/Mutation/removeUserTag.spec.ts b/tests/resolvers/Mutation/removeUserTag.spec.ts index cb9f4e815d..7fd2375a9a 100644 --- a/tests/resolvers/Mutation/removeUserTag.spec.ts +++ b/tests/resolvers/Mutation/removeUserTag.spec.ts @@ -49,14 +49,17 @@ beforeAll(async () => { { userId: testUser?._id, tagId: rootTag?._id, + organizationId: rootTag?.organizationId, }, { userId: testUser?._id, tagId: childTag1?._id, + organizationId: childTag1?.organizationId, }, { userId: testUser?._id, tagId: childTag2?._id, + organizationId: childTag2?.organizationId, }, ]); }); diff --git a/tests/resolvers/UserTag/usersAssignedTo.spec.ts b/tests/resolvers/UserTag/usersAssignedTo.spec.ts index 19db25982b..0333954353 100644 --- a/tests/resolvers/UserTag/usersAssignedTo.spec.ts +++ b/tests/resolvers/UserTag/usersAssignedTo.spec.ts @@ -45,6 +45,7 @@ beforeAll(async () => { await TagUser.create({ tagId: testTag?._id, userId: randomUser?._id, + organizationId: testTag?.organizationId, }); }); @@ -80,11 +81,13 @@ describe("usersAssignedTo resolver", () => { const tagUser1 = await TagUser.findOne({ tagId: testTag?._id, userId: testUser?._id, + organizationId: testTag?.organizationId, }); const tagUser2 = await TagUser.findOne({ tagId: testTag?._id, userId: randomUser?._id, + organizationId: testTag?.organizationId, }); const user1 = await User.findOne({