|
|
|
@@ -0,0 +1,180 @@
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
|
import { Timeframe, Restaurant, TimeframeSelection } from '../interfaces/catering-schedule';
|
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
import { SoapApiHelper } from '../classes/api-helper';
|
|
|
|
|
import { environment } from 'src/environments/environment';
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class CoronaCateringService {
|
|
|
|
|
|
|
|
|
|
public static readonly STORAGE_NAME = 'catering-selection';
|
|
|
|
|
|
|
|
|
|
constructor(private http: HttpClient) { }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets a timeframe and restaurant selection for the specified date.
|
|
|
|
|
*
|
|
|
|
|
* @param restaruant restaurant
|
|
|
|
|
* @param timeframe timeframe of visit
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
public async selectTimeframeAsync(restaruant: Restaurant, timeframe: Timeframe, date: Date): Promise<void> {
|
|
|
|
|
date = new Date(date.toDateString()); // make sure hours, minutes and seconds are removed
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
if (this.isTimeframeSelected(date)) {
|
|
|
|
|
reject('You already selected a timeframe for this date.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.addGuest(restaruant, timeframe, date)
|
|
|
|
|
.subscribe(
|
|
|
|
|
_ => {
|
|
|
|
|
this.saveSelection(restaruant, timeframe, date);
|
|
|
|
|
resolve();
|
|
|
|
|
},
|
|
|
|
|
error => reject(error)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the timeframe and restaurant selection of a specific date.
|
|
|
|
|
*
|
|
|
|
|
* @param restaurant restaurant
|
|
|
|
|
* @param timeframe timeframe of visit
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
public async changeTimeframeAsync(restaurant: Restaurant, timeframe: Timeframe, date: Date): Promise<void> {
|
|
|
|
|
return new Promise((reolve, reject) => {
|
|
|
|
|
if (!this.isTimeframeSelected(date)) {
|
|
|
|
|
reject('Can not change selection. No timeframe is selected yet for this date.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let selection = this.getSelection(date);
|
|
|
|
|
this.removeGuest(selection.restaurant, selection.timeframe, selection.date)
|
|
|
|
|
.subscribe(
|
|
|
|
|
_ => {
|
|
|
|
|
this.addGuest(restaurant, timeframe, date)
|
|
|
|
|
.subscribe(
|
|
|
|
|
__ => this.saveSelection(restaurant, timeframe, date),
|
|
|
|
|
error => reject(error)
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
error => reject(error)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads all selections of the user.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public getSelections(): TimeframeSelection[] {
|
|
|
|
|
return this.loadSelections();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads the selection of a specific date. Returns null if no timeframe is selected.
|
|
|
|
|
*
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
public getSelection(date: Date): TimeframeSelection {
|
|
|
|
|
date = new Date(date.toDateString());
|
|
|
|
|
return this.loadSelections().find(selection => selection.date.getTime() === date.getTime());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Saves a timeframe selection to the local storage.
|
|
|
|
|
*
|
|
|
|
|
* @param restaurant restaurant
|
|
|
|
|
* @param timeframe timeframe of visit
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
private saveSelection(restaurant: Restaurant, timeframe: Timeframe, date: Date): void {
|
|
|
|
|
const today = new Date(new Date().toDateString());
|
|
|
|
|
let selections = this.loadSelections();
|
|
|
|
|
selections = selections.filter(selection => selection.date.getTime() >= today.getTime());
|
|
|
|
|
let index = this.loadSelections().findIndex(selection => selection.date.getTime() === date.getTime());
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
selections[index] = { date, restaurant, timeframe };
|
|
|
|
|
} else {
|
|
|
|
|
selections.push({ date, restaurant, timeframe });
|
|
|
|
|
}
|
|
|
|
|
localStorage.setItem(CoronaCateringService.STORAGE_NAME, JSON.stringify(selections));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads all tiemframe selections done by the user.
|
|
|
|
|
*/
|
|
|
|
|
private loadSelections(): TimeframeSelection[] {
|
|
|
|
|
let storage = localStorage.getItem(CoronaCateringService.STORAGE_NAME);
|
|
|
|
|
if (!storage) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
let selections = <TimeframeSelection[]>JSON.parse(storage);
|
|
|
|
|
selections = selections.map(row => <TimeframeSelection>{
|
|
|
|
|
date: new Date(row.date),
|
|
|
|
|
timeframe: row.timeframe,
|
|
|
|
|
restaurant: row.restaurant,
|
|
|
|
|
});
|
|
|
|
|
return selections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the user already selected a timeframe for the specified date.
|
|
|
|
|
*
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
private isTimeframeSelected(date: Date): boolean {
|
|
|
|
|
return this.loadSelections().findIndex(selection => selection.date.getTime() === date.getTime()) >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Increments the counter on the backend for the specified date, timeframe and restaurant.
|
|
|
|
|
*
|
|
|
|
|
* @param restaruant restaurant
|
|
|
|
|
* @param timeframe timeframe of visit
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
private addGuest(restaurant: Restaurant, timeframe: Timeframe, date: Date): Observable<void> {
|
|
|
|
|
return SoapApiHelper.call(this.http, environment.corona_catering_api + '/AddGuest', { restaurant, timeframe, date });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decrements the counter on the backend for the specified date, timeframe and restaurant.
|
|
|
|
|
*
|
|
|
|
|
* @param restaruant restaurant
|
|
|
|
|
* @param timeframe timeframe of visit
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
private removeGuest(restaurant: Restaurant, timeframe: Timeframe, date: Date): Observable<void> {
|
|
|
|
|
return SoapApiHelper.call(this.http, environment.corona_catering_api + '/RemoveGuest', { restaurant, timeframe, date });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads the max visitors per timeframe for the OASE.
|
|
|
|
|
*/
|
|
|
|
|
public getMaxNbOase(): Observable<number> {
|
|
|
|
|
return SoapApiHelper.call<number>(this.http, environment.corona_catering_api + '/GetMaxNbOase', {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads the max visitors per timeframe for the timeout.
|
|
|
|
|
*/
|
|
|
|
|
public getMaxNbTimeout(): Observable<number> {
|
|
|
|
|
return SoapApiHelper.call<number>(this.http, environment.corona_catering_api + '/GetMaxNbTimeout', {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads the queue size of the specified timeframe.
|
|
|
|
|
*
|
|
|
|
|
* @param restaurant restaurant
|
|
|
|
|
* @param timeframe timeframe of visit
|
|
|
|
|
* @param date date of visit
|
|
|
|
|
*/
|
|
|
|
|
public getQueue(restaurant: Restaurant, timeframe: Timeframe, date: Date): Observable<number> {
|
|
|
|
|
return SoapApiHelper.call<number>(this.http, environment.corona_catering_api + '/GetQueue', { restaurant, timeframe, date });
|
|
|
|
|
}
|
|
|
|
|
}
|