Skip to content

Commit

Permalink
Merge pull request #21 from vaimee/refactor-organization
Browse files Browse the repository at this point in the history
Refactor Organization Service
  • Loading branch information
relu91 authored Jul 26, 2023
2 parents cab6f1f + 6b7cc1b commit 500e548
Show file tree
Hide file tree
Showing 24 changed files with 487 additions and 459 deletions.
3 changes: 1 addition & 2 deletions frontend/src/app/auth/guards/manager.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export const managerGuard = () => {
switchMap(() => {
const url = window.location.pathname;
const organizationAddress = url.split('/')[2];
organizationService.setTarget(organizationAddress);
return organizationService.isManager(metamaskService.userAddress);
return organizationService.isManager(metamaskService.userAddress, organizationAddress);
}),
switchMap(isManager => (isManager ? of(true) : of(location.back()))),
catchError(error => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export class IssueCertificateComponent implements OnInit, OnDestroy {

ngOnInit(): void {
this.loadForm();
this.organizationService.setTarget(this.organizationAddress);
this.memberChip.items$ = this.organizationService.getMembers();
this.memberChip.items$ = this.organizationService.getMembers(this.organizationAddress);
this.memberChip.items$.subscribe(members => {
this.memberChip.all = members;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span matListItemTitle>{{ organization.name }}</span>
<span matListItemLine>{{ organization.headline }}</span>
<p matListItemLine>
Bologna, Italy &#8226; <mat-icon class="line-icon" fontIcon="auto_awesome_mosaic"></mat-icon>
<mat-icon class="line-icon" fontIcon="auto_awesome_mosaic"></mat-icon>
{{ organization.projectCount }} &#8226; <mat-icon class="line-icon" fontIcon="group"></mat-icon>
{{ getOrganizationMembers(organization) }}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export class OrganizationComponent implements OnInit {
this.loadingService.waitForObservables([this.organization$]);
this.pageNotFoundCheck(this.organization$);

this.projects$ = this.organizationService.getProjects();
this.isMember$ = this.organizationService.isCurrentUserMember();
this.isPendingMember$ = this.organizationService.isCurrentUserPendingMember();
this.isManager$ = this.organizationService.isCurrentUserManager();
this.isOwner$ = this.organizationService.isCurrentUserOwner();
this.projects$ = this.organizationService.getProjects(this.organizationAddress);
this.isMember$ = this.organizationService.isCurrentUserMember(this.organizationAddress);
this.isPendingMember$ = this.organizationService.isCurrentUserPendingMember(this.organizationAddress);
this.isManager$ = this.organizationService.isCurrentUserManager(this.organizationAddress);
this.isOwner$ = this.organizationService.isCurrentUserOwner(this.organizationAddress);

this.userId$ = this.profileService.getUserId();
this.userRole$ = this.getUserRole();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,23 @@ export class ShowMembersComponent implements OnInit {
}
public isMemberManager(address: string): Observable<boolean> {
if (this.areMembersManager[address]) return this.areMembersManager[address];
this.areMembersManager[address] = this.organizationService.isManager(address);
this.areMembersManager[address] = this.organizationService.isManager(address, this.organizationAddress);
return this.areMembersManager[address];
}

private dialogSetUp() {
this.organizationService.setTarget(this.organizationAddress);
switch (this.memberType) {
case 'member':
this.members$ = this.organizationService.getMembers();
this.members$ = this.organizationService.getMembers(this.organizationAddress);
this.title = 'Member';
return;
case 'manager':
this.members$ = this.organizationService.getManagers();
this.owner$ = this.organizationService.getOwnerAddress();
this.members$ = this.organizationService.getManagers(this.organizationAddress);
this.owner$ = this.organizationService.getOwnerAddress(this.organizationAddress);
this.title = 'Manager';
return;
case 'pendingMember':
this.members$ = this.organizationService.getPendingMembers();
this.members$ = this.organizationService.getPendingMembers(this.organizationAddress);
this.title = 'Pending Member';
return;
default:
Expand All @@ -78,7 +77,7 @@ export class ShowMembersComponent implements OnInit {
public promoteToManager(memberAddress: string) {
this.loadingService.show();
if (this.memberType === 'member')
return this.organizationService.promoteToManager(memberAddress).subscribe({
return this.organizationService.promoteToManager(memberAddress, this.organizationAddress).subscribe({
next: () => this.handleObservable('member promoted'),
error: err => this.handleObservable('failed to promote member', err),
});
Expand All @@ -89,7 +88,7 @@ export class ShowMembersComponent implements OnInit {
public approvePendingMember(profileId: string) {
this.loadingService.show();
if (this.memberType === 'pendingMember')
return this.organizationService.approvePendingMember(Number(profileId)).subscribe({
return this.organizationService.approvePendingMember(Number(profileId), this.organizationAddress).subscribe({
next: () => this.handleObservable('member approved'),
error: err => this.handleObservable('failed to approve member', err),
});
Expand All @@ -100,7 +99,7 @@ export class ShowMembersComponent implements OnInit {
public remove(profileId: string) {
this.loadingService.show();
if (this.memberType === 'pendingMember')
return this.organizationService.rejectPendingMember(Number(profileId)).subscribe({
return this.organizationService.rejectPendingMember(Number(profileId), this.organizationAddress).subscribe({
next: () => this.handleObservable('pending member removed'),
error: err => this.handleObservable('failed to remove pending member', err),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +0,0 @@
.certificateCardBlock {
position: relative;
}
.certificateCardBlock .mat-icon {
width: 60px;
height: 60px;
line-height: 60px;
font-size: 40px;
background: #f9f9f9;
border-radius: 50%;
text-align: center;
}
.certificateCardBlock .certificatesItemIcon {
position: absolute;
top: 10px;
right: 10px;
}
.certificateCardBlock .certificatesItemIcon .mat-icon {
width: 30px;
height: 30px;
font-size: 20px;
background: #f9f9f9;
border-radius: 50%;
line-height: 30px;
text-align: center;
}

@media (min-width: 768px) and (max-width: 1024px) {
.certificateCardBlock .cards .d-flex {
display: block !important;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
<div class="certificateCardBlock p-4 bg-white skill-min-height">
<h2 class="d-flex justify-content-between align-items-center mb-3">Certificates</h2>
<ng-container *ngIf="certificates.length > 0; else noCertificates">
<mat-card
class="border p-3 cards bg-dark text-white rounded-3 mb-3 border-0"
*ngFor="let certificate of certificates">
<div class="d-flex align-items-center">
<div class="flex-shrink-0">
<mat-icon class="text-dark">{{ certificate?.image }}</mat-icon>
</div>
<div class="flex-grow-1 ms-3">
<h6>{{ certificate?.name }}</h6>
<small class="text-Light text-end d-block">{{ certificate?.date | date }} </small>
</div>
</div>
<div class="certificatesItemIcon">
<mat-icon class="text-black">{{ certificate?.image }}</mat-icon>
</div>
</mat-card>
<ng-container *ngIf="certificates$ | async as certificates">
<ng-container *ngIf="certificates.length > 0">
<div class="mt-4 bg-white p-4">
<h2 class="mb-3">Certificates</h2>
<mat-list>
<mat-list-item *ngFor="let certificate of certificates">
<img class="round-image" matListItemAvatar src="{{ certificate.image }}" alt="{{ certificate.name }} icon" />
<span matListItemTitle>{{ certificate.name }}</span>
<p matListItemLine>{{ certificate.description }}</p>
</mat-list-item>
</mat-list>
<ng-template #noCertificates> No certificates found </ng-template>
</div>
</ng-container>
<ng-template #noCertificates> No certificates found </ng-template>
</div>
</ng-container>
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Component, Input } from '@angular/core';
import { Observable, toArray } from 'rxjs';

import { Component, Input, OnInit } from '@angular/core';

import { Certificate } from '@shared/interfaces';
import { ProfileService } from '@shared/services/profile.service';

@Component({
selector: 'app-certificates',
templateUrl: './certificates.component.html',
styleUrls: ['./certificates.component.css'],
})
export class CertificatesComponent {
@Input() certificates!: Array<Certificate>;
export class CertificatesComponent implements OnInit {
@Input() profileId!: number;
certificates$!: Observable<Certificate[]>;

constructor(private profileService: ProfileService) {}
ngOnInit(): void {
this.certificates$ = this.profileService.getCertificates(this.profileId).pipe(toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export class LogHoursComponent implements OnInit, OnDestroy {

ngOnInit(): void {
this.loadForm();
this.organizationService.getOrganizationsOfProfile(7).subscribe(data => {
console.log(data);
});
this.affiliations$ = this.organizationService.getCurrentUserAffiliations();
}
reset() {
this.submitted = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ <h4>Organizations</h4>
</div>
<div class="cv">
<app-skills [profileId]="id"></app-skills>
<app-certificates [profileId]="id"></app-certificates>
<app-project-list [projects$]="projects$" [showOrganization]="true"></app-project-list>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, catchError, concat, distinct, map, of, switchMap } from 'rxjs';
import { Observable, catchError, map, of, switchMap } from 'rxjs';

import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
Expand All @@ -17,6 +17,7 @@ import { EndorseDialogComponent } from '@profiles/components/endorse-dialog/endo
import { EndorsersListComponent } from '@profiles/components/endorsers-list/endorsers-list.component';
import { EndorseDialogInterface } from '@profiles/interfaces/endorse-dialog-data.interface';
import { Profile } from '@profiles/interfaces/profile.interface';
import { organizations } from '@vaimee/my3sec-contracts/dist/contracts';

Check warning on line 20 in frontend/src/app/modules/profiles/components/profile-body/profile-body.component.ts

View workflow job for this annotation

GitHub Actions / lint

'organizations' is defined but never used
import { DataTypes } from '@vaimee/my3sec-contracts/dist/contracts/My3SecHub';

import { UpdateProfileComponent } from '../update-profile/update-profile.component';
Expand Down Expand Up @@ -96,24 +97,28 @@ export class ProfileBodyComponent implements OnInit {
})
);
});

this.projects$ = this.profileData$.pipe(
switchMap(profile => this.organizationService.getProjectsOfProfile(parseInt(profile.id)))
);

this.organizations$ = this.profileData$.pipe(
switchMap(profile =>
concat(
this.organizationService.getOrganizationsOfProfile(parseInt(profile.id)),
this.organizationService.getOrganizationsManagerOfProfile(parseInt(profile.id))
this.organizationService.getOrganizationsOfProfile(parseInt(profile.id)).pipe(
switchMap(organizationsAsMember =>
this.organizationService.getOrganizationsManagerOfProfile(parseInt(profile.id)).pipe(
map(organizationsAsManager => {
const allOrganizations = [...organizationsAsManager, ...organizationsAsMember];
return allOrganizations.filter(
(organization, index) =>
index === allOrganizations.findIndex(testedOrg => organization.address === testedOrg.address)
);
})
)
)
)
),
distinct()
)
);

this.profileData$
.pipe(switchMap(profile => this.my3secHubContractService.getCertificates(parseInt(profile.id))))
.subscribe(a => console.log(a));
this.loadingService.waitForObservables([this.profileData$, this.organizations$, this.projects$]);
}

loadDefaultProfile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export class CreateProjectComponent implements OnInit, OnDestroy {

ngOnInit(): void {
this.loadForm();
this.organizationService.setTarget(this.organizationAddress);
this.memberChip.items$ = this.organizationService.getMembers();
this.memberChip.items$ = this.organizationService.getMembers(this.organizationAddress);
this.memberChip.items$.subscribe(members => {
this.memberChip.all = members;
});
Expand Down Expand Up @@ -100,8 +99,8 @@ export class CreateProjectComponent implements OnInit, OnDestroy {

const createProject$ =
this.memberChip.selectedItems.length === 0
? this.organizationService.createProjectWithoutMembers(formValue)
: this.organizationService.createProject(formValue, this.memberChip.selectedItems);
? this.organizationService.createProjectWithoutMembers(formValue, this.organizationAddress)
: this.organizationService.createProject(formValue, this.memberChip.selectedItems, this.organizationAddress);

createProject$.subscribe({
next: projectId => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export class ProjectListComponent implements OnInit {
}
ngOnInit(): void {
if (this.projects$ === undefined) {
this.organizationService.setTarget(this.organizationAddress);
this.projects$ = this.organizationService.getProjects();
this.projects$ = this.organizationService.getProjects(this.organizationAddress);
}
this.loadingService.waitForObservables([this.projects$]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ export class ProjectComponent implements OnInit {
}

public setUp() {
this.organizationService.setTarget(this.organizationAddress);
this.project$ = this.organizationService.getProject(this.projectId);
this.project$ = this.organizationService.getProject(this.projectId, this.organizationAddress);
this.pageNotFoundCheck(this.project$);
this.isManager$ = this.organizationService.isCurrentUserManager();
this.isManager$ = this.organizationService.isCurrentUserManager(this.organizationAddress);

forkJoin([this.project$, this.isManager$])
.pipe(filter(([project, isManager]) => project.status === Status.IN_PROGRESS && isManager))
Expand All @@ -75,7 +74,7 @@ export class ProjectComponent implements OnInit {

public updateProject(status: Status) {
this.loadingService.show();
this.organizationService.updateProject(this.projectId, status).subscribe({
this.organizationService.updateProject(this.projectId, status, this.organizationAddress).subscribe({
next: () => this.handleObservable('project updated'),
error: err => this.handleObservable('failed to update project', err),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,32 @@ export class ShowMembersComponent implements OnInit {
}

private dialogSetUp() {
this.organizationService.setTarget(this.organizationAddress);
if (this.isAddMember) this.members$ = this.organizationService.getOrganizationMembersNotInProject(this.projectId);
else this.members$ = this.organizationService.getProjectMembers(this.projectId);
if (this.isAddMember)
this.members$ = this.organizationService.getOrganizationMembersNotInProject(
this.projectId,
this.organizationAddress
);
else this.members$ = this.organizationService.getProjectMembers(this.projectId, this.organizationAddress);
}

public remove(profileId: string) {
this.loadingService.show();
return this.organizationService.removeProjectMember(this.projectId, Number(profileId)).subscribe({
next: () => this.handleObservable('member removed'),
error: err => this.handleObservable('failed to remove member', err),
});
return this.organizationService
.removeProjectMember(this.projectId, Number(profileId), this.organizationAddress)
.subscribe({
next: () => this.handleObservable('member removed'),
error: err => this.handleObservable('failed to remove member', err),
});
}

public add(profileId: string) {
this.loadingService.show();
return this.organizationService.addProjectMember(this.projectId, Number(profileId)).subscribe({
next: () => this.handleObservable('member added'),
error: err => this.handleObservable('failed to add member', err),
});
return this.organizationService
.addProjectMember(this.projectId, Number(profileId), this.organizationAddress)
.subscribe({
next: () => this.handleObservable('member added'),
error: err => this.handleObservable('failed to add member', err),
});
}

public close() {
Expand Down
Loading

0 comments on commit 500e548

Please sign in to comment.