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

libraries: add notification settings #516

Merged
merged 1 commit into from
Mar 16, 2021
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
20 changes: 20 additions & 0 deletions projects/admin/src/app/classes/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
// required as json properties is not lowerCamelCase

import { WeekDay } from '@angular/common';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
Copy link
Contributor

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';

Copy link
Contributor

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.

import * as moment from 'moment';
import { WeekDays } from './week-days';


export function _(str) {
return marker(str);
}

Comment on lines +27 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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 ================================================
Expand All @@ -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 ================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Copy link
Contributor

Choose a reason for hiding this comment

The 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(
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand All @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getBuildEvent(): Observable<boolean> {

return this.buildEvent.asObservable();
}

create() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create(): void {

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 = []) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializeOpeningHours(openingHours = []): void {

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 = []) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setOpeningHours(openingHours = []): void {

for (let step = 0; step < 7; step++) {
const atimes = this.getTimesByDayIndex(step);
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -115,10 +171,10 @@ export class LibraryFormService {
});
}

reset() {
this.build();
}

/**
* Populate the form
* @param library - library
*/
populate(library: Library) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

populate(library: Library): void {

this.form.patchValue({
name: library.name,
Expand All @@ -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 = []) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 = []) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
Expand Down
Loading