Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed membership request being added in the user's DB #2582

Merged
merged 17 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import * as graphqlEslint from "@graphql-eslint/eslint-plugin";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended
});

export default [
Expand Down Expand Up @@ -44,7 +44,9 @@ export default [

languageOptions: {
parser: tsParser,
globals: globals.node,
globals: {
...globals.node,
},
},
rules: {
"no-restricted-imports": [
Expand Down Expand Up @@ -142,7 +144,7 @@ export default [
},

languageOptions: {
parser: parser,
parser: graphqlEslint.parser,
},
},
{
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 25 additions & 8 deletions src/resolvers/Mutation/sendMembershipRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ export const sendMembershipRequest: MutationResolvers["sendMembershipRequest"] =

// Checks if the user is blocked
const user = await User.findById(context.userId).lean();

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

if (membershipRequestExists) {
// Check if the request is already in the user's document
if (
user != null &&
!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,16 +147,15 @@ export const sendMembershipRequest: MutationResolvers["sendMembershipRequest"] =
await cacheOrganizations([updatedOrganization]);
}

// add membership request to user
await User.updateOne(
{
_id: context.userId,
},
// Updating User
await User.findByIdAndUpdate(
context.userId,
{
$push: {
membershipRequests: createdMembershipRequest._id,
},
},
{ new: true, runValidators: true },
syedali237 marked this conversation as resolved.
Show resolved Hide resolved
);

return createdMembershipRequest.toObject();
Expand Down
Loading