UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

115 lines 4.9 kB
import { z } from 'zod'; import { globalOptionsZod } from '../../../../Command.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; import { validation } from '../../../../utils/validation.js'; import { odata } from '../../../../utils/odata.js'; import { calendar } from '../../../../utils/calendar.js'; export const options = z.strictObject({ ...globalOptionsZod.shape, userId: z.string().refine(id => validation.isValidGuid(id), { error: e => `'${e.input}' is not a valid GUID.` }).optional(), userName: z.string().refine(name => validation.isValidUserPrincipalName(name), { error: e => `'${e.input}' is not a valid UPN.` }).optional(), calendarId: z.string().optional(), calendarName: z.string().optional(), startDateTime: z.string().refine(date => validation.isValidISODateTime(date), { error: e => `'${e.input}' is not a valid ISO date-time.` }).optional(), endDateTime: z.string().refine(date => validation.isValidISODateTime(date), { error: e => `'${e.input}' is not a valid ISO date-time.` }).optional(), timeZone: z.string().optional(), properties: z.string().optional(), filter: z.string().optional() }); class OutlookEventListCommand extends GraphCommand { get name() { return commands.EVENT_LIST; } get description() { return 'Retrieves a list of events from a specific calendar of a user.'; } get schema() { return options; } getRefinedSchema(schema) { return schema .refine(options => [options.userId, options.userName].filter(x => x !== undefined).length === 1, { error: 'Specify either userId or userName, but not both' }) .refine(options => !(options.calendarId && options.calendarName), { error: 'Specify either calendarId or calendarName, but not both.' }); } defaultProperties() { return ['id', 'subject']; } async commandAction(logger, args) { try { if (this.verbose) { await logger.logToStderr('Getting a list of the events...'); } let events; const endpoint = await this.getRequestUrl(args.options); if (args.options.timeZone) { const requestOptions = { url: endpoint, headers: { accept: 'application/json;odata.metadata=none', Prefer: `outlook.timezone="${args.options.timeZone}"` }, responseType: 'json' }; events = await odata.getAllItems(requestOptions); } else { events = await odata.getAllItems(endpoint); } await logger.log(events); } catch (err) { this.handleRejectedODataJsonPromise(err); } } async getRequestUrl(options) { const queryParameters = []; if (options.properties) { const allProperties = options.properties.split(','); const selectProperties = allProperties.filter(prop => !prop.includes('/')); const expandProperties = allProperties.filter(prop => prop.includes('/')); if (selectProperties.length > 0) { queryParameters.push(`$select=${selectProperties}`); } if (expandProperties.length > 0) { const fieldExpands = expandProperties.map(p => `${p.split('/')[0]}($select=${p.split('/')[1]})`); queryParameters.push(`$expand=${fieldExpands.join(',')}`); } } if (options.filter || options.startDateTime || options.endDateTime) { let filter = options.filter || ''; if (options.startDateTime) { filter += `${filter ? ' and ' : ''}start/dateTime ge '${options.startDateTime}'`; } if (options.endDateTime) { filter += `${filter ? ' and ' : ''}start/dateTime lt '${options.endDateTime}'`; } queryParameters.push(`$filter=${filter}`); } const queryString = queryParameters.length > 0 ? `?${queryParameters.join('&')}` : ''; const userIdentifier = options.userId ?? options.userName; let calendarId = options.calendarId; if (options.calendarName) { calendarId = (await calendar.getUserCalendarByName(userIdentifier, options.calendarName)).id; } return calendarId ? `${this.resource}/v1.0/users('${userIdentifier}')/calendars/${calendarId}/events${queryString}` : `${this.resource}/v1.0/users('${userIdentifier}')/events${queryString}`; } } export default new OutlookEventListCommand(); //# sourceMappingURL=event-list.js.map