Skip to content

Fix/3728 remove non admin orga owner #53

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
75 changes: 46 additions & 29 deletions src/components/ManageOrgaSpaceModal/ManageOrgaSpaceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
setSpaceOwnersByUserId,
setSpaceOwnersByEmail,
setOrganizationRoleByEmail,
setSpaceRoleByEmail
setSpaceRoleByEmail,
} from '@services/index';
import {
Confidentiality,
Expand Down Expand Up @@ -245,10 +245,10 @@ function ManageOrgaSpaceModal({
const updateOrganizationOwnersMutation = useMutation(
(newOwners?: string[]) =>
setOrganizationOwnersByUserId(
newOwners || updatedOwners.map(owner => owner.id),
newOwners || updatedOwners.map((owner) => owner.id),
orgID as string
),
{
),
{
onSuccess: () => {
queryClient.invalidateQueries(['orgasWithSpaces']);
queryClient.invalidateQueries(['organization', orgID]);
Expand All @@ -263,10 +263,10 @@ function ManageOrgaSpaceModal({
const updateOrganizationOwnersByEmailMutation = useMutation(
(newOwners?: string[]) =>
setOrganizationOwnersByEmail(
newOwners || updatedOwners.map(owner => owner.email),
newOwners || updatedOwners.map((owner) => owner.email),
orgID as string
),
{
),
{
onSuccess: () => {
queryClient.invalidateQueries(['orgasWithSpaces']);
queryClient.invalidateQueries(['organization', orgID]);
Expand Down Expand Up @@ -333,7 +333,7 @@ function ManageOrgaSpaceModal({
newOwners?: string[];
}) =>
setSpaceOwnersByUserId(
newOwners || updatedOwners.map(owner => owner.id),
newOwners || updatedOwners.map((owner) => owner.id),
orgID as string,
currentSpaceID
),
Expand All @@ -343,18 +343,14 @@ function ManageOrgaSpaceModal({
queryClient.invalidateQueries(['spaces', `o_${orgID}`]);
queryClient.invalidateQueries(['space', spaceID, `o_${orgID}`]);
queryClient.invalidateQueries(['spaceOwners', spaceID, `o_${orgID}`]);
queryClient.invalidateQueries([
'spaceUsers',
spaceID,
`o_${orgID}`,
]);
queryClient.invalidateQueries(['spaceUsers', spaceID, `o_${orgID}`]);
},
onError: () => {
ErrorToast('ErrorToast.title-owners-edit');
},
}
);

const updateSpaceOwnersByEmailMutation = useMutation(
({
currentSpaceID,
Expand All @@ -364,7 +360,7 @@ function ManageOrgaSpaceModal({
newOwners?: string[];
}) =>
setSpaceOwnersByEmail(
newOwners || updatedOwners.map(owner => owner.email),
newOwners || updatedOwners.map((owner) => owner.email),
orgID as string,
currentSpaceID
),
Expand All @@ -374,11 +370,7 @@ function ManageOrgaSpaceModal({
queryClient.invalidateQueries(['spaces', `o_${orgID}`]);
queryClient.invalidateQueries(['space', spaceID, `o_${orgID}`]);
queryClient.invalidateQueries(['spaceOwners', spaceID, `o_${orgID}`]);
queryClient.invalidateQueries([
'spaceUsers',
spaceID,
`o_${orgID}`,
]);
queryClient.invalidateQueries(['spaceUsers', spaceID, `o_${orgID}`]);
},
onError: () => {
ErrorToast('ErrorToast.title-owners-edit');
Expand Down Expand Up @@ -418,17 +410,30 @@ function ManageOrgaSpaceModal({
if (updated) {
const removedUsers = updatedUsersFixed.filter(
(updatedUser: OrgaUser | SpaceUser) =>
updatedUser.permissions.length === 0
updatedUser.permissions.length === 0 ||
!JSON.stringify(updatedUser.permissions).includes('ADMIN')
);

if (removedUsers) {
let newOwners = updatedOwners.map(owner => owner.id).filter((id) => !id.includes('@'));
if (removedUsers.length > 0) {
let newOwners = updatedOwners
.map((owner) => owner.id)
.filter((id) => !id.includes('@'));
let numberOfOwners = newOwners.length - removedUsers.length;

removedUsers.forEach((removedUser: OrgaUser | SpaceUser) => {
if (updatedOwners.map(owner => owner.id).includes(removedUser.id)) {
if (
numberOfOwners > 0 &&
updatedOwners.map((owner) => owner.id).includes(removedUser.id)
) {
newOwners = newOwners.filter((id) => id !== removedUser.id);
}
});
if (haveOwnersChanged(updatedOwners.map(owner => owner.id), newOwners)) {
if (
haveOwnersChanged(
updatedOwners.map((owner) => owner.id),
newOwners
)
) {
if (modalType === 'editSpace') {
updateSpaceOwnersMutation.mutate({
newOwners,
Expand All @@ -453,17 +458,24 @@ function ManageOrgaSpaceModal({
if (!response.ok) throw new Error(response.data.errorCode);
if (isOwner) {
if (haveUsersChanged(initialUsers, updatedUsers as SpaceUser[])) {
removeOwnerWithUser(updatedUsers as SpaceUser[]);
updateOrganizationUsersMutation.mutate();
removeOwnerWithUser(updatedUsers as SpaceUser[]);
}
}
return response;
})
.then((response) => {
if (!response.ok) throw new Error();
if (isOwner) {
if (haveOwnersChanged(initialOwners, updatedOwners.map(owner => owner.id))) {
updateOrganizationOwnersByEmailMutation.mutate(updatedOwners.map(owner => owner.email));
if (
haveOwnersChanged(
initialOwners,
updatedOwners.map((owner) => owner.id)
)
) {
updateOrganizationOwnersByEmailMutation.mutate(
updatedOwners.map((owner) => owner.email)
);
}
}
return response;
Expand Down Expand Up @@ -534,7 +546,12 @@ function ManageOrgaSpaceModal({
.then((response) => {
if (!response.ok) throw new Error();
if (isOwner) {
if (haveOwnersChanged(initialOwners, updatedOwners.map(owner => owner.id))) {
if (
haveOwnersChanged(
initialOwners,
updatedOwners.map((owner) => owner.id)
)
) {
updateSpaceOwnersByEmailMutation.mutate({
currentSpaceID: spaceID as string,
});
Expand Down