MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / get

Function get

pages/calendar.ts:12–68  ·  view source on GitHub ↗
({ request, user, match, session, isRunningLocally }: RequestHandlerParams)

Source from the content-addressed store, hash-verified

10type CalendarView = 'day' | 'week' | 'month';
11
12async function get({ request, user, match, session, isRunningLocally }: RequestHandlerParams) {
13 const baseUrl = (await AppConfig.getConfig()).auth.baseUrl;
14 const calendarConfig = await AppConfig.getCalendarConfig();
15
16 if (!calendarConfig.enableCalDavServer) {
17 throw new Error('CalDAV server is not enabled');
18 }
19
20 if (!(await AppConfig.isAppEnabled('calendar'))) {
21 throw new Error('Calendar app is not enabled');
22 }
23
24 const userId = user!.id;
25 const timezoneId = user!.extra.timezone?.id || 'UTC';
26 const timezoneUtcOffset = user!.extra.timezone?.utcOffset || 0;
27
28 const searchParams = new URL(request.url).searchParams;
29
30 const view = (searchParams.get('view') as CalendarView) || 'week';
31 const startDate = searchParams.get('startDate') || new Date().toISOString().substring(0, 10);
32
33 let userCalendars = await CalendarModel.list(userId);
34
35 // Create default calendar if none exists
36 if (userCalendars.length === 0) {
37 const randomColor = CALENDAR_COLOR_OPTIONS[Math.floor(Math.random() * CALENDAR_COLOR_OPTIONS.length)];
38 await CalendarModel.create(userId, 'Calendar', getColorAsHex(randomColor));
39
40 userCalendars = await CalendarModel.list(userId);
41 }
42
43 const visibleCalendarIds = userCalendars.filter((calendar) => calendar.isVisible).map((calendar) => calendar.uid!);
44
45 const dateRange = getDateRangeForCalendarView(startDate, view);
46
47 const userCalendarEvents = await CalendarEventModel.list(userId, visibleCalendarIds, dateRange);
48
49 const htmlContent = defaultHtmlContent({
50 userCalendars,
51 userCalendarEvents,
52 baseUrl,
53 view,
54 startDate,
55 timezoneId,
56 timezoneUtcOffset,
57 });
58
59 return basicLayoutResponse(htmlContent, {
60 currentPath: match.pathname.input,
61 titlePrefix: 'Calendar',
62 match,
63 request,
64 user,
65 session,
66 isRunningLocally,
67 });
68}
69

Callers

nothing calls this directly

Calls 10

getColorAsHexFunction · 0.90
basicLayoutResponseFunction · 0.90
getConfigMethod · 0.80
getCalendarConfigMethod · 0.80
isAppEnabledMethod · 0.80
defaultHtmlContentFunction · 0.70
getMethod · 0.45
listMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected