Merged in bugfix/PMCORE-3237 (pull request #8059)

PMCORE-3237 Service - Validation import custom case list

Approved-by: Julio Cesar Laura Avendaño
This commit is contained in:
Roly Gutierrez
2021-08-23 15:47:03 +00:00
committed by Julio Cesar Laura Avendaño
4 changed files with 93 additions and 8 deletions

View File

@@ -281,15 +281,17 @@ class CaseListTest extends TestCase
*/ */
public function it_should_test_import() public function it_should_test_import()
{ {
$additionalTables = factory(AdditionalTables::class)->create();
$data = [ $data = [
'type' => 'inbox', 'type' => 'inbox',
'name' => 'test1', 'name' => 'test1',
'description' => 'my description', 'description' => 'my description',
'tableUid' => '', 'tableUid' => $additionalTables->ADD_TAB_UID,
'columns' => [], 'columns' => [],
'iconList' => 'deafult.png', 'iconList' => 'deafult.png',
'iconColor' => 'red', 'iconColor' => 'red',
'iconColorScreen' => 'blue' 'iconColorScreen' => 'blue',
'tableName' => $additionalTables->ADD_TAB_NAME
]; ];
$json = json_encode($data); $json = json_encode($data);
$tempFile = sys_get_temp_dir() . '/test_' . random_int(10000, 99999); $tempFile = sys_get_temp_dir() . '/test_' . random_int(10000, 99999);
@@ -300,7 +302,10 @@ class CaseListTest extends TestCase
'error' => 0 'error' => 0
] ]
]; ];
$request_data = []; $request_data = [
'invalidFields' => 'continue',
'duplicateName' => 'continue'
];
$ownerId = 1; $ownerId = 1;
$result = CaseList::import($request_data, $ownerId); $result = CaseList::import($request_data, $ownerId);

View File

@@ -3527,6 +3527,12 @@ msgstr "No actions available for this case."
msgid "Case Demo" msgid "Case Demo"
msgstr "Case Demo" msgstr "Case Demo"
# TRANSLATION
# LABEL/ID_CASELIST_CAN_NOT_BE_IMPORTED_THE_PMTABLE_NOT_EXIST
#: LABEL/ID_CASELIST_CAN_NOT_BE_IMPORTED_THE_PMTABLE_NOT_EXIST
msgid "Custom Case List {0} can not be imported because the PM Table does not exist in this Workspace."
msgstr "Custom Case List {0} can not be imported because the PM Table does not exist in this Workspace."
# TRANSLATION # TRANSLATION
# LABEL/ID_CASE_DELETE_SUCCESFULLY # LABEL/ID_CASE_DELETE_SUCCESFULLY
#: LABEL/ID_CASE_DELETE_SUCCESFULLY #: LABEL/ID_CASE_DELETE_SUCCESFULLY
@@ -10061,6 +10067,12 @@ msgstr "Group already exists. You need set an action to continue. Available acti
msgid "Project already exists. Set an action to continue. Available actions: [{0}]." msgid "Project already exists. Set an action to continue. Available actions: [{0}]."
msgstr "Project already exists. Set an action to continue. Available actions: [{0}]." msgstr "Project already exists. Set an action to continue. Available actions: [{0}]."
# TRANSLATION
# LABEL/ID_IMPORTING_CASELIST_WITH_THE_SAME_NAME_SELECT_OPTION
#: LABEL/ID_IMPORTING_CASELIST_WITH_THE_SAME_NAME_SELECT_OPTION
msgid "You are about importing a Case List with the same name {0}, please select an option."
msgstr "You are about importing a Case List with the same name {0}, please select an option."
# TRANSLATION # TRANSLATION
# LABEL/ID_IMPORTING_ERROR # LABEL/ID_IMPORTING_ERROR
#: LABEL/ID_IMPORTING_ERROR #: LABEL/ID_IMPORTING_ERROR
@@ -21953,6 +21965,12 @@ msgstr "The PM Table associated with this DynaForm could not be found."
msgid "The data from this case was saved in the database, but it was not saved in the PM Table, which it couldn't be found." msgid "The data from this case was saved in the database, but it was not saved in the PM Table, which it couldn't be found."
msgstr "The data from this case was saved in the database, but it was not saved in the PM Table, which it couldn't be found." msgstr "The data from this case was saved in the database, but it was not saved in the PM Table, which it couldn't be found."
# TRANSLATION
# LABEL/ID_PMTABLE_NOT_HAVE_ALL_CASELIST_FIELDS_WOULD_YOU_LIKE_CONTINUE
#: LABEL/ID_PMTABLE_NOT_HAVE_ALL_CASELIST_FIELDS_WOULD_YOU_LIKE_CONTINUE
msgid "The PM Table {0} does not have all the Custom Case List fields for file {1}. Would you like continue?"
msgstr "The PM Table {0} does not have all the Custom Case List fields for file {1}. Would you like continue?"
# TRANSLATION # TRANSLATION
# LABEL/ID_PMTABLE_NOT_IMPORT_HAS_ERRORS # LABEL/ID_PMTABLE_NOT_IMPORT_HAS_ERRORS
#: LABEL/ID_PMTABLE_NOT_IMPORT_HAS_ERRORS #: LABEL/ID_PMTABLE_NOT_IMPORT_HAS_ERRORS

