Skip to content

Commit

Permalink
refactor: User types
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Feb 15, 2024
1 parent 05935ee commit 6ac6d7a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="h-100 d-flex flex-column">
<app-nav-bar *ngIf="authService.currentLoginUser.role !== null"></app-nav-bar>
<app-nav-bar></app-nav-bar>
<div class="flex-grow-1 overflow-auto">
<router-outlet ></router-outlet>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/app/audit/pre-audit/pre-audit.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ <h2 ngbAccordionHeader>
[routerLink]="[audit.auditId]" routerLinkActive="active text-white">
<div class="flex-grow-1">
{{ audit.auditName | titlecase }}
@if (authService.currentLoginUser.role === 'superAdmin' && audit.user.role.role !== 'superAdmin') {
({{ audit.user.userName | titlecase }})
@if (authService.currentLoginUser?.role === 'superAdmin' && audit.user?.role?.role !== 'superAdmin') {
({{ audit.user?.userName | titlecase }})
}
</div>
<app-option-dropdown class="visible-hover">
<button ngbDropdownItem (click)="rename(section.key, audit)">Rename</button>
<div class="dropdown-divider"></div>
@if ((authService.currentLoginUser.role === 'admin' || authService.currentLoginUser.role === 'superAdmin') && audit.user.role.role !== 'guest') {
@if ((authService.currentLoginUser?.role === 'admin' || authService.currentLoginUser?.role === 'superAdmin') && audit.user?.role?.role !== 'guest') {
<button ngbDropdownItem (click)="openAddDataCollectorModal(audit)">
Add DataCollector
</button>
<div class="dropdown-divider"></div>
}
@if (authService.currentLoginUser.role !== 'dataCollector' || (audit.user.role.role !== 'admin' && audit.user.role.role !== 'superAdmin')) {
@if (authService.currentLoginUser?.role !== 'dataCollector' || (audit.user?.role?.role !== 'admin' && audit.user?.role?.role !== 'superAdmin')) {
<button ngbDropdownItem class="text-danger" (click)="delete(section.key, audit)">
Delete
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/app/audit/pre-audit/pre-audit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class PreAuditComponent implements OnInit {
}

ngOnInit(): void {
if (this.authService.currentLoginUser.role === 'dataCollector') {
if (this.authService.currentLoginUser?.role === 'dataCollector') {
this.auditService.getAllDataCollectorAudit().subscribe(res => {
this.groupAudits(res);
});
Expand Down
6 changes: 3 additions & 3 deletions src/app/settings/setting.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div ngbAccordionItem *ngFor="let auditGroup of auditsGrouped; let i = index" id="{{ 'auditPanel' + i }}">
<h2 ngbAccordionHeader>
{{ auditGroup.auditName | titlecase }}
<div *ngIf="authService.currentLoginUser.role === 'superAdmin'">
<div *ngIf="authService.currentLoginUser?.role === 'superAdmin'">
<div *ngFor="let audit of auditGroup.audits">
&nbsp;
({{ audit.admin_name| titlecase }})
Expand All @@ -12,7 +12,7 @@ <h2 ngbAccordionHeader>
<div ngbAccordionCollapse>
<div ngbAccordionBody class="mb-3">
<div class="label marked-rows" style="color : white">
<div *ngIf="authService.currentLoginUser.role === 'superAdmin'">
<div *ngIf="authService.currentLoginUser?.role === 'superAdmin'">
<div *ngFor="let audit of auditGroup.audits">
<ul>
<li *ngFor="let dataCollector of audit.data_collectors">
Expand All @@ -21,7 +21,7 @@ <h2 ngbAccordionHeader>
</ul>
</div>
</div>
<div *ngIf="authService.currentLoginUser.role === 'admin'">
<div *ngIf="authService.currentLoginUser?.role === 'admin'">
<ul>
<li *ngFor="let audit of auditGroup.audits">
{{ audit.assignedTo | titlecase}}
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/components/nav-bar/nav-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class NavBarComponent {
logout(): void {
this.authService.logout().subscribe((res: any) => {
localStorage.clear();
this.authService.currentLoginUser = {};
this.authService.currentLoginUser = undefined;
this.router.navigate(['/auth/login']);
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/shared/model/audit.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {PreAuditData} from './pre-audit-data.interface';
import {UserWithRoleObject} from "./user.interface";

export interface Audit {
auditId: number;
auditCreatedOn: string;
auditName: string;
deleteStatus: false;
userId: number;
user?: any; // TODO User
user?: UserWithRoleObject;
pre_audit_form: PreAuditData;
}
15 changes: 15 additions & 0 deletions src/app/shared/model/user.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface User {
id: number;
userName: string;
email: string;
roleId: number;
expirationDate: string | null;
}

export interface UserWithRoleName extends User {
role: string;
}

export interface UserWithRoleObject extends User {
role: { roleId: number; role: string; };
}
17 changes: 9 additions & 8 deletions src/app/shared/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment.prod';
import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {environment} from 'src/environments/environment.prod';
import {UserWithRoleName} from "../model/user.interface";

@Injectable({
providedIn: 'root'
})
export class AuthService {

rootUrl = environment.url + 'authApi/v1/';
currentLoginUser: any = {};
currentLoginUser?: UserWithRoleName;

constructor(private http: HttpClient,
) { }
Expand All @@ -22,8 +23,8 @@ export class AuthService {
return this.http.get(`${this.rootUrl}all-states/`);
}

getUser(): Observable<any> {
return this.http.get(`${this.rootUrl}user/`);
getUser(): Observable<UserWithRoleName> {
return this.http.get<UserWithRoleName>(`${this.rootUrl}user/`);
}

signUp(data: any): Observable<any> {
Expand All @@ -45,4 +46,4 @@ export class AuthService {
changePassword(data: any): Observable<any> {
return this.http.post(`${this.rootUrl}change-password/`, data);
}
}
}

0 comments on commit 6ac6d7a

Please sign in to comment.