Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLG TEST SERVER] #7306

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 77 additions & 24 deletions ProcessMaker/Http/Controllers/Api/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,41 +273,93 @@ private function checkIfAssetsExist($request)
$template = ProcessTemplates::findOrFail($request->id);
$payload = json_decode($template->manifest, true);

// Get assets form the template
$existingOptions = [];

foreach ($payload['export'] as $key => $asset) {
if (Str::contains($asset['model'], 'CommentConfiguration')
) {
unset($payload['export'][$key]);
continue;
// Collect all UUIDs form the payload that are not excluded
$uuids = array_filter(
array_keys($payload['export']),
function ($key) use ($payload) {
return !Str::contains($payload['export'][$key]['model'], 'CommentConfiguration')
&& !Str::contains($payload['export'][$key]['type'], 'Category');
}

if (!$asset['model']::where('uuid', $key)->exists()
|| $payload['root'] === $asset['attributes']['uuid']
|| Str::contains($asset['type'], 'Category')
) {
continue;
}

$item = [
'type' => ($asset['type'] === 'Process') ? 'SubProcess' : $asset['type'],
'uuid' => $key,
'model' => $asset['model'],
);

// Retrieve all the models from the database in a single query
$models = collect($payload['export'])
->filter(function ($asset, $key) use ($uuids, $payload) {
return in_array($key, $uuids) && !$this->isRootOrCategory($payload, $asset);
})
->mapWithKeys(function ($asset, $key) {
return [$key => $asset['model']];
});
// Fetch existing assets
$existingModels = collect($models)->mapWithKeys(function ($model, $uuid) {
return [$uuid => $model::where('uuid', $uuid)->exists()];
});

// Prepare the result array
return $models->filter(function ($exists, $uuid) use ($existingModels) {
return $existingModels[$uuid];
})->map(function ($model, $uuid) use ($payload) {
$asset = $payload['export'][$uuid];

return [
'type' => $asset['type'] === 'Process' ? 'SubProcess' : asset['type'],
'uuid' => $uuid,
'model' => $model,
'name' => $asset['name'],
'mode' => 'copy',
];
})->values()->toArray();
}

$existingOptions[] = $item;
}

return $existingOptions;
private function isRootOrCategory($payload, $asset)
{
return $payload['root'] === $asset['attributes']['uuid']
|| Str::contains($asset['type'], 'Category');
}

// private function checkIfAssetsExist($request)
// {
// $template = ProcessTemplates::findOrFail($request->id);
// $payload = json_decode($template->manifest, true);

// // Get assets form the template
// $existingOptions = [];

// foreach ($payload['export'] as $key => $asset) {
// if (Str::contains($asset['model'], 'CommentConfiguration')
// ) {
// unset($payload['export'][$key]);
// continue;
// }

// if (!$asset['model']::where('uuid', $key)->exists()
// || $payload['root'] === $asset['attributes']['uuid']
// || Str::contains($asset['type'], 'Category')
// ) {
// continue;
// }

// $item = [
// 'type' => ($asset['type'] === 'Process') ? 'SubProcess' : $asset['type'],
// 'uuid' => $key,
// 'model' => $asset['model'],
// 'name' => $asset['name'],
// 'mode' => 'copy',
// ];

// $existingOptions[] = $item;
// }

// return $existingOptions;
// }

protected function createProcess(Request $request)
{
\Log::debug('==== CREATE PROCESS =====');
$request->validate(Template::rules($request->id, $this->types['process'][4]));
\Log::debug('==== CREATE PROCESS VALIDATION RAN =====');
$postOptions = $this->checkIfAssetsExist($request);
\Log::debug('==== CREATE PROCESS CHECK IF ASSETS EXIST COMPLETED =====', ['postOptions' => $postOptions]);

if (!empty($postOptions)) {
$response = [
Expand All @@ -317,6 +369,7 @@ protected function createProcess(Request $request)
];
} else {
$response = $this->template->create('process', $request);
\Log::debug('==== CREATE PROCESS RESPONSE =====', ['response' => $response]);
}

if ($request->has('projects')) {
Expand Down
1 change: 1 addition & 0 deletions ProcessMaker/Http/Controllers/ProcessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public function import(Request $request, Process $process)
return redirect()->route('processes.import');
}
$importIsRunning = ImportV2::isRunning();
\Log::debug("======== PROCESS CONTROLLER IMPORT IS RUNNING =================================");

return view('processes.import', compact('importIsRunning'));
}
Expand Down
10 changes: 8 additions & 2 deletions ProcessMaker/Jobs/ImportV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function handle(): void

if ($lock->get()) {
$logger->clear();
$this->runImport($logger);
$newId = $this->runImport($logger);
\Log::debug("++++++++++++++++ return new id", ['id' => $newId]);
$lock->release();
} else {
// Don't throw exception because that will unlock the running job
Expand All @@ -68,15 +69,19 @@ private function runImport($logger)
{
$this->checkHash();

\Log::debug("++++++++++++++ RUNNING IMPORT IMPORTV2");

$payload = json_decode(Storage::get(self::FILE_PATH), true);


$options = [];
if (!$this->isPreview) {
$options = json_decode(Storage::get(self::OPTIONS_PATH), true);
}

try {
$payload = Importer::handlePasswordDecrypt($payload, $this->password);
\Log::debug("++++++++++++++ RUNNING IMPORT IMPORTV2");
} catch (ImportPasswordException $e) {
$logger->exception($e);

Expand All @@ -94,7 +99,8 @@ private function runImport($logger)
'processVersion' => 2,
], 200);
} else {
$importer->doImport();
$manifest = $importer->doImport();
return $manifest[$payload['root']]->log['newId'];
}

$logger->log('Done');
Expand Down
Loading
Loading