PMCORE-3237 Service - Validation import custom case list
This commit is contained in:
@@ -281,15 +281,17 @@ class CaseListTest extends TestCase
|
||||
*/
|
||||
public function it_should_test_import()
|
||||
{
|
||||
$additionalTables = factory(AdditionalTables::class)->create();
|
||||
$data = [
|
||||
'type' => 'inbox',
|
||||
'name' => 'test1',
|
||||
'description' => 'my description',
|
||||
'tableUid' => '',
|
||||
'tableUid' => $additionalTables->ADD_TAB_UID,
|
||||
'columns' => [],
|
||||
'iconList' => 'deafult.png',
|
||||
'iconColor' => 'red',
|
||||
'iconColorScreen' => 'blue'
|
||||
'iconColorScreen' => 'blue',
|
||||
'tableName' => $additionalTables->ADD_TAB_NAME
|
||||
];
|
||||
$json = json_encode($data);
|
||||
$tempFile = sys_get_temp_dir() . '/test_' . random_int(10000, 99999);
|
||||
@@ -300,7 +302,10 @@ class CaseListTest extends TestCase
|
||||
'error' => 0
|
||||
]
|
||||
];
|
||||
$request_data = [];
|
||||
$request_data = [
|
||||
'invalidFields' => 'continue',
|
||||
'duplicateName' => 'continue'
|
||||
];
|
||||
$ownerId = 1;
|
||||
$result = CaseList::import($request_data, $ownerId);
|
||||
|
||||
|
||||
@@ -3515,6 +3515,12 @@ msgstr "No actions available for this case."
|
||||
msgid "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
|
||||
# LABEL/ID_CASE_DELETE_SUCCESFULLY
|
||||
#: LABEL/ID_CASE_DELETE_SUCCESFULLY
|
||||
@@ -10043,6 +10049,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}]."
|
||||
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
|
||||
# LABEL/ID_IMPORTING_ERROR
|
||||
#: LABEL/ID_IMPORTING_ERROR
|
||||
@@ -21929,6 +21941,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."
|
||||
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
|
||||
# LABEL/ID_PMTABLE_NOT_IMPORT_HAS_ERRORS
|
||||
#: LABEL/ID_PMTABLE_NOT_IMPORT_HAS_ERRORS
|
||||
|
||||
@@ -57392,6 +57392,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_CASECANCEL','en','No actions available for this case.','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_UID','en','Case Uid','2021-04-04') ,
|
||||
( 'LABEL','ID_CASES','en','HOME','2014-01-15') ,
|
||||
@@ -58523,6 +58524,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_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_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_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') ,
|
||||
@@ -60553,6 +60555,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_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_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_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') ,
|
||||
|
||||
@@ -7,6 +7,7 @@ use G;
|
||||
use ProcessMaker\BusinessModel\Table;
|
||||
use ProcessMaker\Core\System;
|
||||
use ProcessMaker\Model\AdditionalTables;
|
||||
use ProcessMaker\Model\Fields;
|
||||
use ProcessMaker\Model\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -246,7 +247,8 @@ class CaseList extends Model
|
||||
->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.*'
|
||||
'CASE_LIST.*',
|
||||
'ADDITIONAL_TABLES.ADD_TAB_NAME'
|
||||
])
|
||||
->get()
|
||||
->first();
|
||||
@@ -256,6 +258,7 @@ class CaseList extends Model
|
||||
|
||||
$result = CaseList::getAliasFromColumnName($model->toArray());
|
||||
$result['columns'] = json_decode($result['columns']);
|
||||
$result['tableName'] = $model->ADD_TAB_NAME;
|
||||
|
||||
//clean invalid items
|
||||
unset($result['id']);
|
||||
@@ -275,21 +278,77 @@ class CaseList extends Model
|
||||
|
||||
/**
|
||||
* The import requires a $ _FILES content in json format to create a record.
|
||||
* @param array $request_data
|
||||
* @param array $requestData
|
||||
* @param int $ownerId
|
||||
* @return array
|
||||
* @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 ||
|
||||
$_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']);
|
||||
try {
|
||||
$array = json_decode($content, true);
|
||||
|
||||
$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());
|
||||
return $result;
|
||||
} catch (Exception $e) {
|
||||
|
||||
Reference in New Issue
Block a user