Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HEAD HTTP verb, improvements, search fix. #288

Merged
merged 2 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/app/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 22 additions & 6 deletions src/app/services/api.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -201,6 +200,17 @@ export class ApiService {
return this.http.get<Application>(`${this.pathAPI}/${queryString}`, { });
}

getApplicationsCount(): Observable<any> {
const queryString = `application?isDeleted=false`;
return this.http.head<HttpResponse<Object>>(`${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',
Expand Down Expand Up @@ -503,9 +513,15 @@ export class ApiService {
//
// Comments
//
getCommentsByPeriodIdNoFields(periodId: string): Observable<Comment[]> {
const queryString = `comment?isDeleted=false&_commentPeriod=${periodId}&pageNum=0&pageSize=1000000`; // max 1M records
return this.http.get<Comment[]>(`${this.pathAPI}/${queryString}`, { });
getCommentCountByPeriodId(periodId: string): Observable<number> {
const queryString = `comment?isDeleted=false&_commentPeriod=${periodId}`; // max 1M records
return this.http.head<HttpResponse<Object>>(`${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) {
Expand Down
27 changes: 15 additions & 12 deletions src/app/services/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ export class ApplicationService {
// get count of applications
getCount(): Observable<number> {
// 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
Expand Down Expand Up @@ -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 => {
Expand All @@ -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));
Expand All @@ -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;
}

Expand Down
41 changes: 2 additions & 39 deletions src/app/services/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ export class CommentService {
private documentService: DocumentService
) { }

// get count of comments for the specified application id
getCountByApplicationId(appId: string): Observable<number> {
// 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<Promise<any>> = [];

// // 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<Comment[]> {
Expand Down Expand Up @@ -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<number> {
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<Comment[]> {
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
Expand Down
31 changes: 9 additions & 22 deletions src/app/utils/token-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<any>) => {
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;
}));
}
}