Commit 75f92e2c authored by Vassilis Kanellopoulos's avatar Vassilis Kanellopoulos
Browse files

sync with sch inventory on first login if no labs or assets for school

parent 6d83243b
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace SchInventory; namespace SchInventory;
use GuzzleHttp\ClientInterface; use GuzzleHttp\ClientInterface;
use Psr\Http\Message\UriInterface;
/** /**
* Inventory service implementation using GuzzleHttp library * Inventory service implementation using GuzzleHttp library
...@@ -43,16 +44,14 @@ class GuzzleHttpService implements ServiceInterface ...@@ -43,16 +44,14 @@ class GuzzleHttpService implements ServiceInterface
$responseData = json_decode($response->getBody()->getContents(), true); $responseData = json_decode($response->getBody()->getContents(), true);
return new EquipmentCollection( return isset($responseData['flat_results']) ? $responseData['flat_results'] : [];
array_map([$this, 'hydrateEquipment'], $responseData['flat_results'])
);
} }
/** /**
* Creates the uri with the unit query parameter * Creates the uri with the unit query parameter
* *
* @param mixed $unit * @param mixed $unit
* @return Psr\Http\Message\UriInterface * @return UriInterface
*/ */
private function createBaseUri($unit) private function createBaseUri($unit)
{ {
...@@ -61,16 +60,4 @@ class GuzzleHttpService implements ServiceInterface ...@@ -61,16 +60,4 @@ class GuzzleHttpService implements ServiceInterface
return $baseUri->withQueryValue($baseUri, 'unit', $unit); return $baseUri->withQueryValue($baseUri, 'unit', $unit);
} }
private function hydrateEquipment(array $data)
{
return new Equipment(
(isset($data['id']) ? $data['id'] : null),
(isset($data['item_template.category.name']) ? $data['item_template.category.name'] : null),
(isset($data['item_template.description']) ? $data['item_template.description'] : null),
(isset($data['location.name']) ? $data['location.name'] : null),
(isset($data['item_template.manufacturer.name']) ? $data['item_template.manufacturer.name'] : null),
(isset($data['property_number']) ? $data['property_number'] : null)
);
}
} }
<?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\LabServiceInterface;
use GrEduLabs\Schools\Service\SchoolServiceInterface; use GrEduLabs\Schools\Service\SchoolServiceInterface;
use SchMM\FetchUnit; use SchMM\FetchUnit;
use SchSync\Middleware\CreateLabs;
use SchSync\Middleware\CreateSchool; use SchSync\Middleware\CreateSchool;
use SchSync\Middleware\CreateUser; use SchSync\Middleware\CreateUser;
use Slim\App; use Slim\App;
...@@ -47,13 +51,25 @@ return function (App $app) { ...@@ -47,13 +51,25 @@ return function (App $app) {
$c->get('logger') $c->get('logger')
); );
}; };
$container[CreateLabs::class] = function ($c) {
return new CreateLabs(
$c->get(LabServiceInterface::class),
$c->get(AssetServiceInterface::class),
$c->get('SchInventory\\Service'),
$c->get(SchoolServiceInterface::class),
$c->get('authentication_service'),
$c->get(Lab::class),
$c->get('logger')
);
};
}); });
$events('on', 'app.bootstrap', function ($stop, $app, $container) { $events('on', 'app.bootstrap', function ($stop, $app, $container) {
foreach ($container->get('router')->getRoutes() as $route) { foreach ($container->get('router')->getRoutes() as $route) {
if ('user.login.sso' === $route->getName()) { if ('user.login.sso' === $route->getName()) {
$route->add(CreateUser::class) $route->add(CreateUser::class)
->add(CreateSchool::class); ->add(CreateSchool::class)
->add(CreateLabs::class);
break; break;
} }
} }
......
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace SchSync\Middleware;
use GrEduLabs\Schools\Service\AssetServiceInterface;
use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use Psr\Log\LoggerInterface;
use RedBeanPHP\R;
use SchInventory\ServiceInterface as InventoryService;
use Slim\Http\Request;
use Slim\Http\Response;
use Zend\Authentication\AuthenticationServiceInterface;
class CreateLabs
{
/**
*
* @var LabServiceInterface
*/
protected $labService;
/**
*
* @var AssetServiceInterface
*/
protected $assetService;
/**
*
* @var InventoryService
*/
protected $inventoryService;
/**
*
* @var SchoolServiceInterface
*/
protected $schoolService;
/**
*
* @var AuthenticationServiceInterface
*/
protected $authService;
/**
*
* @var callable
*/
protected $inputFilter;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(
LabServiceInterface $labService,
AssetServiceInterface $assetService,
InventoryService $inventoryService,
SchoolServiceInterface $schoolService,
AuthenticationServiceInterface $authService,
callable $inputFilter,
LoggerInterface $logger
) {
$this->labService = $labService;
$this->assetService = $assetService;
$this->inventoryService = $inventoryService;
$this->schoolService = $schoolService;
$this->authService = $authService;
$this->inputFilter = $inputFilter;
$this->logger = $logger;
}
public function __invoke(Request $req, Response $res, callable $next)
{
$res = $next($req, $res);
$identity = $this->authService->getIdentity();
if (null === $identity) {
return $res;
}
$user = R::load('user', $identity->id);
if (!$user->school_id) {
return $res;
}
$school_id = $user->school_id;
$school = $this->schoolService->getSchool($school_id);
if (0 < count($this->labService->getLabsBySchoolId($school_id))) {
return $res;
}
$equipment = $this->inventoryService->getUnitEquipment($school['registry_no']);
$labTypes = array_reduce($this->labService->getLabTypes(), function ($map, $type) {
$map[trim($type['name'])] = $type['id'];
return $map;
}, []);
$assetTypes = array_reduce($this->assetService->getAllItemCategories(), function ($map, $type) {
$map[trim($type['name'])] = $type['id'];
return $map;
}, []);
$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;
}
$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;
}, []);
R::storeAll($locations);
return $res;
}
}
...@@ -252,22 +252,22 @@ CREATE TABLE `lab` ( ...@@ -252,22 +252,22 @@ CREATE TABLE `lab` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`school_id` int(11) unsigned NOT NULL, `school_id` int(11) unsigned NOT NULL,
`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`labtype_id` int(11) unsigned DEFAULT NULL, `labtype_id` int(11) unsigned NOT NULL,
`responsible_id` int(11) unsigned DEFAULT NULL, `responsible_id` int(11) unsigned DEFAULT NULL,
`area` int(11) unsigned NOT NULL, `area` int(11) unsigned DEFAULT NULL,
`attachment` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `attachment` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`attachment_mime` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `attachment_mime` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`has_network` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL, `has_network` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`has_server` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL, `has_server` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`use_ext_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `use_ext_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`use_in_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `use_in_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `school_id` (`school_id`), KEY `index_foreignkey_lab_school` (`school_id`),
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 SET NULL ON UPDATE SET NULL, 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 SET NULL ON UPDATE SET NULL, CONSTRAINT `c_fk_lab_responsible_id` FOREIGN KEY (`responsible_id`) REFERENCES `teacher` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `lab_ibfk_1` FOREIGN KEY (`school_id`) REFERENCES `school` (`id`) 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 */;
...@@ -323,9 +323,9 @@ UNLOCK TABLES; ...@@ -323,9 +323,9 @@ UNLOCK TABLES;
-- Table structure for table `lab_lesson` -- Table structure for table `lab_lesson`
-- --
DROP TABLE IF EXISTS `lab_lesson`;
/*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */; /*!40101 SET character_set_client = utf8 */;
CREATE TABLE `lab_lesson` ( CREATE TABLE `lab_lesson` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`lesson_id` int(11) unsigned DEFAULT NULL, `lesson_id` int(11) unsigned DEFAULT NULL,
...@@ -336,7 +336,7 @@ CREATE TABLE `lab_lesson` ( ...@@ -336,7 +336,7 @@ CREATE TABLE `lab_lesson` (
KEY `index_foreignkey_lab_lesson_lab` (`lab_id`), KEY `index_foreignkey_lab_lesson_lab` (`lab_id`),
CONSTRAINT `c_fk_lab_lesson_lab_id` FOREIGN KEY (`lab_id`) REFERENCES `lab` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `c_fk_lab_lesson_lab_id` FOREIGN KEY (`lab_id`) REFERENCES `lab` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `c_fk_lab_lesson_lesson_id` FOREIGN KEY (`lesson_id`) REFERENCES `lesson` (`id`) ON DELETE CASCADE ON UPDATE CASCADE CONSTRAINT `c_fk_lab_lesson_lesson_id` FOREIGN KEY (`lesson_id`) REFERENCES `lesson` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=9 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