Skip to content

Commit

Permalink
refactor(frontend): set organization contract in every function rathe…
Browse files Browse the repository at this point in the history
…r than in the service
  • Loading branch information
ivanzy committed Jul 25, 2023
1 parent 0bfdaaa commit 6b7cc1b
Show file tree
Hide file tree
Showing 16 changed files with 417 additions and 385 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 @@ -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
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
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map, of, startWith } from 'rxjs';
import { map, startWith } from 'rxjs';

import { COMMA, ENTER } from '@angular/cdk/keycodes';
import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core';
Expand Down Expand Up @@ -55,9 +55,8 @@ export class CreateTaskComponent implements OnInit, OnDestroy {

ngOnInit(): void {
this.loadForm();
this.organizationService.setTarget(this.organizationAddress);
this.skillChip.items$ = this.skillService.getSkills();
this.memberChip.items$ = this.organizationService.getProjectMembers(this.projectId);
this.memberChip.items$ = this.organizationService.getProjectMembers(this.projectId, this.organizationAddress);

this.skillChip.items$.subscribe(skills => {
this.skillChip.all = skills;
Expand Down Expand Up @@ -120,12 +119,18 @@ export class CreateTaskComponent implements OnInit, OnDestroy {

const createTask$ =
this.memberChip.selectedItems.length === 0
? this.organizationService.createTaskWithoutMembers(this.projectId, formValue, this.skillChip.selectedItems)
? this.organizationService.createTaskWithoutMembers(
this.projectId,
formValue,
this.skillChip.selectedItems,
this.organizationAddress
)
: this.organizationService.createTask(
this.projectId,
formValue,
this.skillChip.selectedItems,
this.memberChip.selectedItems
this.memberChip.selectedItems,
this.organizationAddress
);

createTask$.subscribe({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export class ShowMembersComponent implements OnInit {
}

private dialogSetUp() {
this.organizationService.setTarget(this.organizationAddress);
this.members$ = this.organizationService.getProjectMembersNotInTask(this.projectId, this.taskId);
this.members$ = this.organizationService.getProjectMembersNotInTask(
this.projectId,
this.taskId,
this.organizationAddress
);
}

public add(profileId: string) {
this.loadingService.show();
return this.organizationService.addTaskMember(this.taskId, Number(profileId)).subscribe({
return this.organizationService.addTaskMember(this.taskId, Number(profileId), this.organizationAddress).subscribe({
next: () => this.handleObservable('member added'),
error: err => this.handleObservable('failed to add member', err),
});
Expand Down
39 changes: 26 additions & 13 deletions frontend/src/app/modules/tasks/components/task/task.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ export class TaskComponent implements OnInit {
}

public setUp() {
this.organizationService.setTarget(this.organizationAddress);
this.isManager$ = this.organizationService.isCurrentUserManager();
this.isMember$ = this.organizationService.isCurrentUserTaskMember(this.taskId);
this.task$ = this.organizationService.getTaskById(this.taskId);
this.isManager$ = this.organizationService.isCurrentUserManager(this.organizationAddress);
this.isMember$ = this.organizationService.isCurrentUserTaskMember(this.taskId, this.organizationAddress);
this.task$ = this.organizationService.getTaskById(this.taskId, this.organizationAddress);
this.pageNotFoundCheck(this.task$);
this.profilesLoggedTime$ = this.organizationService.getTaskLoggedTimeOfProfiles(this.taskId, this.task$);
this.profilesLoggedTime$ = this.organizationService.getTaskLoggedTimeOfProfiles(
this.taskId,
this.task$,
this.organizationAddress
);
this.hasNotWithdrawnReward$ = this.my3secHub
.hasCurrentUserWithdrawnExperience(this.organizationAddress, this.taskId)
.pipe(map(hasWithdrawnReward => !hasWithdrawnReward));
Expand All @@ -87,7 +90,7 @@ export class TaskComponent implements OnInit {
};
const numericId = Number(task.id);
this.loadingService.show();
this.organizationService.updateTask(numericId, taskStruct).subscribe({
this.organizationService.updateTask(numericId, taskStruct, this.organizationAddress).subscribe({
next: () => this.handleObservable('Task updated!'),
error: err => this.handleObservable('failed to update task', err),
});
Expand Down Expand Up @@ -116,7 +119,11 @@ export class TaskComponent implements OnInit {

dialogRef.afterClosed().subscribe(changed => {
if (!changed) return;
this.profilesLoggedTime$ = this.organizationService.getTaskLoggedTimeOfProfiles(this.taskId, this.task$);
this.profilesLoggedTime$ = this.organizationService.getTaskLoggedTimeOfProfiles(
this.taskId,
this.task$,
this.organizationAddress
);
});
}

Expand All @@ -136,18 +143,24 @@ export class TaskComponent implements OnInit {
dialogRef.afterClosed().subscribe((showMembersOutput: ShowMembersOutput) => {
if (showMembersOutput.profileId) return this.router.navigate(['/profiles', showMembersOutput.profileId]);
if (!showMembersOutput.changed) return;
this.task$ = this.organizationService.getTask(this.projectId, this.taskId);
this.profilesLoggedTime$ = this.organizationService.getTaskLoggedTimeOfProfiles(this.taskId, this.task$);
this.task$ = this.organizationService.getTask(this.projectId, this.taskId, this.organizationAddress);
this.profilesLoggedTime$ = this.organizationService.getTaskLoggedTimeOfProfiles(
this.taskId,
this.task$,
this.organizationAddress
);
return;
});
}

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

public secondsToHours(seconds: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { FormBuilder, FormGroup, ValidatorFn, Validators } from '@angular/forms';

import { ProfileMetadata } from '@shared/interfaces';
import { ImageConversionService } from '@shared/services/image-conversion.service';
Expand Down
Loading

0 comments on commit 6b7cc1b

Please sign in to comment.