From 3784f9fdccdeb1d0a5f89cf46b6ff76503b1c5c7 Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Wed, 20 Jul 2022 20:00:24 -0400 Subject: [PATCH] LP1879517: Surveys shouldn't end before they begin To test: 1. Go to Admin > Local > Surveys. 2. Create a new survey. 3. Attempt to create a survey where the end date comes before the start date. Without this patch, you will get no notice that this is invalid, and you can save the invalid record. 4. Edit an existing survey. 5. Repeat step 3 while editing the existing survey. 6. Apply the patch. 7. Repeat steps 1-5. Note that you now get a notice and cannot save if the end date is before the start date. This commit generalizes a validator already present in the booking module, and corrects several small bugs related to the datetime-select component. Signed-off-by: Jane Sandberg Signed-off-by: Susan Morrison Signed-off-by: Jason Boyer --- Open-ILS/src/eg2/src/app/core/format.service.ts | 2 +- Open-ILS/src/eg2/src/app/core/format.spec.ts | 5 ++ .../datetime-select.component.spec.ts | 20 +++++++++ .../datetime-select/datetime-select.component.ts | 12 ++++- .../app/share/fm-editor/fm-editor.component.html | 33 ++++++++++----- .../src/app/share/fm-editor/fm-editor.component.ts | 5 ++ .../dates_in_order_validator.directive.ts | 44 ++++++++++++++++++++ .../validators/dates_in_order_validator.spec.ts | 16 +++++++ .../admin/local/survey/survey-edit.component.html | 29 +++++++------ .../staff/admin/local/survey/survey.component.html | 16 ++++--- .../staff/admin/local/survey/survey.component.ts | 9 ++++- .../create-reservation-dialog.component.html | 2 +- .../booking/create-reservation-dialog.component.ts | 12 +---- Open-ILS/src/eg2/src/app/staff/common.module.ts | 3 + 14 files changed, 160 insertions(+), 48 deletions(-) create mode 100644 Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.spec.ts create mode 100644 Open-ILS/src/eg2/src/app/share/validators/dates_in_order_validator.directive.ts create mode 100644 Open-ILS/src/eg2/src/app/share/validators/dates_in_order_validator.spec.ts diff --git a/Open-ILS/src/eg2/src/app/core/format.service.ts b/Open-ILS/src/eg2/src/app/core/format.service.ts index b58f551..5386a8d 100644 --- a/Open-ILS/src/eg2/src/app/core/format.service.ts +++ b/Open-ILS/src/eg2/src/app/core/format.service.ts @@ -235,7 +235,7 @@ export class FormatService { * Create a Moment from an ISO string */ momentizeIsoString(isoString: string, timezone: string): moment.Moment { - return (isoString.length) ? moment(isoString, timezone) : moment(); + return (isoString?.length) ? moment(isoString).tz(timezone) : moment(); } /** diff --git a/Open-ILS/src/eg2/src/app/core/format.spec.ts b/Open-ILS/src/eg2/src/app/core/format.spec.ts index 4da4830..40cbb29 100644 --- a/Open-ILS/src/eg2/src/app/core/format.spec.ts +++ b/Open-ILS/src/eg2/src/app/core/format.spec.ts @@ -143,6 +143,11 @@ describe('FormatService', () => { const moment = service.momentizeDateTimeString('7/3/12, 6:06 PM', 'Africa/Addis_Ababa', false, 'fr-CA'); expect(moment.isValid()).toBe(true); }); + it('can momentize ISO strings', () => { + const moment = service.momentizeIsoString('2022-07-29T17:56:00.000Z', 'America/New_York'); + expect(moment.isValid()).toBe(true); + expect(moment.format('YYYY')).toBe('2022'); + }); }); diff --git a/Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.spec.ts b/Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.spec.ts new file mode 100644 index 0000000..b049dbd --- /dev/null +++ b/Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.spec.ts @@ -0,0 +1,20 @@ +import * as moment from "moment"; +import { DateTimeSelectComponent } from "./datetime-select.component"; + + +describe('DateTimeSelectComponent', () => { + const mockFormatService = jasmine.createSpyObj('FormatService', ['transform', 'momentizeIsoString']); + mockFormatService.momentizeIsoString.and.returnValue(moment('2020-12-11T01:30:05.606Z').tz('America/Vancouver')); + const mockDateTimeValidator = jasmine.createSpyObj('DateTimeValidator', ['']); + const mockNgControl = jasmine.createSpyObj('ngControl', ['']); + const component = new DateTimeSelectComponent(mockFormatService, mockDateTimeValidator, mockNgControl); + + it('accepts an initialIso input and converts it to the correct timezone', () => { + component.initialIso = '2020-12-11T01:30:05.606Z'; + component.timezone = 'America/Vancouver'; + component.ngOnInit(); + expect(component.date.value).toEqual({year: 2020, month: 12, day: 10}); + expect(component.time.value).toEqual({hour: 17, minute: 30, second: 0}); + }); + +}); diff --git a/Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.ts b/Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.ts index d9564a3..29cc2ae 100644 --- a/Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.ts +++ b/Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.ts @@ -1,7 +1,6 @@ -import {Component, EventEmitter, Input, Output, forwardRef, ViewChild, OnInit, Optional, Self} from '@angular/core'; +import {Component, EventEmitter, Input, Output, ViewChild, OnInit, Optional, Self} from '@angular/core'; import {FormatService} from '@eg/core/format.service'; import {AbstractControl, ControlValueAccessor, FormControl, FormGroup, NgControl} from '@angular/forms'; -import {NgbDatepicker, NgbTimeStruct, NgbDateStruct} from '@ng-bootstrap/ng-bootstrap'; import {DatetimeValidator} from '@eg/share/validators/datetime_validator.directive'; import * as moment from 'moment-timezone'; import {DateUtil} from '@eg/share/util/date'; @@ -133,7 +132,14 @@ export class DateTimeSelectComponent implements OnInit, ControlValueAccessor { } - writeValue(value: moment.Moment) { + writeValue(value: moment.Moment|string) { + if (typeof value === 'string') { + if (value.length === 0) { + return; + }; + value = this.format.momentizeIsoString(value, this.timezone); + } + if (value !== undefined && value !== null) { this.dateTimeForm.patchValue({ stringVersion: this.format.transform({value: value, datatype: 'timestamp', datePlusTime: true})}); diff --git a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html index 518125c..8a734fb 100644 --- a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html +++ b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html @@ -13,20 +13,27 @@