diff --git a/src/app/commenting/review-comments/review-comments.component.ts b/src/app/commenting/review-comments/review-comments.component.ts index 70366536..4a8a88be 100644 --- a/src/app/commenting/review-comments/review-comments.component.ts +++ b/src/app/commenting/review-comments/review-comments.component.ts @@ -78,16 +78,14 @@ export class ReviewCommentsComponent implements OnInit, OnDestroy { if (data.application) { this.application = data.application; - this.commentService.getCountByApplicationId(this.application._id) + this.commentService.getCountByPeriodId(this.application.currentPeriod._id) .takeUntil(this.ngUnsubscribe) .subscribe( value => { this.pageCount = value ? Math.ceil(value / this.PAGE_SIZE) : 1; + // get initial data + this.getData(); }); - - // get initial data - this.getData(); - } else { alert('Uh-oh, couldn\'t load application'); // application not found --> navigate back to search diff --git a/src/app/search/search.component.ts b/src/app/search/search.component.ts index 0023f56b..76d7fafe 100644 --- a/src/app/search/search.component.ts +++ b/src/app/search/search.component.ts @@ -81,7 +81,7 @@ export class SearchComponent implements OnInit, OnDestroy { .subscribe( search => { // console.log('search =', search); - if (search.totalFeatures > 0 && search.features && search.features.length > 0) { + if (search && search.totalFeatures > 0 && search.features && search.features.length > 0) { const groupedFeatures = _.groupBy(search.features, 'properties.DISPOSITION_TRANSACTION_SID'); const self = this; // for closure below _.each(groupedFeatures, function (value: any, key: string) { diff --git a/src/app/services/api.ts b/src/app/services/api.ts index a2bb8050..ab8352e9 100644 --- a/src/app/services/api.ts +++ b/src/app/services/api.ts @@ -1,10 +1,9 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; -// import { HttpParams, HttpHeaders } from '@angular/common/http'; +import { HttpClient, HttpResponse } from '@angular/common/http'; import { Params } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import { throwError } from 'rxjs'; -import 'rxjs/add/operator/map'; +import { map } from 'rxjs/operators' import 'rxjs/add/operator/catch'; import * as _ from 'lodash'; import { JwtHelperService } from '@auth0/angular-jwt'; @@ -201,6 +200,17 @@ export class ApiService { return this.http.get(`${this.pathAPI}/${queryString}`, { }); } + getApplicationsCount(): Observable { + const queryString = `application?isDeleted=false`; + return this.http.head>(`${this.pathAPI}/${queryString}`, {observe: 'response'}) + .pipe( + map( res => { + // Retrieve the count from the header. + return parseInt(res.headers.get('x-total-count')); + }) + ); + } + getApplicationByTantalisID(tantalisID: number) { const fields = [ 'agency', @@ -503,9 +513,15 @@ export class ApiService { // // Comments // - getCommentsByPeriodIdNoFields(periodId: string): Observable { - const queryString = `comment?isDeleted=false&_commentPeriod=${periodId}&pageNum=0&pageSize=1000000`; // max 1M records - return this.http.get(`${this.pathAPI}/${queryString}`, { }); + getCommentCountByPeriodId(periodId: string): Observable { + const queryString = `comment?isDeleted=false&_commentPeriod=${periodId}`; // max 1M records + return this.http.head>(`${this.pathAPI}/${queryString}`, {observe: 'response'}) + .pipe( + map( res => { + // Retrieve the count from the header. + return parseInt(res.headers.get('x-total-count')); + }) + ); } getCommentsByPeriodId(periodId: string, pageNum: number, pageSize: number, sortBy: string) { diff --git a/src/app/services/application.service.ts b/src/app/services/application.service.ts index 443180d4..1dd3e019 100644 --- a/src/app/services/application.service.ts +++ b/src/app/services/application.service.ts @@ -89,11 +89,8 @@ export class ApplicationService { // get count of applications getCount(): Observable { // get just the applications, and count them - return this.api.getApplications() - .map(applications => { - return applications.length; - }) - .catch(this.api.handleError); + return this.api.getApplicationsCount() + .catch(this.api.handleError); } // get all applications @@ -144,7 +141,6 @@ export class ApplicationService { getFeatures ? this.featureService.getByApplicationId(application._id) : of(null), getDocuments ? this.documentService.getAllByApplicationId(application._id) : of(null), getCurrentPeriod ? this.commentPeriodService.getAllByApplicationId(application._id) : of(null), - getNumComments ? this.commentService.getCountByApplicationId(application._id) : of(null), getDecision ? this.decisionService.getByApplicationId(application._id) : of(null) ) .map(payloads => { @@ -164,7 +160,10 @@ export class ApplicationService { }); } - if (getCurrentPeriod) { + // If we're getting the number of comments, we need to know the current period. + // This logic is flawed, because we should be explicit over white comment period + // we're getting. + if (getCurrentPeriod || getNumComments) { const periods = []; _.each(payloads[2], function (p) { periods.push(new CommentPeriod(p)); @@ -181,15 +180,19 @@ export class ApplicationService { const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); application.currentPeriod['daysRemaining'] = moment(cp.endDate).diff(moment(today), 'days') + 1; // including today } - } - if (getNumComments) { - const numComments = payloads[3]; - application['numComments'] = numComments.toString(); + if (getNumComments && cp) { + this.commentService.getCountByPeriodId(cp._id) + .subscribe( + numComments => { + application['numComments'] = numComments; + } + ); + } } if (getDecision) { - const decision = payloads[4]; + const decision = payloads[3]; application.decision = decision; } diff --git a/src/app/services/comment.service.ts b/src/app/services/comment.service.ts index 6460aa39..b939349a 100644 --- a/src/app/services/comment.service.ts +++ b/src/app/services/comment.service.ts @@ -27,34 +27,6 @@ export class CommentService { private documentService: DocumentService ) { } - // get count of comments for the specified application id - getCountByApplicationId(appId: string): Observable { - // first get the comment periods - return this.commentPeriodService.getAllByApplicationId(appId) - .mergeMap(periods => { - if (periods.length === 0) { - return of(0); - } - - // count comments for first comment period only - return this.getCountByPeriodId(periods[0]._id); - - // FUTURE: this code is for multiple comment periods - // const promises: Array> = []; - - // // now get the counts for all periods - // periods.forEach(period => { - // promises.push(this.getCountByPeriodId(period._id).toPromise()); - // }); - - // return Promise.all(promises) - // .then((allCounts: number[]) => { - // return allCounts.reduce((total, num) => { return total + num; }); - // }); - }) - .catch(this.api.handleError); - } - // get all comments for the specified application id // (without documents) getAllByApplicationId(appId: string, pageNum: number = 0, pageSize: number = 10, sortBy: string = null): Observable { @@ -87,17 +59,8 @@ export class CommentService { // get count of comments for the specified comment period id // TODO: count only pending comments? (need comment status) getCountByPeriodId(periodId: string): Observable { - return this.api.getCommentsByPeriodIdNoFields(periodId) - .map((comments: Comment[]) => { - return comments.length; - }) - .catch(this.api.handleError); - } - - // get count of comments for the specified comment period id - getCommentsByPeriodId(periodId: string): Observable { - return this.api.getCommentsByPeriodIdNoFields(periodId) - .catch(this.api.handleError); + return this.api.getCommentCountByPeriodId(periodId) + .catch(this.api.handleError); } // get all comments for the specified comment period id diff --git a/src/app/utils/token-interceptor.ts b/src/app/utils/token-interceptor.ts index 332e5197..2871d629 100644 --- a/src/app/utils/token-interceptor.ts +++ b/src/app/utils/token-interceptor.ts @@ -10,7 +10,7 @@ import { import { KeycloakService } from 'app/services/keycloak.service'; import { Observable } from 'rxjs'; -import { tap, catchError } from 'rxjs/operators'; +import { tap, catchError, map } from 'rxjs/operators'; import { _throw } from 'rxjs/observable/throw'; @Injectable() @@ -27,26 +27,13 @@ export class TokenInterceptor implements HttpInterceptor { 'Authorization': 'Bearer ' + authToken } }); - return next.handle(request); - // TODO: Handle failed requests gracefully/pop a modal/other - // return next.handle(request).pipe(tap( - // (err: any) => { - // if (err instanceof HttpErrorResponse) { - // console.log(err); - // if (err.status === 401) { - // // this.router.navigate(['/login']); - // } - // } - // } - // ),catchError(e => { - // if (e instanceof HttpErrorResponse) { - // console.log('Processing http error', e); - // if (e.status === 403) { - // // this.router.navigate(['/login']); - // } - // } - // return _throw(e); - // }) - // ); + return next.handle(request).pipe( + map((resp: HttpResponse) => { + if (resp) { + // console.log('interceptor header keys: ', resp.headers && resp.headers.get('x-total-count')); + // console.log('interceptor X-Service-Name: ', resp.headers.get('X-Service-Name')); + } + return resp; + })); } }