From dcf347243e1d28dc22a7aa20ba7ab38b037be012 Mon Sep 17 00:00:00 2001 From: Esteban Gallego Date: Thu, 16 Nov 2023 15:16:28 -0500 Subject: [PATCH] Job for Synchronizing for Wizard Templates --- .../Console/Commands/SyncWizardTemplates.php | 53 +++++++++ ProcessMaker/Jobs/SyncWizardTemplates.php | 103 ++++++++++++++++++ config/services.php | 7 ++ 3 files changed, 163 insertions(+) create mode 100644 ProcessMaker/Console/Commands/SyncWizardTemplates.php create mode 100644 ProcessMaker/Jobs/SyncWizardTemplates.php diff --git a/ProcessMaker/Console/Commands/SyncWizardTemplates.php b/ProcessMaker/Console/Commands/SyncWizardTemplates.php new file mode 100644 index 0000000000..8b49f01f7b --- /dev/null +++ b/ProcessMaker/Console/Commands/SyncWizardTemplates.php @@ -0,0 +1,53 @@ +option('queue')) { + // $randomDelay = random_int(10, 120); + // Job::dispatch()->delay(now()->addMinutes($randomDelay)); + // } else { + // Job::dispatchSync(); + // } + + Job::dispatchSync(); + + return 0; + } +} diff --git a/ProcessMaker/Jobs/SyncWizardTemplates.php b/ProcessMaker/Jobs/SyncWizardTemplates.php new file mode 100644 index 0000000000..23cbe3ad52 --- /dev/null +++ b/ProcessMaker/Jobs/SyncWizardTemplates.php @@ -0,0 +1,103 @@ + '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(); + } + } + } +} diff --git a/config/services.php b/config/services.php index 895c32926c..1738ca4f63 100644 --- a/config/services.php +++ b/config/services.php @@ -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'), + ], + ];