Skip to content

Commit

Permalink
update TagUser model to include organizationId
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Nov 1, 2024
1 parent 428fc28 commit de316a2
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/models/TagUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/resolvers/Mutation/assignToUserTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
8 changes: 7 additions & 1 deletion src/resolvers/Mutation/assignUserTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 2 additions & 0 deletions src/resolvers/User/tagsAssignedWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const tagsAssignedWith: UserResolvers["tagsAssignedWith"] = async (
TagUser.find({
...filter,
userId: parent._id,
organizationId: args.organizationId,
})
.sort(sort)
.limit(parsedArgs.limit)
Expand All @@ -85,6 +86,7 @@ export const tagsAssignedWith: UserResolvers["tagsAssignedWith"] = async (

TagUser.find({
userId: parent._id,
organizationId: args.organizationId,
})
.countDocuments()
.exec(),
Expand Down
3 changes: 3 additions & 0 deletions tests/helpers/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const createAndAssignUsersToTag = async (
await TagUser.create({
userId: user?._id,
tagId: tag?._id,
organizationId: tag?.organizationId,
});
testUsers.push(user);
}
Expand All @@ -93,6 +94,7 @@ export const createTagsAndAssignToUser = async (
await TagUser.create({
userId: testUser?._id,
tagId: testTag?._id,
organizationId: testTag?.organizationId,
});

const tags: TestUserTagType[] = [testTag];
Expand All @@ -108,6 +110,7 @@ export const createTagsAndAssignToUser = async (
await TagUser.create({
tagId: newTag?._id,
userId: testUser?._id,
organizationId: newTag.organizationId,
});
}

Expand Down
4 changes: 4 additions & 0 deletions tests/resolvers/Mutation/assignToUserTags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
]);

Expand Down Expand Up @@ -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,
}),
]);

Expand Down
4 changes: 4 additions & 0 deletions tests/resolvers/Mutation/removeFromUserTags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
]);

Expand Down Expand Up @@ -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,
}),
]);

Expand Down
3 changes: 3 additions & 0 deletions tests/resolvers/Mutation/removeUserTag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
]);
});
Expand Down
3 changes: 3 additions & 0 deletions tests/resolvers/UserTag/usersAssignedTo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ beforeAll(async () => {
await TagUser.create({
tagId: testTag?._id,
userId: randomUser?._id,
organizationId: testTag?.organizationId,
});
});

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit de316a2

Please sign in to comment.