231 lines
6.2 KiB
TypeScript
231 lines
6.2 KiB
TypeScript
import {Component, EventEmitter, Inject, OnInit, Output} from '@angular/core';
|
|
import {SelectionModel} from "@angular/cdk/collections";
|
|
import {HttpClient} from "@angular/common/http";
|
|
import {ToastrService} from "ngx-toastr";
|
|
import {ConfigService} from "@services/config.service";
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
|
import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from "@angular/material/dialog";
|
|
import {
|
|
SaveScriptComponent
|
|
} from "../../../groups/components/client-main-view/run-script-assistant/save-script/save-script.component";
|
|
import {FormBuilder, FormGroup} from "@angular/forms";
|
|
|
|
@Component({
|
|
selector: 'app-create-task-script',
|
|
templateUrl: './create-task-script.component.html',
|
|
styleUrl: './create-task-script.component.css'
|
|
})
|
|
export class CreateTaskScriptComponent implements OnInit {
|
|
form: FormGroup;
|
|
baseUrl: string;
|
|
@Output() dataChange = new EventEmitter<any>();
|
|
errorMessage = '';
|
|
loading: boolean = false;
|
|
scripts: any[] = [];
|
|
scriptContent: string = "";
|
|
parameters: any = {};
|
|
selectedTabIndex: number = 0;
|
|
selectedBasicAction: string = '';
|
|
commandType: string = 'existing';
|
|
selectedScript: any = null;
|
|
newScript: string = '';
|
|
executionOrder: Number = 0;
|
|
selection = new SelectionModel(true, []);
|
|
parameterNames: string[] = Object.keys(this.parameters);
|
|
commandTask: any;
|
|
constructor(
|
|
private fb: FormBuilder,
|
|
private http: HttpClient,
|
|
public dialogRef: MatDialogRef<CreateTaskScriptComponent>,
|
|
private toastService: ToastrService,
|
|
private configService: ConfigService,
|
|
private router: Router,
|
|
private dialog: MatDialog,
|
|
private route: ActivatedRoute,
|
|
@Inject(MAT_DIALOG_DATA) public data: any
|
|
) {
|
|
this.baseUrl = this.configService.apiUrl;
|
|
this.commandTask = this.data.commandTask;
|
|
|
|
this.loadScripts()
|
|
this.form = this.fb.group({
|
|
content: [''],
|
|
order: [''],
|
|
})
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
|
|
}
|
|
|
|
loadScripts(): void {
|
|
this.loading = true;
|
|
|
|
this.http.get(`${this.baseUrl}/commands?readOnly=false&enabled=true`).subscribe((data: any) => {
|
|
this.scripts = data['hydra:member'];
|
|
this.loading = false;
|
|
}, (error) => {
|
|
this.toastService.error(error.error['hydra:description']);
|
|
this.loading = false;
|
|
});
|
|
}
|
|
|
|
onTabChange(index: number): void {
|
|
this.selectedTabIndex = index;
|
|
if (index === 0) {
|
|
this.selectedBasicAction = '';
|
|
} else {
|
|
this.selectedScript = null;
|
|
this.newScript = '';
|
|
}
|
|
}
|
|
|
|
onBasicActionChange(): void {
|
|
this.parameters = {};
|
|
this.parameterNames = Object.keys(this.parameters);
|
|
}
|
|
|
|
getBasicActionType(): string {
|
|
const actionMap: { [key: string]: string } = {
|
|
'poweron': 'POWER-ON',
|
|
'shutdown': 'SHUTDOWN',
|
|
'reboot': 'REBOOT',
|
|
'login': 'LOGIN',
|
|
};
|
|
|
|
return actionMap[this.selectedBasicAction] || '';
|
|
}
|
|
|
|
validateBasicAction(): boolean {
|
|
if (!this.selectedBasicAction) {
|
|
this.toastService.error('Debe seleccionar un tipo de acción');
|
|
return false;
|
|
}
|
|
|
|
if (this.executionOrder === null || this.executionOrder === undefined) {
|
|
this.toastService.error('Debe especificar el orden de ejecución');
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
saveNewScript() {
|
|
if (!this.newScript.trim()) {
|
|
this.toastService.error('Debe ingresar un script antes de guardar.');
|
|
return;
|
|
}
|
|
const dialogRef = this.dialog.open(SaveScriptComponent, {
|
|
width: '400px',
|
|
data: this.newScript
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe(result => {
|
|
if (result) {
|
|
this.toastService.success('Script guardado correctamente');
|
|
}
|
|
});
|
|
}
|
|
|
|
onScriptChange() {
|
|
if (this.selectedScript) {
|
|
this.scriptContent = this.selectedScript.script;
|
|
|
|
const matches = this.scriptContent.match(/@(\w+)/g) || [];
|
|
const uniqueParams = Array.from(new Set(matches.map(m => m.slice(1))));
|
|
|
|
this.parameters = {};
|
|
uniqueParams.forEach(param => this.parameters[param] = '');
|
|
|
|
this.parameterNames = uniqueParams;
|
|
|
|
this.updateScript();
|
|
}
|
|
}
|
|
|
|
onParamChange(name: string, value: string): void {
|
|
this.parameters[name] = value;
|
|
this.updateScript();
|
|
}
|
|
|
|
updateScript(): void {
|
|
let updatedScript = this.selectedScript.script;
|
|
|
|
for (const [key, value] of Object.entries(this.parameters)) {
|
|
const regex = new RegExp(`@${key}\\b`, 'g');
|
|
updatedScript = updatedScript.replace(regex, value || `@${key}`);
|
|
}
|
|
|
|
this.scriptContent = updatedScript;
|
|
}
|
|
|
|
validateScript(): boolean {
|
|
if (this.commandType === 'new') {
|
|
if (!this.newScript.trim()) {
|
|
this.toastService.error('Debe ingresar un script');
|
|
return false;
|
|
}
|
|
} else if (this.commandType === 'existing') {
|
|
if (!this.selectedScript) {
|
|
this.toastService.error('Debe seleccionar un script');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (this.executionOrder === null || this.executionOrder === undefined) {
|
|
this.toastService.error('Debe especificar el orden de ejecución');
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
onSubmit() {
|
|
let isValid = false;
|
|
let content = '';
|
|
let type = '';
|
|
|
|
if (this.selectedTabIndex === 0) {
|
|
isValid = this.validateBasicAction();
|
|
if (isValid) {
|
|
content = this.getBasicActionType();
|
|
type = 'basic-action';
|
|
}
|
|
} else { // Pestaña de scripts
|
|
isValid = this.validateScript();
|
|
if (isValid) {
|
|
content = this.commandType === 'existing' ? this.scriptContent : this.newScript;
|
|
type = 'run-script';
|
|
}
|
|
}
|
|
|
|
if (!isValid) {
|
|
return;
|
|
}
|
|
|
|
if (!this.data) {
|
|
this.toastService.error('Error: No se recibieron datos del diálogo');
|
|
return;
|
|
}
|
|
|
|
this.http.post(`${this.baseUrl}/command-task-scripts`, {
|
|
commandTask: this.commandTask['@id'],
|
|
content: content,
|
|
order: this.executionOrder,
|
|
type: this.selectedBasicAction,
|
|
}).subscribe({
|
|
next: () => {
|
|
this.toastService.success('Tarea creada con éxito');
|
|
this.dialogRef.close(true);
|
|
},
|
|
error: (error) => {
|
|
this.toastService.error(error.error['hydra:description']);
|
|
}
|
|
})
|
|
}
|
|
|
|
onCancel(): void {
|
|
this.dialogRef.close(false);
|
|
}
|
|
}
|