-
Notifications
You must be signed in to change notification settings - Fork 7
/
calendar.fn.ts
73 lines (64 loc) · 2.39 KB
/
calendar.fn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { get } from '@placeos/ts-client';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { toQueryString } from '@placeos/common';
import { OrganisationService } from 'libs/organisation/src/lib/organisation.service';
import { Space } from 'libs/spaces/src/lib/space.class';
import { Calendar } from './calendar.class';
import { CalendarAvailabilityQueryParams } from './calendar.interfaces';
const CALENDAR_ENDPOINT = '/api/staff/v1/calendars';
/** List calendars associated with the logged in user */
export function queryCalendars(): Observable<Calendar[]> {
return get(CALENDAR_ENDPOINT).pipe(
map((i) => i.map((c) => new Calendar(c)))
);
}
/** List room calendars for available spaces based on the given query */
export function queryCalendarAvailability(
q: CalendarAvailabilityQueryParams
): Observable<Calendar[]> {
const query = toQueryString(q);
return get(
`${CALENDAR_ENDPOINT}/availability${query ? '?' + query : ''}`
).pipe(map((i) => i.map((c) => new Calendar(c))));
}
const calendarsToSpaces = (org?) =>
map((list: Calendar[]) =>
list
.filter((cal) => !!cal.resource)
.map(
(cal) =>
new Space({
...cal.resource,
level: org?.levelWithID(cal.resource.zones),
availability: cal.availability,
})
)
.filter((space) => space.bookable)
);
/** List available spaces based on the given query */
export function querySpaceCalendarAvailability(
q: CalendarAvailabilityQueryParams,
org?: OrganisationService
): Observable<Space[]> {
return queryCalendarAvailability(q).pipe(calendarsToSpaces(org));
}
export function queryUserFreeBusy(q: CalendarAvailabilityQueryParams) {
const query = toQueryString(q);
return get(
`${CALENDAR_ENDPOINT}/free_busy${query ? '?' + query : ''}`
).pipe(map((i) => i as Calendar[]));
}
/** List room calendars for available spaces based on the given query */
export function querySpaceFreeBusy(
q: CalendarAvailabilityQueryParams,
org?: OrganisationService
): Observable<Space[]> {
const query = toQueryString(q);
return get(
`${CALENDAR_ENDPOINT}/free_busy${query ? '?' + query : ''}`
).pipe(
map((i) => i.map((c) => new Calendar(c))),
calendarsToSpaces(org)
);
}