From a04a36c067cd30efd01780217d93b53dff02fe24 Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Tue, 12 Jan 2021 12:24:54 +0100 Subject: [PATCH] feat: add admin validation for structure claim --- src/app/admin/admin.module.ts | 4 +- .../claim-structure.component.html} | 0 .../claim-structure.component.scss} | 0 .../claim-structure.component.spec.ts | 24 ++++++++++ .../claim-structure.component.ts | 43 ++++++++++++++++++ .../components/panel/panel.component.html | 4 +- .../validation-attachment.component.spec.ts | 25 ----------- .../validation-attachment.component.ts | 44 ------------------- .../admin/models/demandAttachment.model.ts | 2 +- src/app/admin/services/admin.service.ts | 17 ++++--- src/app/header/header.component.html | 1 + src/app/header/header.component.ts | 7 ++- src/app/menu-phone/menu-phone.component.html | 11 +++++ src/app/menu-phone/menu-phone.component.ts | 12 ++++- src/app/profile/profile.component.html | 9 +++- src/app/profile/services/profile.service.ts | 4 +- 16 files changed, 121 insertions(+), 86 deletions(-) rename src/app/admin/components/{validation-attachment/validation-attachment.component.html => claim-structure/claim-structure.component.html} (100%) rename src/app/admin/components/{validation-attachment/validation-attachment.component.scss => claim-structure/claim-structure.component.scss} (100%) create mode 100644 src/app/admin/components/claim-structure/claim-structure.component.spec.ts create mode 100644 src/app/admin/components/claim-structure/claim-structure.component.ts delete mode 100644 src/app/admin/components/validation-attachment/validation-attachment.component.spec.ts delete mode 100644 src/app/admin/components/validation-attachment/validation-attachment.component.ts diff --git a/src/app/admin/admin.module.ts b/src/app/admin/admin.module.ts index 6c3f81f57..80f80da23 100644 --- a/src/app/admin/admin.module.ts +++ b/src/app/admin/admin.module.ts @@ -1,11 +1,11 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { PanelComponent } from './components/panel/panel.component'; -import { ValidationAttachmentComponent } from './components/validation-attachment/validation-attachment.component'; +import { ClaimStructureComponent } from './components/claim-structure/claim-structure.component'; import { SharedModule } from '../shared/shared.module'; @NgModule({ - declarations: [PanelComponent, ValidationAttachmentComponent], + declarations: [PanelComponent, ClaimStructureComponent], imports: [CommonModule, SharedModule], }) export class AdminModule {} diff --git a/src/app/admin/components/validation-attachment/validation-attachment.component.html b/src/app/admin/components/claim-structure/claim-structure.component.html similarity index 100% rename from src/app/admin/components/validation-attachment/validation-attachment.component.html rename to src/app/admin/components/claim-structure/claim-structure.component.html diff --git a/src/app/admin/components/validation-attachment/validation-attachment.component.scss b/src/app/admin/components/claim-structure/claim-structure.component.scss similarity index 100% rename from src/app/admin/components/validation-attachment/validation-attachment.component.scss rename to src/app/admin/components/claim-structure/claim-structure.component.scss diff --git a/src/app/admin/components/claim-structure/claim-structure.component.spec.ts b/src/app/admin/components/claim-structure/claim-structure.component.spec.ts new file mode 100644 index 000000000..1603383da --- /dev/null +++ b/src/app/admin/components/claim-structure/claim-structure.component.spec.ts @@ -0,0 +1,24 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ClaimStructureComponent } from './claim-structure.component'; + +describe('ClaimStructureComponent', () => { + let component: ClaimStructureComponent; + let fixture: ComponentFixture<ClaimStructureComponent>; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ClaimStructureComponent], + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(ClaimStructureComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/admin/components/claim-structure/claim-structure.component.ts b/src/app/admin/components/claim-structure/claim-structure.component.ts new file mode 100644 index 000000000..5b8f854be --- /dev/null +++ b/src/app/admin/components/claim-structure/claim-structure.component.ts @@ -0,0 +1,43 @@ +import { Component, OnInit } from '@angular/core'; +import { DemandAttachment } from '../../models/demandAttachment.model'; +import { AdminService } from '../../services/admin.service'; + +@Component({ + selector: 'app-admin-claim-structure', + templateUrl: './claim-structure.component.html', + styleUrls: ['./claim-structure.component.scss'], +}) +export class ClaimStructureComponent implements OnInit { + public demandsAttachment: DemandAttachment[]; + constructor(private adminService: AdminService) {} + + ngOnInit(): void { + this.adminService.getPendingStructure().subscribe((demands) => { + this.demandsAttachment = demands; + }); + } + + // Todo : Appeler removeDemand(demand) dans le subscribe de acceptAttachmentStructure() quand l'api sera faite. + public acceptDemand(demand: DemandAttachment): void { + console.log('accept'); + this.adminService.acceptStructureClaim(demand.userEmail, demand.structureId).subscribe((data) => { + this.demandsAttachment = data; + }); + } + + // Todo : Appeler removeDemand(demand) dans le subscribe de acceptAttachmentStructure() quand l'api sera faite. + public refuseDemand(demand: DemandAttachment): void { + console.log('refuse'); + this.adminService.refuseStructureClaim(demand.userEmail, demand.structureId).subscribe((data) => { + this.demandsAttachment = data; + }); + } + + // Remove the request that was accepted or refused + // private removeDemand(demand: DemandAttachment): void { + // const index = this.demandsAttachment.findIndex((d: DemandAttachment) => d === demand); + // if (index > -1) { + // this.demandsAttachment.splice(index, 1); + // } + // } +} diff --git a/src/app/admin/components/panel/panel.component.html b/src/app/admin/components/panel/panel.component.html index 34dc8aa94..163961e48 100644 --- a/src/app/admin/components/panel/panel.component.html +++ b/src/app/admin/components/panel/panel.component.html @@ -1,4 +1,4 @@ <div fxLayout="column" class="content-container"> - <div fxLayout="row" fxLayoutAlign="center center"><h1>Panel d'administration</h1></div> - <app-admin-validation-attachment></app-admin-validation-attachment> + <div fxLayout="row" fxLayoutAlign="center center"><h1>Administration</h1></div> + <app-admin-claim-structure></app-admin-claim-structure> </div> diff --git a/src/app/admin/components/validation-attachment/validation-attachment.component.spec.ts b/src/app/admin/components/validation-attachment/validation-attachment.component.spec.ts deleted file mode 100644 index c37036540..000000000 --- a/src/app/admin/components/validation-attachment/validation-attachment.component.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { ValidationAttachmentComponent } from './validation-attachment.component'; - -describe('ValidationAttachmentComponent', () => { - let component: ValidationAttachmentComponent; - let fixture: ComponentFixture<ValidationAttachmentComponent>; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [ ValidationAttachmentComponent ] - }) - .compileComponents(); - }); - - beforeEach(() => { - fixture = TestBed.createComponent(ValidationAttachmentComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); diff --git a/src/app/admin/components/validation-attachment/validation-attachment.component.ts b/src/app/admin/components/validation-attachment/validation-attachment.component.ts deleted file mode 100644 index e7d1ff21f..000000000 --- a/src/app/admin/components/validation-attachment/validation-attachment.component.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { userInfo } from 'os'; -import { Structure } from '../../../models/structure.model'; -import { User } from '../../../models/user.model'; -import { demandAttachment } from '../../models/demandAttachment.model'; -import { AdminService } from '../../services/admin.service'; - -@Component({ - selector: 'app-admin-validation-attachment', - templateUrl: './validation-attachment.component.html', - styleUrls: ['./validation-attachment.component.scss'], -}) -export class ValidationAttachmentComponent implements OnInit { - demandsAttachment: demandAttachment[]; - constructor(private adminService: AdminService) {} - - ngOnInit(): void { - this.adminService.getPendingAttachmentsStructure().subscribe((demands) => { - this.demandsAttachment = demands; - }); - } - - // Todo : Appeler removeDemand(demand) dans le subscribe de acceptAttachmentStructure() quand l'api sera faite. - public acceptDemand(demand: demandAttachment): void { - console.log('accept'); - this.adminService.acceptAttachmentStructure(demand.userEmail, demand.structureId); - this.removeDemand(demand); - } - - // Todo : Appeler removeDemand(demand) dans le subscribe de acceptAttachmentStructure() quand l'api sera faite. - public refuseDemand(demand: demandAttachment): void { - console.log('refuse'); - this.adminService.refuseAttachmentStructure(demand.userEmail, demand.structureId); - this.removeDemand(demand); - } - - // Remove the request that was accepted or refused - private removeDemand(demand: demandAttachment): void { - const index = this.demandsAttachment.findIndex((d: demandAttachment) => d === demand); - if (index > -1) { - this.demandsAttachment.splice(index, 1); - } - } -} diff --git a/src/app/admin/models/demandAttachment.model.ts b/src/app/admin/models/demandAttachment.model.ts index a7fb8378f..b19d1f375 100644 --- a/src/app/admin/models/demandAttachment.model.ts +++ b/src/app/admin/models/demandAttachment.model.ts @@ -1,4 +1,4 @@ -export class demandAttachment { +export class DemandAttachment { userEmail: string; structureId: number; } diff --git a/src/app/admin/services/admin.service.ts b/src/app/admin/services/admin.service.ts index 4b50fd04a..0167109c5 100644 --- a/src/app/admin/services/admin.service.ts +++ b/src/app/admin/services/admin.service.ts @@ -1,22 +1,25 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; -import { demandAttachment } from '../models/demandAttachment.model'; +import { DemandAttachment } from '../models/demandAttachment.model'; @Injectable({ providedIn: 'root', }) export class AdminService { + private readonly baseUrl = 'api/admin'; constructor(private http: HttpClient) {} // Return pendingAttachments of all profiles. - public getPendingAttachmentsStructure(): Observable<demandAttachment[]> { - return this.http.get<any>('api/users/pendingAttachments'); + public getPendingStructure(): Observable<DemandAttachment[]> { + return this.http.get<DemandAttachment[]>(`${this.baseUrl}/pendingStructures`); } - // Todo : Api post qui retourne vrai si aucune erreur sinon httpException - public acceptAttachmentStructure(mailUser, idStructure) {} + public acceptStructureClaim(userEmail: string, structureId: number): Observable<DemandAttachment[]> { + return this.http.post<DemandAttachment[]>(`${this.baseUrl}/validatePendingStructure`, { userEmail, structureId }); + } - // Todo : Api post qui retourne vrai si aucune erreur sinon httpException - public refuseAttachmentStructure(mailUser, idStructure) {} + public refuseStructureClaim(userEmail: string, structureId: number): Observable<DemandAttachment[]> { + return this.http.post<DemandAttachment[]>(`${this.baseUrl}/rejectPendingStructure`, { userEmail, structureId }); + } } diff --git a/src/app/header/header.component.html b/src/app/header/header.component.html index 863bf211b..07dc562c9 100644 --- a/src/app/header/header.component.html +++ b/src/app/header/header.component.html @@ -20,6 +20,7 @@ <span class="body"></span> </span> </a> + <a *ngIf="isAdmin" routerLink="/admin" [routerLinkActive]="'active'">Administration</a> <a *ngIf="isLoggedIn" routerLink="/profile" [routerLinkActive]="'active'" fxLayout="row" fxLayoutGap="1.5vh"> <app-svg-icon [type]="'ico'" [iconClass]="'icon-32'" [icon]="'user'" [iconColor]="'currentColor'"></app-svg-icon> </a> diff --git a/src/app/header/header.component.ts b/src/app/header/header.component.ts index cceb2dcf1..22e7fc842 100644 --- a/src/app/header/header.component.ts +++ b/src/app/header/header.component.ts @@ -1,4 +1,5 @@ import { Component, OnInit } from '@angular/core'; +import { ProfileService } from '../profile/services/profile.service'; import { AuthService } from '../services/auth.service'; @Component({ @@ -11,7 +12,7 @@ export class HeaderComponent implements OnInit { public isPopUpOpen = false; public displaySignUp = true; - constructor(private authService: AuthService) {} + constructor(private authService: AuthService, private profileService: ProfileService) {} ngOnInit(): void {} public openMenu(): void { @@ -41,4 +42,8 @@ export class HeaderComponent implements OnInit { this.isPopUpOpen = false; } } + + public get isAdmin(): boolean { + return this.profileService.isAdmin(); + } } diff --git a/src/app/menu-phone/menu-phone.component.html b/src/app/menu-phone/menu-phone.component.html index dbd120b9f..407d164c3 100644 --- a/src/app/menu-phone/menu-phone.component.html +++ b/src/app/menu-phone/menu-phone.component.html @@ -13,12 +13,23 @@ <!-- <a routerLink="/resources" [routerLinkActive]="'active'" i18n>Ressources</a> <a routerLink="/projects" [routerLinkActive]="'active'" i18n>Projets</a> --> <a routerLink="/about" [routerLinkActive]="'active'" (click)="closeMenu()" i18n>Qui sommes-nous ?</a> + + <a *ngIf="isAdmin" routerLink="/admin" [routerLinkActive]="'active'">Administration</a> <!--<a routerLink="/login" [routerLinkActive]="'active'" i18n><span class="clickable ico-mglass purple"></span></a> <a routerLink="/home" [routerLinkActive]="'active'" i18n ><span class="ico-profile" fxLayout="column" fxLayoutAlign="center center"> <span class="head"></span> <span class="body"></span> </span ></a> --> + <a *ngIf="isLoggedIn" routerLink="/profile" [routerLinkActive]="'active'" fxLayout="row" fxLayoutGap="1.5vh"> + <app-svg-icon + [type]="'ico'" + [iconClass]="'icon-32'" + [icon]="'user'" + [iconColor]="'currentColor'" + ></app-svg-icon> + </a> + <button *ngIf="isLoggedIn" (click)="logout()">Logout</button> </div> </div> <!--<div class="footer" fxLayout="row" fxLayoutGap="1.5vh"> diff --git a/src/app/menu-phone/menu-phone.component.ts b/src/app/menu-phone/menu-phone.component.ts index c0d48f25e..a0276b4c6 100644 --- a/src/app/menu-phone/menu-phone.component.ts +++ b/src/app/menu-phone/menu-phone.component.ts @@ -1,4 +1,6 @@ import { Component, EventEmitter, OnInit, Output } from '@angular/core'; +import { ProfileService } from '../profile/services/profile.service'; +import { AuthService } from '../services/auth.service'; @Component({ selector: 'app-menu-phone', @@ -6,7 +8,7 @@ import { Component, EventEmitter, OnInit, Output } from '@angular/core'; styleUrls: ['./menu-phone.component.scss'], }) export class MenuPhoneComponent implements OnInit { - constructor() {} + constructor(private authService: AuthService, private profileService: ProfileService) {} @Output() closeEvent = new EventEmitter(); ngOnInit(): void {} @@ -14,4 +16,12 @@ export class MenuPhoneComponent implements OnInit { closeMenu(): void { this.closeEvent.emit(); } + + public get isLoggedIn(): boolean { + return this.authService.isLoggedIn(); + } + + public get isAdmin(): boolean { + return this.profileService.isAdmin(); + } } diff --git a/src/app/profile/profile.component.html b/src/app/profile/profile.component.html index 51293a253..c80a5534f 100644 --- a/src/app/profile/profile.component.html +++ b/src/app/profile/profile.component.html @@ -4,13 +4,18 @@ <div *ngIf="userProfile" fxLayout="column" fxLayoutAlign="center" fxLayoutGap="10px"> <p>Id: {{ userProfile._id }}</p> <p>Email: {{ userProfile.email }}</p> - <p> + <p fxLayout="column" *ngIf="userProfile.structuresLink.length > 0"> Mes structures : <span *ngFor="let structureId of userProfile.structuresLink"> <strong>{{ structureId }}</strong> </span> </p> - <button *ngIf="profileService.isAdmin()" routerLink="/admin">Accèder au panel d'administration</button> + <p fxLayout="column" *ngIf="userProfile.pendingStructuresLink.length > 0"> + Mes structures en attente de validation: + <span *ngFor="let structureId of userProfile.pendingStructuresLink"> + <strong>{{ structureId }}</strong> + </span> + </p> <button (click)="toogleAddStructure()">Ajouter une structure</button> <button (click)="toogleChangeEmail()">Changer d'email</button> <form diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts index 38679bbba..41449912b 100644 --- a/src/app/profile/services/profile.service.ts +++ b/src/app/profile/services/profile.service.ts @@ -9,7 +9,9 @@ import { User } from '../../models/user.model'; export class ProfileService { private readonly baseUrl = 'api/users'; private currentProfile: User = null; - constructor(private http: HttpClient) {} + constructor(private http: HttpClient) { + this.getProfile(); + } public async getProfile(): Promise<User> { // Get profil by API only on first time -- GitLab