Skip to content

Commit

Permalink
Fixed: issue-#1873
Browse files Browse the repository at this point in the history
  • Loading branch information
raggettii committed Dec 9, 2024
1 parent 740ca31 commit 56e8b5b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 50 deletions.
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 @@ import {
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 @@ describe("resolvers -> Mutation -> removeMember", () => {
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

0 comments on commit 56e8b5b

Please sign in to comment.