From 76c3c42a258cf886ab66563d9715f76f08f4d890 Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Thu, 15 Apr 2021 10:02:41 +0200 Subject: [PATCH] refacto: change time format --- src/app/carto/carto.component.ts | 2 +- src/app/form/form.component.ts | 2 +- src/app/models/time.model.ts | 14 ++--- src/app/services/structure.service.ts | 58 +++++++++---------- .../hour-picker/hour-picker.component.ts | 33 ++--------- src/app/shared/validator/form.ts | 6 +- .../structure-details.component.ts | 5 +- 7 files changed, 45 insertions(+), 75 deletions(-) diff --git a/src/app/carto/carto.component.ts b/src/app/carto/carto.component.ts index d55b420b7..e0319adcc 100644 --- a/src/app/carto/carto.component.ts +++ b/src/app/carto/carto.component.ts @@ -85,7 +85,7 @@ export class CartoComponent implements OnInit { if (this.geolocation) { structure = this.getStructurePosition(structure, lon, lat); } - return this.structureService.updateOpeningStructure(structure, DateTime.local()); + return this.structureService.updateOpeningStructure(structure); }) ).then((structureList) => { structureList = _.sortBy(structureList, ['distance']); diff --git a/src/app/form/form.component.ts b/src/app/form/form.component.ts index 4e25894a4..331209f4e 100644 --- a/src/app/form/form.component.ts +++ b/src/app/form/form.component.ts @@ -840,7 +840,7 @@ export class FormComponent implements OnInit { let user: User; if (this.isEditMode) { this.structureService.editStructure(structure).subscribe((s: Structure) => { - this.createdStructure = this.structureService.updateOpeningStructure(s, DateTime.local()); + this.createdStructure = this.structureService.updateOpeningStructure(s); this.editForm = this.createStructureForm(s); }); } else { diff --git a/src/app/models/time.model.ts b/src/app/models/time.model.ts index f339333be..9f8e76d6e 100644 --- a/src/app/models/time.model.ts +++ b/src/app/models/time.model.ts @@ -1,6 +1,6 @@ export class Time { - openning: number; - closing: number; + openning: string; + closing: string; constructor(obj?: any) { Object.assign(this, obj); @@ -14,13 +14,7 @@ export class Time { return this.formatDate(this.closing); } - private formatDate(n: number): string { - if (n.toString().length === 3) { - const tabNum = n.toString().match(/.{1,1}/g); - return tabNum[0] + 'h' + tabNum[1] + tabNum[2]; - } else if (n.toString().length === 4) { - const tabNum = n.toString().match(/.{1,2}/g); - return tabNum[0] + 'h' + tabNum[1]; - } + private formatDate(n: string): string { + return n.replace(':', 'h'); } } diff --git a/src/app/services/structure.service.ts b/src/app/services/structure.service.ts index 507582392..4305bcf43 100644 --- a/src/app/services/structure.service.ts +++ b/src/app/services/structure.service.ts @@ -4,6 +4,7 @@ import { WeekDay } from '@angular/common'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import * as _ from 'lodash'; +const { DateTime } = require('luxon'); import { Structure } from '../models/structure.model'; import { Day } from '../models/day.model'; @@ -98,31 +99,23 @@ export class StructureService { * Update opening hours of structure * @param structure Structure model */ - public updateOpeningStructure(structure: Structure, dateTime): Structure { + public updateOpeningStructure(structure: Structure): Structure { // Get current day of week - const currentDate = dateTime; + const currentDate = DateTime.local(); const dayOfWeek: number = currentDate.weekday; - // Checks if minutes start with zero to avoid deletion - let now: number; - if (currentDate.minute.toString().length !== 1) { - now = parseInt('' + currentDate.hour + currentDate.minute, 10); - } else { - now = parseInt('' + currentDate.hour + 0 + currentDate.minute, 10); - } - // Get the schedules of a structure according to his day to indicate if it's open const structureSchedules: Day = structure.getDayhours(dayOfWeek); structure.isOpen = false; if (structureSchedules.open) { structureSchedules.time.forEach((period: Time) => { - if (this.compareSchedules(period.openning, period.closing, now)) { + if (this.compareSchedules(period.openning, period.closing, currentDate)) { structure.isOpen = true; } }); } - structure.openedOn = this.getNextOpening(structure, dayOfWeek, now); + structure.openedOn = this.getNextOpening(structure, dayOfWeek, currentDate); //TODO: return structure; } @@ -132,8 +125,14 @@ export class StructureService { * @param endTime end of period * @param currentTime actual time */ - private compareSchedules(startTime: number, endTime: number, currentTime: number): boolean { - return currentTime >= startTime && currentTime <= endTime; + private compareSchedules(startTime: string, endTime: string, currentTime: typeof DateTime): boolean { + const day = currentTime.toISO().split('T')[0]; + let start = DateTime.fromISO(`${day}T${startTime}`); + if (startTime.length === 4) { + start = DateTime.fromISO(`${day}T0${startTime}`); + } + const end = DateTime.fromISO(`${day}T${endTime}`); + return currentTime > start && currentTime < end; } // Get enum key @@ -145,8 +144,11 @@ export class StructureService { }); return keys.length > 0 ? parseInt(keys[0]) : null; } - private getNextOpening(s: Structure, dayOfWeek: number, hourBase: number): OpeningDay { + + private getNextOpening(s: Structure, dayOfWeek: number, currentTime: typeof DateTime): OpeningDay { let periodBeforeCurrentDay = null; + const time = currentTime.toISO().split('T')[1]; + const currentHour = new Date('1/1/1999 ' + time.split('+')[0]); // Browse day of week for (const [i, period] of Object.entries(s.hours)) { @@ -154,13 +156,16 @@ export class StructureService { // Check if it's current day if (i === this.numberToDay(dayOfWeek)) { if ( - (period.time[0].openning <= hourBase && period.time[0].closing >= hourBase) || - (period.time[1] && period.time[1].openning <= hourBase && period.time[1].closing >= hourBase) + (new Date('1/1/1999 ' + period.time[0].openning) <= currentHour && + new Date('1/1/1999 ' + period.time[0].closing) >= currentHour) || + (period.time[1] && + new Date('1/1/1999 ' + period.time[1].openning) <= currentHour && + new Date('1/1/1999 ' + period.time[1].closing) >= currentHour) ) { return new OpeningDay(i, null); - } else if (period.time[0].openning >= hourBase) { + } else if (new Date('1/1/1999 ' + period.time[0].openning) > currentHour) { return new OpeningDay(i, this.numberToHour(period.time[0].openning)); - } else if (period.time[1] && period.time[1].openning >= hourBase) { + } else if (period.time[1] && new Date('1/1/1999 ' + period.time[1].openning) > currentHour) { return new OpeningDay(i, this.numberToHour(period.time[1].openning)); } // Return the next day > current day. @@ -181,23 +186,18 @@ export class StructureService { return Weekday[n]; } - private numberToHour(n: number): string { - if (n.toString().length === 3) { - const tabNum = n.toString().match(/.{1,1}/g); - return tabNum[0] + 'h' + tabNum[1] + tabNum[2]; - } else if (n.toString().length === 4) { - const tabNum = n.toString().match(/.{1,2}/g); - return tabNum[0] + 'h' + tabNum[1]; - } + private numberToHour(n: string): string { + return n.replace(':', 'h'); } + public getStructureWithOwners(structureId: string, profile: User): Observable<StructureWithOwners> { return this.http.post<any>(`${this.baseUrl}/${structureId}/withOwners`, { emailUser: profile.email }); } - public sendMailOnStructureError(structureId: string, content: string, profile: User) { + public sendMailOnStructureError(structureId: string, content: string): Observable<any> { return this.http.post<any>(`${this.baseUrl}/reportStructureError`, { structureId, - content: content, + content, }); } } diff --git a/src/app/shared/components/hour-picker/hour-picker.component.ts b/src/app/shared/components/hour-picker/hour-picker.component.ts index 43c401154..ee97a1e4a 100644 --- a/src/app/shared/components/hour-picker/hour-picker.component.ts +++ b/src/app/shared/components/hour-picker/hour-picker.component.ts @@ -97,21 +97,21 @@ export class HourPickerComponent implements OnChanges, OnDestroy { .map((hour: Time) => { if (hour.openning && hour.closing) { return { - start: this.formatNumericalHours(hour.openning), - end: this.formatNumericalHours(hour.closing), + start: hour.openning, + end: hour.closing, error: null, }; } else { if (hour.openning) { return { - start: this.formatNumericalHours(hour.openning), + start: hour.openning, end: '', error: 'incomplete', }; } else { return { start: '', - end: this.formatNumericalHours(hour.closing), + end: hour.closing, error: 'incomplete', }; } @@ -135,8 +135,8 @@ export class HourPickerComponent implements OnChanges, OnDestroy { time: data.hours.map( (hour) => new Time({ - openning: this.formatStringHours(hour.start), - closing: this.formatStringHours(hour.end), + openning: hour.start, + closing: hour.end, }) ), }); @@ -154,27 +154,6 @@ export class HourPickerComponent implements OnChanges, OnDestroy { }); } - /** - * convert 1300 to '13:00' - */ - private formatNumericalHours(hour: number): string { - const numberStr = hour.toString(); - if (numberStr.length === 3) { - return `0${numberStr[0]}:${numberStr[1]}${numberStr[2]}`; - } else { - const splitStr = numberStr.match(/.{1,2}/g); - return `${splitStr[0]}:${splitStr[1]}`; - } - } - - /** - * convert '13:00' to 1300 - */ - private formatStringHours(hour: string): number { - const numberStr = hour.split(':')[0] + hour.split(':')[1]; - return parseInt(numberStr); - } - /** * Intégrer les horaires dans les horaires par défaut du composant */ diff --git a/src/app/shared/validator/form.ts b/src/app/shared/validator/form.ts index b07e69864..e4f88da4b 100644 --- a/src/app/shared/validator/form.ts +++ b/src/app/shared/validator/form.ts @@ -15,10 +15,10 @@ export function MustMatch(controlName: string, matchingControlName: string): any }; } -export function CheckHours(openning: number) { +export function CheckHours(openning: string) { return (control: AbstractControl) => { - const regex = new RegExp('^[0-9]*$'); - if (regex.test(control.value) && openning < control.value) { + const regex = new RegExp('([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]'); + if (regex.test(control.value) && new Date('1/1/1999 ' + openning) < new Date('1/1/1999 ' + control.value)) { return null; } else { return { forbiddenName: { value: control.value } }; diff --git a/src/app/structure-list/components/structure-details/structure-details.component.ts b/src/app/structure-list/components/structure-details/structure-details.component.ts index 0382434a5..5f51fc31d 100644 --- a/src/app/structure-list/components/structure-details/structure-details.component.ts +++ b/src/app/structure-list/components/structure-details/structure-details.component.ts @@ -300,16 +300,13 @@ export class StructureDetailsComponent implements OnInit { } public displayModalError(): void { - //do we need to check for user is logged ? this.structureErrorModalOpenned = !this.structureErrorModalOpenned; } public sendErrorEmail(modalValue: any): void { this.displayModalError(); if (modalValue.shouldSend) { - this.structureService - .sendMailOnStructureError(this.structure._id, modalValue.content, this.currentProfile) - .subscribe(() => {}); + this.structureService.sendMailOnStructureError(this.structure._id, modalValue.content).subscribe(() => {}); } } } -- GitLab