From 49997a3dccd2c4e2903c444ed64f086cc02e3f51 Mon Sep 17 00:00:00 2001 From: Roly Gutierrez Date: Fri, 23 Jul 2021 10:45:49 -0400 Subject: [PATCH] PMCORE-3114 Service - Import a custom case list Service from a JSON --- .../src/ProcessMaker/Model/CaseListTest.php | 92 +++++++++++++++++-- .../src/ProcessMaker/Model/CaseList.php | 72 ++++++++++++++- .../ProcessMaker/Services/Api/CaseList.php | 42 ++++++++- 3 files changed, 194 insertions(+), 12 deletions(-) diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/CaseListTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/CaseListTest.php index 7e7442058..5cc08b2f0 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/CaseListTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/CaseListTest.php @@ -120,7 +120,7 @@ class CaseListTest extends TestCase 'createDate' => date('Y-m-d H:m:s'), 'updateDate' => date('Y-m-d H:m:s') ]; - $caseList = CaseList::createSetting($data); + $caseList = CaseList::createSetting($data, $data['userId']); //asserts $this->assertEquals($data['type'], $caseList->CAL_TYPE); @@ -154,7 +154,7 @@ class CaseListTest extends TestCase 'createDate' => date('Y-m-d H:m:s'), 'updateDate' => date('Y-m-d H:m:s') ]; - $model = CaseList::createSetting($data); + $model = CaseList::createSetting($data, $data['userId']); $id = $model->CAL_ID; $data2 = [ @@ -162,7 +162,7 @@ class CaseListTest extends TestCase 'name' => 'new name', 'description' => 'new deescription', ]; - $caseList = CaseList::updateSetting($id, $data2); + $caseList = CaseList::updateSetting($id, $data2, $data['userId']); //asserts $this->assertEquals($data2['type'], $caseList->CAL_TYPE); @@ -196,7 +196,7 @@ class CaseListTest extends TestCase 'createDate' => date('Y-m-d H:m:s'), 'updateDate' => date('Y-m-d H:m:s') ]; - $model = CaseList::createSetting($data); + $model = CaseList::createSetting($data, $data['userId']); $id = $model->CAL_ID; $caseList = CaseList::deleteSetting($id); @@ -233,10 +233,10 @@ class CaseListTest extends TestCase 'iconColor' => 'red', 'iconColorScreen' => 'blue' ]; - $model1 = CaseList::createSetting($data); - $model2 = CaseList::createSetting($data); - $model3 = CaseList::createSetting($data); - $model4 = CaseList::createSetting($data); + $model1 = CaseList::createSetting($data, $data['userId']); + $model2 = CaseList::createSetting($data, $data['userId']); + $model3 = CaseList::createSetting($data, $data['userId']); + $model4 = CaseList::createSetting($data, $data['userId']); //assert total $result = CaseList::getSetting('inbox', '', 0, 10); @@ -272,4 +272,80 @@ class CaseListTest extends TestCase $this->assertEquals(0, $result['total']); $this->assertEquals(0, count($result['data'])); } + + /** + * This tests the import method. + * @test + * @covers \ProcessMaker\Model\CaseList::import() + */ + public function it_should_test_import() + { + $data = [ + 'type' => 'inbox', + 'name' => 'test1', + 'description' => 'my description', + 'tableUid' => '', + 'columns' => [], + 'iconList' => 'deafult.png', + 'iconColor' => 'red', + 'iconColorScreen' => 'blue' + ]; + $json = json_encode($data); + $tempFile = sys_get_temp_dir() . '/test_' . random_int(10000, 99999); + file_put_contents($tempFile, $json); + $_FILES = [ + 'file_content' => [ + 'tmp_name' => $tempFile, + 'error' => 0 + ] + ]; + $request_data = []; + $ownerId = 1; + $result = CaseList::import($request_data, $ownerId); + + //assert + $this->assertArrayHasKey('type', $result); + $this->assertArrayHasKey('name', $result); + $this->assertArrayHasKey('description', $result); + + $this->assertEquals($data['type'], $result['type']); + $this->assertEquals($data['name'], $result['name']); + $this->assertEquals($data['description'], $result['description']); + } + + /** + * This tests the export method. + * @test + * @covers \ProcessMaker\Model\CaseList::export() + */ + public function it_should_test_export() + { + CaseList::truncate(); + $data = [ + 'type' => 'inbox', + 'name' => 'test export', + 'description' => 'my description', + 'tableUid' => '', + 'columns' => [], + 'userId' => 1, + 'iconList' => 'deafult.png', + 'iconColor' => 'red', + 'iconColorScreen' => 'blue' + ]; + CaseList::createSetting($data, $data['userId']); + + $result = CaseList::export($data['userId']); + + //assert + $this->assertArrayHasKey('type', $result['data']); + $this->assertArrayHasKey('name', $result['data']); + $this->assertArrayHasKey('description', $result['data']); + + $this->assertEquals($data['type'], $result['data']['type']); + $this->assertEquals($data['name'], $result['data']['name']); + $this->assertEquals($data['description'], $result['data']['description']); + + //assert file export + $this->assertFileExists($result['filename']); + } } diff --git a/workflow/engine/src/ProcessMaker/Model/CaseList.php b/workflow/engine/src/ProcessMaker/Model/CaseList.php index a225079f9..443a61ecf 100644 --- a/workflow/engine/src/ProcessMaker/Model/CaseList.php +++ b/workflow/engine/src/ProcessMaker/Model/CaseList.php @@ -2,6 +2,8 @@ namespace ProcessMaker\Model; +use Exception; +use G; use ProcessMaker\Core\System; use ProcessMaker\Model\AdditionalTables; use ProcessMaker\Model\User; @@ -98,12 +100,14 @@ class CaseList extends Model /** * Create and save this model from array values. * @param array $values + * @param int $ownerId * @return object */ - public static function createSetting(array $values) + public static function createSetting(array $values, int $ownerId) { $attributes = CaseList::getColumnNameFromAlias($values); + $attributes['USR_ID'] = $ownerId; $attributes['CAL_CREATE_DATE'] = date("Y-m-d H:i:s"); $attributes['CAL_UPDATE_DATE'] = date("Y-m-d H:i:s"); if (empty($attributes['CAL_COLUMNS'])) { @@ -120,12 +124,14 @@ class CaseList extends Model * Update and save this model from array values. * @param int $id * @param array $values + * @param int $ownerId * @return object */ - public static function updateSetting(int $id, array $values) + public static function updateSetting(int $id, array $values, int $ownerId) { $attributes = CaseList::getColumnNameFromAlias($values); + $attributes['USR_ID'] = $ownerId; $attributes['CAL_UPDATE_DATE'] = date("Y-m-d H:i:s"); if (empty($attributes['CAL_COLUMNS'])) { $attributes['CAL_COLUMNS'] = []; @@ -212,4 +218,66 @@ class CaseList extends Model 'data' => $data ]; } + + /** + * The export creates a temporary file with record data in json format. + * @param int $id + * @throws Exception + */ + public static function export(int $id) + { + $model = CaseList::where('CAL_ID', '=', $id) + ->leftJoin('USERS', 'USERS.USR_ID', '=', 'CASE_LIST.USR_ID') + ->leftJoin('ADDITIONAL_TABLES', 'ADDITIONAL_TABLES.ADD_TAB_UID', '=', 'CASE_LIST.ADD_TAB_UID') + ->select([ + 'CASE_LIST.*' + ]) + ->get() + ->first(); + if (empty($model)) { + throw new Exception(G::LoadTranslation('ID_DOES_NOT_EXIST')); + } + + $result = CaseList::getAliasFromColumnName($model->toArray()); + $result['columns'] = json_decode($result['columns']); + + //clean invalid items + unset($result['id']); + unset($result['userId']); + unset($result['createDate']); + unset($result['updateDate']); + + //random name to distinguish the different sessions + $filename = sys_get_temp_dir() . "/pm" . random_int(10000, 99999); + file_put_contents($filename, json_encode($result)); + return [ + 'filename' => $filename, + 'downloadFilename' => $result['name'] . ' ' . date('Y-m-d H:i:s') . '.json', + 'data' => $result + ]; + } + + /** + * The import requires a $ _FILES content in json format to create a record. + * @param array $request_data + * @param int $ownerId + * @return array + * @throws Exception + */ + public static function import(array $request_data, int $ownerId) + { + if ($_FILES['file_content']['error'] !== UPLOAD_ERR_OK || + $_FILES['file_content']['tmp_name'] === '') { + throw new Exception(G::LoadTranslation('ID_ERROR_UPLOADING_FILENAME')); + } + $content = file_get_contents($_FILES['file_content']['tmp_name']); + try { + $array = json_decode($content, true); + $caseList = CaseList::createSetting($array, $ownerId); + $result = CaseList::getAliasFromColumnName($caseList->toArray()); + return $result; + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } } diff --git a/workflow/engine/src/ProcessMaker/Services/Api/CaseList.php b/workflow/engine/src/ProcessMaker/Services/Api/CaseList.php index 6f634a5f8..d6f18f2f7 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/CaseList.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/CaseList.php @@ -38,7 +38,8 @@ class CaseList extends Api */ public function doPost(array $request_data) { - $caseList = CaseListBusinessModel::createSetting($request_data); + $ownerId = $this->getUserId(); + $caseList = CaseListBusinessModel::createSetting($request_data, $ownerId); $caseList = CaseListBusinessModel::getAliasFromColumnName($caseList->toArray()); return $caseList; } @@ -55,7 +56,8 @@ class CaseList extends Api */ public function doPut(int $id, array $request_data) { - $caseList = CaseListBusinessModel::updateSetting($id, $request_data); + $ownerId = $this->getUserId(); + $caseList = CaseListBusinessModel::updateSetting($id, $request_data, $ownerId); if (is_null($caseList)) { throw new RestException(Api::STAT_APP_EXCEPTION, G::LoadTranslation('ID_DOES_NOT_EXIST')); } @@ -145,4 +147,40 @@ class CaseList extends Api { return CaseListBusinessModel::getSetting('unassigned', $search, $offset, $limit); } + + /** + * Get unassigned Case List settings. + * @url GET /:id/export + * @access protected + * @class AccessControl {@permission PM_CASES} + * @throws RestException + */ + public function doExport(int $id) + { + try { + $result = CaseListBusinessModel::export($id); + G::streamFile($result['filename'], true, $result['downloadFilename']); + } catch (Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } + + /** + * Get unassigned Case List settings. + * @url POST /import + * @param array $request_data + * @access protected + * @class AccessControl {@permission PM_CASES} + * @throws RestException + * @return array + */ + public function doImport(array $request_data) + { + try { + $ownerId = $this->getUserId(); + return CaseListBusinessModel::import($request_data, $ownerId); + } catch (Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } }