Skip to content

Commit

Permalink
fixed membership request being added in the user's DB
Browse files Browse the repository at this point in the history
  • Loading branch information
syedali237 committed Oct 12, 2024
1 parent b677650 commit c911769
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/resolvers/Mutation/sendMembershipRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,15 @@ export const sendMembershipRequest: MutationResolvers["sendMembershipRequest"] =

// Checks if the user is blocked
const user = await User.findById(context.userId).lean();
if (user === null) {
throw new errors.NotFoundError(
requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE),
USER_NOT_FOUND_ERROR.CODE,
USER_NOT_FOUND_ERROR.PARAM,
);

Check warning on line 86 in src/resolvers/Mutation/sendMembershipRequest.ts

View check run for this annotation

Codecov / codecov/patch

src/resolvers/Mutation/sendMembershipRequest.ts#L82-L86

Added lines #L82 - L86 were not covered by tests
}

if (
user !== null &&
organization.blockedUsers.some((blockedUser) =>
new mongoose.Types.ObjectId(blockedUser.toString()).equals(user._id),
)
Expand All @@ -97,20 +104,37 @@ export const sendMembershipRequest: MutationResolvers["sendMembershipRequest"] =
organization: organization._id,
});

// console.log("Membership request already exists:", membershipRequestExists);

if (membershipRequestExists) {
// Check if the request is already in the user's document
if (!user.membershipRequests.includes(membershipRequestExists._id)) {
// If it's not in the user's document, add it
await User.findByIdAndUpdate(
context.userId,
{
$push: {
membershipRequests: membershipRequestExists._id,
},
},
{ new: true, runValidators: true },
);
}

throw new errors.ConflictError(
requestContext.translate(MEMBERSHIP_REQUEST_ALREADY_EXISTS.MESSAGE),
MEMBERSHIP_REQUEST_ALREADY_EXISTS.CODE,
MEMBERSHIP_REQUEST_ALREADY_EXISTS.PARAM,
);
}

// Creating Membership Request
const createdMembershipRequest = await MembershipRequest.create({
user: context.userId,
organization: organization._id,
});

// add membership request to organization
// Updating Membership Request in organization
const updatedOrganization = await Organization.findOneAndUpdate(
{
_id: organization._id,
Expand All @@ -129,17 +153,20 @@ export const sendMembershipRequest: MutationResolvers["sendMembershipRequest"] =
await cacheOrganizations([updatedOrganization]);
}

// add membership request to user
await User.updateOne(
{
_id: context.userId,
},
// Updating User
const updateResult = await User.findByIdAndUpdate(
context.userId,
{
$push: {
membershipRequests: createdMembershipRequest._id,
},
},
{ new: true, runValidators: true },
);

if (!updateResult) {
throw new Error("Failed to update user with membership request");

Check warning on line 168 in src/resolvers/Mutation/sendMembershipRequest.ts

View check run for this annotation

Codecov / codecov/patch

src/resolvers/Mutation/sendMembershipRequest.ts#L168

Added line #L168 was not covered by tests
}

return createdMembershipRequest.toObject();
};

0 comments on commit c911769

Please sign in to comment.