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

record: added a function to convert "total hits" for Elasticsearch #260

Merged
merged 1 commit into from
Sep 21, 2020
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
4 changes: 2 additions & 2 deletions projects/rero/ng-core/src/lib/record/record.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import { AddFieldEditorComponent } from './editor/add-field-editor/add-field-edi
import { ArrayTypeComponent } from './editor/array-type/array-type.component';
import { DropdownLabelEditorComponent } from './editor/dropdown-label-editor/dropdown-label-editor.component';
import { EditorComponent } from './editor/editor.component';
import { hooksFormlyExtension, registerTranslateExtension, fieldIdGenerator } from './editor/extensions';
import { fieldIdGenerator, hooksFormlyExtension, registerTranslateExtension } from './editor/extensions';
import { HorizontalWrapperComponent } from './editor/horizontal-wrapper/horizontal-wrapper.component';
import { MultiSchemaTypeComponent } from './editor/multischema/multischema.component';
import { ObjectTypeComponent } from './editor/object-type/object-type.component';
import { RemoteTypeaheadComponent } from './editor/remote-typeahead/remote-typeahead.component';
import { SwitchComponent } from './editor/switch/switch.component';
import { ToggleWrapperComponent } from './editor/toggle-wrapper/toggle-wrappers.component';
import { DatepickerTypeComponent } from './editor/type/datepicker-type.component';
Expand All @@ -53,7 +54,6 @@ import { RecordSearchComponent } from './search/record-search.component';
import { JsonComponent } from './search/result/item/json.component';
import { RecordSearchResultComponent } from './search/result/record-search-result.component';
import { RecordSearchResultDirective } from './search/result/record-search-result.directive';
import { RemoteTypeaheadComponent } from './editor/remote-typeahead/remote-typeahead.component';

@NgModule({
declarations: [
Expand Down
3 changes: 1 addition & 2 deletions projects/rero/ng-core/src/lib/record/record.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { HttpErrorResponse } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { getTestBed, TestBed } from '@angular/core/testing';
import { TranslateFakeLoader, TranslateLoader, TranslateModule } from '@ngx-translate/core';
Expand Down Expand Up @@ -74,7 +73,7 @@ describe('RecordService', () => {
};

service.getRecords('documents', '', 1, 10, [{ key: 'author', values: ['John doe'] }]).subscribe((data: Record) => {
expect(data.hits.total).toBe(2);
expect(service.totalHits(data.hits.total)).toBe(2);
});

const req = httpMock.expectOne(request => request.method === 'GET' && request.url === url + '/');
Expand Down
27 changes: 25 additions & 2 deletions projects/rero/ng-core/src/lib/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class RecordService {
query += ` NOT pid:${excludePid}`;
}
return this.getRecords(recordType, query, 1, 1).pipe(
map((res: Record) => res.hits.total),
map((res: Record) => this.totalHits(res.hits.total)),
map((total) => (total ? { alreadyTaken: value } : null)),
debounceTime(1000)
);
Expand Down Expand Up @@ -282,12 +282,35 @@ export class RecordService {
query += ` NOT pid:${excludePid}`;
}
return this.getRecords(recordType, query, 1, 1).pipe(
map((res: Record) => res.hits.total),
map((res: Record) => this.totalHits(res.hits.total)),
map((total) => (total ? { alreadyTaken: value } : null)),
debounceTime(500)
);
}

/**
* Transform a total value string or object representation
* (ES compatibility v6 and v7)
* @param total - string or object
* @param relation - boolean
* @return integer, text or null
*/
totalHits(total: any, relation = false): any {
switch (typeof total) {
case 'object':
if (relation) {
return `${this._translateService.instant(total.relation)} ${total.value.toString()}`;
}
return Number(total.value);
case 'number':
return total;
case 'string':
return Number(total);
default:
return null;
}
}

/**
* Error handling during api call process.
* @param error - HttpErrorResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ describe('RecordSearchComponent', () => {
links: {}
};

const recordServiceSpy = jasmine.createSpyObj('RecordService', ['getRecords', 'delete']);
const recordServiceSpy = jasmine.createSpyObj('RecordService', ['getRecords', 'delete', 'totalHits']);
recordServiceSpy.getRecords.and.returnValue(of(emptyRecords));
recordServiceSpy.delete.and.returnValue(of({}));
recordServiceSpy.totalHits.and.returnValue(10);

const recordUiServiceSpy = jasmine.createSpyObj('RecordUiService', [
'getResourceConfig',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
* Loads total count of records for each resource.
*/
ngOnInit() {

// Subscribe on aggregation filters changes and do search.
this._aggregationsFiltersSubscription = this._recordSearchService.aggregationsFilters.subscribe(
(aggregationsFilters: Array<AggregationsFilter>) => {
Expand Down Expand Up @@ -228,7 +229,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
type.key, '', 1, 1, [],
this._config.preFilters || {},
this._config.listHeaders || null).subscribe((records: Record) => {
type.total = records.hits.total;
type.total = this._recordService.totalHits(records.hits.total);
});
}
}
Expand Down Expand Up @@ -329,7 +330,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
* @return integer representing the number of results.
*/
get total(): number {
return this.hits && this.hits.total ? this.hits.total : 0;
return this.hits && this.hits.total ? this._recordService.totalHits(this.hits.total) : 0;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions projects/rero/ng-core/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './lib/core-config.service';
export * from './lib/core.module';
export * from './lib/dialog/dialog.component';
export * from './lib/dialog/dialog.service';
export * from './lib/error/error.component';
export * from './lib/pipe/callback-array-filter.pipe';
export * from './lib/pipe/default.pipe';
export * from './lib/pipe/get-record.pipe';
Expand All @@ -37,6 +38,8 @@ export * from './lib/record/editor/editor.service';
export * from './lib/record/editor/extensions';
export * from './lib/record/editor/multischema/multischema.component';
export * from './lib/record/editor/object-type/object-type.component';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.component';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.service';
export * from './lib/record/editor/switch/switch.component';
export * from './lib/record/editor/toggle-wrapper/toggle-wrappers.component';
export * from './lib/record/editor/type/datepicker-type.component';
Expand All @@ -61,6 +64,4 @@ export * from './lib/utils/sort-by-keys';
export * from './lib/utils/utils';
export * from './lib/validator/time.validator';
export * from './lib/validator/unique.validator';
export * from './lib/error/error.component';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.service';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.component';