From 6d763f22eb82277fdce1b6aebbe153cd48aabc9a Mon Sep 17 00:00:00 2001 From: apuente Date: Wed, 6 Nov 2024 13:09:18 +0100 Subject: [PATCH] refs #1060 Add execute command from clasroom menu --- ogWebconsole/src/app/app.module.ts | 2 + .../execute-command.component.html | 5 +- .../components/groups/groups.component.html | 6 + .../app/components/groups/groups.component.ts | 17 +++ .../execute-command-ou.component.css | 57 +++++++++ .../execute-command-ou.component.html | 45 +++++++ .../execute-command-ou.component.ts | 120 ++++++++++++++++++ 7 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.css create mode 100644 ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.html create mode 100644 ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.ts diff --git a/ogWebconsole/src/app/app.module.ts b/ogWebconsole/src/app/app.module.ts index 55da408..216fb55 100644 --- a/ogWebconsole/src/app/app.module.ts +++ b/ogWebconsole/src/app/app.module.ts @@ -119,6 +119,7 @@ import { ClientsComponent } from './components/ogboot/pxe/clients/clients.compon import { RepositoriesComponent } from './components/repositories/repositories.component'; import { CreateRepositoryComponent } from './components/repositories/create-repository/create-repository.component'; import { ExecuteCommandComponent } from './components/commands/main-commands/execute-command/execute-command.component'; +import { ExecuteCommandOuComponent } from './components/groups/shared/execute-command-ou/execute-command-ou.component'; @NgModule({ declarations: [ AppComponent, @@ -195,6 +196,7 @@ import { ExecuteCommandComponent } from './components/commands/main-commands/exe RepositoriesComponent, CreateRepositoryComponent, ExecuteCommandComponent, + ExecuteCommandOuComponent, ], bootstrap: [AppComponent], imports: [BrowserModule, diff --git a/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.html b/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.html index 40a5a74..60e52d3 100644 --- a/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.html +++ b/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.html @@ -3,7 +3,6 @@
- Unidad Organizativa @@ -11,7 +10,6 @@ - Subunidad Organizativa @@ -19,7 +17,6 @@ -
@@ -40,4 +37,4 @@ - \ No newline at end of file + diff --git a/ogWebconsole/src/app/components/groups/groups.component.html b/ogWebconsole/src/app/components/groups/groups.component.html index 2c73dec..289e1ca 100644 --- a/ogWebconsole/src/app/components/groups/groups.component.html +++ b/ogWebconsole/src/app/components/groups/groups.component.html @@ -127,6 +127,12 @@ delete Borrar elemento + + +
diff --git a/ogWebconsole/src/app/components/groups/groups.component.ts b/ogWebconsole/src/app/components/groups/groups.component.ts index 27648bd..e0763d9 100644 --- a/ogWebconsole/src/app/components/groups/groups.component.ts +++ b/ogWebconsole/src/app/components/groups/groups.component.ts @@ -25,6 +25,8 @@ import {ClientTabViewComponent} from "./components/client-tab-view/client-tab-vi import { OrganizationalUnitTabViewComponent } from "./components/organizational-unit-tab-view/organizational-unit-tab-view.component"; +import { ExecuteCommandComponent } from '../commands/main-commands/execute-command/execute-command.component'; +import { ExecuteCommandOuComponent } from './shared/execute-command-ou/execute-command-ou.component'; @Component({ selector: 'app-groups', @@ -298,6 +300,21 @@ export class GroupsComponent implements OnInit { } } + onExecuteCommand(event: MouseEvent, child: any, name: string, type:string): void { + console.log('Executing command on:', child); + + this.dialog.open(ExecuteCommandOuComponent, { + width: '50%', + data: { childUnitUuid: child } + }).afterClosed().subscribe((result) => { + if (result) { + console.log('Comando ejecutado con éxito'); + } else { + console.log('Ejecución de comando cancelada'); + } + }); + } + openSnackBar(isError: boolean, message: string) { if (isError) { this.toastService.error(' Error al eliminar la entidad: ' + message, 'Error'); diff --git a/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.css b/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.css new file mode 100644 index 0000000..b908066 --- /dev/null +++ b/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.css @@ -0,0 +1,57 @@ +.form-container { + display: flex; + flex-direction: column; + gap: 16px; + padding: 16px; + } + + .command-form { + width: 100%; + } + + .full-width { + width: 100%; + } + + .checkbox-group { + display: flex; + flex-direction: column; + gap: 8px; + padding-top: 8px; + } + + .checkbox-group label { + font-weight: bold; + margin-bottom: 8px; + } + + .mat-checkbox { + margin-left: 8px; + } + + .mat-dialog-title { + font-size: 20px; + font-weight: 600; + margin-bottom: 12px; + } + + .mat-dialog-content { + max-height: 60vh; + overflow-y: auto; + } + + .mat-dialog-actions { + display: flex; + justify-content: flex-end; + gap: 8px; + padding: 16px; + } + + button[mat-button] { + font-weight: 500; + } + + button[mat-button]:disabled { + color: rgba(0, 0, 0, 0.38); + } + \ No newline at end of file diff --git a/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.html b/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.html new file mode 100644 index 0000000..81c14d4 --- /dev/null +++ b/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.html @@ -0,0 +1,45 @@ +