View File

@@ -57394,6 +57394,7 @@ INSERT INTO TRANSLATION (TRN_CATEGORY,TRN_ID,TRN_LANG,TRN_VALUE,TRN_UPDATE_DATE
( 'LABEL','ID_CASE','en','Case','2014-01-15') , ( 'LABEL','ID_CASE','en','Case','2014-01-15') ,
( 'LABEL','ID_CASECANCEL','en','No actions available for this case.','2014-01-15') , ( 'LABEL','ID_CASECANCEL','en','No actions available for this case.','2014-01-15') ,
( 'LABEL','ID_CASEDEMO','en','Case Demo','2014-01-15') , ( 'LABEL','ID_CASEDEMO','en','Case Demo','2014-01-15') ,
( 'LABEL','ID_CASELIST_CAN_NOT_BE_IMPORTED_THE_PMTABLE_NOT_EXIST','en','Custom Case List {0} can not be imported because the PM Table does not exist in this Workspace.','2021-08-20') ,
( 'LABEL','ID_CASE_DELETE_SUCCESFULLY','en','The Case was deleted successfully.','2020-01-08') , ( 'LABEL','ID_CASE_DELETE_SUCCESFULLY','en','The Case was deleted successfully.','2020-01-08') ,
( 'LABEL','ID_CASE_UID','en','Case Uid','2021-04-04') , ( 'LABEL','ID_CASE_UID','en','Case Uid','2021-04-04') ,
( 'LABEL','ID_CASES','en','HOME','2014-01-15') , ( 'LABEL','ID_CASES','en','HOME','2014-01-15') ,
@@ -58526,6 +58527,7 @@ INSERT INTO TRANSLATION (TRN_CATEGORY,TRN_ID,TRN_LANG,TRN_VALUE,TRN_UPDATE_DATE
( 'LABEL','ID_IMPORTER_FILE_PROCESSMAKER_PROJECT_VERSION_IS_MISSING','en','ProcessMaker Project version is missing on file source.','2014-05-20') , ( 'LABEL','ID_IMPORTER_FILE_PROCESSMAKER_PROJECT_VERSION_IS_MISSING','en','ProcessMaker Project version is missing on file source.','2014-05-20') ,
( 'LABEL','ID_IMPORTER_GROUP_ALREADY_EXISTS_SET_ACTION_TO_CONTINUE','en','Group already exists. You need set an action to continue. Available actions: [{0}].','2014-10-21') , ( 'LABEL','ID_IMPORTER_GROUP_ALREADY_EXISTS_SET_ACTION_TO_CONTINUE','en','Group already exists. You need set an action to continue. Available actions: [{0}].','2014-10-21') ,
( 'LABEL','ID_IMPORTER_PROJECT_ALREADY_EXISTS_SET_ACTION_TO_CONTINUE','en','Project already exists. Set an action to continue. Available actions: [{0}].','2014-10-21') , ( 'LABEL','ID_IMPORTER_PROJECT_ALREADY_EXISTS_SET_ACTION_TO_CONTINUE','en','Project already exists. Set an action to continue. Available actions: [{0}].','2014-10-21') ,
( 'LABEL','ID_IMPORTING_CASELIST_WITH_THE_SAME_NAME_SELECT_OPTION','en','You are about importing a Case List with the same name {0}, please select an option.','2021-08-20') ,
( 'LABEL','ID_IMPORTING_ERROR','en','Importing Error','2014-01-15') , ( 'LABEL','ID_IMPORTING_ERROR','en','Importing Error','2014-01-15') ,
( 'LABEL','ID_IMPORTING_USERS','en','Importing Users...','2015-09-15') , ( 'LABEL','ID_IMPORTING_USERS','en','Importing Users...','2015-09-15') ,
( 'LABEL','ID_IMPORT_ALREADY_EXISTS','en','The process you are trying to import already exists. Please select one of the following options to continue:','2014-10-21') , ( 'LABEL','ID_IMPORT_ALREADY_EXISTS','en','The process you are trying to import already exists. Please select one of the following options to continue:','2014-10-21') ,
@@ -60557,6 +60559,7 @@ INSERT INTO TRANSLATION (TRN_CATEGORY,TRN_ID,TRN_LANG,TRN_VALUE,TRN_UPDATE_DATE
( 'LABEL','ID_PMTABLE_INVALID_NAME','en','Could not create the table with the name "{0}" because it is a reserved word.','2014-01-15') , ( 'LABEL','ID_PMTABLE_INVALID_NAME','en','Could not create the table with the name "{0}" because it is a reserved word.','2014-01-15') ,
( 'LABEL','ID_PMTABLE_NOT_FOUND','en','The PM Table associated with this DynaForm could not be found.','2014-11-12') , ( 'LABEL','ID_PMTABLE_NOT_FOUND','en','The PM Table associated with this DynaForm could not be found.','2014-11-12') ,
( 'LABEL','ID_PMTABLE_NOT_FOUNDED_SAVED_DATA','en','The data from this case was saved in the database, but it was not saved in the PM Table, which it couldn''t be found.','2014-11-12') , ( 'LABEL','ID_PMTABLE_NOT_FOUNDED_SAVED_DATA','en','The data from this case was saved in the database, but it was not saved in the PM Table, which it couldn''t be found.','2014-11-12') ,
( 'LABEL','ID_PMTABLE_NOT_HAVE_ALL_CASELIST_FIELDS_WOULD_YOU_LIKE_CONTINUE','en','The PM Table {0} does not have all the Custom Case List fields for file {1}. Would you like continue?','2014-11-12') ,
( 'LABEL','ID_PMTABLE_NOT_IMPORT_HAS_ERRORS','en','File "{0}" was not imported has errors:','2016-02-18') , ( 'LABEL','ID_PMTABLE_NOT_IMPORT_HAS_ERRORS','en','File "{0}" was not imported has errors:','2016-02-18') ,
( 'LABEL','ID_PMTABLE_PRIMARY_KEY_FIELD_IS_MISSING_IN_ATTRIBUTE','en','The primary key field {0} of the PM Table is missing in the attribute {1}.','2014-05-20') , ( 'LABEL','ID_PMTABLE_PRIMARY_KEY_FIELD_IS_MISSING_IN_ATTRIBUTE','en','The primary key field {0} of the PM Table is missing in the attribute {1}.','2014-05-20') ,
( 'LABEL','ID_PMTABLE_REQUIRED','en','It is required to select a PM table','2014-10-27') , ( 'LABEL','ID_PMTABLE_REQUIRED','en','It is required to select a PM table','2014-10-27') ,

View File

@@ -7,6 +7,7 @@ use G;
use ProcessMaker\BusinessModel\Table; use ProcessMaker\BusinessModel\Table;
use ProcessMaker\Core\System; use ProcessMaker\Core\System;
use ProcessMaker\Model\AdditionalTables; use ProcessMaker\Model\AdditionalTables;
use ProcessMaker\Model\Fields;
use ProcessMaker\Model\User; use ProcessMaker\Model\User;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@@ -246,7 +247,8 @@ class CaseList extends Model
->leftJoin('USERS', 'USERS.USR_ID', '=', 'CASE_LIST.USR_ID') ->leftJoin('USERS', 'USERS.USR_ID', '=', 'CASE_LIST.USR_ID')
->leftJoin('ADDITIONAL_TABLES', 'ADDITIONAL_TABLES.ADD_TAB_UID', '=', 'CASE_LIST.ADD_TAB_UID') ->leftJoin('ADDITIONAL_TABLES', 'ADDITIONAL_TABLES.ADD_TAB_UID', '=', 'CASE_LIST.ADD_TAB_UID')
->select([ ->select([
'CASE_LIST.*' 'CASE_LIST.*',
'ADDITIONAL_TABLES.ADD_TAB_NAME'
]) ])
->get() ->get()
->first(); ->first();
@@ -256,6 +258,7 @@ class CaseList extends Model
$result = CaseList::getAliasFromColumnName($model->toArray()); $result = CaseList::getAliasFromColumnName($model->toArray());
$result['columns'] = json_decode($result['columns']); $result['columns'] = json_decode($result['columns']);
$result['tableName'] = $model->ADD_TAB_NAME;
//clean invalid items //clean invalid items
unset($result['id']); unset($result['id']);
@@ -275,21 +278,77 @@ class CaseList extends Model
/** /**
* The import requires a $ _FILES content in json format to create a record. * The import requires a $ _FILES content in json format to create a record.
* @param array $request_data * @param array $requestData
* @param int $ownerId * @param int $ownerId
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public static function import(array $request_data, int $ownerId) public static function import(array $requestData, int $ownerId)
{ {
if ($_FILES['file_content']['error'] !== UPLOAD_ERR_OK || if ($_FILES['file_content']['error'] !== UPLOAD_ERR_OK ||
$_FILES['file_content']['tmp_name'] === '') { $_FILES['file_content']['tmp_name'] === '') {
throw new Exception(G::LoadTranslation('ID_ERROR_UPLOADING_FILENAME')); throw new Exception(G::LoadTranslation('ID_ERROR_UPLOAD_FILE_CONTACT_ADMINISTRATOR'));
} }
$content = file_get_contents($_FILES['file_content']['tmp_name']); $content = file_get_contents($_FILES['file_content']['tmp_name']);
try { try {
$array = json_decode($content, true); $array = json_decode($content, true);
$caseList = CaseList::createSetting($array, $ownerId);
$tableName = $array['tableName'];
unset($array['tableName']);
//the pmtable not exist
$table = AdditionalTables::where('ADD_TAB_NAME', '=', $tableName)
->get()
->first();
if ($table === null) {
return [
'status' => 'tableNotExist',
'message' => G::LoadTranslation('ID_CASELIST_CAN_NOT_BE_IMPORTED_THE_PMTABLE_NOT_EXIST', [$_FILES['file_content']['name']])
];
}
$array['tableUid'] = $table->ADD_TAB_UID;
//the fields have differences between the import file and the current table
$requestData['invalidFields'] = $requestData['invalidFields'] ?? '';
if ($requestData['invalidFields'] !== 'continue') {
$fields = Fields::where('ADD_TAB_UID', '=', $array['tableUid'])
->whereNotIn('FLD_NAME', self::$excludeColumns)
->select('FLD_NAME')
->get()
->transform(function ($object) {
return $object->FLD_NAME;
})
->toArray();
foreach ($array['columns'] as $value) {
if (!in_array($value['field'], $fields)) {
return [
'status' => 'invalidFields',
'message' => G::LoadTranslation('ID_PMTABLE_NOT_HAVE_ALL_CASELIST_FIELDS_WOULD_YOU_LIKE_CONTINUE', [$tableName, $_FILES['file_content']['name']])
];
}
}
}
//the name of the case list already exist
$list = CaseList::where('CAL_NAME', '=', $array['name'])
->get()
->first();
$requestData['duplicateName'] = $requestData['duplicateName'] ?? '';
if ($requestData['duplicateName'] !== 'continue') {
if ($list !== null) {
return [
'status' => 'duplicateName',
'message' => G::LoadTranslation('ID_IMPORTING_CASELIST_WITH_THE_SAME_NAME_SELECT_OPTION', [$array['name']])
];
}
}
if ($requestData['duplicateName'] === 'continue' && $list !== null) {
$caseList = CaseList::updateSetting($list->CAL_ID, $array, $ownerId);
} else {
$caseList = CaseList::createSetting($array, $ownerId);
}
$result = CaseList::getAliasFromColumnName($caseList->toArray()); $result = CaseList::getAliasFromColumnName($caseList->toArray());
return $result; return $result;
} catch (Exception $e) { } catch (Exception $e) {