diff --git a/ProcessMaker/Http/Controllers/Api/WizardTemplateController.php b/ProcessMaker/Http/Controllers/Api/WizardTemplateController.php new file mode 100644 index 0000000000..a77a9cf062 --- /dev/null +++ b/ProcessMaker/Http/Controllers/Api/WizardTemplateController.php @@ -0,0 +1,28 @@ +input('per_page', 10); + $column = $request->input('order_by', 'id'); + $direction = $request->input('order_direction', 'asc'); + + $query = WizardTemplate::query() + ->orderBy($column, $direction); + + $data = $query->paginate($perPage); + + return new ApiCollection($data); + } +} diff --git a/ProcessMaker/Jobs/SyncWizardTemplates.php b/ProcessMaker/Jobs/SyncWizardTemplates.php index 23cbe3ad52..c0b49c78b6 100644 --- a/ProcessMaker/Jobs/SyncWizardTemplates.php +++ b/ProcessMaker/Jobs/SyncWizardTemplates.php @@ -11,8 +11,9 @@ use Illuminate\Support\Facades\Http; use ProcessMaker\ImportExport\Importer; use ProcessMaker\ImportExport\Options; +use ProcessMaker\Models\Process; +use ProcessMaker\Models\ProcessCategory; use ProcessMaker\Models\WizardTemplate; -use ProcessMaker\Models\WizardTemplateCategory; class SyncWizardTemplates implements ShouldQueue { @@ -46,10 +47,10 @@ public function handle() ? explode(',', $config['wizard_categories']) : [$config['wizard_categories']]; - $wizardTemplateCategoryId = WizardTemplateCategory::firstOrCreate( - ['name' => 'Default Wizard Templates'], + $wizardTemplateCategoryId = ProcessCategory::firstOrCreate( + ['name' => 'Wizard Templates'], [ - 'name' => 'Default Wizard Templates', + 'name' => 'Wizard Templates', 'status' => 'ACTIVE', 'is_system' => 0, ] @@ -64,11 +65,11 @@ public function handle() // 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) { + foreach ($data as $templateCategory => $wizardTemplates) { if (!in_array($templateCategory, $categories) && !in_array('all', $categories)) { continue; } - foreach ($templates as $template) { + foreach ($wizardTemplates 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. @@ -86,17 +87,22 @@ public function handle() throw new Exception("Unable to fetch wizard template {$template['name']}."); } $payload = $response->json(); - $dataKey = "export.{$payload['root']}.attributes.wizard_template_category_id"; + $dataKey = "export.{$payload['root']}.attributes.process_category_id"; data_set($payload, $dataKey, $wizardTemplateCategoryId); $options = new Options([ 'mode' => 'update', - 'isTemplate' => true, + 'asset_type' => 'WIZARD_TEMPLATE', 'saveAssetsMode' => 'saveAllAssets', ]); - $importer = new Importer($payload, $options); $importer->doImport(); + + $template = Process::where('uuid', $template['uuid'])->first(); + + WizardTemplate::create([ + 'process_id' => $template->id, + ]); } } } diff --git a/ProcessMaker/Models/WizardTemplate.php b/ProcessMaker/Models/WizardTemplate.php new file mode 100644 index 0000000000..73e099e7a7 --- /dev/null +++ b/ProcessMaker/Models/WizardTemplate.php @@ -0,0 +1,38 @@ +belongsTo(Process::class, 'process_id'); + } + + /** + * Get the process template associated with the wizard template. + */ + public function process_template(): BelongsTo + { + return $this->belongsTo(ProcessTemplates::class, 'process_template_id'); + } +} diff --git a/database/factories/ProcessMaker/Models/WizardTemplateFactory.php b/database/factories/ProcessMaker/Models/WizardTemplateFactory.php new file mode 100644 index 0000000000..2412eb901f --- /dev/null +++ b/database/factories/ProcessMaker/Models/WizardTemplateFactory.php @@ -0,0 +1,21 @@ + $this->faker->uuid, + 'process_template_id' => null, + 'process_id' => Process::factory()->create()->id, + ]; + } +} diff --git a/routes/api.php b/routes/api.php index f88ff77de6..7bd266f9ba 100644 --- a/routes/api.php +++ b/routes/api.php @@ -32,9 +32,8 @@ use ProcessMaker\Http\Controllers\Api\TemplateController; use ProcessMaker\Http\Controllers\Api\UserController; use ProcessMaker\Http\Controllers\Api\UserTokenController; -use ProcessMaker\Http\Controllers\Process\ModelerController; +use ProcessMaker\Http\Controllers\Api\WizardTemplateController; use ProcessMaker\Http\Controllers\TestStatusController; -use ProcessMaker\Http\Middleware\ValidateEditUserAndPasswordPermission; Route::middleware('auth:api', 'setlocale', 'bindings', 'sanitize')->prefix('api/1.0')->name('api.')->group(function () { // Users @@ -255,6 +254,9 @@ Route::get('modeler/templates/{type}/{id}', [TemplateController::class, 'show'])->name('modeler.template.show')->middleware('template-authorization'); Route::post('templates/{type}/import/validation', [TemplateController::class, 'preImportValidation'])->name('template.preImportValidation')->middleware('template-authorization'); + // Wizard Templates + Route::get('wizard-templates', [WizardTemplateController::class, 'index'])->name('wizard-templates.index'); + // Process Translations Route::get('process/translations', [ProcessTranslationController::class, 'index'])->name('process-translation.index')->middleware('can:view-process-translations'); Route::get('process/translations/pending', [ProcessTranslationController::class, 'pending'])->name('process-translation.pending')->middleware('can:view-process-translations'); diff --git a/tests/Feature/Api/WizardTemplatesTest.php b/tests/Feature/Api/WizardTemplatesTest.php new file mode 100644 index 0000000000..0980b32998 --- /dev/null +++ b/tests/Feature/Api/WizardTemplatesTest.php @@ -0,0 +1,35 @@ +count($total)->create(); + + $params = [ + 'order_by' => 'id', + 'order_direction' => 'asc', + 'per_page' => 10, + ]; + $route = route('api.wizard-templates.index', $params); + $response = $this->apiCall('GET', $route); + + $response->assertStatus(200); + $response->assertJsonCount(10, 'data'); + $response->assertJson([ + 'meta' => [ + 'per_page' => $params['per_page'], + 'total' => $total, + ], + ]); + } +}