Ejecutar Comando o Grupo de Comandos

+ + + + + + Seleccione Comando + + {{ command.name }} + + + + + Seleccione Grupo de Comandos + + {{ group.name }} + + + +
+ +
+ + {{ client.name }} + +
+
+

No hay clientes disponibles

+
+
+ + +
+ + + + + diff --git a/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.ts b/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.ts new file mode 100644 index 0000000..7831cab --- /dev/null +++ b/ogWebconsole/src/app/components/groups/shared/execute-command-ou/execute-command-ou.component.ts @@ -0,0 +1,120 @@ +import { Component, Inject, OnInit } from '@angular/core'; +import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; +import { HttpClient } from '@angular/common/http'; +import { FormBuilder, FormGroup } from '@angular/forms'; +import { ToastrService } from 'ngx-toastr'; + +@Component({ + selector: 'app-execute-command-ou', + templateUrl: './execute-command-ou.component.html', + styleUrls: ['./execute-command-ou.component.css'] +}) +export class ExecuteCommandOuComponent implements OnInit { + form: FormGroup; + clients: any[] = []; + commands: any[] = []; + commandGroups: any[] = []; + baseUrl: string = import.meta.env.NG_APP_BASE_API_URL; + + constructor( + private dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: any, + private http: HttpClient, + private fb: FormBuilder, + private toastService: ToastrService, + ) { + this.form = this.fb.group({ + selectedCommand: [null], + selectedCommandGroup: [null], + clientSelection: [[]] + }); + } + + ngOnInit(): void { + this.loadClients(); + this.loadCommands(); + this.loadCommandGroups(); + } + + loadClients(): void { + this.http.get(`${this.baseUrl}/organizational-units/${this.data.childUnitUuid}`).subscribe( + response => { + this.clients = this.getAllClients(response); + const clientIds = this.clients.map(client => client.uuid); + this.form.get('clientSelection')?.setValue(clientIds); + }, + error => console.error('Error al cargar los clientes:', error) + ); + } + + getAllClients(unit: any): any[] { + let allClients = unit.clients || []; + if (unit.children && unit.children.length > 0) { + unit.children.forEach((child: any) => { + allClients = allClients.concat(this.getAllClients(child)); + }); + } + return allClients; + } + + loadCommands(): void { + this.http.get(`${this.baseUrl}/commands?page=1&itemsPerPage=30`).subscribe( + response => { + this.commands = response['hydra:member'] || []; + }, + error => this.toastService.error('Error al cargar comandos:', error) + ); + } + + loadCommandGroups(): void { + this.http.get(`${this.baseUrl}/command-groups?page=1&itemsPerPage=30`).subscribe( + response => { + this.commandGroups = response['hydra:member'] || []; + }, + error => this.toastService.error('Error al cargar grupos de comandos:', error) + ); + } + + toggleClientSelection(clientId: string): void { + const selectedClients = this.form.get('clientSelection')?.value || []; + if (selectedClients.includes(clientId)) { + this.form.get('clientSelection')?.setValue(selectedClients.filter((id: string) => id !== clientId)); + } else { + this.form.get('clientSelection')?.setValue([...selectedClients, clientId]); + } + } + + executeCommand(): void { + const selectedCommandUuid = this.form.get('selectedCommand')?.value || this.form.get('selectedCommandGroup')?.value; + const isCommandGroup = !!this.form.get('selectedCommandGroup')?.value; + + if (!selectedCommandUuid) { + console.warn('No se ha seleccionado ningún comando o grupo de comandos'); + return; + } + + const payload = { + clients: (this.form.get('clientSelection')?.value || []).map((clientId: string) => `/clients/${clientId}`) + }; + + const url = isCommandGroup + ? `${this.baseUrl}/command-groups/${selectedCommandUuid}/execute` + : `${this.baseUrl}/commands/${selectedCommandUuid}/execute`; + + this.http.post(url, payload) + .subscribe({ + next: () => { + this.toastService.success('Comando ejecutado con éxito'); + this.dialogRef.close(true); + }, + error: (error) => { + this.toastService.error('Error al ejecutar el comando:', error); + this.dialogRef.close(false); + } + }); + } + + closeModal(): void { + this.dialogRef.close(false); + } +}