remove unused inputfilter; add logging; fix schools schema

parent 75f92e2c
<?php <?php
use GrEduLabs\Schools\InputFilter\Lab;
use GrEduLabs\Schools\InputFilter\School as SchoolInputFilter; use GrEduLabs\Schools\InputFilter\School as SchoolInputFilter;
use GrEduLabs\Schools\Service\AssetServiceInterface; use GrEduLabs\Schools\Service\AssetServiceInterface;
use GrEduLabs\Schools\Service\LabServiceInterface; use GrEduLabs\Schools\Service\LabServiceInterface;
...@@ -58,7 +57,6 @@ return function (App $app) { ...@@ -58,7 +57,6 @@ return function (App $app) {
$c->get('SchInventory\\Service'), $c->get('SchInventory\\Service'),
$c->get(SchoolServiceInterface::class), $c->get(SchoolServiceInterface::class),
$c->get('authentication_service'), $c->get('authentication_service'),
$c->get(Lab::class),
$c->get('logger') $c->get('logger')
); );
}; };
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace SchSync\Middleware; namespace SchSync\Middleware;
use Exception;
use GrEduLabs\Schools\Service\AssetServiceInterface; use GrEduLabs\Schools\Service\AssetServiceInterface;
use GrEduLabs\Schools\Service\LabServiceInterface; use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\SchoolServiceInterface; use GrEduLabs\Schools\Service\SchoolServiceInterface;
...@@ -52,12 +53,6 @@ class CreateLabs ...@@ -52,12 +53,6 @@ class CreateLabs
*/ */
protected $authService; protected $authService;
/**
*
* @var callable
*/
protected $inputFilter;
/** /**
* @var LoggerInterface * @var LoggerInterface
*/ */
...@@ -69,7 +64,6 @@ class CreateLabs ...@@ -69,7 +64,6 @@ class CreateLabs
InventoryService $inventoryService, InventoryService $inventoryService,
SchoolServiceInterface $schoolService, SchoolServiceInterface $schoolService,
AuthenticationServiceInterface $authService, AuthenticationServiceInterface $authService,
callable $inputFilter,
LoggerInterface $logger LoggerInterface $logger
) { ) {
$this->labService = $labService; $this->labService = $labService;
...@@ -77,7 +71,6 @@ class CreateLabs ...@@ -77,7 +71,6 @@ class CreateLabs
$this->inventoryService = $inventoryService; $this->inventoryService = $inventoryService;
$this->schoolService = $schoolService; $this->schoolService = $schoolService;
$this->authService = $authService; $this->authService = $authService;
$this->inputFilter = $inputFilter;
$this->logger = $logger; $this->logger = $logger;
} }
...@@ -100,8 +93,14 @@ class CreateLabs ...@@ -100,8 +93,14 @@ class CreateLabs
if (0 < count($this->labService->getLabsBySchoolId($school_id))) { if (0 < count($this->labService->getLabsBySchoolId($school_id))) {
return $res; return $res;
} }
try {
$equipment = $this->inventoryService->getUnitEquipment($school['registry_no']);
} catch (Exception $e) {
$this->logger->error(sprintf('Problem retrieving assets from inventory for school %s', $school_id));
$this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);
$equipment = $this->inventoryService->getUnitEquipment($school['registry_no']); return $res;
}
$labTypes = array_reduce($this->labService->getLabTypes(), function ($map, $type) { $labTypes = array_reduce($this->labService->getLabTypes(), function ($map, $type) {
$map[trim($type['name'])] = $type['id']; $map[trim($type['name'])] = $type['id'];
...@@ -113,46 +112,51 @@ class CreateLabs ...@@ -113,46 +112,51 @@ class CreateLabs
return $map; return $map;
}, []); }, []);
try {
$locations = array_reduce($equipment, function ($uniq, $item) use ($school_id, $labTypes, $assetTypes) {
if (!isset($uniq[$item['location.id']])) {
$locationName = $item['location.name'];
$detected = reset(array_filter(array_keys($labTypes), function ($type) use ($locationName) {
return false !== stripos($locationName, $type) ||
false !== stripos($type, $locationName);
}));
$labType = $detected ? $labTypes[$detected] : end($labTypes);
$data = [
'school_id' => (int) $school_id,
'name' => $locationName,
'labtype_id' => (int) $labType,
];
$lab = R::dispense('lab');
$lab->import($data);
$uniq[$item['location.id']] = $lab;
}
$locations = array_reduce($equipment, function ($uniq, $item) use ($school_id, $labTypes, $assetTypes) { $categoryName = $item['item_template.category.name'];
if (!isset($uniq[$item['location.id']])) {
$locationName = $item['location.name'];
$detected = reset(array_filter(array_keys($labTypes), function ($type) use ($locationName) {
return false !== stripos($locationName, $type) || $type = reset(array_filter(array_keys($assetTypes), function ($type) use ($categoryName) {
false !== stripos($type, $locationName); return $type == $categoryName;
})); }));
$labType = $detected ? $labTypes[$detected] : end($labTypes); $type = ($type) ? $assetTypes[$type] : false;
$data = [
'school_id' => (int) $school_id, if ($type !== false) {
'name' => $locationName, if (!isset($uniq[$item['location.id']]->ownSchoolAsset[$type])) {
'labtype_id' => (int) $labType, $asset = R::dispense('schoolasset');
]; $asset->school_id = (int) $school_id;
$lab = R::dispense('lab'); $asset->itemcategory_id = (int) $type;
$lab->import($data); $uniq[$item['location.id']]->ownSchoolAsset[$type] = $asset;
$uniq[$item['location.id']] = $lab; }
} $uniq[$item['location.id']]->ownSchoolAsset[$type]->qty += 1;
$categoryName = $item['item_template.category.name'];
$type = reset(array_filter(array_keys($assetTypes), function ($type) use ($categoryName) {
return $type == $categoryName;
}));
$type = ($type) ? $assetTypes[$type] : false;
if ($type !== false) {
if (!$uniq[$item['location.id']]->ownSchoolAsset[$type]) {
$asset = R::dispense('schoolasset');
$asset->school_id = (int) $school_id;
$asset->itemcategory_id = (int) $type;
$uniq[$item['location.id']]->ownSchoolAsset[$type] = $asset;
} }
$uniq[$item['location.id']]->ownSchoolAsset[$type]->qty += 1;
}
return $uniq; return $uniq;
}, []); }, []);
R::storeAll($locations); R::storeAll($locations);
$this->logger->info(sprintf('Add assets from inventory for school %s', $school_id));
} catch (Exception $e) {
$this->logger->error(sprintf('Problem inserting assets for school %s in database', $school_id));
$this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);
}
return $res; return $res;
} }
......
...@@ -266,7 +266,7 @@ CREATE TABLE `lab` ( ...@@ -266,7 +266,7 @@ CREATE TABLE `lab` (
KEY `index_foreignkey_lab_labtype` (`labtype_id`), KEY `index_foreignkey_lab_labtype` (`labtype_id`),
KEY `index_foreignkey_lab_responsible` (`responsible_id`), KEY `index_foreignkey_lab_responsible` (`responsible_id`),
CONSTRAINT `c_fk_lab_labtype_id` FOREIGN KEY (`labtype_id`) REFERENCES `labtype` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT `c_fk_lab_labtype_id` FOREIGN KEY (`labtype_id`) REFERENCES `labtype` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `c_fk_lab_responsible_id` FOREIGN KEY (`responsible_id`) REFERENCES `teacher` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT `c_fk_lab_responsible_id` FOREIGN KEY (`responsible_id`) REFERENCES `teacher` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `c_fk_lab_school_id` FOREIGN KEY (`school_id`) REFERENCES `school` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE CONSTRAINT `c_fk_lab_school_id` FOREIGN KEY (`school_id`) REFERENCES `school` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
...@@ -358,7 +358,10 @@ CREATE TABLE `schoolasset` ( ...@@ -358,7 +358,10 @@ CREATE TABLE `schoolasset` (
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `index_foreignkey_schoolasset_itemcategory` (`itemcategory_id`), KEY `index_foreignkey_schoolasset_itemcategory` (`itemcategory_id`),
KEY `index_foreignkey_schoolasset_school` (`school_id`), KEY `index_foreignkey_schoolasset_school` (`school_id`),
KEY `index_foreignkey_schoolasset_lab` (`lab_id`) KEY `index_foreignkey_schoolasset_lab` (`lab_id`),
CONSTRAINT `c_fk_schoolasset_itemcategory_id` FOREIGN KEY (`itemcategory_id`) REFERENCES `itemcategory` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `c_fk_schoolasset_school_id` FOREIGN KEY (`school_id`) REFERENCES `school` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `c_fk_schoolasset_lab_id` FOREIGN KEY (`lab_id`) REFERENCES `lab` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment