diff --git a/vendor/magento/module-company/Controller/Team/Check.php b/vendor/magento/module-company/Controller/Team/Check.php new file mode 100644 index 000000000000..13b9894498d2 --- /dev/null +++ b/vendor/magento/module-company/Controller/Team/Check.php @@ -0,0 +1,131 @@ +structureManager->getAllowedIds($this->companyContext->getCustomerId()); + $teamId = $this->request->getParam('team_id'); + + if (!in_array($teamId, $allowedIds['teams'])) { + return $this->jsonError(__('You are not allowed to do this.')); + } + + try { + $structure = $this->structureManager->getStructureByTeamId($teamId); + if ($structure) { + $structureNode = $this->structureManager->getTreeById($structure->getId()); + $this->structureManager->addDataToTree($structureNode); + $result = $this->nodeHasActiveChildren($structureNode); + } + } catch (\Magento\Framework\Exception\LocalizedException $e) { + return $this->jsonError($e->getMessage()); + } catch (\Exception $e) { + $this->logger->critical($e); + return $this->jsonError(__('Something went wrong.')); + } + + return $this->jsonSuccess( + ['hasChildren' => $structureNode && $structureNode->hasChildren(), 'hasActiveChildren' => $result] + ); + } + + /** + * Return error json + * + * @param string|Phrase $message + * @return Json + */ + private function jsonError(string|Phrase $message): Json + { + return $this->resultFactory->create(ResultFactory::TYPE_JSON) + ->setData(['status' => 'error', 'message' => $message]); + } + + /** + * Return success json + * + * @param array $data + * @return Json + */ + private function jsonSuccess(array $data): Json + { + return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData(['status' => 'ok', 'data' => $data]); + } + + /** + * Check if structure node has active children + * + * @param Node $node + * @return bool + */ + private function nodeHasActiveChildren(Node $node): bool + { + $result = false; + if ($node->getChildren()) { + foreach ($node->getChildren() as $child) { + if ($child->getIsActive()) { + $result = true; + break; + } + } + } + return $result; + } +} diff --git a/vendor/magento/module-company/Model/Team/Delete.php b/vendor/magento/module-company/Model/Team/Delete.php index d2a52d1ab311..a57ac23dc107 100644 --- a/vendor/magento/module-company/Model/Team/Delete.php +++ b/vendor/magento/module-company/Model/Team/Delete.php @@ -55,13 +55,26 @@ public function delete(\Magento\Company\Api\Data\TeamInterface $team) $structure = $this->structureManager->getStructureByTeamId($team->getId()); if ($structure) { $structureNode = $this->structureManager->getTreeById($structure->getId()); - if ($structureNode && $structureNode->hasChildren()) { - throw new LocalizedException( - __( - 'This team has child users or teams aligned to it and cannot be deleted.' - . ' Please re-align the child users or teams first.' - ) - ); + if ($structureNode) { + $this->structureManager->addDataToTree($structureNode); + if ($structureNode->getChildren()) { + foreach ($structureNode->getChildren() as $child) { + if ($child->getIsActive()) { + throw new LocalizedException( + __( + 'This team has active users or teams assigned to it and cannot be deleted.' + . ' Please unassign the users or teams first.' + ) + ); + } + } + } + if ($structureNode->hasChildren()) { + $parentId = $structureNode->getParent()->getId(); + foreach ($structureNode->getChildren() as $child) { + $this->structureManager->moveNode($child->getId(), $parentId); + } + } } $this->structureRepository->deleteById($structure->getId()); } diff --git a/vendor/magento/module-company/i18n/en_US.csv b/vendor/magento/module-company/i18n/en_US.csv index ab176be7336f..16da93487886 100644 --- a/vendor/magento/module-company/i18n/en_US.csv +++ b/vendor/magento/module-company/i18n/en_US.csv @@ -117,7 +117,7 @@ Inactive,Inactive "Could not save company: %1","Could not save company: %1" "Cannot delete structure with id %1","Cannot delete structure with id %1" "Could not create team","Could not create team" -"This team has child users or teams aligned to it and cannot be deleted. Please re-align the child users or teams first.","This team has child users or teams aligned to it and cannot be deleted. Please re-align the child users or teams first." +"This team has active users or teams assigned to it and cannot be deleted. Please unassign the users or teams first.","This team has active users or teams assigned to it and cannot be deleted. Please unassign the users or teams first." "Could not update team","Could not update team" "Cannot delete team with id %1","Cannot delete team with id %1" "You cannot assign a different role to a company admin.","You cannot assign a different role to a company admin." diff --git a/vendor/magento/module-company/view/frontend/templates/company/management.phtml b/vendor/magento/module-company/view/frontend/templates/company/management.phtml index 7e3a4b1dfcd6..3403546793ea 100755 --- a/vendor/magento/module-company/view/frontend/templates/company/management.phtml +++ b/vendor/magento/module-company/view/frontend/templates/company/management.phtml @@ -4,41 +4,47 @@ * See COPYING.txt for license details. */ ?> - -hasCustomerCompany()) : ?> + +hasCustomerCompany()): ?>