Skip to content

Job for Synchronizing for Wizard Templates #5649

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

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
53 changes: 53 additions & 0 deletions ProcessMaker/Console/Commands/SyncWizardTemplates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace ProcessMaker\Console\Commands;

use Illuminate\Console\Command;
use ProcessMaker\Jobs\SyncWizardTemplates as Job;

class SyncWizardTemplates extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'processmaker:sync-wizard-templates
{--queue : Queue this command to run asynchronously}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Synchronize wizard templates from a central repository';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// if ($this->option('queue')) {
// $randomDelay = random_int(10, 120);
// Job::dispatch()->delay(now()->addMinutes($randomDelay));
// } else {
// Job::dispatchSync();
// }

Job::dispatchSync();

return 0;
}
}
103 changes: 103 additions & 0 deletions ProcessMaker/Jobs/SyncWizardTemplates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace ProcessMaker\Jobs;

use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use ProcessMaker\ImportExport\Importer;
use ProcessMaker\ImportExport\Options;
use ProcessMaker\Models\WizardTemplate;
use ProcessMaker\Models\WizardTemplateCategory;

class SyncWizardTemplates implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Execute the job.
* Function to handle the execution of this job when it is run.
* Here the function fetches default templates list from Github and saves them to the database.
* @return void
*/
public function handle()
{
$config = config('services.wizard_templates_github');
$url = $config['base_url'] .
$config['wizard_repo'] . '/' .
$config['wizard_branch'] . '/index.json';

// If there are multiple categories of templates defined in the .env then separate them into an array.
$categories = (strpos($config['wizard_categories'], ',') !== false)
? explode(',', $config['wizard_categories'])
: [$config['wizard_categories']];

$wizardTemplateCategoryId = WizardTemplateCategory::firstOrCreate(
['name' => 'Default Wizard Templates'],
[
'name' => 'Default Wizard Templates',
'status' => 'ACTIVE',
'is_system' => 0,
]
)->getKey();

// Get the default template list from Github.
$response = Http::get($url);

if (!$response->successful()) {
throw new Exception('Unable to fetch wizard template list.');
}

// Extract the json data from the response and iterate over the categories and templates to retrieve them.
$data = $response->json();
foreach ($data as $templateCategory => $templates) {
if (!in_array($templateCategory, $categories) && !in_array('all', $categories)) {
continue;
}
foreach ($templates as $template) {
$existingTemplate = WizardTemplate::where('uuid', $template['uuid'])->first();
// If the template already exists in the database with a user then skip it,
// since we don't want to overwrite their changes.
if (!is_null($existingTemplate) && !is_null($existingTemplate->user_id)) {
continue;
}

$url = $config['base_url'] .
$config['wizard_repo'] . '/' .
$config['wizard_branch'] . '/' .
$template['relative_path'];

$response = Http::get($url);
if (!$response->successful()) {
throw new Exception("Unable to fetch wizard template {$template['name']}.");
}
$payload = $response->json();
$dataKey = "export.{$payload['root']}.attributes.wizard_template_category_id";
data_set($payload, $dataKey, $wizardTemplateCategoryId);

$options = new Options([
'mode' => 'update',
'isTemplate' => true,
'saveAssetsMode' => 'saveAllAssets',
]);

$importer = new Importer($payload, $options);
$importer->doImport();
}
}
}
}
7 changes: 7 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@
'template_categories' => env('DEFAULT_TEMPLATE_CATEGORIES', 'accounting-and-finance,customer-success,human-resources,marketing-and-sales,operations,it'),
],

'wizard_templates_github' => [
'base_url' => 'https://raw.githubusercontent.com/processmaker/',
'wizard_repo' => env('WIZARD_TEMPLATE_REPO', 'wizard-templates'),
'wizard_branch' => env('WIZARD_TEMPLATE_BRANCH', 'main'),
'wizard_categories' => env('WIZARD_TEMPLATE_CATEGORIES', 'all'),
],

];