({ request, user }: RequestHandlerParams)
| 18 | } |
| 19 | |
| 20 | async function post({ request, user }: RequestHandlerParams) { |
| 21 | if (!(await AppConfig.isAppEnabled('calendar'))) { |
| 22 | return new Response('Forbidden', { status: 403 }); |
| 23 | } |
| 24 | |
| 25 | const requestBody = await request.clone().json() as RequestBody; |
| 26 | |
| 27 | if (requestBody.id) { |
| 28 | const calendar = await CalendarModel.get(user!.id, requestBody.id); |
| 29 | |
| 30 | if (!calendar) { |
| 31 | return new Response('Not found', { status: 404 }); |
| 32 | } |
| 33 | |
| 34 | calendar.displayName = requestBody.name; |
| 35 | calendar.calendarColor = requestBody.color?.startsWith('#') |
| 36 | ? requestBody.color |
| 37 | : getColorAsHex((requestBody.color || 'bg-gray-700') as typeof CALENDAR_COLOR_OPTIONS[number]); |
| 38 | |
| 39 | await CalendarModel.update(user!.id, calendar.url, calendar.displayName, calendar.calendarColor); |
| 40 | |
| 41 | if (requestBody.isVisible !== calendar.isVisible) { |
| 42 | const userToUpdate = await UserModel.getById(user!.id); |
| 43 | |
| 44 | if (requestBody.isVisible) { |
| 45 | userToUpdate.extra.hidden_calendar_ids = userToUpdate.extra.hidden_calendar_ids?.filter((id) => |
| 46 | id !== calendar.uid! |
| 47 | ); |
| 48 | } else if (Array.isArray(userToUpdate.extra.hidden_calendar_ids)) { |
| 49 | userToUpdate.extra.hidden_calendar_ids.push(calendar.uid!); |
| 50 | } else { |
| 51 | userToUpdate.extra.hidden_calendar_ids = [calendar.uid!]; |
| 52 | } |
| 53 | |
| 54 | await UserModel.update(userToUpdate); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const newCalendars = await CalendarModel.list(user!.id); |
| 59 | |
| 60 | const responseBody: ResponseBody = { success: true, newCalendars }; |
| 61 | |
| 62 | return new Response(JSON.stringify(responseBody)); |
| 63 | } |
| 64 | |
| 65 | export default page({ |
| 66 | post, |
nothing calls this directly
no test coverage detected