Jose Brito 1 سال پیش
والد
کامیت
e0a8914e0b

+ 44 - 1
sistema-mantenimiento-front/src/app/components/personal-management/specialties-management/specialties-management.component.ts

@@ -4,6 +4,9 @@ import { MatTableDataSource } from '@angular/material/table';
 import { lastValueFrom } from 'rxjs';
 import { InterventionService } from 'src/app/services/personal-management/intervention.service';
 import { SpecialtyFormComponent } from './specialty-form/specialty-form.component';
+import { EncService } from 'src/app/services/enc/enc.service';
+import { FunctionsService } from 'src/app/services/functions.service';
+import { ResourcesService } from 'src/app/services/resources/resources.service';
 
 @Component({
   selector: 'app-specialties-management',
@@ -22,6 +25,8 @@ export class SpecialtiesManagementComponent implements OnInit {
   constructor(
     private _interventionService: InterventionService,
     private _dialog: MatDialog,
+    private _encService: EncService,
+    private _resourcesService: ResourcesService,
   ) {
     this.isLoading = true;
     this.hasError = false;
@@ -38,6 +43,10 @@ export class SpecialtiesManagementComponent implements OnInit {
 
   async getSpecialties(){
     try{
+      this.isLoading = true;
+      this.hasError = false;
+      this.errorStr = '';
+      
       let idUser = localStorage.getItem('idusuario')!;
       let specialties = await lastValueFrom(this._interventionService.getSpecialties(idUser, 1));
       console.log(specialties);
@@ -83,7 +92,41 @@ export class SpecialtiesManagementComponent implements OnInit {
     });
     
     dialogRef.afterClosed().subscribe(res => {
-      console.log(res);
+      if(res != null && res != undefined && res != ''){
+        this.saveSpecialty(res, type);
+      }
     });
   }
+
+  async saveSpecialty(dataEnc: string, type: string){
+    try{
+      let dataDec = await this._encService.decrypt(dataEnc);
+      let dataObj = JSON.parse(dataDec);
+      let idUser = localStorage.getItem('idusuario')!;
+      let formData = new FormData();
+      let codeEnc = await this._encService.encrypt(dataObj.code);
+
+      formData.append('id_user', idUser);
+      formData.append('linea', '1');
+      formData.append('specialty_code', codeEnc);
+      formData.append('spacialty_name', dataObj.name);
+
+      if(type == 'new'){
+        await lastValueFrom(this._interventionService.registerSpecialty(formData));
+        this._resourcesService.openSnackBar('La especialidad se registró correctamente.');
+      }else{
+        console.log(formData);
+      }
+
+      this.getSpecialties();
+    }catch(error: any){
+      if(error.error == undefined){
+        this._resourcesService.openSnackBar('Ocurrió un error inesperado.');
+      }else if(error.error.msg == undefined){
+        this._resourcesService.openSnackBar('Ocurrió un error inesperado.');
+      }else{
+        this._resourcesService.openSnackBar(error.error.msg);
+      }
+    }
+  }
 }

+ 30 - 1
sistema-mantenimiento-front/src/app/components/personal-management/specialties-management/specialty-form/specialty-form.component.html

@@ -1 +1,30 @@
-<p>specialty-form works!</p>
+<h1 mat-dialog-title class="prevent-select">{{ action }} especialidad</h1>
+<div mat-dialog-content class="prevent-select">
+  <div class="is-loading animated fadeIn fast" *ngIf="isLoading">
+    <mat-spinner align="center"></mat-spinner>
+    <h3>Cargando datos ...</h3>
+  </div>
+  <div class="has-error animated fadeIn pt-64" *ngIf="!isLoading && hasError">
+    <mat-icon class="red_primary_font">error</mat-icon>
+    <h2>{{ errorStr }}</h2>
+  </div>
+  <div class="dialog_details_column animated fadeIn" *ngIf="!isLoading && !hasError">
+    <mat-form-field appearance="outline" class="w-100 mt-8">
+      <mat-label>Código de la especialidad</mat-label>
+      <input matInput [formControl]="specialtyCodeControl" #specialtyCode (input)="validateCode(specialtyCode.value)">
+      <mat-error *ngIf="specialtyCodeControl.hasError('required')">Este campo es requerido.</mat-error>
+      <mat-error *ngIf="specialtyCodeControl.hasError('minlength')">Este campo debe tener al menos 6 caracteres.</mat-error>
+      <mat-error *ngIf="specialtyCodeControl.hasError('maxlength')">La longitud máxima de este campo es de 6 caracteres.</mat-error>
+    </mat-form-field>
+    <mat-form-field appearance="outline" class="w-100 mt-8">
+      <mat-label>Nombre de la especialidad</mat-label>
+      <input matInput [formControl]="specialtyNameControl" (input)="specialtyNameControl.markAsTouched()">
+      <mat-error *ngIf="specialtyNameControl.hasError('required')">Este campo es requerido.</mat-error>
+      <mat-error *ngIf="specialtyNameControl.hasError('maxlength')">La longitud máxima de este campo es de 75 caracteres.</mat-error>
+    </mat-form-field>
+  </div>
+</div>
+<div mat-dialog-actions align="end">
+  <button mat-button mat-dialog-close>Cerrar</button>
+  <button mat-button [disabled]="specialtyCodeControl.invalid || specialtyNameControl.invalid" (click)="closeForm()">Guardar</button>
+</div>

+ 57 - 2
sistema-mantenimiento-front/src/app/components/personal-management/specialties-management/specialty-form/specialty-form.component.ts

@@ -1,10 +1,65 @@
-import { Component } from '@angular/core';
+import { Component, Inject, OnInit } from '@angular/core';
+import { FormControl, Validators } from '@angular/forms';
+import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
+import { EncService } from 'src/app/services/enc/enc.service';
 
 @Component({
   selector: 'app-specialty-form',
   templateUrl: './specialty-form.component.html',
   styleUrl: './specialty-form.component.css'
 })
-export class SpecialtyFormComponent {
+export class SpecialtyFormComponent implements OnInit {
+  isLoading: boolean;
+  hasError: boolean;
+  errorStr: string;
+  action: string;
+  isNew: boolean;
 
+  specialtyCodeControl: FormControl;
+  specialtyNameControl: FormControl;
+
+  constructor(
+    @Inject(MAT_DIALOG_DATA) private _data: any,
+    private _dialogRef: MatDialogRef<SpecialtyFormComponent>,
+    private _encService: EncService,
+  ){
+    this.isLoading = true;
+    this.hasError = false;
+    this.errorStr = '';
+    this.action = '';
+    this.isNew = true;
+
+    this.specialtyCodeControl = new FormControl('', [Validators.required, Validators.minLength(6), Validators.maxLength(6)]);
+    this.specialtyNameControl = new FormControl('', [Validators.required, Validators.maxLength(75)]);
+  }
+
+  ngOnInit(): void {
+    this.isNew = this._data.type == 'new';
+    this.action = this.isNew ? 'Registrar' : 'Editar';
+
+    if(this.isNew){
+      this.isLoading = false;
+    }else{
+      console.log(this._data);
+    }
+  }
+
+  validateCode(value: string){
+    let valueUpper = value.toUpperCase();
+
+    this.specialtyCodeControl.setValue(valueUpper);
+    this.specialtyCodeControl.markAsTouched();
+  }
+
+  async closeForm(){
+    let dataObj = {
+      code: this.specialtyCodeControl.value,
+      name: this.specialtyNameControl.value,
+    }
+
+    let dataStr = JSON.stringify(dataObj);
+    let dataEnc = await this._encService.encrypt(dataStr);
+
+    this._dialogRef.close(dataEnc);
+  }
 }

+ 9 - 0
sistema-mantenimiento-front/src/app/interfaces/specialties.interface.ts

@@ -0,0 +1,9 @@
+export interface SpecialtiesListResponse{
+  error: boolean;
+  msg: string;
+  response: SpecialtiesListItem[]
+}
+
+export interface SpecialtiesListItem{
+  
+}

+ 5 - 0
sistema-mantenimiento-front/src/app/services/personal-management/intervention.service.ts

@@ -45,4 +45,9 @@ export class InterventionService {
     return this._httpRequestService.postQuery(`${this._url}/contract/register`, response)
       .pipe(map((data: any) => data));
   }
+
+  public registerSpecialty(response: FormData) {
+    return this._httpRequestService.postQuery(`specialties/register`, response)
+      .pipe(map((data: any) => data));
+  }
 }