import { request } from "@esri/arcgis-rest-request" ; import { getPortalUrl } from "../util/get-portal-url" ; import { IGroupSharingOptions , ISharingResponse , getUserMembership } from "./helpers" ; import { isItemSharedWithGroup } from "./is-item-shared-with-group" ; import { getUser } from "../users/get-user" ; param requestOptions returns export function unshareItemWithGroup ( requestOptions ) { return isItemSharedWithGroup ( requestOptions ) . then ( isShared => { if ( ! isShared ) { return Promise . resolve ( { itemId : requestOptions . id , shortcut : true , notUnsharedFrom : [ ] } as ISharingResponse ) ; } const { authentication : { username } , owner } = requestOptions ; return Promise . all ( [ getUserMembership ( requestOptions ) , getUser ( { username , authentication : requestOptions . authentication } ) ] ) . then ( ( [ membership , currentUser ] ) => { const itemOwner = owner || username ; const isItemOwner = itemOwner === username ; const isAdmin = currentUser . role === 'org_admin' && ! currentUser . roleId ; if ( ! isItemOwner && ! isAdmin && [ 'admin' , 'owner' ] . indexOf ( membership ) < 0 ) { throw Error ( `This item can not be unshared from group ${ requestOptions . groupId } by ${ username } as they not the item owner, an org admin, group admin or group owner.` ) ; } return unshareFromGroup ( requestOptions ) ; } ) . then ( sharingResponse => { if ( sharingResponse . notUnsharedFrom . length ) { throw Error ( `Item ${ requestOptions . id } could not be unshared to group ${ requestOptions . groupId }` ) ; } else { return sharingResponse ; } } ) ; } ) ; } function unshareFromGroup ( requestOptions ) { const username = requestOptions . authentication . username ; const itemOwner = requestOptions . owner || username ; let url = `${ getPortalUrl ( requestOptions ) }/content/items/${ requestOptions . id }/unshare` ; if ( itemOwner === username ) { url = `${ getPortalUrl ( requestOptions ) }/content/users/${ itemOwner }/items/${ requestOptions . id }/unshare` ; } requestOptions . params = { groups : requestOptions . groupId } ; return request ( url , requestOptions ) ; }