import { wrapError } from '../client/error_wrapper' ; import { RouteInitialization } from '../types' ; import { calendarSchema , calendarIdSchema , calendarIdsSchema } from './schemas/calendars_schema' ; import { CalendarManager , Calendar , FormCalendar } from '../models/calendar' ; import type { MlClient } from '../lib/ml_client' ; function getAllCalendars ( mlClient ) { const cal = new CalendarManager ( mlClient ) ; return cal . getAllCalendars ( ) ; } function getCalendar ( mlClient , calendarId ) { const cal = new CalendarManager ( mlClient ) ; return cal . getCalendar ( calendarId ) ; } function newCalendar ( mlClient , calendar ) { const cal = new CalendarManager ( mlClient ) ; return cal . newCalendar ( calendar ) ; } function updateCalendar ( mlClient , calendarId , calendar ) { const cal = new CalendarManager ( mlClient ) ; return cal . updateCalendar ( calendarId , calendar ) ; } function deleteCalendar ( mlClient , calendarId ) { const cal = new CalendarManager ( mlClient ) ; return cal . deleteCalendar ( calendarId ) ; } function getCalendarsByIds ( mlClient , calendarIds ) { const cal = new CalendarManager ( mlClient ) ; return cal . getCalendarsByIds ( calendarIds ) ; } export function calendars ( { router , routeGuard } ) { apiGroup api apiName apiDescription router . get ( { path : '/api/ml/calendars' , validate : false , options : { tags : [ 'access:ml:canGetCalendars' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const resp = await getAllCalendars ( mlClient ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/calendars/{calendarIds}' , validate : { params : calendarIdsSchema , } , options : { tags : [ 'access:ml:canGetCalendars' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { let returnValue ; try { const calendarIds = request . params . calendarIds . split ( ',' ) ; if ( calendarIds . length === 1 ) { returnValue = await getCalendar ( mlClient , calendarIds [ 0 ] ) ; } else { returnValue = await getCalendarsByIds ( mlClient , calendarIds ) ; } return response . ok ( { body : returnValue , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . put ( { path : '/api/ml/calendars' , validate : { body : calendarSchema , } , options : { tags : [ 'access:ml:canCreateCalendar' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const body = request . body ; const resp = await newCalendar ( mlClient , body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . put ( { path : '/api/ml/calendars/{calendarId}' , validate : { params : calendarIdSchema , body : calendarSchema , } , options : { tags : [ 'access:ml:canCreateCalendar' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { calendarId } = request . params ; const body = request . body ; const resp = await updateCalendar ( mlClient , calendarId , body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . delete ( { path : '/api/ml/calendars/{calendarId}' , validate : { params : calendarIdSchema , } , options : { tags : [ 'access:ml:canDeleteCalendar' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { calendarId } = request . params ; const resp = await deleteCalendar ( mlClient , calendarId ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }