ソースを参照

Finalización de la función de permisos para exportar reportes de excel

Jose Brito 1 年間 前
コミット
734d391c28

+ 14 - 0
sistema-mantenimiento-front/src/app/components/control-panel/data-export-selection/data-election-config/data-election-config.component.css

@@ -18,4 +18,18 @@
 
 .item-permissions mat-slide-toggle{
   margin-right: 4px;
+}
+
+.mat-mdc-slide-toggle.mat-accent{
+  --mdc-switch-selected-focus-state-layer-color: #4CAF50 !important;
+  --mdc-switch-selected-handle-color: #4CAF50 !important;
+  --mdc-switch-selected-hover-state-layer-color: #4CAF50 !important;
+  --mdc-switch-selected-pressed-state-layer-color: #4CAF50 !important;
+  --mdc-switch-selected-focus-handle-color: #087F23 !important;
+  --mdc-switch-selected-hover-handle-color: #087F23 !important;
+  --mdc-switch-selected-pressed-handle-color: #087F23 !important;
+  --mdc-switch-selected-focus-track-color: #80E27E !important;
+  --mdc-switch-selected-hover-track-color: #80E27E !important;
+  --mdc-switch-selected-pressed-track-color: #80E27E !important;
+  --mdc-switch-selected-track-color: #80E27E !important;
 }

+ 20 - 7
sistema-mantenimiento-front/src/app/components/control-panel/data-export-selection/data-election-config/data-election-config.component.html

@@ -29,28 +29,36 @@ items_container_full_width: screenSize <= 640 }">
             <div class="form-cell C12" *ngFor="let module of permissions">
               <div class="item-permissions">
                 <div class="title">{{ module.id }} - {{ module.name }}</div>
-                <mat-slide-toggle>Permitir exportar</mat-slide-toggle>
+                <mat-slide-toggle [checked]="exportPermissions.get(module.id)" (change)="permissionsChange($event, module.id, module.children)">
+                  Permitir exportar
+                </mat-slide-toggle>
               </div>
-              <div class="form-column mt-4 pl-16" *ngIf="module.children.length > 0" style="box-sizing: border-box;">
+              <div class="form-column mt-4 pl-32" *ngIf="module.children.length > 0" style="box-sizing: border-box;">
                 <div class="form-row">
                   <div class="form-cell C12" *ngFor="let submodule of module.children">
                     <div class="item-permissions">
                       <div class="title">{{ submodule.id }} - {{ submodule.name }}</div>
-                      <mat-slide-toggle>Permitir exportar</mat-slide-toggle>
+                      <mat-slide-toggle [checked]="exportPermissions.get(submodule.id)" (change)="permissionsChange($event, submodule.id, submodule.children)">
+                        Permitir exportar
+                      </mat-slide-toggle>
                     </div>
-                    <div class="form-column mt-4 pl-16" *ngIf="submodule.children.length > 0" style="box-sizing: border-box;">
+                    <div class="form-column mt-4 pl-32" *ngIf="submodule.children.length > 0" style="box-sizing: border-box;">
                       <div class="form-row">
                         <div class="form-cell C12" *ngFor="let funct of submodule.children">
                           <div class="item-permissions">
                             <div class="title">{{ funct.id }} - {{ funct.name }}</div>
-                            <mat-slide-toggle>Permitir exportar</mat-slide-toggle>
+                            <mat-slide-toggle [checked]="exportPermissions.get(funct.id)" (change)="permissionsChange($event, funct.id, funct.children)">
+                              Permitir exportar
+                            </mat-slide-toggle>
                           </div>
-                          <div class="form-column mt-4 pl-16" *ngIf="funct.children != undefined && funct.children.length > 0" style="box-sizing: border-box;">
+                          <div class="form-column mt-4 pl-32" *ngIf="funct.children != undefined && funct.children.length > 0" style="box-sizing: border-box;">
                             <div class="form-row">
                               <div class="form-cell C12" *ngFor="let screen of funct.children">
                                 <div class="item-permissions">
                                   <div class="title">{{ screen.id }} - {{ screen.name }}</div>
-                                  <mat-slide-toggle>Permitir exportar</mat-slide-toggle>
+                                  <mat-slide-toggle [checked]="exportPermissions.get(screen.id)" (change)="permissionsChange($event, screen.id, undefined)">
+                                    Permitir exportar
+                                  </mat-slide-toggle>
                                 </div>
                               </div>
                             </div>
@@ -66,5 +74,10 @@ items_container_full_width: screenSize <= 640 }">
         </div>
       </div>
     </mat-card-content>
+    <mat-card-actions align="end">
+      <button mat-button (click)="saveExportPermissions()">
+        Guardar permisos
+      </button>
+    </mat-card-actions>
   </mat-card>
 </div>

+ 111 - 3
sistema-mantenimiento-front/src/app/components/control-panel/data-export-selection/data-election-config/data-election-config.component.ts

@@ -1,8 +1,10 @@
 import { Component, OnInit } from '@angular/core';
 import { ActivatedRoute } from '@angular/router';
 import { lastValueFrom } from 'rxjs';
+import { ProfileExportPermissionsListItem, ProfileExportPermissionsListResponse } from 'src/app/interfaces/control-panel.interface';
 import { PermissionsInterface } from 'src/app/interfaces/permissions.interface';
 import { ProfileInterface } from 'src/app/interfaces/profile.interface';
+import { ControlPanelService } from 'src/app/services/control-panel.service';
 import { EncService } from 'src/app/services/enc/enc.service';
 import { ResourcesService } from 'src/app/services/resources/resources.service';
 import { UsersProfilesService } from 'src/app/services/users-profiles.service';
@@ -17,23 +19,28 @@ export class DataElectionConfigComponent implements OnInit {
   isLoading: boolean;
   hasError: boolean;
   errorStr: string;
+  idProfile: string;
 
   permissions: PermissionsInterface[];
   exportPermissions: Map<string, boolean>;
+  profileExportPermissions: ProfileExportPermissionsListItem[];
 
   constructor(
     private _activatedRoute: ActivatedRoute,
     private _resourcesService: ResourcesService,
     private _usersProfilesService: UsersProfilesService,
     private _encService: EncService,
+    private _controlPanelService: ControlPanelService,
   ) {
     this.screenSize = window.innerWidth;
     this.isLoading = true;
     this.hasError = false;
     this.errorStr = '';
+    this.idProfile = '';
 
     this.permissions = [];
     this.exportPermissions = new Map();
+    this.profileExportPermissions = [];
   }
 
   ngOnInit(): void {
@@ -43,11 +50,50 @@ export class DataElectionConfigComponent implements OnInit {
         this._resourcesService.openSnackBar('No se envió información.');
         this.goBack(1);
       }else{
-        this.getProfile(idProfile);
+        this.idProfile = idProfile;
+        this.getProfileExportPermissions(idProfile);
       }
     });
   }
 
+  async getProfileExportPermissions(idProfile: string){
+    try{
+      let idUser = localStorage.getItem('idusuario')!;
+      let profilePermissions: ProfileExportPermissionsListResponse = await lastValueFrom(this._controlPanelService.getProfileExportPermissions(
+        idProfile,
+        idUser,
+        1
+      ));
+
+      this.hasError = profilePermissions.error;
+      this.errorStr = profilePermissions.msg;
+
+      if(this.hasError){
+        this.isLoading = false;
+      }else{
+        let profilePermissionsArr: ProfileExportPermissionsListItem[] = [];
+        for(const permission of profilePermissions.response){
+          permission.id = await this._encService.decrypt(permission.id);
+          profilePermissionsArr.push(permission);
+        }
+
+        this.profileExportPermissions = profilePermissionsArr;
+        this.getProfile(idProfile);
+      }
+    }catch(error: any){
+      if(error.error == undefined){
+        this.errorStr = 'Ocurrió un error inesperado.';
+      }else if(error.error.msg == undefined){
+        this.errorStr = 'Ocurrió un error inesperado.';
+      }else{
+        this.errorStr = error.error.msg;
+      }
+
+      this.hasError = true;
+      this.isLoading = false;
+    }
+  }
+
   goBack(steps: number){
     window.history.go(steps * -1);
   }
@@ -76,8 +122,14 @@ export class DataElectionConfigComponent implements OnInit {
           }
 
           if(permission.access > 0){
+            let exportAccess = false;
+            let accessFilt = this.profileExportPermissions.filter(item => item.id == permission.id);
+            if(accessFilt.length > 0){
+              exportAccess = accessFilt[0].value;
+            }
+            
             permissionsArr.push(permission);
-            this.exportPermissions.set(permission.id, false);
+            this.exportPermissions.set(permission.id, exportAccess);
           }
         }
 
