-
Notifications
You must be signed in to change notification settings - Fork 13
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
libraries: add notification settings #516
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,15 @@ | |
// required as json properties is not lowerCamelCase | ||
|
||
import { WeekDay } from '@angular/common'; | ||
import { marker } from '@biesbjerg/ngx-translate-extract-marker'; | ||
import * as moment from 'moment'; | ||
import { WeekDays } from './week-days'; | ||
|
||
|
||
export function _(str) { | ||
return marker(str); | ||
} | ||
|
||
Comment on lines
+27
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Remove this (test extraction) |
||
export interface OpeningHours { | ||
day: string; | ||
is_open: boolean; | ||
|
@@ -52,6 +58,19 @@ export interface ExceptionDates { | |
repeat?: Repeat; | ||
} | ||
|
||
export enum NotificationType { | ||
DUE_SOON = _('due_soon'), | ||
RECALL = _('recall'), | ||
OVERDUE = _('overdue'), | ||
AVAILABILITY = _('availability') | ||
} | ||
|
||
export interface NotificationSettings { | ||
type: NotificationType; | ||
email: string; | ||
delay?: number; | ||
} | ||
|
||
export class Library { | ||
|
||
// CLASS ATTRIBUTES ================================================ | ||
|
@@ -63,6 +82,7 @@ export class Library { | |
code: string = null; | ||
opening_hours: Array<OpeningHours> = []; | ||
exception_dates?: Array<ExceptionDates>; | ||
notification_settings?: Array<NotificationSettings>; | ||
organisation: Organisation; | ||
|
||
// GETTER & SETTER ================================================ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,27 +16,45 @@ | |
*/ | ||
|
||
import { Injectable } from '@angular/core'; | ||
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; | ||
import { TimeValidator } from '@rero/ng-core'; | ||
import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; | ||
import { RecordService, TimeValidator } from '@rero/ng-core'; | ||
import { Subject } from 'rxjs'; | ||
import { Library, NotificationSettings, NotificationType } from '../../../classes/library'; | ||
import { WeekDays } from '../../../classes/week-days'; | ||
|
||
import { Library } from '../../../classes/library'; | ||
|
||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class LibraryFormService { | ||
|
||
/** Angular form group */ | ||
public form; | ||
|
||
|
||
/** RERO ILS notification types */ | ||
private notificationTypes = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing doc string |
||
|
||
/** Observable for build event */ | ||
private buildEvent = new Subject(); | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param _fb - FormBuilder | ||
* @param _recordService - RecordService | ||
*/ | ||
constructor( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing doc string |
||
private fb: FormBuilder | ||
) { | ||
this.build(); | ||
} | ||
private _fb: FormBuilder, | ||
private _recordService: RecordService | ||
) { } | ||
|
||
build() { | ||
this.form = this.fb.group({ | ||
/** | ||
* Build form | ||
*/ | ||
build(): void { | ||
this.form = this._fb.group({ | ||
name: ['', [ | ||
Validators.required, | ||
Validators.minLength(4) | ||
|
@@ -48,24 +66,51 @@ export class LibraryFormService { | |
Validators.required | ||
] | ||
}], | ||
opening_hours: this.fb.array([]) | ||
opening_hours: this._fb.array([]), | ||
notification_settings: this._fb.array([]) | ||
}); | ||
this.initializeOpeningHours(); | ||
this.initializeNotificationSettings(); | ||
} | ||
|
||
/** | ||
* Get build event | ||
*/ | ||
getBuildEvent() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return this.buildEvent.asObservable(); | ||
} | ||
|
||
create() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this._recordService | ||
.getSchemaForm('notifications') | ||
.subscribe((jsonSchema: any) => { | ||
this.notificationTypes = jsonSchema.schema.properties.notification_type.enum; | ||
this.build(); | ||
this.buildEvent.next(true); | ||
}); | ||
} | ||
|
||
/** | ||
* Build and set default values for opening hours at form initialization | ||
* @param openingHours - opening hours | ||
*/ | ||
initializeOpeningHours(openingHours = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const days = Object.keys(WeekDays); | ||
const hours = this.form.get('opening_hours'); | ||
for (let step = 0; step < 7; step++) { | ||
hours.push(this.buildOpeningHours( | ||
false, | ||
days[step], | ||
this.fb.array([]) | ||
this._fb.array([]) | ||
)); | ||
} | ||
this.setOpeningHours(openingHours); | ||
} | ||
|
||
/** | ||
* Set opening hours from record data | ||
* @param openingHours - opening hours | ||
*/ | ||
setOpeningHours(openingHours = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for (let step = 0; step < 7; step++) { | ||
const atimes = this.getTimesByDayIndex(step); | ||
|
@@ -85,8 +130,14 @@ export class LibraryFormService { | |
} | ||
} | ||
|
||
/** | ||
* Create opening hour form control | ||
* @param isOpen - is open | ||
* @param day - day | ||
* @param times - times array | ||
*/ | ||
buildOpeningHours(isOpen, day, times): FormGroup { | ||
return this.fb.group({ | ||
return this._fb.group({ | ||
is_open: [isOpen], | ||
day: [day], | ||
times | ||
|
@@ -95,9 +146,14 @@ export class LibraryFormService { | |
}); | ||
} | ||
|
||
/** | ||
* Create times form control | ||
* @param startTime - start time | ||
* @param endTime - end time | ||
*/ | ||
buildTimes(startTime = '00:01', endTime = '23:59'): FormGroup { | ||
const regex = '^(?!(0:00)|(00:00)$)([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$'; | ||
return this.fb.group({ | ||
return this._fb.group({ | ||
start_time: [startTime, { | ||
validators: [ | ||
Validators.required, | ||
|
@@ -115,10 +171,10 @@ export class LibraryFormService { | |
}); | ||
} | ||
|
||
reset() { | ||
this.build(); | ||
} | ||
|
||
/** | ||
* Populate the form | ||
* @param library - library | ||
*/ | ||
populate(library: Library) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.form.patchValue({ | ||
name: library.name, | ||
|
@@ -127,21 +183,74 @@ export class LibraryFormService { | |
code: library.code, | ||
}); | ||
this.setOpeningHours(library.opening_hours); | ||
this.setNotificationSettings(library.notification_settings); | ||
} | ||
|
||
/** | ||
* Build an set default values for notification settings | ||
* @param notificationSettings - notification settings | ||
*/ | ||
initializeNotificationSettings(notificationSettings = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add return type |
||
const settings = this.form.get('notification_settings'); | ||
this.notificationTypes.forEach(type => { | ||
settings.push(this.getSettingsByType(type)); | ||
}); | ||
this.setNotificationSettings(notificationSettings); | ||
} | ||
|
||
/** | ||
* Get setting by type | ||
* @param settingType - setting type | ||
*/ | ||
getSettingsByType(settingType: NotificationType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add return type |
||
const model: NotificationSettings = { | ||
type: settingType, | ||
email: '' | ||
}; | ||
switch (settingType) { | ||
case(NotificationType.AVAILABILITY): | ||
model.delay = 0; | ||
break; | ||
} | ||
return this._fb.group(model); | ||
} | ||
|
||
/** | ||
* Set values from record | ||
* @param notificationSettings - notification settings | ||
*/ | ||
setNotificationSettings(notificationSettings = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add return type |
||
if (notificationSettings.length > 0) { | ||
const formSettings = this.form.get('notification_settings'); | ||
for (let step = 0; step < formSettings.value.length; step++) { | ||
const formSetting = formSettings.get(String(step)); | ||
const currentSetting = notificationSettings.find(element => element.type === formSetting.get('type').value); | ||
if (currentSetting !== undefined) { | ||
formSetting.get('email').setValue(currentSetting.email); | ||
if (currentSetting.delay !== undefined) { | ||
formSetting.get('delay').setValue(currentSetting.delay); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
setId(id) { this.form.value.id = id; } | ||
setLibraryPid(pid) { this.form.value.pid = pid; } | ||
setSchema(schema) { this.form.value.$schema = schema; } | ||
|
||
get name() { return this.form.get('name'); } | ||
get address() { return this.form.get('address'); } | ||
get email() { return this.form.get('email'); } | ||
get code() { return this.form.get('code'); } | ||
get opening_hours() { | ||
get name(): AbstractControl { return this.form.get('name'); } | ||
get address(): AbstractControl { return this.form.get('address'); } | ||
get email(): AbstractControl { return this.form.get('email'); } | ||
get code(): AbstractControl { return this.form.get('code'); } | ||
get opening_hours(): FormArray { | ||
return this.form.get('opening_hours') as FormArray; | ||
} | ||
get notification_settings(): FormArray { | ||
return this.form.get('notification_settings') as FormArray; | ||
} | ||
|
||
getValues() { return this.form.value; } | ||
getValues(): any { return this.form.value; } | ||
|
||
addTime(dayIndex): void { | ||
this.getTimesByDayIndex(dayIndex).push(this.buildTimes()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this does not works with the tests.