UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

103 lines 4.41 kB
import auth from '../../../../Auth.js'; import request from '../../../../request.js'; import { accessToken } from '../../../../utils/accessToken.js'; import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; import { cli } from '../../../../cli/cli.js'; import { globalOptionsZod } from '../../../../Command.js'; import { z } from 'zod'; export const options = z.strictObject({ ...globalOptionsZod.shape, id: z.string().alias('i'), userId: z.string().refine(id => validation.isValidGuid(id), { error: e => `'${e.input}' is not a valid GUID.` }).optional(), userName: z.string() .refine(upn => validation.isValidUserPrincipalName(upn) === true, { error: e => `'${e.input}' is not a valid user principal name for option 'userName'.` }) .optional(), comment: z.string().optional(), force: z.boolean().optional().alias('f') }); class OutlookEventCancelCommand extends GraphCommand { get name() { return commands.EVENT_CANCEL; } get description() { return 'Cancels a calendar event'; } get schema() { return options; } getRefinedSchema(schema) { return schema .refine(options => !(options.userId && options.userName), { error: `Specify either 'userId' or 'userName', but not both.` }); } async commandAction(logger, args) { const isAppOnlyAccessToken = accessToken.isAppOnlyAccessToken(auth.connection.accessTokens[auth.defaultResource].accessToken); let principalUrl = ''; const token = auth.connection.accessTokens[auth.defaultResource].accessToken; if (isAppOnlyAccessToken) { if (!args.options.userId && !args.options.userName) { throw `The option 'userId' or 'userName' is required when cancelling an event using application permissions.`; } } else { if (args.options.userId) { const currentUserId = accessToken.getUserIdFromAccessToken(token); if (args.options.userId !== currentUserId) { throw `You can only cancel your own events when using delegated permissions. The specified userId '${args.options.userId}' does not match the current user '${currentUserId}'.`; } } if (args.options.userName) { const currentUserName = accessToken.getUserNameFromAccessToken(token); if (args.options.userName.toLowerCase() !== currentUserName.toLowerCase()) { throw `You can only cancel your own events when using delegated permissions. The specified userName '${args.options.userName}' does not match the current user '${currentUserName}'.`; } } } if (args.options.userId || args.options.userName) { const userIdentifier = args.options.userId ?? args.options.userName; principalUrl += `users('${userIdentifier}')`; } else { principalUrl += 'me'; } const cancelEvent = async () => { try { if (this.verbose) { await logger.logToStderr(`Cancelling event with id '${args.options.id}'...`); } const requestOptions = { url: `${this.resource}/v1.0/${principalUrl}/events/${args.options.id}/cancel`, headers: { accept: 'application/json;odata.metadata=none', 'content-type': 'application/json' }, data: { comment: args.options.comment } }; await request.post(requestOptions); } catch (err) { this.handleRejectedODataJsonPromise(err); } }; if (args.options.force) { await cancelEvent(); } else { const result = await cli.promptForConfirmation({ message: `Are you sure you want to cancel event with id '${args.options.id}'?` }); if (result) { await cancelEvent(); } } } } export default new OutlookEventCancelCommand(); //# sourceMappingURL=event-cancel.js.map