mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
feat: show menu for multiple days for canteens and cafes
Closes #19, #79
This commit is contained in:
committed by
Jovan Krunić
parent
66b8720da0
commit
3c079cd189
@@ -13,6 +13,9 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* tslint:disable */
|
||||
|
||||
import moment from 'moment'
|
||||
|
||||
export const sampleResources = [{
|
||||
'errorNames': [],
|
||||
"instance": {
|
||||
@@ -981,8 +984,8 @@ export const sampleResources = [{
|
||||
'offers': [
|
||||
{
|
||||
'availability': 'in stock',
|
||||
'availabilityStarts': '2017-01-30T00:00:00.000Z',
|
||||
'availabilityEnds': '2017-01-30T23:59:59.999Z',
|
||||
'availabilityStarts': moment().startOf('day').add(2,'days').toISOString(),
|
||||
'availabilityEnds': moment().endOf('day').add(2,'days').toISOString(),
|
||||
'prices': {
|
||||
'default': 6.5,
|
||||
'student': 5,
|
||||
@@ -1101,8 +1104,8 @@ export const sampleResources = [{
|
||||
'offers': [
|
||||
{
|
||||
'availability': 'in stock',
|
||||
'availabilityStarts': '2017-01-30T00:00:00.000Z',
|
||||
'availabilityEnds': '2017-01-30T23:59:59.999Z',
|
||||
'availabilityStarts': moment().startOf('day').toISOString(),
|
||||
'availabilityEnds': moment().endOf('day').add(2,'days').toISOString(),
|
||||
'prices': {
|
||||
'default': 4.85,
|
||||
'student': 2.85,
|
||||
@@ -1117,7 +1120,7 @@ export const sampleResources = [{
|
||||
'type': 'remote'
|
||||
},
|
||||
'type': 'organization',
|
||||
'uid': '3b9b3df6-3a7a-58cc-922f-c7335c002634'
|
||||
'uid': 'b7206fb5-bd77-5572-928f-16aa70910f64'
|
||||
},
|
||||
'inPlace': {
|
||||
'geo': {
|
||||
@@ -1138,7 +1141,7 @@ export const sampleResources = [{
|
||||
'alternateNames': [
|
||||
'MensaHardenberg'
|
||||
],
|
||||
'uid': '72fbc8a3-ebd1-58f9-9526-ad65cba2e402',
|
||||
'uid': 'b7206fb5-bd77-5572-928f-16aa70910f64',
|
||||
'address': {
|
||||
'addressCountry': 'Germany',
|
||||
'addressLocality': 'Berlin',
|
||||
@@ -1191,8 +1194,8 @@ export const sampleResources = [{
|
||||
'uid': '3b9b3df6-3a7a-58cc-922f-c7335c002634'
|
||||
},
|
||||
'availability': 'in stock',
|
||||
'availabilityStarts': '2017-01-30T00:00:00.000Z',
|
||||
'availabilityEnds': '2017-01-30T23:59:59.999Z',
|
||||
'availabilityStarts': moment().startOf('day').add(2,'days').toISOString(),
|
||||
'availabilityEnds': moment().endOf('day').add(2,'days').toISOString(),
|
||||
'inPlace': {
|
||||
'geo': {
|
||||
'point': {
|
||||
@@ -1264,8 +1267,8 @@ export const sampleResources = [{
|
||||
],
|
||||
'offers': [
|
||||
{
|
||||
'availabilityEnds': '2017-03-27T23:59:59.000Z',
|
||||
'availabilityStarts': '2017-03-27T00:00:00.000Z',
|
||||
'availabilityEnds': moment().endOf('day').toISOString(),
|
||||
'availabilityStarts': moment().startOf('day').toISOString(),
|
||||
'availability': 'in stock',
|
||||
'inPlace': {
|
||||
'type': 'room',
|
||||
@@ -1273,7 +1276,7 @@ export const sampleResources = [{
|
||||
'categories': [
|
||||
'cafe'
|
||||
],
|
||||
'uid': 'e5492c9c-064e-547c-8633-c8fc8955cfcf',
|
||||
'uid': 'b7206fb5-bd77-5572-928f-16aa70910f64',
|
||||
'alternateNames': [
|
||||
'Cafeteria LEVEL'
|
||||
],
|
||||
@@ -1306,7 +1309,7 @@ export const sampleResources = [{
|
||||
'type': 'remote'
|
||||
},
|
||||
'type': 'organization',
|
||||
'uid': '3b9b3df6-3a7a-58cc-922f-c7335c002634'
|
||||
'uid': 'b7206fb5-bd77-5572-928f-16aa70910f64'
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
@@ -14,10 +14,13 @@
|
||||
*/
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {SCAcademicEvent, SCArticle, SCBook, SCBuilding, SCCatalog,
|
||||
SCDateSeries, SCDish, SCFavorite, SCMessage, SCPerson, SCRoom,
|
||||
SCThing, SCThingOriginType, SCThingType, SCToDo, SCToDoPriority} from '@openstapps/core';
|
||||
import {
|
||||
SCAcademicEvent, SCArticle, SCBook, SCBuilding, SCCatalog,
|
||||
SCDateSeries, SCDish, SCFavorite, SCMessage, SCPerson, SCRoom, SCSearchFilter,
|
||||
SCThing, SCThingOriginType, SCThingType, SCToDo, SCToDoPriority,
|
||||
} from '@openstapps/core';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {checkFilter} from '../fakesearch/filters';
|
||||
import {sampleResources} from './resources/test-resources';
|
||||
|
||||
// tslint:disable:no-magic-numbers
|
||||
@@ -339,7 +342,7 @@ const sampleDateSeries: SCDateSeries[] = [
|
||||
},
|
||||
];
|
||||
|
||||
export const sampleThingsMap: {[key in SCThingType | string]: SCThing[]} = {
|
||||
export const sampleThingsMap: { [key in SCThingType | string]: SCThing[] } = {
|
||||
'academic event': sampleAcademicEvents,
|
||||
article: sampleArticles,
|
||||
book: sampleBooks,
|
||||
@@ -364,35 +367,6 @@ export const sampleThingsMap: {[key in SCThingType | string]: SCThing[]} = {
|
||||
tour: [],
|
||||
video: [],
|
||||
};
|
||||
// tslint:enable:no-magic-numbers
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* provides all or certain sample things to be used in tests or backendless development
|
||||
*/
|
||||
export function getSampleThings(...uids: string[]): SCThing[] {
|
||||
const sampleThings: SCThing[] = [];
|
||||
if (typeof uids !== 'undefined' && uids.length > 0) {
|
||||
uids.forEach((uid) => {
|
||||
Object.keys(sampleThingsMap)
|
||||
.forEach((key) => {
|
||||
sampleThingsMap[key].forEach((thing) => {
|
||||
if (thing.uid === uid) {
|
||||
sampleThings.push(thing);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
Object.keys(sampleThingsMap)
|
||||
.forEach((key) => {
|
||||
sampleThings.push(...sampleThingsMap[key]);
|
||||
});
|
||||
}
|
||||
|
||||
return sampleThings;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
@@ -407,17 +381,13 @@ export class SampleThings {
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http;
|
||||
}
|
||||
// getSampleThings(): Observable<any> {
|
||||
// return this.http.get('http://localhost:8100/assets/json/joined.json');
|
||||
// // return of(sampleDishes[0]);
|
||||
// }
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
// tslint:disable-next-line:prefer-function-over-method no-any
|
||||
getSampleThing(uid: string): Observable<any[]> {
|
||||
// const jsonContent: any[] = await this.http.get('http://localhost:8100/assets/json/joined.json').toPromise();
|
||||
// tslint:disable-next-line:no-any
|
||||
const sampleThings: any[] = [];
|
||||
for (const resource of sampleResources) {
|
||||
if (resource.instance.uid as SCThingType === uid) {
|
||||
@@ -433,16 +403,18 @@ export class SampleThings {
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
getSampleThings(): Observable<any[]> {
|
||||
// const jsonContent: any[] = await this.http.get('http://localhost:8100/assets/json/joined.json').toPromise();
|
||||
// tslint:disable-next-line:prefer-function-over-method no-any
|
||||
getSampleThings(filter?: SCSearchFilter): Observable<any[]> {
|
||||
// tslint:disable-next-line:no-any
|
||||
const sampleThings: any[] = [];
|
||||
sampleResources.forEach((resource) => {
|
||||
for (const resource of sampleResources) {
|
||||
// tslint:disable-next-line:max-line-length
|
||||
// if ([SCThingType.Video].includes(resource.instance.type as SCThingType)) {
|
||||
if (typeof filter === 'undefined' || checkFilter(resource.instance as SCThing, filter)) {
|
||||
sampleThings.push(resource.instance);
|
||||
}
|
||||
// }
|
||||
});
|
||||
}
|
||||
|
||||
return of(sampleThings);
|
||||
}
|
||||
|
||||
@@ -382,13 +382,13 @@ export class FakeBackendInterceptor implements HttpInterceptor {
|
||||
if (request.body.filter.arguments.field === 'uid') {
|
||||
return this.sampleFetcher.getSampleThing(request.body.filter.arguments.value)
|
||||
// tslint:disable-next-line:no-any
|
||||
.pipe(map((sampleData: any) => {
|
||||
return new HttpResponse({status: 200, body: {data: sampleData}});
|
||||
}), delay(this.RESPONSE_DELAY)); // add delay for skeleton screens to be seen (see !16)
|
||||
.pipe(map((sampleData: any) => {
|
||||
return new HttpResponse({status: 200, body: {data: sampleData}});
|
||||
}), delay(this.RESPONSE_DELAY)); // add delay for skeleton screens to be seen (see !16)
|
||||
}
|
||||
}
|
||||
|
||||
return this.sampleFetcher.getSampleThings()
|
||||
return this.sampleFetcher.getSampleThings(request.body.filter)
|
||||
// tslint:disable-next-line:no-any
|
||||
.pipe(map((sampleData: any) => {
|
||||
return new HttpResponse({status: 200, body: {data: sampleData, facets: facetsMock}});
|
||||
|
||||
170
src/app/_helpers/fakesearch/filters.ts
Normal file
170
src/app/_helpers/fakesearch/filters.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (C) 2020 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {SCSearchBooleanFilter, SCSearchFilter, SCSearchValueFilter, SCThing} from '@openstapps/core';
|
||||
import {logger} from '../ts-logger';
|
||||
|
||||
/**
|
||||
* Checks if any filter applies to an SCThing
|
||||
*/
|
||||
export function checkFilter(thing: SCThing, filter: SCSearchFilter): boolean {
|
||||
switch (filter.type) {
|
||||
case 'availability': /*TODO*/
|
||||
break;
|
||||
case 'boolean':
|
||||
return applyBooleanFilter(thing, filter);
|
||||
case 'distance': /*TODO*/
|
||||
break;
|
||||
case 'value':
|
||||
return applyValueFilter(thing, filter);
|
||||
}
|
||||
|
||||
void logger.error(`Not implemented filter method "${filter.type}" in fake backend!`);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a value filter applies to an SCThing
|
||||
*/
|
||||
function applyValueFilter(thing: SCThing, filter: SCSearchValueFilter): boolean {
|
||||
const path = filter.arguments.field.split('.');
|
||||
const thingFieldValue = traverseToFieldPath(thing, path, filter.arguments.value);
|
||||
|
||||
if (!(thingFieldValue.found)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return thingFieldValue.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Object that can be accessed using foo[bar]
|
||||
*/
|
||||
interface IndexableObject {
|
||||
// tslint:disable-next-line:no-any
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of a search for a field and comparison to a desired value
|
||||
*/
|
||||
type FieldSearchResult = {
|
||||
/**
|
||||
* Weather the field was found
|
||||
*/
|
||||
found: true;
|
||||
|
||||
/**
|
||||
* The result of the comparison
|
||||
*/
|
||||
result: boolean;
|
||||
} | {
|
||||
/**
|
||||
* Weather the field was found
|
||||
*/
|
||||
found: false;
|
||||
};
|
||||
|
||||
// tslint:disable-next-line:completed-docs
|
||||
function traverseToFieldPath(value: IndexableObject, path: string[], desiredFieldValue: unknown): FieldSearchResult {
|
||||
if (path.length === 0) {
|
||||
void logger.error(`Value filter provided with zero length path`);
|
||||
|
||||
return {found: false};
|
||||
}
|
||||
|
||||
if (value.hasOwnProperty(path[0])) {
|
||||
const nestedProperty = value[path[0]];
|
||||
|
||||
if (path.length === 1) {
|
||||
return esStyleFieldHandler(nestedProperty, (nestedValue) => {
|
||||
return {
|
||||
found: true,
|
||||
result: nestedValue === desiredFieldValue,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return esStyleFieldHandler(nestedProperty, (nestedValue) => {
|
||||
if (typeof nestedValue === 'object') {
|
||||
return traverseToFieldPath(
|
||||
nestedValue as IndexableObject,
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
path.slice(1),
|
||||
desiredFieldValue);
|
||||
}
|
||||
|
||||
return {found: false};
|
||||
});
|
||||
}
|
||||
|
||||
return {found: false};
|
||||
}
|
||||
|
||||
/**
|
||||
* ES treats arrays like normal fields
|
||||
*/
|
||||
function esStyleFieldHandler<T>(field: T | T[],
|
||||
handler: (value: T) => FieldSearchResult): FieldSearchResult {
|
||||
if (Array.isArray(field)) {
|
||||
for (const nestedField of field) {
|
||||
const result = handler(nestedField);
|
||||
|
||||
if (result.found && result.result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: found is not accurate
|
||||
return {found: false};
|
||||
}
|
||||
|
||||
return handler(field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a boolean filter applies to an SCThing
|
||||
*/
|
||||
function applyBooleanFilter(thing: SCThing, filter: SCSearchBooleanFilter): boolean {
|
||||
let out = false;
|
||||
|
||||
switch (filter.arguments.operation) {
|
||||
case 'and':
|
||||
out = true;
|
||||
for (const nesterFilter of filter.arguments.filters) {
|
||||
out = out && checkFilter(thing, nesterFilter);
|
||||
}
|
||||
|
||||
return out;
|
||||
case 'or':
|
||||
for (const nesterFilter of filter.arguments.filters) {
|
||||
out = out || checkFilter(thing, nesterFilter);
|
||||
}
|
||||
|
||||
return out;
|
||||
case 'not':
|
||||
if (filter.arguments.filters.length === 1) {
|
||||
return !checkFilter(thing, filter.arguments.filters[0]);
|
||||
}
|
||||
void logger.error(`Too many filters for "not" boolean operation`);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void logger.error(`Not implemented boolean filter "${filter.arguments.operation}"`);
|
||||
|
||||
return false;
|
||||
}
|
||||
19
src/app/_helpers/ts-logger.ts
Normal file
19
src/app/_helpers/ts-logger.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (C) 2020 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {NGXLogger} from 'ngx-logger';
|
||||
|
||||
export let logger: NGXLogger;
|
||||
|
||||
export const initLogger = (newLogger: NGXLogger) => logger = newLogger;
|
||||
Reference in New Issue
Block a user