diff --git a/src/firebase/api/authentication.js b/src/firebase/api/authentication.js index 1c2a87c..7309f76 100644 --- a/src/firebase/api/authentication.js +++ b/src/firebase/api/authentication.js @@ -94,6 +94,11 @@ export const uploadProfilePicture = (picture, email) => { const saveUser = (id, profile) => { return db.collection('/users').doc(id).set(profile); }; + +export const deleteUser = (id) => { + return db.collection('/users').doc(id).delete(); +}; + export const signOut = () => auth.signOut(); export const sendResetPasswordEmail = ({ email }) => { diff --git a/src/hooks/useTenders.js b/src/hooks/useTenders.js index b19f23d..ccff538 100644 --- a/src/hooks/useTenders.js +++ b/src/hooks/useTenders.js @@ -3,6 +3,7 @@ import { useEffect, useState } from 'react'; import { deleteInvite, + deleteUser, inviteUser, streamInvitedUsers, streamUsers, @@ -56,6 +57,10 @@ const useTenders = () => { return deleteInvite(row); }; + const removeUser = (id) => { + return deleteUser(id); + }; + const updateTender = (id, field, value) => { return updateUser({ id: id, field: field, value: value }); }; @@ -66,6 +71,7 @@ const useTenders = () => { addInvite, removeInvite, updateTender, + removeUser, }; }; diff --git a/src/pages/Admin/UserManagement/index.js b/src/pages/Admin/UserManagement/index.js index a5eed46..7d8b4bd 100644 --- a/src/pages/Admin/UserManagement/index.js +++ b/src/pages/Admin/UserManagement/index.js @@ -31,8 +31,14 @@ const StyledButton = styled(Button)` margin-bottom: ${({ theme }) => theme.baseUnit}px; `; const UserManagement = () => { - const { tenderState, invitedTenders, updateTender, addInvite, removeInvite } = - useContext(TendersContext); + const { + tenderState, + invitedTenders, + updateTender, + addInvite, + removeInvite, + removeUser, + } = useContext(TendersContext); const { studylines } = useContext(AuthContext); const [isProfileVisible, setIsProfileVisible] = useState(false); const [searchParam, setSearchParam] = useState(''); @@ -70,6 +76,17 @@ const UserManagement = () => { ); }; + const onRemoveAllClosedAccounts = () => { + const closedAccounts = tenderState.tenders.filter( + (t) => t.roles.length < 1 + ); + closedAccounts.forEach((t) => { + removeUser(t.id) + .then(() => message.success(`${t.displayName} has been removed`)) + .catch((error) => message.error('An error ocurred: ' + error.message)); + }); + }; + const onInviteDelete = (row) => { removeInvite(row) .then(() => message.success('Invite removed')) @@ -92,6 +109,18 @@ const UserManagement = () => { + + window.confirm( + 'Are you sure you want to delete all closed accounts' + ) + ? onRemoveAllClosedAccounts() + : console.log('nothing deleted') + } + > + Delete all closed accounts +