-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
many: backport some usefull functions
* Adds new test script. * Improves test by adding tester app into the tests. * Adds new form validators: to test remotely the uniqueness of a value, to check a range of time. * Adds a function to clean a json before backend POST: remove empty values. * Improves the valueAlreadyExists function of the record-service to use getRecords instead of a direct http call. * Add variables to record-service for default and max number of results. Co-Authored-by: Johnny Mariéthoz <Johnny.Mariethoz@rero.ch> Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
- Loading branch information
Showing
10 changed files
with
165 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Invenio angular core | ||
* Copyright (C) 2019 RERO | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* 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 { AbstractControl, ValidatorFn, FormArray } from '@angular/forms'; | ||
import moment from 'moment'; | ||
|
||
// @dynamic | ||
export class TimeValidator { | ||
static greaterThanValidator(start: string, end: string): ValidatorFn { | ||
return (control: AbstractControl): { [key: string]: any } => { | ||
if (control) { | ||
let isLessThan = false; | ||
const startTime = control.get('start_time'); | ||
const endTime = control.get('end_time'); | ||
const startDate = moment(startTime.value, 'HH:mm'); | ||
const endDate = moment(endTime.value, 'HH:mm'); | ||
if (startDate.format('HH:mm') !== '00:00' || endDate.format('HH:mm') !== '00:00') { | ||
isLessThan = startDate.diff(endDate) >= 0; | ||
} | ||
return isLessThan ? { lessThan: { value: isLessThan }} : null; | ||
} | ||
}; | ||
} | ||
|
||
static RangePeriodValidator(): ValidatorFn { | ||
return (control: AbstractControl): { [key: string]: any } => { | ||
if (control.value) { | ||
let isRangeLessThan = false; | ||
const times = control.get('times') as FormArray; | ||
if (control.get('is_open').value && times.value.length > 1) { | ||
const firstStartDate = moment(times.at(0).get('start_time').value, 'HH:mm'); | ||
const firstEndDate = moment(times.at(0).get('end_time').value, 'HH:mm'); | ||
const lastStartDate = moment(times.at(1).get('start_time').value, 'HH:mm'); | ||
const lastEndDate = moment(times.at(1).get('end_time').value, 'HH:mm'); | ||
if (firstStartDate > lastStartDate) { | ||
isRangeLessThan = firstStartDate.diff(lastStartDate) <= 0 | ||
|| firstStartDate.diff(lastEndDate) <= 0; | ||
} else { | ||
isRangeLessThan = lastStartDate.diff(firstEndDate) <= 0 | ||
|| lastStartDate.diff(firstEndDate) <= 0; | ||
} | ||
} | ||
return isRangeLessThan ? { rangeLessThan: { value: isRangeLessThan} } : null; | ||
} | ||
}; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
projects/rero/ng-core/src/lib/validator/unique.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Invenio angular core | ||
* Copyright (C) 2019 RERO | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* 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 { AbstractControl } from '@angular/forms'; | ||
import { RecordService } from '../record/record.service'; | ||
|
||
// @dynamic | ||
export class UniqueValidator { | ||
static createValidator( | ||
recordService: RecordService, | ||
recordType: string, | ||
fieldName: string, | ||
excludePid: string = null | ||
) { | ||
return (control: AbstractControl) => { | ||
return recordService.valueAlreadyExists ( | ||
recordType, | ||
fieldName, | ||
control.value, | ||
excludePid | ||
); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bash | ||
# -*- coding: utf-8 -*- | ||
# | ||
# RERO ILS | ||
# Copyright (C) 2019 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# 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/>. | ||
|
||
RED='\033[0;31m' | ||
GREEN='\033[0;0;32m' | ||
NC='\033[0m' # No Color | ||
|
||
display_error_message () { | ||
echo -e "${RED}$1${NC}" 1>&2 | ||
} | ||
|
||
display_success_message () { | ||
echo -e "${GREEN}$1${NC}" 1>&2 | ||
} | ||
|
||
display_success_message "Linting the projects..." | ||
ng lint | ||
|
||
display_success_message "Run the tests" | ||
ng test --no-watch --no-progress --browsers=ChromeHeadlessCI |