Skip to content

Commit

Permalink
#6 new voting is not created if there is at least 1 active voting
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzhak committed Oct 25, 2018
1 parent ebe757d commit dbdba06
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions src/app/shared/services/devzendao/devzendao.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { EventEmitter, Injectable, Output } from '@angular/core';
import { MessageService } from 'primeng/api';
import { Observable, from, forkJoin, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';

Expand Down Expand Up @@ -45,6 +46,7 @@ export class DevzendaoService {

constructor(
public http: HttpClient,
public messageService: MessageService,
public txSenderService: TxSenderService,
public web3Service: Web3Service
) {
Expand Down Expand Up @@ -240,7 +242,8 @@ export class DevzendaoService {
* @param address
*/
addGroupMemberAuto(groupName, address): Observable<string> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.addGroupMemberAuto,
[groupName, address],
Expand All @@ -256,7 +259,8 @@ export class DevzendaoService {
* @param guestAddress
*/
changeTheGuestAuto(guestAddress): Observable<any> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.changeTheGuestAuto,
[guestAddress],
Expand All @@ -272,7 +276,8 @@ export class DevzendaoService {
* @param guestAddress
*/
emergencyChangeTheGuestAuto(guestAddress): Observable<any> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.emergency_ChangeTheGuestAuto,
[guestAddress],
Expand All @@ -288,7 +293,8 @@ export class DevzendaoService {
* @param guestHasCome whether guest visited the show
*/
moveToNextEpisodeAuto(guestHasCome): Observable<any> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.moveToNextEpisodeAuto,
[guestHasCome],
Expand All @@ -305,7 +311,8 @@ export class DevzendaoService {
* @param address
*/
removeGroupMemberAuto(groupName, address): Observable<void> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.removeGroupMemberAuto,
[groupName, address],
Expand All @@ -321,7 +328,8 @@ export class DevzendaoService {
* @param nextHostAddress
*/
selectNextHostAuto(nextHostAddress): Observable<any> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.selectNextHostAuto,
[nextHostAddress],
Expand All @@ -338,7 +346,8 @@ export class DevzendaoService {
* @param value
*/
updateDaoParamsAuto(paramHash, value): Observable<string> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.updateDaoParamsAuto,
[paramHash, value],
Expand All @@ -354,7 +363,8 @@ export class DevzendaoService {
* @param outputAddress
*/
withdrawEtherAuto(outputAddress): Observable<any> {
return this.web3Service.getAccounts().pipe(
return this.hasActiveVoting().pipe(
switchMap(hasActiveVoting => this.hasActiveVotingHandler(hasActiveVoting)),
switchMap(accounts => this.txSenderService.send(
this.devZenDaoAutoContract.methods.withdrawEtherAuto,
[outputAddress],
Expand Down Expand Up @@ -504,4 +514,42 @@ export class DevzendaoService {
if(!name) throw Error(`param name not found for hash ${hash}`);
}

/**
* Checks whether there are active votings
*/
hasActiveVoting() {
return this.getProposalsCount().pipe(
switchMap(proposalsCount => {
if(proposalsCount == 0) return of([]);
let requests = [];
for(let i = 0; i < proposalsCount; i++) {
requests.push(this.getProposalAtIndex(i));
}
return forkJoin(requests);
}),
switchMap(proposalAddresses => {
if(proposalAddresses.length == 0) return of([]);
let requests = [];
for(let i = 0; i < proposalAddresses.length; i++) {
requests.push(this.isFinished(proposalAddresses[i]));
}
return forkJoin(requests);
}),
switchMap(isFinishedArray => {
return isFinishedArray.includes(false) ? of(true) : of(false);
})
)
}

/**
* Has active voting handler for auto methods
*/
hasActiveVotingHandler(hasActiveVoting) {
if(hasActiveVoting) {
this.messageService.add({severity:'error', summary:'Error', detail:'You can\'t start a new voting while there is at least one active voting'});
throw new Error('You can\'t start a new voting while there is at least one active voting');
}
return this.web3Service.getAccounts();
}

}

0 comments on commit dbdba06

Please sign in to comment.