UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

132 lines 6.21 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 request from '../../../../request.js'; import { accessToken } from '../../../../utils/accessToken.js'; import auth from '../../../../Auth.js'; import { calendarGroup } from '../../../../utils/calendarGroup.js'; const calendarColors = ['auto', 'lightBlue', 'lightGreen', 'lightOrange', 'lightGray', 'lightYellow', 'lightTeal', 'lightPink', 'lightBrown', 'lightRed', 'maxColor']; export const options = z.strictObject({ ...globalOptionsZod.shape, id: z.string().alias('i'), name: z.string().optional().alias('n'), 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(), calendarGroupId: z.string().optional(), calendarGroupName: z.string().optional(), color: z.enum(calendarColors).optional(), isDefault: z.boolean().optional() }); class OutlookCalendarSetCommand extends GraphCommand { get name() { return commands.CALENDAR_SET; } get description() { return 'Updates a calendar for a user'; } get schema() { return options; } getRefinedSchema(schema) { return schema .refine(options => !(options.userId && options.userName), { error: 'Specify either userId or userName, but not both.' }) .refine(options => !(options.calendarGroupId && options.calendarGroupName), { error: 'Specify either calendarGroupId or calendarGroupName, but not both.' }) .refine(options => [options.name, options.color, options.isDefault].filter(o => o !== undefined).length > 0, { error: 'Specify at least one of the following options: name, color, or isDefault.' }); } async commandAction(logger, args) { try { const token = auth.connection.accessTokens[auth.defaultResource].accessToken; const isAppOnlyAccessToken = accessToken.isAppOnlyAccessToken(token); let requestUrl; if (isAppOnlyAccessToken) { if (!args.options.userId && !args.options.userName) { throw 'When running with application permissions either userId or userName is required.'; } const userIdentifier = args.options.userId ?? args.options.userName; requestUrl = this.buildRequestUrl(userIdentifier); } else { if (args.options.userId || args.options.userName) { const currentUserId = accessToken.getUserIdFromAccessToken(token); const currentUserName = accessToken.getUserNameFromAccessToken(token); const isOtherUser = (args.options.userId && args.options.userId !== currentUserId) || (args.options.userName && args.options.userName.toLowerCase() !== currentUserName?.toLowerCase()); if (isOtherUser) { const scopes = accessToken.getScopesFromAccessToken(token); const hasSharedScope = scopes.some(s => s === 'Calendars.ReadWrite.Shared'); if (!hasSharedScope) { throw 'To update calendars of other users, the Entra ID application used for authentication must have the Calendars.ReadWrite.Shared delegated permission assigned.'; } } const userIdentifier = args.options.userId ?? args.options.userName; requestUrl = this.buildRequestUrl(userIdentifier); } else { requestUrl = this.buildRequestUrl(undefined); } } if (this.verbose) { await logger.logToStderr(`Updating calendar '${args.options.id}'...`); } let calendarGroupId = args.options.calendarGroupId; if (args.options.calendarGroupName) { const userIdForGroup = args.options.userId ?? args.options.userName ?? accessToken.getUserIdFromAccessToken(token); const calendarGroupResult = await calendarGroup.getUserCalendarGroupByName(userIdForGroup, args.options.calendarGroupName, 'id'); calendarGroupId = calendarGroupResult.id; } const url = this.buildCalendarUrl(requestUrl, args.options.id, calendarGroupId); const requestOptions = { url, headers: { accept: 'application/json;odata.metadata=none' }, responseType: 'json', data: this.createRequestBody(args) }; const result = await request.patch(requestOptions); await logger.log(result); } catch (err) { this.handleRejectedODataJsonPromise(err); } } buildRequestUrl(userIdentifier) { if (userIdentifier) { return `${this.resource}/v1.0/users('${userIdentifier}')`; } return `${this.resource}/v1.0/me`; } buildCalendarUrl(baseUrl, calendarId, calendarGroupId) { if (calendarGroupId) { return `${baseUrl}/calendarGroups/${calendarGroupId}/calendars/${calendarId}`; } return `${baseUrl}/calendars/${calendarId}`; } createRequestBody(args) { const data = {}; if (args.options.name !== undefined) { data.name = args.options.name; } if (args.options.color !== undefined) { data.color = args.options.color; } if (args.options.isDefault !== undefined) { data.isDefaultCalendar = args.options.isDefault; } return data; } } export default new OutlookCalendarSetCommand(); //# sourceMappingURL=calendar-set.js.map