Skip to content

Commit

Permalink
fix: Enable and disable surveys
Browse files Browse the repository at this point in the history
The logic to enable and disable surveys was not implemented, and the
state was not shown correctly. The front-end logic now corresponds to
the back-end logic (org.apache.fineract.spm.service.SpmService).
  • Loading branch information
rhopman authored and adamsaghy committed Nov 28, 2024
1 parent 31f1890 commit 43c8fdb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/app/system/manage-surveys/manage-surveys.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'labels.inputs.Status' | translate }} </th>
<td mat-cell *matCellDef="let survey">
<div [className]="!isActive(survey.validFrom, survey.validTo) === true ? ('labels.inputs.Disabled' | translate) : ('labels.inputs.Enabled' | translate)">
<fa-icon matTooltip="{{ !isActive(survey.validFrom, survey.validTo) === true ? ('labels.inputs.Disabled' | translate) : ('labels.inputs.Enabled' | translate) }}" matTooltipPosition="right" icon="circle" size="lg"></fa-icon>
<div [className]="isActive(survey.validFrom, survey.validTo) ? 'enabled' : 'disabled'">
<fa-icon matTooltip="{{ !isActive(survey.validFrom, survey.validTo) ? ('labels.inputs.Disabled' | translate) : ('labels.inputs.Enabled' | translate) }}" matTooltipPosition="right" icon="circle" size="lg"></fa-icon>
</div>
</td>
</ng-container>

<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{'labels.inputs.Action' | translate}} </th>
<td mat-cell *matCellDef="let survey">
<button mat-button *ngIf="!isActive(survey.validFrom, survey.validTo)" color="accent">
<button mat-button *ngIf="!isActive(survey.validFrom, survey.validTo)" color="accent" (click)="activate(survey); $event.stopPropagation();">
<fa-icon icon="lock-open" class="m-r-10"></fa-icon>{{'labels.buttons.Activate' | translate }}
</button>
<button mat-button *ngIf="isActive(survey.validFrom, survey.validTo)" color="warn">
<button mat-button *ngIf="isActive(survey.validFrom, survey.validTo)" color="warn" (click)="deactivate(survey); $event.stopPropagation();">
<fa-icon icon="lock" class="m-r-10"></fa-icon>{{'labels.buttons.Deactivate' | translate}}
</button>
</td>
Expand Down
45 changes: 39 additions & 6 deletions src/app/system/manage-surveys/manage-surveys.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute } from '@angular/router';

/** Custom Services */
import { SystemService } from '../system.service';

/**
* Manage Surveys component.
*/
Expand All @@ -31,20 +34,23 @@ export class ManageSurveysComponent implements OnInit {
* Retrieves the surveys data from `resolve`.
* @param {ActivatedRoute} route Activated Route.
*/
constructor(private route: ActivatedRoute) {
constructor(
private route: ActivatedRoute,
private systemService: SystemService,
) {
this.route.data.subscribe(( data: { surveys: any }) => {
this.surveysData = data.surveys;
});
}

/**
* Returns whether an survey is active based on its duration
* @param {number} validFrom Date valid from
* @param {number} validTo Date valid to
* @param {string} validFrom Date valid from (yyyy-MM-dd)
* @param {string} validTo Date valid to (yyyy-MM-dd)
*/
isActive(validFrom: number, validTo: number) {
const curdate = new Date().getTime();
return (curdate > validFrom && curdate < validTo);
isActive(validFrom: string, validTo: string) {
const curdate = new Date().toISOString().split('T')[0];
return (curdate >= validFrom && curdate <= validTo);
}

/**
Expand Down Expand Up @@ -78,4 +84,31 @@ export class ManageSurveysComponent implements OnInit {
this.dataSource.filter = filterValue.trim().toLowerCase();
}

/**
* Activates a survey.
* @param {any} survey Survey to activate.
*/
activate(survey: any) {
this.systemService.activateSurvey(survey.id).subscribe(() => {
const today = new Date().toISOString().split('T')[0];
// This mimics the server-side logic
survey.validFrom = today;
survey.validTo = today;
});
}

/**
* Deactivates a survey.
* @param {any} survey Survey to deactivate.
*/
deactivate(survey: any) {
this.systemService.deactivateSurvey(survey.id).subscribe(() => {
const date = new Date();
date.setDate(date.getDate() - 1); // Set to yesterday
const yesterday = date.toISOString().split('T')[0];
// This mimics the server-side logic
survey.validTo = yesterday;
});
}

}
18 changes: 18 additions & 0 deletions src/app/system/system.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,24 @@ export class SystemService {
return this.http.put(`/surveys/${surveyId}`, survey);
}

/**
* Activates a survey.
* @param {number} surveyId Survey ID.
* @returns {Observable<any>}
*/
activateSurvey(surveyId: number): Observable<any> {
return this.http.post(`/surveys/${surveyId}?command=activate`, null);
}

/**
* Deactivates a survey.
* @param {number} surveyId Survey ID.
* @returns {Observable<any>}
*/
deactivateSurvey(surveyId: number): Observable<any> {
return this.http.post(`/surveys/${surveyId}?command=deactivate`, null);
}


/**
* @returns {Observable<any>} Fetches Jobs.
Expand Down

0 comments on commit 43c8fdb

Please sign in to comment.