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

Feature Added: An option for users to leave the Organization (Fixes PalisadoesFoundation/talawa-admin#1873) #2753

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 0 additions & 19 deletions src/resolvers/Mutation/removeMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import {
MEMBER_NOT_FOUND_ERROR,
ORGANIZATION_NOT_FOUND_ERROR,
USER_NOT_FOUND_ERROR,
USER_REMOVING_SELF,
} from "../../constants";
import { errors, requestContext } from "../../libraries";
import type { InterfaceOrganization } from "../../models";
import { Organization, User } from "../../models";
import { cacheOrganizations } from "../../services/OrganizationCache/cacheOrganizations";
import { findOrganizationsInCache } from "../../services/OrganizationCache/findOrganizationsInCache";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { adminCheck } from "../../utilities";
/**
* This function enables to remove a member.
* @param _parent - parent of current request
Expand All @@ -29,7 +27,6 @@ import { adminCheck } from "../../utilities";
export const removeMember: MutationResolvers["removeMember"] = async (
_parent,
args,
context,
) => {
let organization: InterfaceOrganization;

Expand All @@ -55,13 +52,6 @@ export const removeMember: MutationResolvers["removeMember"] = async (
);
}

const currentUser = await User.findOne({
_id: context.userId,
});

// Checks whether current user making the request is an admin of organization.
await adminCheck(context.userId, organization);

const user = await User.findOne({
_id: args.data.userId,
}).lean();
Expand All @@ -87,15 +77,6 @@ export const removeMember: MutationResolvers["removeMember"] = async (
);
}

// Check if the current user is removing self
if (user._id.equals(currentUser?._id)) {
throw new errors.ConflictError(
requestContext.translate(USER_REMOVING_SELF.MESSAGE),
USER_REMOVING_SELF.CODE,
USER_REMOVING_SELF.PARAM,
);
}

const userIsOrganizationAdmin = organization?.admins.some((admin) =>
new mongoose.Types.ObjectId(admin.toString()).equals(user._id),
);
Expand Down
32 changes: 1 addition & 31 deletions tests/resolvers/Mutation/removeMember.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
ADMIN_REMOVING_CREATOR,
MEMBER_NOT_FOUND_ERROR,
ORGANIZATION_NOT_FOUND_ERROR,
USER_NOT_AUTHORIZED_ADMIN,
USER_NOT_FOUND_ERROR,
USER_REMOVING_SELF,
} from "../../../src/constants";
Expand Down Expand Up @@ -142,37 +141,8 @@
await removeMemberResolverOrgNotFoundError?.({}, args, context);
} catch (error: unknown) {
expect(spy).toHaveBeenCalledWith(ORGANIZATION_NOT_FOUND_ERROR.MESSAGE);
}
});

it(`throws UnauthorizedError if current user with _id === context.userId is
not an admin of the organization with _id === args.data.organizationId`, async () => {
const { requestContext } = await import("../../../src/libraries");
const spy = vi
.spyOn(requestContext, "translate")
.mockImplementation((message) => `Translated ${message}`);

try {
const args: MutationRemoveMemberArgs = {
data: {
organizationId: testOrganization?.id,
userId: new Types.ObjectId().toString(),
},
};

const context = {
userId: testUsers[2]?.id,
};

const { removeMember: removeMemberResolverAdminError } = await import(
"../../../src/resolvers/Mutation/removeMember"
);

await removeMemberResolverAdminError?.({}, args, context);
} catch (error: unknown) {
expect(spy).toHaveBeenCalledWith(USER_NOT_AUTHORIZED_ADMIN.MESSAGE);
expect((error as Error).message).toEqual(
`Translated ${USER_NOT_AUTHORIZED_ADMIN.MESSAGE}`,
`Translated ${ORGANIZATION_NOT_FOUND_ERROR.MESSAGE}`,
);
}
});
Expand Down Expand Up @@ -258,7 +228,7 @@

await removeMemberResolverRemoveSelfError?.({}, args, context);
} catch (error: unknown) {
expect(spy).toHaveBeenCalledWith(USER_REMOVING_SELF.MESSAGE);

Check failure on line 231 in tests/resolvers/Mutation/removeMember.spec.ts

View workflow job for this annotation

GitHub Actions / Testing Application (22.x)

tests/resolvers/Mutation/removeMember.spec.ts > resolvers -> Mutation -> removeMember > should throw admin cannot remove self error when user with _id === args.data.userId === context.userId

AssertionError: expected "translate" to be called with arguments: [ Array(1) ] Received: 1st translate call: Array [ - "Error: Current user cannot remove self, instead you can use leave Org function", + "Error: Current admin cannot remove another admin", ] Number of calls: 1 ❯ tests/resolvers/Mutation/removeMember.spec.ts:231:19
expect((error as Error).message).toEqual(
`Translated ${USER_REMOVING_SELF.MESSAGE}`,
);
Expand Down
Loading