@@ -99,7 +151,7 @@ export class DataElectionConfigComponent implements OnInit {
     }
   }
 
-  async processChildren(children: PermissionsInterface[]): Promise<PermissionsInterface[]>{
+  private async processChildren(children: PermissionsInterface[]): Promise<PermissionsInterface[]>{
     let childrenFn: PermissionsInterface[] = [];
     for(const permission of children){
       permission.id = await this._encService.decrypt(permission.id);
@@ -111,10 +163,66 @@ export class DataElectionConfigComponent implements OnInit {
       }
       
       if(permission.access > 0){
+        let exportAccess = false;
+        let accessFilt = this.profileExportPermissions.filter(item => item.id == permission.id);
+        if(accessFilt.length > 0){
+          exportAccess = accessFilt[0].value;
+        }
+        
         childrenFn.push(permission);
+        this.exportPermissions.set(permission.id, exportAccess);
       }
     }
     
     return childrenFn;
   }
+
+  permissionsChange(event: any, id: string, children: PermissionsInterface[] | undefined){
+    this.exportPermissions.set(id, event.checked);
+    if(children != undefined){
+      this.changeChildrenCheckStatus(children, event.checked);
+    }
+  }
+
+  private changeChildrenCheckStatus(children: PermissionsInterface[], status: boolean){
+    for(const child of children){
+      this.exportPermissions.set(child.id, status);
+      if(child.children != undefined && child.children.length > 0){
+        this.changeChildrenCheckStatus(child.children, status);
+      }
+    }
+  }
+
+  async saveExportPermissions(){
+    try{
+      let idUser = localStorage.getItem('idusuario')!;
+      let profileExportPermissionsObj: any = {};
+
+      for(const [key, value] of this.exportPermissions){
+        profileExportPermissionsObj[key.trim()] = value;
+      }
+
+      let profileExportPermissionsStr = JSON.stringify(profileExportPermissionsObj);
+      let profileExportPermissionsEnc = await this._encService.encrypt(profileExportPermissionsStr);
+      let formData = new FormData();
+
+      formData.append('id_user', idUser);
+      formData.append('linea', '1');
+      formData.append('permissions', profileExportPermissionsEnc);
+      formData.append('id_profile', this.idProfile);
+
+      await lastValueFrom(this._controlPanelService.saveProfileExportPermissions(formData));
+
+      this._resourcesService.openSnackBar('Los permisos se actualizaron correctamente.');
+      this.goBack(1);
+    }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);
+      }
+    }
+  }
 }

+ 11 - 0
sistema-mantenimiento-front/src/app/interfaces/control-panel.interface.ts

@@ -95,4 +95,15 @@ export interface BroadcastListUsers{
   NOMBRE_USUARIO: string;
   ID_PERFIL: string;
   NOMBRE_PERFIL: string;
+}
+
+export interface ProfileExportPermissionsListResponse{
+  error: boolean;
+  msg: string;
+  response: ProfileExportPermissionsListItem[];
+}
+
+export interface ProfileExportPermissionsListItem{
+  id: string;
+  value: boolean;
 }

+ 8 - 0
sistema-mantenimiento-front/src/app/services/control-panel.service.ts

@@ -34,6 +34,10 @@ export class ControlPanelService {
     return this.getQuery(`control-panel/get-broadcast-list/${idList}/${idUser}/${line}`).pipe(map((data: any) => data));
   }
 
+  getProfileExportPermissions(idProfile: string, idUser: string, line: number){
+    return this.getQuery(`control-panel/get-profile-export-permissions/${idProfile}/${idUser}/${line}`).pipe(map((data: any) => data));
+  }
+
   registerPanel(body: any){
     return this.postQuery("control-panel/register-panel", body).pipe(map((data: any) => data));
   }
@@ -66,6 +70,10 @@ export class ControlPanelService {
     return this.postQuery("control-panel/delete-broadcast-list", body).pipe(map((data: any) => data));
   }
 
+  saveProfileExportPermissions(body: any){
+    return this.postQuery("control-panel/save-profile-export-permissions", body).pipe(map((data: any) => data));
+  }
+
   getQuery(query: string){
     const JWT = `Bearer ${localStorage.getItem('token')}`;
     const URL = `${apiTemp}${query}`;